mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-16 21:37:51 +00:00
- Implementa Edge Function para processamento de áudio - Adiciona integração com OpenAI Whisper e GPT-4 - Configura Database Trigger para story_recordings - Implementa análise automática de leitura - Atualiza documentação e variáveis de ambiente
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { OpenAI } from 'https://deno.land/x/openai@v4.20.1/mod.ts'
|
|
|
|
const openai = new OpenAI({
|
|
apiKey: Deno.env.get('OPENAI_API_KEY')
|
|
})
|
|
|
|
export async function processAudioWithWhisper(audioUrl: string): Promise<string> {
|
|
try {
|
|
// Download do áudio
|
|
const audioResponse = await fetch(audioUrl)
|
|
if (!audioResponse.ok) {
|
|
throw new Error('Falha ao baixar áudio')
|
|
}
|
|
const audioBlob = await audioResponse.blob()
|
|
|
|
// Debug do áudio
|
|
console.log('Audio blob:', {
|
|
size: audioBlob.size,
|
|
type: audioBlob.type
|
|
})
|
|
|
|
// Converte Blob para File
|
|
const audioFile = new File([audioBlob], 'audio.mp3', {
|
|
type: audioBlob.type || 'audio/mpeg'
|
|
})
|
|
|
|
// Transcrição com Whisper
|
|
const transcription = await openai.audio.transcriptions.create({
|
|
file: audioFile,
|
|
model: 'whisper-1',
|
|
language: 'pt'
|
|
// removido response_format pois estava causando erro
|
|
})
|
|
|
|
console.log('Transcription response:', transcription) // Debug
|
|
|
|
if (!transcription || typeof transcription === 'undefined') {
|
|
throw new Error('Resposta da transcrição vazia')
|
|
}
|
|
|
|
return transcription.text || ''
|
|
} catch (error) {
|
|
console.error('Erro detalhado na transcrição:', error)
|
|
throw error
|
|
}
|
|
}
|