add chinese subtitles

This commit is contained in:
2026-10-03 12:14:17 +08:00
parent 138d446d73
commit 6bd64ee4a9
22 changed files with 922 additions and 77 deletions

View File

@@ -45,6 +45,8 @@ from .models import (
SentenceBoundaryDocument,
SentenceExplanationRequest,
SentenceExplanationResponse,
SubtitleTranslationItem,
SubtitleTranslationResponse,
PasswordResetRequest,
SentenceTextUpdate,
UserDubShareSummary,
@@ -333,6 +335,60 @@ def create_app(
raise HTTPException(status_code=503, detail=str(exc)) from exc
return SentenceExplanationResponse(explanation=explanation)
@application.post(
"/api/v1/videos/{video_hash}/subtitle-translation",
response_model=SubtitleTranslationResponse,
)
def translate_subtitles(
video_hash: str = SHA256_PATH,
user: AuthenticatedUser = Depends(current_user),
) -> SubtitleTranslationResponse:
del user
document = video_repository.get_document(video_hash)
if document is None:
raise HTTPException(status_code=404, detail="Course is not available.")
payload = [
(sentence.index, sentence.text or "")
for sentence in document.sentences
if (sentence.text or "").strip()
]
if not payload:
raise HTTPException(status_code=422, detail="No sentence text is available to translate.")
cached = [
SubtitleTranslationItem(index=sentence.index, zh_text=sentence.zh_text)
for sentence in document.sentences
if sentence.zh_text and sentence.zh_text.strip()
]
complete = len(cached) == len(payload) and all(
(sentence.zh_text or "").strip()
for sentence in document.sentences
if (sentence.text or "").strip()
)
if complete:
return SubtitleTranslationResponse(
video_hash=document.video_hash,
translations=cached,
)
video = video_repository.get_video(video_hash)
try:
translated = application.state.ai_tutor.translate_sentences(
payload,
course_title=(video["title"] if video is not None else "") or "",
)
except AITutorError as exc:
raise HTTPException(status_code=503, detail=str(exc)) from exc
saved = video_repository.save_sentence_translations(video_hash, translated)
if saved is None:
raise HTTPException(status_code=404, detail="Course is not available.")
return SubtitleTranslationResponse(
video_hash=saved.video_hash,
translations=[
SubtitleTranslationItem(index=sentence.index, zh_text=sentence.zh_text)
for sentence in saved.sentences
if sentence.zh_text and sentence.zh_text.strip()
],
)
@application.post(
"/api/v1/audio/sentence-boundaries",
response_model=SentenceBoundaryDocument,