feat(srt translation): add api for srt translation
This commit is contained in:
60
app/api/v1/srt_translator.py
Normal file
60
app/api/v1/srt_translator.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from app.models.srt_translation import SRTTranslationRequest, SRTTranslationResponse
|
||||
from app.services.srt_translator import process_srt_translation
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/translate-srt", response_model=SRTTranslationResponse)
|
||||
async def translate_srt_file(request: SRTTranslationRequest):
|
||||
"""
|
||||
Translate SRT file from Japanese to Japanese with English translation
|
||||
"""
|
||||
print(f"🔍 API Called with: {request.dict()}")
|
||||
|
||||
try:
|
||||
result = await process_srt_translation(
|
||||
input_path=request.input_path,
|
||||
output_path=request.output_path
|
||||
)
|
||||
|
||||
print(f"🔍 API Returning: {result}")
|
||||
|
||||
return SRTTranslationResponse(
|
||||
success=result["success"],
|
||||
message=result["message"],
|
||||
output_path=result["output_path"],
|
||||
total_subtitles=result["total_subtitles"]
|
||||
)
|
||||
|
||||
except FileNotFoundError:
|
||||
error_msg = f"Input file not found: {request.input_path}"
|
||||
print(f"❌ {error_msg}")
|
||||
raise HTTPException(status_code=404, detail=error_msg)
|
||||
except Exception as e:
|
||||
error_msg = f"Translation failed: {str(e)}"
|
||||
print(f"❌ {error_msg}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
raise HTTPException(status_code=500, detail=error_msg)
|
||||
|
||||
@router.post("/batch-translate-srt")
|
||||
async def batch_translate_srt(requests: list[SRTTranslationRequest]):
|
||||
"""
|
||||
Batch translate multiple SRT files
|
||||
"""
|
||||
results = []
|
||||
for request in requests:
|
||||
try:
|
||||
result = await process_srt_translation(
|
||||
input_path=request.input_path,
|
||||
output_path=request.output_path
|
||||
)
|
||||
results.append(result)
|
||||
except Exception as e:
|
||||
results.append({
|
||||
"success": False,
|
||||
"message": f"Failed: {str(e)}",
|
||||
"input_path": request.input_path
|
||||
})
|
||||
|
||||
return {"results": results}
|
||||
@@ -1,6 +1,7 @@
|
||||
from fastapi import APIRouter
|
||||
from app.models.text_generation import BPOMMobileResponseTextGenerationRequest, BPOMMobileResponseTextGenerationResponse
|
||||
from app.services.text_generation import generate_text
|
||||
from app.core.deepseek_client import chat_with_openai
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user