mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 05:47:52 +00:00
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { OpenAI } from 'https://deno.land/x/openai@v4.20.1/mod.ts'
|
|
import { createLogger } from './logger.ts'
|
|
|
|
const openai = new OpenAI({
|
|
apiKey: Deno.env.get('OPENAI_API_KEY')
|
|
})
|
|
|
|
export async function processAudioWithWhisper(audioUrl: string, logger: Logger): Promise<string> {
|
|
try {
|
|
logger.info('whisper_start', 'Iniciando download do áudio', { url: audioUrl })
|
|
|
|
const audioResponse = await fetch(audioUrl)
|
|
if (!audioResponse.ok) {
|
|
logger.error('whisper_download', new Error(`HTTP ${audioResponse.status}`))
|
|
throw new Error('Falha ao baixar áudio')
|
|
}
|
|
|
|
const audioBlob = await audioResponse.blob()
|
|
logger.info('whisper_download_complete', 'Download concluído', {
|
|
size: audioBlob.size,
|
|
type: audioBlob.type
|
|
})
|
|
|
|
// 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
|
|
}
|
|
}
|