60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
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} |