continue fixing
This commit is contained in:
@@ -29,6 +29,7 @@ from fastapi.staticfiles import StaticFiles
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
from starlette.requests import ClientDisconnect
|
||||
from .ai_tutor import AITutor, AITutorError
|
||||
from .translation_jobs import SubtitleTranslationService
|
||||
|
||||
from .assessment import AssessmentService
|
||||
from .auth import AuthenticatedUser
|
||||
@@ -45,7 +46,6 @@ from .models import (
|
||||
SentenceBoundaryDocument,
|
||||
SentenceExplanationRequest,
|
||||
SentenceExplanationResponse,
|
||||
SubtitleTranslationItem,
|
||||
SubtitleTranslationResponse,
|
||||
PasswordResetRequest,
|
||||
SentenceTextUpdate,
|
||||
@@ -129,6 +129,10 @@ def create_app(
|
||||
application.state.oss_uploader = oss
|
||||
application.state.video_processor = processor
|
||||
application.state.assessment_service = assessment_service
|
||||
application.state.subtitle_translations = SubtitleTranslationService(
|
||||
video_repository,
|
||||
lambda: application.state.ai_tutor,
|
||||
)
|
||||
|
||||
static_dir = Path(__file__).resolve().parent / "static"
|
||||
if static_dir.is_dir():
|
||||
@@ -338,14 +342,7 @@ def create_app(
|
||||
def stored_subtitle_translations(
|
||||
document: SentenceBoundaryDocument,
|
||||
) -> SubtitleTranslationResponse:
|
||||
return SubtitleTranslationResponse(
|
||||
video_hash=document.video_hash,
|
||||
translations=[
|
||||
SubtitleTranslationItem(index=sentence.index, zh_text=sentence.zh_text)
|
||||
for sentence in document.sentences
|
||||
if sentence.zh_text and sentence.zh_text.strip()
|
||||
],
|
||||
)
|
||||
return application.state.subtitle_translations.snapshot(document)
|
||||
|
||||
@application.get(
|
||||
"/api/v1/videos/{video_hash}/subtitle-translation",
|
||||
@@ -356,10 +353,10 @@ def create_app(
|
||||
user: AuthenticatedUser = Depends(current_user),
|
||||
) -> SubtitleTranslationResponse:
|
||||
del user
|
||||
document = video_repository.get_document(video_hash)
|
||||
if document is None:
|
||||
snapshot = application.state.subtitle_translations.current(video_hash)
|
||||
if snapshot is None:
|
||||
raise HTTPException(status_code=404, detail="Course is not available.")
|
||||
return stored_subtitle_translations(document)
|
||||
return snapshot
|
||||
|
||||
@application.post(
|
||||
"/api/v1/videos/{video_hash}/subtitle-translation",
|
||||
@@ -370,52 +367,14 @@ def create_app(
|
||||
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 = stored_subtitle_translations(document).translations
|
||||
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 stored_subtitle_translations(document)
|
||||
video = video_repository.get_video(video_hash)
|
||||
existing = {
|
||||
item.index: item.zh_text
|
||||
for item in cached
|
||||
}
|
||||
try:
|
||||
translated = application.state.ai_tutor.translate_sentences(
|
||||
payload,
|
||||
course_title=(video["title"] if video is not None else "") or "",
|
||||
existing=existing,
|
||||
persist=lambda part: video_repository.save_sentence_translations(
|
||||
video_hash,
|
||||
part,
|
||||
),
|
||||
)
|
||||
return application.state.subtitle_translations.ensure(video_hash)
|
||||
except KeyError:
|
||||
raise HTTPException(status_code=404, detail="Course is not available.") from None
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=422, detail=str(exc)) from exc
|
||||
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",
|
||||
|
||||
Reference in New Issue
Block a user