Implementación inicial de Nucleo Whisper
- Configurado proyecto Nuxt 4 con PWA - Integrado OpenAI Whisper API para transcripción de audio - Implementada captura de audio desde navegador - Creada UI con grabación y visualización de transcripciones - Configurado Authentik Proxy para autenticación - Setup de Docker y Gitea Actions para despliegue
This commit is contained in:
106
nuxt4/server/api/whisper/transcribe.post.ts
Normal file
106
nuxt4/server/api/whisper/transcribe.post.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { FormData } from 'formdata-node'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Verificar autenticación mediante headers de Authentik
|
||||
const headers = getRequestHeaders(event)
|
||||
const username = headers['x-authentik-username']
|
||||
|
||||
if (!username) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'No autenticado'
|
||||
})
|
||||
}
|
||||
|
||||
// Obtener la API key de OpenAI desde las variables de entorno
|
||||
const apiKey = process.env.OPENAI_API_KEY
|
||||
if (!apiKey) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: 'API Key de OpenAI no configurada'
|
||||
})
|
||||
}
|
||||
|
||||
// Leer el archivo de audio del request
|
||||
const form = await readMultipartFormData(event)
|
||||
if (!form || form.length === 0) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'No se recibió ningún archivo de audio'
|
||||
})
|
||||
}
|
||||
|
||||
// Encontrar el archivo de audio
|
||||
const audioFile = form.find(part => part.name === 'file')
|
||||
if (!audioFile) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'No se encontró el archivo de audio en el formulario'
|
||||
})
|
||||
}
|
||||
|
||||
// Obtener parámetros opcionales
|
||||
const languageParam = form.find(part => part.name === 'language')
|
||||
const promptParam = form.find(part => part.name === 'prompt')
|
||||
|
||||
const language = languageParam?.data.toString() || 'es'
|
||||
const prompt = promptParam?.data.toString()
|
||||
|
||||
// Crear FormData para enviar a OpenAI
|
||||
const formData = new FormData()
|
||||
|
||||
// Crear un Blob desde el buffer
|
||||
const blob = new Blob([audioFile.data], {
|
||||
type: audioFile.type || 'audio/webm'
|
||||
})
|
||||
|
||||
formData.append('file', blob, audioFile.filename || 'audio.webm')
|
||||
formData.append('model', 'whisper-1')
|
||||
formData.append('language', language)
|
||||
|
||||
if (prompt) {
|
||||
formData.append('prompt', prompt)
|
||||
}
|
||||
|
||||
// Enviar a OpenAI Whisper API
|
||||
const response = await fetch('https://api.openai.com/v1/audio/transcriptions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiKey}`
|
||||
},
|
||||
body: formData as any
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.text()
|
||||
console.error('Error de OpenAI:', errorData)
|
||||
throw createError({
|
||||
statusCode: response.status,
|
||||
message: `Error de OpenAI Whisper: ${response.statusText}`
|
||||
})
|
||||
}
|
||||
|
||||
const result = await response.json()
|
||||
|
||||
// Log para auditoría
|
||||
console.log(`[Whisper] Transcripción exitosa para usuario: ${username}`)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
transcription: result.text,
|
||||
user: username
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('[Whisper] Error:', error)
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: error.message || 'Error al procesar la transcripción'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user