feat: add DeepSeek subtitle AI workflow
This commit is contained in:
1 parent
bcebeaef82
commit
72072c884c
15 files changed
+710
No files matched your search
@@ -6,6 +6,7 @@ from fastapi import FastAPI, File, Form, HTTPException, UploadFile
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from .ai_config import get_ai_status, load_deepseek_config
|
||||
from .db import (
|
||||
DATABASE_PATH,
|
||||
create_video,
|
||||
@@ -16,7 +17,10 @@ from .db import (
|
||||
table_names,
|
||||
get_video,
|
||||
update_video,
|
||||
upsert_video_subtitle,
|
||||
)
|
||||
from .deepseek_client import DeepSeekClient, DeepSeekError
|
||||
from .subtitle_ai import process_subtitles_with_ai
|
||||
|
||||
|
||||
PROJECT_DIR = Path(__file__).resolve().parent.parent
|
||||
@@ -348,6 +352,73 @@ def api_delete_video(video_id: int) -> dict[str, object]:
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/ai/status")
|
||||
def api_ai_status() -> dict[str, object]:
|
||||
return get_ai_status()
|
||||
|
||||
|
||||
@app.post("/api/ai/test")
|
||||
def api_ai_test() -> dict[str, object]:
|
||||
status = get_ai_status()
|
||||
if not status["configured"]:
|
||||
raise HTTPException(status_code=400, detail=status["message"])
|
||||
return {"ok": True, "message": "DeepSeek API 配置已读取,处理字幕时会正式调用。"}
|
||||
|
||||
|
||||
@app.post("/api/videos/{video_id}/ai/subtitles")
|
||||
def api_process_video_subtitles(video_id: int) -> dict[str, object]:
|
||||
status = get_ai_status()
|
||||
if not status["configured"]:
|
||||
raise HTTPException(status_code=400, detail=status["message"])
|
||||
|
||||
video = get_video(video_id)
|
||||
if video is None:
|
||||
raise HTTPException(status_code=404, detail="视频不存在")
|
||||
|
||||
subtitles = video.get("subtitles", {})
|
||||
if not isinstance(subtitles, dict):
|
||||
raise HTTPException(status_code=400, detail="字幕数据异常。")
|
||||
|
||||
english_subtitle = subtitles.get("en", {})
|
||||
if not isinstance(english_subtitle, dict) or not english_subtitle.get("content"):
|
||||
raise HTTPException(status_code=400, detail="请先上传英文字幕。")
|
||||
|
||||
try:
|
||||
result = process_subtitles_with_ai(
|
||||
str(english_subtitle["content"]),
|
||||
DeepSeekClient(load_deepseek_config()),
|
||||
)
|
||||
except (DeepSeekError, ValueError) as error:
|
||||
raise HTTPException(status_code=400, detail=str(error)) from error
|
||||
|
||||
upsert_video_subtitle(
|
||||
video_id,
|
||||
language_code="en",
|
||||
language_name="English",
|
||||
format="vtt",
|
||||
file_path=str(english_subtitle.get("filePath") or ""),
|
||||
content=str(result["subtitle_en_content"]),
|
||||
)
|
||||
|
||||
chinese_subtitle = subtitles.get("zh", {})
|
||||
chinese_file_path = ""
|
||||
if isinstance(chinese_subtitle, dict):
|
||||
chinese_file_path = str(chinese_subtitle.get("filePath") or "")
|
||||
upsert_video_subtitle(
|
||||
video_id,
|
||||
language_code="zh",
|
||||
language_name="中文",
|
||||
format="vtt",
|
||||
file_path=chinese_file_path,
|
||||
content=str(result["subtitle_zh_content"]),
|
||||
)
|
||||
|
||||
return {
|
||||
"video": get_video(video_id),
|
||||
"report": result["report"],
|
||||
}
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_index() -> FileResponse:
|
||||
return FileResponse(PROJECT_DIR / "index.html")
|
||||
|
||||
Reference in new issue
Block a user