feat: add DeepSeek subtitle AI workflow

This commit is contained in:
TTlaji committed 2026-06-03 00:57:59 +08:00
1 parent bcebeaef82
commit 72072c884c
15 files changed
+710

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from backend import ai_config
from backend.ai_config import get_ai_status, load_deepseek_config
def test_ai_status_reports_missing_key(monkeypatch):
monkeypatch.delenv("DEEPSEEK_API_KEY", raising=False)
status = get_ai_status()
assert status["configured"] is False
assert "DEEPSEEK_API_KEY" in status["message"]
def test_load_deepseek_config_uses_defaults(monkeypatch):
monkeypatch.setenv("DEEPSEEK_API_KEY", "test-key")
monkeypatch.delenv("DEEPSEEK_BASE_URL", raising=False)
monkeypatch.delenv("DEEPSEEK_MODEL", raising=False)
config = load_deepseek_config()
assert config.api_key == "test-key"
assert config.base_url == "https://api.deepseek.com"
assert config.model == "deepseek-chat"
class EnvPatch:
def __init__(self):
self.originals = {}
def setenv(self, key, value):
import os
self.originals.setdefault(key, os.environ.get(key))
os.environ[key] = value
def delenv(self, key, raising=True):
import os
self.originals.setdefault(key, os.environ.get(key))
if key in os.environ:
del os.environ[key]
elif raising:
raise KeyError(key)
def restore(self):
import os
for key, value in self.originals.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
if __name__ == "__main__":
patch = EnvPatch()
original_read_env_file = ai_config.read_env_file
ai_config.read_env_file = lambda path=ai_config.ENV_PATH: {}
try:
test_ai_status_reports_missing_key(patch)
patch.restore()
test_load_deepseek_config_uses_defaults(patch)
finally:
patch.restore()
ai_config.read_env_file = original_read_env_file
+65
View File
@@ -0,0 +1,65 @@
import sys
from pathlib import Path
from fastapi import HTTPException
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from backend import ai_config, main
class EnvPatch:
def __init__(self):
self.originals = {}
def delenv(self, key, raising=True):
import os
self.originals.setdefault(key, os.environ.get(key))
if key in os.environ:
del os.environ[key]
elif raising:
raise KeyError(key)
def restore(self):
import os
for key, value in self.originals.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
def test_ai_status_route_reports_missing_key(monkeypatch):
monkeypatch.delenv("DEEPSEEK_API_KEY", raising=False)
status = main.api_ai_status()
assert status["configured"] is False
assert "DEEPSEEK_API_KEY" in status["message"]
def test_ai_test_route_rejects_missing_key(monkeypatch):
monkeypatch.delenv("DEEPSEEK_API_KEY", raising=False)
try:
main.api_ai_test()
except HTTPException as error:
assert error.status_code == 400
assert "DEEPSEEK_API_KEY" in error.detail
else:
raise AssertionError("api_ai_test should reject missing DeepSeek API key")
if __name__ == "__main__":
patch = EnvPatch()
original_read_env_file = ai_config.read_env_file
ai_config.read_env_file = lambda path=ai_config.ENV_PATH: {}
try:
test_ai_status_route_reports_missing_key(patch)
patch.restore()
test_ai_test_route_rejects_missing_key(patch)
finally:
patch.restore()
ai_config.read_env_file = original_read_env_file
+37
View File
@@ -0,0 +1,37 @@
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from backend.subtitle_ai import process_subtitles_with_ai
class FakeClient:
def chat_json(self, system_prompt, user_payload):
return {
"translations": [
{"index": 1, "zh": "我第一次做投资人是在 24 岁。"}
]
}
def test_process_subtitles_with_ai_returns_clean_en_and_zh_vtt():
raw = """WEBVTT
00:00:00.872 --> 00:00:04.959
My first job as an investor was when I was 24 years old,
00:00:00.872 --> 00:00:04.959
My first job as an investor was when I was 24 years old,
"""
result = process_subtitles_with_ai(raw, FakeClient())
assert "My first job as an investor" in result["subtitle_en_content"]
assert "我第一次做投资人是在 24 岁。" in result["subtitle_zh_content"]
assert result["report"]["duplicate_count"] == 1
assert result["report"]["translation_count"] == 1
if __name__ == "__main__":
test_process_subtitles_with_ai_returns_clean_en_and_zh_vtt()
+48
View File
@@ -0,0 +1,48 @@
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from backend.subtitle_tools import build_vtt, clean_english_subtitle
def test_clean_english_subtitle_removes_empty_and_duplicate_cues():
raw = """WEBVTT
00:00:00.000 --> 00:00:00.001
 
00:00:00.872 --> 00:00:04.959
My first job as an investor
was when I was 24 years old,
00:00:00.872 --> 00:00:04.959
My first job as an investor
was when I was 24 years old,
"""
result = clean_english_subtitle(raw)
assert result.original_count == 3
assert result.cleaned_count == 1
assert result.duplicate_count == 1
assert result.empty_count == 1
assert result.cues[0].start == "00:00:00.872"
assert result.cues[0].end == "00:00:04.959"
assert result.cues[0].text == "My first job as an investor\nwas when I was 24 years old,"
def test_build_vtt_outputs_numbered_blocks():
result = build_vtt([
{
"start": "00:00:00.872",
"end": "00:00:04.959",
"text": "My first job as an investor",
}
])
assert result == "WEBVTT\n\n1\n00:00:00.872 --> 00:00:04.959\nMy first job as an investor\n"
if __name__ == "__main__":
test_clean_english_subtitle_removes_empty_and_duplicate_cues()
test_build_vtt_outputs_numbered_blocks()