Repository navigation
Expand file tree
/
Copy pathapi.py
More file actions
209 lines (173 loc) · 8.22 KB
/
Copy pathapi.py
File metadata and controls
209 lines (173 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""Local, single-user API. Start from the repository root with uv run start.py --api."""
from pathlib import Path
import shutil
import sys
from threading import Lock
from typing import Literal
from urllib.parse import urlparse
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from pydantic import BaseModel, ConfigDict, Field
def _configure_utf8_console():
"""Allow Rich and task threads to print Unicode on Windows."""
for stream in (sys.stdout, sys.stderr):
if hasattr(stream, "reconfigure"):
stream.reconfigure(encoding="utf-8", errors="replace")
_configure_utf8_console()
from core.pipeline import get_steps
from core.task_runner import TaskRunner
from core.utils.config_utils import load_key, update_key
app = FastAPI(title="VideoLingo", description="One operation at a time, using config.yaml and output/.")
runner = TaskRunner()
operation_lock = Lock()
OUTPUT = Path("output")
class InputRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
source: str = Field(min_length=1, description="Local file path or HTTP(S) video URL; an SRT file to translate subtitles without a video")
subtitles: str | None = Field(default=None, min_length=1, description="Local SRT file that is used instead of the recognition")
existing: Literal["reject", "archive", "replace"] = "reject"
class RunRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
stage: Literal["transcribe", "subtitles", "dubbing", "all"] = "all"
dubbing: bool = False
target_language: str | None = Field(default=None, min_length=1)
source_language: str | None = Field(default=None, min_length=1)
def require_idle():
if runner.is_active:
raise HTTPException(409, "An operation is active; wait for /status before continuing.")
def archive_output():
from core.utils.onekeycleanup import cleanup
cleanup()
if OUTPUT.exists() and any(OUTPUT.iterdir()):
raise RuntimeError("Some output files could not be archived; inspect output/ before retrying.")
def subtitle_file(path):
"""The SRT file of a request, which has to be one with subtitles in it."""
from core._2_import_subtitles import read_cues
path = Path(path).resolve()
if not path.is_file() or path.suffix.lower() != ".srt" or path.is_relative_to(OUTPUT.resolve()):
raise HTTPException(422, "Subtitles must be an existing SRT file outside output/")
try:
read_cues(path.read_bytes())
except ValueError as exc:
raise HTTPException(422, str(exc)) from exc
return path
def prepare_input(source, existing, subtitles=None):
from core._1_ytdlp import (download_video_ytdlp, write_input_manifest,
sanitize_filename, GENERATED_AUDIO_NAMES, GENERATED_VIDEO_NAMES)
from core._2_import_subtitles import add_input_subtitles
if OUTPUT.exists() and any(OUTPUT.iterdir()):
if existing == "archive":
archive_output()
elif existing == "replace":
shutil.rmtree(OUTPUT)
OUTPUT.mkdir(exist_ok=True)
if urlparse(source).scheme in {"http", "https"}:
download_video_ytdlp(source, resolution=load_key("ytb_resolution"))
elif Path(source).suffix.lower() == ".srt":
subtitles = Path(source)
else:
source = Path(source)
name = sanitize_filename(source.stem) + source.suffix.lower()
if name.lower() in GENERATED_AUDIO_NAMES | GENERATED_VIDEO_NAMES:
name = "input_" + name
destination = OUTPUT / name
shutil.copy2(source, destination)
kind = "video" if source.suffix.lower()[1:] in load_key("allowed_video_formats") else "audio"
write_input_manifest(str(destination), kind)
if subtitles:
add_input_subtitles(subtitles.name, subtitles.read_bytes())
@app.post("/input", status_code=202)
def set_input(request: InputRequest):
"""Prepare input in the background; poll /status before POST /run."""
with operation_lock:
require_idle()
parsed = urlparse(request.source)
if parsed.scheme in {"http", "https"}:
if not parsed.netloc:
raise HTTPException(422, "Invalid URL")
source = request.source
else:
source = Path(request.source).resolve()
if not source.is_file():
raise HTTPException(422, "Source file does not exist")
if source.is_relative_to(OUTPUT.resolve()):
raise HTTPException(422, "Source must be outside output/; use /run for an existing input.")
formats = load_key("allowed_video_formats") + load_key("allowed_audio_formats")
if source.suffix.lower() == ".srt":
source = subtitle_file(source)
elif source.suffix.lower()[1:] not in formats:
raise HTTPException(422, "Unsupported media format")
source = str(source)
subtitles = subtitle_file(request.subtitles) if request.subtitles else None
if OUTPUT.exists() and any(OUTPUT.iterdir()) and request.existing == "reject":
raise HTTPException(409, "output/ is not empty; explicitly choose archive or replace.")
runner.start([("Prepare input", lambda: prepare_input(source, request.existing, subtitles))])
return {"accepted": True}
@app.post("/run", status_code=202)
def run(request: RunRequest):
"""Run or retry the shared pipeline. Parameters are saved to config.yaml."""
with operation_lock:
require_idle()
from core._1_ytdlp import find_media_file
try:
_, kind = find_media_file()
except ValueError as exc:
raise HTTPException(409, str(exc)) from exc
needs_dubbing = request.stage == "dubbing" or (request.stage == "all" and request.dubbing)
if needs_dubbing and kind in ("audio", "subtitle"):
raise HTTPException(422, "Input without a video supports subtitles only, as in the UI.")
if request.stage == "dubbing" and not all((OUTPUT / name).exists() for name in ("src.srt", "trans.srt")):
raise HTTPException(409, "Generate subtitles before starting dubbing.")
if request.target_language is not None:
update_key("target_language", request.target_language)
if request.source_language is not None:
update_key("whisper.language", request.source_language)
runner.start(get_steps(request.stage, request.dubbing))
return {"accepted": True}
@app.get("/status")
def status():
"""In-memory execution state; output files survive server restarts."""
files = sorted(p.name for p in OUTPUT.glob("*") if p.is_file() and not p.name.startswith('.'))
return {
"state": runner.state,
"active": runner.is_active,
"step": runner.current_label or None,
"step_index": runner.current_step,
"total_steps": runner.total_steps,
"progress": runner.progress,
"error": runner.error_msg or None,
"pause_message": runner.pause_message or None,
"files": files,
}
@app.post("/resume")
def resume():
"""Continue a paused task, such as the checkpoints of pause_before_translate and pause_after_translate."""
with operation_lock:
if runner.state != "paused":
raise HTTPException(409, "No task is paused.")
runner.resume()
return {"state": runner.state}
@app.post("/stop")
def stop():
"""Cooperative stop: wait until active=false before another operation."""
with operation_lock:
runner.stop()
return {"state": runner.state}
@app.get("/files/{name}")
def file(name: str):
path = (OUTPUT / name).resolve()
if path.parent != OUTPUT.resolve() or name.startswith('.') or not path.is_file():
raise HTTPException(404, "Output file not found")
return FileResponse(path, filename=path.name)
@app.post("/archive", status_code=202)
def archive():
"""Move output into history/ using the existing archive behavior."""
with operation_lock:
require_idle()
if not OUTPUT.exists() or not any(OUTPUT.iterdir()):
raise HTTPException(409, "No output to archive")
runner.start([("Archive output", archive_output)])
return {"accepted": True}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)