aivideodubbingapi-r08gi1/app/services/transcription_service.py
Automated Action 74c911b746 Fix dependency issues and update OpenAI API usage
- Updated OpenAI package from 1.3.7 to 1.51.0 for latest API compatibility
- Added PyTorch and torchaudio dependencies for Whisper model support
- Fixed OpenAI API calls to use new AsyncOpenAI client format
- Updated transcription service to use client.audio.transcriptions.create()
- Updated translation service to use client.chat.completions.create()
- Added proper logging to language detection service
- Added environment variable loading with python-dotenv in main.py
- Fixed import order to comply with linting standards

🤖 Generated with BackendIM

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-28 08:19:30 +00:00

60 lines
1.8 KiB
Python

import os
from openai import AsyncOpenAI
from typing import Optional
import logging
import tempfile
from app.services.s3_service import download_file_from_s3
logger = logging.getLogger(__name__)
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
logger.warning("OpenAI API key not configured")
client = AsyncOpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
async def transcribe_video_audio(video_s3_url: str) -> Optional[str]:
if not client:
logger.error("OpenAI API key not configured")
return None
try:
# Extract filename from S3 URL
file_name = video_s3_url.split('/')[-1]
# Download video from S3
video_content = await download_file_from_s3(file_name)
if not video_content:
logger.error("Failed to download video from S3")
return None
# Create temporary file for the video
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:
temp_file.write(video_content)
temp_file_path = temp_file.name
try:
# Transcribe using OpenAI Whisper
with open(temp_file_path, 'rb') as audio_file:
transcript = await client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
# Clean up temporary file
os.unlink(temp_file_path)
return transcript
except Exception as e:
# Clean up temporary file on error
if os.path.exists(temp_file_path):
os.unlink(temp_file_path)
raise e
except Exception as e:
logger.error(f"Error transcribing video: {e}")
return None