feat: Celery setup and transcription task — Whisper + FFmpeg pipeline

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-03 22:18:24 +05:00
parent 2a9cf9e224
commit 849eb0ff48
7 changed files with 229 additions and 0 deletions
View File
+27
View File
@@ -0,0 +1,27 @@
from celery import Celery
from celery.schedules import crontab
from app.config import settings
celery_app = Celery(
"meeting_protocol",
broker=settings.redis_url,
backend=settings.redis_url,
)
celery_app.conf.update(
task_serializer="json",
accept_content=["json"],
result_serializer="json",
timezone="UTC",
enable_utc=True,
task_track_started=True,
beat_schedule={
"cleanup-old-data": {
"task": "app.tasks.cleanup.cleanup_old_data",
"schedule": crontab(hour=3, minute=0),
},
},
)
celery_app.autodiscover_tasks(["app.tasks"])
+69
View File
@@ -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)