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>
This commit is contained in:
Automated Action 2025-06-28 08:19:30 +00:00
parent 55f5e5f5a8
commit 74c911b746
5 changed files with 19 additions and 10 deletions

View File

@ -3,6 +3,9 @@ import tempfile
import whisper
import ffmpeg
from typing import Optional
import logging
logger = logging.getLogger(__name__)
async def detect_language_from_video(video_content: bytes) -> Optional[str]:
@ -51,5 +54,5 @@ async def detect_language_from_video(video_content: bytes) -> Optional[str]:
except Exception as e:
# Log error but don't fail the upload
print(f"Language detection failed: {e}")
logger.error(f"Language detection failed: {e}")
return None

View File

@ -1,5 +1,5 @@
import os
import openai
from openai import AsyncOpenAI
from typing import Optional
import logging
import tempfile
@ -12,11 +12,11 @@ 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
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 OPENAI_API_KEY:
if not client:
logger.error("OpenAI API key not configured")
return None
@ -38,7 +38,7 @@ async def transcribe_video_audio(video_s3_url: str) -> Optional[str]:
try:
# Transcribe using OpenAI Whisper
with open(temp_file_path, 'rb') as audio_file:
transcript = openai.Audio.transcribe(
transcript = await client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"

View File

@ -1,5 +1,5 @@
import os
import openai
from openai import AsyncOpenAI
from typing import Optional
import logging
@ -10,11 +10,11 @@ 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
client = AsyncOpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
async def translate_text(text: str, target_language: str, source_language: str = "auto") -> Optional[str]:
if not OPENAI_API_KEY:
if not client:
logger.error("OpenAI API key not configured")
return None
@ -45,7 +45,7 @@ async def translate_text(text: str, target_language: str, source_language: str =
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(
response = await client.chat.completions.create(
model="gpt-4",
messages=[
{

View File

@ -3,11 +3,15 @@ from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
import logging
from dotenv import load_dotenv
from app.db.session import engine
from app.db.base import Base
from app.routes import auth, videos, transcription, translation, voice_cloning, video_processing, profile
# Load environment variables from .env file
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

View File

@ -4,8 +4,10 @@ python-multipart==0.0.6
sqlalchemy==2.0.23
alembic==1.12.1
boto3==1.34.0
openai==1.3.7
openai==1.51.0
openai-whisper==20231117
torch==2.1.2
torchaudio==2.1.2
python-decouple==3.8
ruff==0.1.6
requests==2.31.0