Automated Action 92e4d992b2 Implement complete AI video dubbing backend with FastAPI
Features:
- JWT authentication with user registration and login
- Video upload to Amazon S3 with file validation (200MB limit)
- Audio transcription using OpenAI Whisper API
- Text translation using GPT-4 API
- Voice cloning and audio synthesis using ElevenLabs API
- Video processing with ffmpeg for audio replacement
- Complete SQLite database with proper models and migrations
- Background task processing for long-running operations
- Health endpoint and comprehensive API documentation

Tech stack:
- FastAPI with SQLAlchemy ORM
- SQLite database with Alembic migrations
- Amazon S3 for file storage
- OpenAI APIs for transcription and translation
- ElevenLabs API for voice cloning
- ffmpeg for video processing
- JWT authentication with bcrypt password hashing
2025-06-24 17:56:12 +00:00

159 lines
4.4 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status, BackgroundTasks
from sqlalchemy.orm import Session
from pydantic import BaseModel
from app.db.session import get_db
from app.models.user import User
from app.models.video import Video
from app.models.transcription import Transcription
from app.models.translation import Translation
from app.utils.auth import get_current_user
from app.services.translation_service import translate_text
router = APIRouter()
class TranslationResponse(BaseModel):
id: int
video_id: int
text: str
created_at: str
class Config:
orm_mode = True
class TranslationStartResponse(BaseModel):
message: str
video_id: int
async def background_translate(video_id: int, transcript_text: str, target_language: str, source_language: str, db: Session):
try:
# Update video status
video = db.query(Video).filter(Video.id == video_id).first()
if video:
video.status = "translating"
db.commit()
# Translate the text
translated_text = await translate_text(transcript_text, target_language, source_language)
if translated_text:
# Save translation to database
translation = Translation(
video_id=video_id,
text=translated_text
)
db.add(translation)
# Update video status
if video:
video.status = "translated"
db.commit()
else:
# Update video status to error
if video:
video.status = "translation_failed"
db.commit()
except Exception:
# Update video status to error
video = db.query(Video).filter(Video.id == video_id).first()
if video:
video.status = "translation_failed"
db.commit()
@router.post("/{video_id}", response_model=TranslationStartResponse)
async def start_translation(
video_id: int,
background_tasks: BackgroundTasks,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
# Check if video exists and belongs to user
video = db.query(Video).filter(
Video.id == video_id,
Video.user_id == current_user.id
).first()
if not video:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Video not found"
)
# Check if transcription exists
transcription = db.query(Transcription).filter(
Transcription.video_id == video_id
).first()
if not transcription:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Transcription not found. Please transcribe the video first."
)
# Check if translation already exists
existing_translation = db.query(Translation).filter(
Translation.video_id == video_id
).first()
if existing_translation:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Translation already exists for this video"
)
# Start background translation
background_tasks.add_task(
background_translate,
video_id,
transcription.text,
video.language_to,
video.language_from,
db
)
return TranslationStartResponse(
message="Translation started in background",
video_id=video_id
)
@router.get("/{video_id}", response_model=TranslationResponse)
async def get_translation(
video_id: int,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
# Check if video exists and belongs to user
video = db.query(Video).filter(
Video.id == video_id,
Video.user_id == current_user.id
).first()
if not video:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Video not found"
)
# Get translation
translation = db.query(Translation).filter(
Translation.video_id == video_id
).first()
if not translation:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Translation not found"
)
return TranslationResponse(
id=translation.id,
video_id=translation.video_id,
text=translation.text,
created_at=str(translation.created_at)
)