pc translate sentences
This commit is contained in:
@@ -355,6 +355,98 @@ class VideoRepository:
|
||||
],
|
||||
)
|
||||
|
||||
def import_local_subtitle_document(
|
||||
self,
|
||||
document: SentenceBoundaryDocument,
|
||||
) -> SentenceBoundaryDocument:
|
||||
"""Save locally generated English subtitles for translation.
|
||||
|
||||
Official course videos are left unchanged. A previous local-only
|
||||
import is replaced when the English cues changed.
|
||||
"""
|
||||
normalized_hash = normalize_video_hash(document.video_hash)
|
||||
document = document.model_copy(update={"video_hash": normalized_hash})
|
||||
existing_video = self.get_video(normalized_hash)
|
||||
existing_document = self.get_document(normalized_hash)
|
||||
if existing_video is not None and existing_video.get("status") != "local_subtitles":
|
||||
if existing_document is None:
|
||||
raise KeyError(normalized_hash)
|
||||
return existing_document
|
||||
if existing_document is not None and _english_fingerprint(existing_document) == _english_fingerprint(document):
|
||||
return existing_document
|
||||
|
||||
now = utc_now()
|
||||
language = next((sentence.language for sentence in document.sentences if sentence.language), None)
|
||||
with self._connect() as connection:
|
||||
if existing_video is None:
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO videos (
|
||||
video_hash, title, filename, stored_filename, content_type,
|
||||
size_bytes, duration_ms, language, status, error_message,
|
||||
algorithm_version, transcription, remote_url, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'local_subtitles', NULL, ?, NULL, NULL, ?, ?)
|
||||
""",
|
||||
(
|
||||
normalized_hash,
|
||||
"Local subtitles",
|
||||
"local-subtitles.json",
|
||||
"",
|
||||
"application/json",
|
||||
0,
|
||||
document.duration_ms,
|
||||
language,
|
||||
document.algorithm_version,
|
||||
now,
|
||||
now,
|
||||
),
|
||||
)
|
||||
else:
|
||||
connection.execute(
|
||||
"""
|
||||
UPDATE videos
|
||||
SET duration_ms = ?, language = COALESCE(?, language),
|
||||
algorithm_version = ?, status = 'local_subtitles',
|
||||
error_message = NULL, updated_at = ?
|
||||
WHERE video_hash = ?
|
||||
""",
|
||||
(
|
||||
document.duration_ms,
|
||||
language,
|
||||
document.algorithm_version,
|
||||
now,
|
||||
normalized_hash,
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"DELETE FROM sentences WHERE video_hash = ?",
|
||||
(normalized_hash,),
|
||||
)
|
||||
connection.executemany(
|
||||
"""
|
||||
INSERT INTO sentences (
|
||||
video_hash, sentence_index, start_ms, end_ms, text,
|
||||
language, zh_text, reference_speech_duration_ms
|
||||
) VALUES (?, ?, ?, ?, ?, ?, NULL, ?)
|
||||
""",
|
||||
[
|
||||
(
|
||||
normalized_hash,
|
||||
sentence.index,
|
||||
sentence.start_ms,
|
||||
sentence.end_ms,
|
||||
sentence.text,
|
||||
sentence.language,
|
||||
sentence.reference_speech_duration_ms,
|
||||
)
|
||||
for sentence in document.sentences
|
||||
],
|
||||
)
|
||||
imported = self.get_document(normalized_hash)
|
||||
if imported is None:
|
||||
raise KeyError(normalized_hash)
|
||||
return imported
|
||||
|
||||
def mark_oss_uploaded(self, video_hash: str, remote_url: str) -> None:
|
||||
with self._connect() as connection:
|
||||
connection.execute(
|
||||
@@ -389,6 +481,7 @@ class VideoRepository:
|
||||
SELECT v.*, COUNT(s.sentence_index) AS sentence_count
|
||||
FROM videos v
|
||||
LEFT JOIN sentences s ON s.video_hash = v.video_hash
|
||||
WHERE v.status != 'local_subtitles'
|
||||
GROUP BY v.video_hash
|
||||
ORDER BY v.created_at DESC
|
||||
"""
|
||||
@@ -1316,6 +1409,13 @@ class VideoRepository:
|
||||
|
||||
|
||||
|
||||
def _english_fingerprint(document: SentenceBoundaryDocument) -> tuple:
|
||||
return tuple(
|
||||
(sentence.index, sentence.start_ms, sentence.end_ms, (sentence.text or "").strip())
|
||||
for sentence in document.sentences
|
||||
)
|
||||
|
||||
|
||||
def _sentence_from_row(row: sqlite3.Row) -> SentenceBoundary:
|
||||
keys = set(row.keys())
|
||||
zh_text = row["zh_text"] if "zh_text" in keys else None
|
||||
|
||||
Reference in New Issue
Block a user