
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
69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
import os
|
|
import openai
|
|
from typing import Optional
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
if not OPENAI_API_KEY:
|
|
logger.warning("OpenAI API key not configured")
|
|
|
|
openai.api_key = OPENAI_API_KEY
|
|
|
|
|
|
async def translate_text(text: str, target_language: str, source_language: str = "auto") -> Optional[str]:
|
|
if not OPENAI_API_KEY:
|
|
logger.error("OpenAI API key not configured")
|
|
return None
|
|
|
|
try:
|
|
# Language mapping for better prompts
|
|
language_names = {
|
|
"en": "English",
|
|
"es": "Spanish",
|
|
"fr": "French",
|
|
"de": "German",
|
|
"it": "Italian",
|
|
"pt": "Portuguese",
|
|
"ru": "Russian",
|
|
"ja": "Japanese",
|
|
"ko": "Korean",
|
|
"zh": "Chinese",
|
|
"ar": "Arabic",
|
|
"hi": "Hindi"
|
|
}
|
|
|
|
target_lang_name = language_names.get(target_language, target_language)
|
|
source_lang_name = language_names.get(source_language, source_language)
|
|
|
|
# Create translation prompt
|
|
if source_language == "auto":
|
|
prompt = f"Translate the following text to {target_lang_name}. Maintain the original tone and meaning:\n\n{text}"
|
|
else:
|
|
prompt = f"Translate the following text from {source_lang_name} to {target_lang_name}. Maintain the original tone and meaning:\n\n{text}"
|
|
|
|
# Use GPT-4 for translation
|
|
response = openai.ChatCompletion.create(
|
|
model="gpt-4",
|
|
messages=[
|
|
{
|
|
"role": "system",
|
|
"content": "You are a professional translator. Provide accurate translations while maintaining the original tone and context. Return only the translated text without any additional commentary."
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": prompt
|
|
}
|
|
],
|
|
max_tokens=2000,
|
|
temperature=0.3
|
|
)
|
|
|
|
translated_text = response.choices[0].message.content.strip()
|
|
return translated_text
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error translating text: {e}")
|
|
return None |