68 lines
2.7 KiB
Python
68 lines
2.7 KiB
Python
import os
|
|
import time
|
|
from sqlalchemy.orm import Session
|
|
import google.generativeai as genai
|
|
from app.models.evidence import Evidence
|
|
from app.core.config import settings
|
|
|
|
class TranscriptionWorker:
|
|
def __init__(self, db: Session):
|
|
self._db = db
|
|
|
|
def process_transcription(self, evidence_id: int):
|
|
"""
|
|
Background task to transcribe audio.
|
|
"""
|
|
try:
|
|
evidence = self._db.query(Evidence).filter(Evidence.id == evidence_id).first()
|
|
if not evidence:
|
|
return
|
|
|
|
evidence.transcription_status = "processing"
|
|
self._db.commit()
|
|
|
|
file_path = evidence.file_path
|
|
if not os.path.exists(file_path):
|
|
evidence.transcription_status = "error"
|
|
evidence.transcription = "Error: File not found"
|
|
self._db.commit()
|
|
return
|
|
|
|
api_key = settings.GOOGLE_API_KEY
|
|
if api_key:
|
|
try:
|
|
genai.configure(api_key=api_key)
|
|
|
|
# Upload to Gemini (Media Service)
|
|
audio_file = genai.upload_file(path=file_path, mime_type=evidence.media_type or "audio/wav")
|
|
|
|
# Use Gemini 2.0 Flash Lite (as requested in original code, though it said 1.5 in comment)
|
|
model = genai.GenerativeModel("gemini-2.0-flash-lite")
|
|
response = model.generate_content([
|
|
"Por favor, transcribe exactamente lo que se dice en este audio. Solo devuelve el texto transcrito.",
|
|
audio_file
|
|
])
|
|
|
|
evidence.transcription = response.text
|
|
evidence.transcription_status = "completed"
|
|
except Exception as e:
|
|
evidence.transcription_status = "error"
|
|
evidence.transcription = f"Error: {str(e)}"
|
|
else:
|
|
# Mock transcription if no API key
|
|
time.sleep(5) # Simulate work
|
|
evidence.transcription = f"[LOCAL MOCK TRANSCRIPTION] Transcripción asíncrona completada para {os.path.basename(file_path)}"
|
|
evidence.transcription_status = "completed"
|
|
|
|
self._db.commit()
|
|
except Exception as e:
|
|
print(f"Transcription error: {e}")
|
|
try:
|
|
evidence = self._db.query(Evidence).filter(Evidence.id == evidence_id).first()
|
|
if evidence:
|
|
evidence.transcription_status = "error"
|
|
evidence.transcription = f"Unexpected error: {str(e)}"
|
|
self._db.commit()
|
|
except:
|
|
pass
|