72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
import os
|
|
import time
|
|
from sqlalchemy.orm import Session
|
|
from models import Evidence
|
|
import google.generativeai as genai
|
|
from database import SessionLocal
|
|
|
|
def process_transcription(evidence_id: int):
|
|
"""
|
|
Background task to transcribe audio.
|
|
In a real scenario, this would call a local model runner like Ollama (Whisper)
|
|
or an external API like Gemini.
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
evidence = db.query(Evidence).filter(Evidence.id == evidence_id).first()
|
|
if not evidence:
|
|
return
|
|
|
|
evidence.transcription_status = "processing"
|
|
db.commit()
|
|
|
|
# Simulate local processing or call a local model
|
|
# For now, we'll try to use Gemini if available, or a mock
|
|
|
|
file_path = evidence.file_path
|
|
if not os.path.exists(file_path):
|
|
evidence.transcription_status = "error"
|
|
evidence.transcription = "Error: File not found"
|
|
db.commit()
|
|
return
|
|
|
|
api_key = os.getenv("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 1.5 Flash for audio-to-text
|
|
model = genai.GenerativeModel("gemini-2.5-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 (Local Model Simulation)
|
|
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"
|
|
|
|
db.commit()
|
|
except Exception as e:
|
|
print(f"Transcription error: {e}")
|
|
try:
|
|
evidence = db.query(Evidence).filter(Evidence.id == evidence_id).first()
|
|
if evidence:
|
|
evidence.transcription_status = "error"
|
|
evidence.transcription = f"Unexpected error: {str(e)}"
|
|
db.commit()
|
|
except:
|
|
pass
|
|
finally:
|
|
db.close()
|