feat: Celery setup and transcription task — Whisper + FFmpeg pipeline
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import os
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.tasks.celery_app import celery_app
|
||||
from app.config import settings
|
||||
from app.models.recording import Recording, RecordingStatus
|
||||
from app.models.transcription import Transcription
|
||||
from app.services.transcription import extract_audio, transcribe_audio
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
sync_db_url = settings.database_url.replace("+asyncpg", "+psycopg2").replace(
|
||||
"postgresql+psycopg2", "postgresql"
|
||||
)
|
||||
sync_engine = create_engine(sync_db_url)
|
||||
SyncSession = sessionmaker(sync_engine)
|
||||
|
||||
|
||||
@celery_app.task(bind=True, name="app.tasks.transcription.transcribe_recording")
|
||||
def transcribe_recording(self, recording_id: str):
|
||||
rec_uuid = uuid.UUID(recording_id)
|
||||
|
||||
with SyncSession() as db:
|
||||
recording = db.execute(
|
||||
select(Recording).where(Recording.id == rec_uuid)
|
||||
).scalar_one_or_none()
|
||||
|
||||
if not recording:
|
||||
return {"error": "Recording not found"}
|
||||
|
||||
recording.status = RecordingStatus.processing
|
||||
db.commit()
|
||||
|
||||
try:
|
||||
file_path = recording.file_path
|
||||
audio_path = file_path
|
||||
|
||||
video_formats = {"mp4", "webm", "mkv", "avi", "mov"}
|
||||
if recording.format in video_formats:
|
||||
audio_path = extract_audio(file_path)
|
||||
|
||||
result = transcribe_audio(audio_path)
|
||||
|
||||
if audio_path != file_path and os.path.exists(audio_path):
|
||||
os.remove(audio_path)
|
||||
|
||||
transcription = Transcription(
|
||||
recording_id=rec_uuid,
|
||||
text=result["text"],
|
||||
segments=result["segments"],
|
||||
language=result["language"],
|
||||
whisper_model=settings.whisper_model,
|
||||
processing_time=result["processing_time"],
|
||||
)
|
||||
db.add(transcription)
|
||||
recording.status = RecordingStatus.done
|
||||
db.commit()
|
||||
db.refresh(transcription)
|
||||
|
||||
return {"transcription_id": str(transcription.id), "status": "done"}
|
||||
|
||||
except Exception as e:
|
||||
recording.status = RecordingStatus.error
|
||||
db.commit()
|
||||
raise self.retry(exc=e, max_retries=1, countdown=30)
|
||||
Reference in New Issue
Block a user