fix: constrain subtitle AI to preserve timestamps

This commit is contained in:
TTlaji committed 2026-06-03 14:07:33 +08:00
1 parent 5ff72bf1b6
commit ae4791143c
2 files changed
+19 -2

No files matched your search

+6 -1
View File
@@ -6,7 +6,11 @@ from .subtitle_tools import build_vtt, clean_english_subtitle
TRANSLATION_SYSTEM_PROMPT = """
你是英语学习产品的字幕翻译助手。输入是按顺序排列的英文字幕。
请只返回 JSON,格式为 {"translations":[{"index":1,"zh":"中文翻译"}]}。
要求:中文自然,适合英语学习;不要增删字幕条数;不要解释。
要求:
1. 只翻译英文文本,不要改写原文含义。
2. 不要修改时间轴,不要生成任何 start/end/time 字段。
3. 不要增删字幕条数,每个 index 必须返回一条中文。
4. 中文自然,适合英语学习;不要解释。
""".strip()
@@ -45,5 +49,6 @@ def process_subtitles_with_ai(english_content: str, client) -> dict[str, object]
"empty_count": cleaned.empty_count,
"translation_count": len(zh_cues),
"timestamp_calibrated": False,
"timestamp_note": "AI 仅翻译文本,中文字幕时间轴从英文字幕原样复制。",
},
}
+13 -1
View File
@@ -8,9 +8,17 @@ from backend.subtitle_ai import process_subtitles_with_ai
class FakeClient:
def chat_json(self, system_prompt, user_payload):
assert "不要修改时间轴" in system_prompt
assert "start" not in user_payload["cues"][0]
assert "end" not in user_payload["cues"][0]
return {
"translations": [
{"index": 1, "zh": "我第一次做投资人是在 24 岁。"}
{
"index": 1,
"start": "00:00:99.000",
"end": "00:00:99.999",
"zh": "我第一次做投资人是在 24 岁。",
}
]
}
@@ -31,6 +39,10 @@ My first job as an investor was when I was 24 years old,
assert "我第一次做投资人是在 24 岁。" in result["subtitle_zh_content"]
assert result["report"]["duplicate_count"] == 1
assert result["report"]["translation_count"] == 1
assert "00:00:00.872 --> 00:00:04.959" in result["subtitle_en_content"]
assert "00:00:00.872 --> 00:00:04.959" in result["subtitle_zh_content"]
assert "timestamp_calibrated" in result["report"]
assert result["report"]["timestamp_calibrated"] is False
if __name__ == "__main__":