@chad.dev/dsh-adclip 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +28 -2
- package/client.js +2157 -0
- package/cordis.patch.yml +10 -0
- package/engine/requirements.txt +9 -0
- package/engine/src/adclip/__init__.py +0 -0
- package/engine/src/adclip/api.py +676 -0
- package/engine/src/adclip/image_params.py +64 -0
- package/engine/src/adclip/intents.py +190 -0
- package/engine/src/adclip/llm.py +142 -0
- package/engine/src/adclip/media/__init__.py +0 -0
- package/engine/src/adclip/media/asr.py +79 -0
- package/engine/src/adclip/media/ffmpeg.py +294 -0
- package/engine/src/adclip/media/stitch.py +124 -0
- package/engine/src/adclip/media/subtitles.py +45 -0
- package/engine/src/adclip/media/vision.py +105 -0
- package/engine/src/adclip/models.py +127 -0
- package/engine/src/adclip/params.py +363 -0
- package/engine/src/adclip/personas.py +60 -0
- package/engine/src/adclip/pipeline/__init__.py +0 -0
- package/engine/src/adclip/pipeline/analyze.py +133 -0
- package/engine/src/adclip/pipeline/generate.py +201 -0
- package/engine/src/adclip/pipeline/image_runner.py +131 -0
- package/engine/src/adclip/pipeline/persona.py +56 -0
- package/engine/src/adclip/pipeline/post.py +33 -0
- package/engine/src/adclip/pipeline/qa.py +41 -0
- package/engine/src/adclip/pipeline/replace.py +191 -0
- package/engine/src/adclip/pipeline/runner.py +138 -0
- package/engine/src/adclip/pipeline/script.py +226 -0
- package/engine/src/adclip/pipeline/segment.py +87 -0
- package/engine/src/adclip/providers/__init__.py +0 -0
- package/engine/src/adclip/providers/ark.py +122 -0
- package/engine/src/adclip/providers/base.py +30 -0
- package/engine/src/adclip/providers/discovery.py +56 -0
- package/engine/src/adclip/providers/kling.py +101 -0
- package/engine/src/adclip/providers/minimax.py +155 -0
- package/engine/src/adclip/providers/minimax_image.py +86 -0
- package/engine/src/adclip/settings.py +109 -0
- package/engine/src/adclip/store.py +378 -0
- package/host/dsh.js +60 -0
- package/host/engine.js +265 -0
- package/host/proxy.js +142 -0
- package/host/supervisor.js +251 -0
- package/icon.svg +6 -0
- package/index.js +39 -0
- package/locale/en.json +12 -0
- package/locale/zh.json +12 -0
- package/package.json +47 -4
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
"""FastAPI surface for Ad+ Studio.
|
|
2
|
+
|
|
3
|
+
No pricing, quotes or billing endpoints exist. The first slice runs a local
|
|
4
|
+
demo pipeline so the DSH UI, storyboard, edits, personas and stitching can be
|
|
5
|
+
exercised end to end without paid credentials.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import copy
|
|
11
|
+
from contextlib import asynccontextmanager
|
|
12
|
+
import mimetypes
|
|
13
|
+
import shutil
|
|
14
|
+
import time
|
|
15
|
+
import zipfile
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
from fastapi import Body, FastAPI, HTTPException, Query, Request
|
|
20
|
+
from fastapi.responses import FileResponse, JSONResponse
|
|
21
|
+
|
|
22
|
+
from . import image_params, intents, llm, params, personas, settings, store
|
|
23
|
+
from .providers import discovery
|
|
24
|
+
from .media import ffmpeg
|
|
25
|
+
from .media import stitch as stitch_mod
|
|
26
|
+
from .models import AssistantRequest, Run, RunStep, StitchRequest
|
|
27
|
+
from .pipeline import image_runner
|
|
28
|
+
from .pipeline import replace as segment_replace
|
|
29
|
+
from .pipeline import runner, segment
|
|
30
|
+
|
|
31
|
+
@asynccontextmanager
|
|
32
|
+
async def _lifespan(_app: FastAPI):
|
|
33
|
+
store.migrate_legacy()
|
|
34
|
+
store.ensure_dirs()
|
|
35
|
+
settings.load_into_env()
|
|
36
|
+
personas.ensure_default()
|
|
37
|
+
yield
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
app = FastAPI(title="Ad+ Studio", version="0.1.0", lifespan=_lifespan)
|
|
41
|
+
VERSION = "0.1.0"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _media_type(path: Path) -> str:
|
|
45
|
+
return mimetypes.guess_type(path.name)[0] or "application/octet-stream"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _probe_asset(asset_id: str) -> dict[str, Any]:
|
|
49
|
+
record = store.get_asset(asset_id)
|
|
50
|
+
if record is None:
|
|
51
|
+
raise HTTPException(status_code=404, detail="asset not found")
|
|
52
|
+
target = store.safe_media_path(record["path"])
|
|
53
|
+
try:
|
|
54
|
+
if record.get("kind") == "video":
|
|
55
|
+
facts = ffmpeg.probe(target)
|
|
56
|
+
record.update({k: facts[k] for k in ("width", "height", "duration_s")})
|
|
57
|
+
elif record.get("kind") == "image":
|
|
58
|
+
from PIL import Image
|
|
59
|
+
|
|
60
|
+
with Image.open(target) as image:
|
|
61
|
+
record["width"], record["height"] = image.size
|
|
62
|
+
except Exception:
|
|
63
|
+
pass
|
|
64
|
+
return record
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@app.get("/health")
|
|
68
|
+
def health() -> dict[str, Any]:
|
|
69
|
+
models = params.catalog()
|
|
70
|
+
return {
|
|
71
|
+
"ok": True,
|
|
72
|
+
"service": "adclip",
|
|
73
|
+
"version": VERSION,
|
|
74
|
+
"demo": True,
|
|
75
|
+
"data": str(store.DATA),
|
|
76
|
+
"models": len(models),
|
|
77
|
+
"available_models": [row["id"] for row in models if row.get("available")],
|
|
78
|
+
"text_llm": llm.available(),
|
|
79
|
+
"text_model": llm.model_label(),
|
|
80
|
+
"text_provider": llm.provider_label(),
|
|
81
|
+
"text_llm_last_error": llm.LAST_ERROR,
|
|
82
|
+
"runs": len(store.list_runs(limit=200)),
|
|
83
|
+
"deps": ffmpeg.dependencies(),
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@app.get("/scenarios")
|
|
88
|
+
def scenarios() -> dict[str, Any]:
|
|
89
|
+
return {"scenarios": params.SCENARIOS}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@app.get("/models")
|
|
93
|
+
def models() -> dict[str, Any]:
|
|
94
|
+
return {"models": params.catalog()}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@app.post("/assets/upload")
|
|
98
|
+
async def upload_asset(request: Request, kind: str = "video", name: str = "") -> dict[str, Any]:
|
|
99
|
+
if kind not in {"image", "video"}:
|
|
100
|
+
raise HTTPException(status_code=400, detail="only image and video assets are supported")
|
|
101
|
+
raw = await request.body()
|
|
102
|
+
if not raw:
|
|
103
|
+
raise HTTPException(status_code=400, detail="empty file")
|
|
104
|
+
if len(raw) > 2 * 1024**3:
|
|
105
|
+
raise HTTPException(status_code=413, detail="file too large")
|
|
106
|
+
import hashlib
|
|
107
|
+
|
|
108
|
+
mime = request.headers.get("content-type") or "application/octet-stream"
|
|
109
|
+
digest = hashlib.sha256(raw).hexdigest()
|
|
110
|
+
existing = store.get_asset_by_sha(digest)
|
|
111
|
+
if existing is not None:
|
|
112
|
+
return {"ok": True, "asset": existing, "deduplicated": True}
|
|
113
|
+
try:
|
|
114
|
+
asset = store.create_asset(raw, name or f"upload-{store.new_id('file')}", kind, mime)
|
|
115
|
+
except ValueError as exc:
|
|
116
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
117
|
+
record = _probe_asset(asset.id)
|
|
118
|
+
return {"ok": True, "asset": record}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@app.get("/assets")
|
|
122
|
+
def list_assets(
|
|
123
|
+
kind: str = "", q: str = "", offset: int = 0, limit: int = 24,
|
|
124
|
+
from_: str = Query("", alias="from"), to: str = "",
|
|
125
|
+
) -> dict[str, Any]:
|
|
126
|
+
rows, total = store.list_assets_page(kind=kind, query=q, offset=offset, limit=limit, date_from=from_, date_to=to)
|
|
127
|
+
return {"assets": rows, "total": total, "offset": max(0, offset), "limit": max(1, min(limit, 200)), "has_more": offset + len(rows) < total}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@app.get("/assets/{asset_id}")
|
|
131
|
+
def get_asset(asset_id: str) -> dict[str, Any]:
|
|
132
|
+
record = store.get_asset(asset_id)
|
|
133
|
+
if record is None:
|
|
134
|
+
raise HTTPException(status_code=404, detail="asset not found")
|
|
135
|
+
return {"asset": record}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@app.get("/media")
|
|
139
|
+
def media(path: str) -> FileResponse:
|
|
140
|
+
try:
|
|
141
|
+
target = store.safe_media_path(path)
|
|
142
|
+
except (ValueError, FileNotFoundError) as exc:
|
|
143
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
144
|
+
return FileResponse(target, media_type=_media_type(target))
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# ---------------------------------------------------------------------------
|
|
148
|
+
# local provider keys
|
|
149
|
+
# ---------------------------------------------------------------------------
|
|
150
|
+
@app.get("/settings/models")
|
|
151
|
+
def get_settings_models() -> dict[str, Any]:
|
|
152
|
+
return {"enabled": settings.enabled_models(), "models": params.catalog()}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@app.post("/settings/models")
|
|
156
|
+
def set_settings_models(payload: dict[str, Any] = Body(...)) -> dict[str, Any]:
|
|
157
|
+
valid = {str(row["id"]) for row in params.catalog()}
|
|
158
|
+
enabled = []
|
|
159
|
+
for value in payload.get("enabled") or []:
|
|
160
|
+
value = str(value or "").strip()
|
|
161
|
+
if value in valid and value not in enabled:
|
|
162
|
+
enabled.append(value)
|
|
163
|
+
settings.set_enabled_models(enabled)
|
|
164
|
+
return {"enabled": enabled, "models": params.catalog()}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@app.get("/models/discover")
|
|
168
|
+
def discover_models() -> dict[str, Any]:
|
|
169
|
+
return {"providers": discovery.discover()}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@app.get("/settings/keys")
|
|
173
|
+
def get_settings_keys() -> dict[str, Any]:
|
|
174
|
+
return {"keys": settings.status()}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
@app.post("/settings/keys")
|
|
178
|
+
def set_settings_key(payload: dict[str, Any] = Body(...)) -> dict[str, Any]:
|
|
179
|
+
try:
|
|
180
|
+
rows = settings.set_key(str(payload.get("name") or ""), str(payload.get("value") or ""))
|
|
181
|
+
except ValueError as exc:
|
|
182
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
183
|
+
return {"keys": rows, "models": params.catalog()}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
# ---------------------------------------------------------------------------
|
|
187
|
+
# personas
|
|
188
|
+
# ---------------------------------------------------------------------------
|
|
189
|
+
@app.get("/personas")
|
|
190
|
+
def list_personas() -> dict[str, Any]:
|
|
191
|
+
return {"personas": store.list_personas()}
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
@app.post("/personas")
|
|
195
|
+
def create_persona(payload: dict[str, Any] = Body(...)) -> dict[str, Any]:
|
|
196
|
+
try:
|
|
197
|
+
return {"persona": personas.create(payload)}
|
|
198
|
+
except ValueError as exc:
|
|
199
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
@app.patch("/personas/{persona_id}")
|
|
203
|
+
def patch_persona(persona_id: str, payload: dict[str, Any] = Body(...)) -> dict[str, Any]:
|
|
204
|
+
result = personas.update(persona_id, payload)
|
|
205
|
+
if result is None:
|
|
206
|
+
raise HTTPException(status_code=404, detail="persona not found")
|
|
207
|
+
return {"persona": result}
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
@app.delete("/personas/{persona_id}")
|
|
211
|
+
def remove_persona(persona_id: str) -> dict[str, Any]:
|
|
212
|
+
return {"removed": personas.delete(persona_id)}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
# ---------------------------------------------------------------------------
|
|
216
|
+
# runs
|
|
217
|
+
# ---------------------------------------------------------------------------
|
|
218
|
+
def _scenario_or_404(scenario_id: str) -> dict[str, Any]:
|
|
219
|
+
scenario = params.scenario(scenario_id)
|
|
220
|
+
if scenario is None:
|
|
221
|
+
raise HTTPException(status_code=400, detail=f"unknown scenario: {scenario_id}")
|
|
222
|
+
if scenario.get("status") != "ready":
|
|
223
|
+
raise HTTPException(status_code=409, detail="scenario is not ready")
|
|
224
|
+
return scenario
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _validate_inputs(scenario: dict[str, Any], inputs: dict[str, Any]) -> None:
|
|
228
|
+
if scenario["id"] == "viral-remake":
|
|
229
|
+
ref = inputs.get("reference_asset_id")
|
|
230
|
+
if not ref or store.get_asset(ref) is None:
|
|
231
|
+
raise HTTPException(status_code=400, detail="reference video is required")
|
|
232
|
+
if scenario["id"] == "text-to-video" and not str(inputs.get("brief") or "").strip():
|
|
233
|
+
raise HTTPException(status_code=400, detail="brief is required")
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _new_run(scenario_id: str, inputs: dict[str, Any], advanced: dict[str, Any], *, title: str = "", parent: Run | None = None, changes: list[dict[str, Any]] | None = None) -> Run:
|
|
237
|
+
run_id = store.new_id("run")
|
|
238
|
+
store.run_dir(run_id).mkdir(parents=True, exist_ok=True)
|
|
239
|
+
(store.run_dir(run_id) / "artifacts").mkdir(parents=True, exist_ok=True)
|
|
240
|
+
persona_map = dict((advanced or {}).get("persona_map") or {})
|
|
241
|
+
persona_snapshot = {}
|
|
242
|
+
persona_obj = (advanced or {}).get("persona")
|
|
243
|
+
if isinstance(persona_obj, dict):
|
|
244
|
+
persona_snapshot = persona_obj
|
|
245
|
+
if not title:
|
|
246
|
+
brief = str(inputs.get("brief") or "").strip()
|
|
247
|
+
head = (brief.split("。")[0].split(",")[0] or "").strip()[:26]
|
|
248
|
+
label = params.scenario(scenario_id)["label"]
|
|
249
|
+
title = f"{label} · {head}" if head else label
|
|
250
|
+
run = Run(
|
|
251
|
+
id=run_id,
|
|
252
|
+
scenario=scenario_id,
|
|
253
|
+
title=title[:60],
|
|
254
|
+
status="queued",
|
|
255
|
+
step="queued",
|
|
256
|
+
generation_mode=str((advanced or {}).get("generation_mode") or "fast"),
|
|
257
|
+
inputs=inputs,
|
|
258
|
+
advanced=advanced,
|
|
259
|
+
persona_map=persona_map,
|
|
260
|
+
persona_snapshot=persona_snapshot,
|
|
261
|
+
steps=[RunStep(name=name, label=label) for name, label, _ in runner.STEPS],
|
|
262
|
+
created_at=store.now(),
|
|
263
|
+
parent_run_id=parent.id if parent else None,
|
|
264
|
+
version=(parent.version + 1) if parent else 1,
|
|
265
|
+
changes=changes or [],
|
|
266
|
+
demo=True,
|
|
267
|
+
)
|
|
268
|
+
store.write_run(run)
|
|
269
|
+
runner.manager.start(run.id)
|
|
270
|
+
return run
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
@app.post("/runs")
|
|
274
|
+
def create_run(payload: dict[str, Any] = Body(...)) -> JSONResponse:
|
|
275
|
+
scenario_id = str(payload.get("scenario") or "viral-remake")
|
|
276
|
+
scenario = _scenario_or_404(scenario_id)
|
|
277
|
+
inputs = dict(payload.get("inputs") or {})
|
|
278
|
+
advanced = params.merged_advanced(dict(payload.get("advanced") or {}))
|
|
279
|
+
_validate_inputs(scenario, inputs)
|
|
280
|
+
run = _new_run(scenario_id, inputs, advanced, title=str(payload.get("title") or ""))
|
|
281
|
+
return JSONResponse(status_code=202, content={"run": run.model_dump()})
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
@app.get("/image/models")
|
|
285
|
+
def get_image_models() -> dict[str, Any]:
|
|
286
|
+
rows = image_params.catalog()
|
|
287
|
+
return {"models": rows, "available": any(row.get("available") for row in rows)}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
@app.post("/images")
|
|
291
|
+
def create_image(payload: dict[str, Any] = Body(...)) -> JSONResponse:
|
|
292
|
+
prompt = str(payload.get("prompt") or "").strip()
|
|
293
|
+
if not prompt:
|
|
294
|
+
raise HTTPException(status_code=400, detail="prompt is required")
|
|
295
|
+
try:
|
|
296
|
+
run = image_runner.create(
|
|
297
|
+
prompt,
|
|
298
|
+
model_id=str(payload.get("model") or ""),
|
|
299
|
+
aspect=str(payload.get("aspect") or "1:1"),
|
|
300
|
+
area=str(payload.get("area") or "2K"),
|
|
301
|
+
)
|
|
302
|
+
except ValueError as exc:
|
|
303
|
+
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
|
304
|
+
return JSONResponse(status_code=202, content={"run": run.model_dump()})
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
@app.post("/images/{run_id}/retry")
|
|
308
|
+
def retry_image(run_id: str) -> JSONResponse:
|
|
309
|
+
parent = store.read_run(run_id)
|
|
310
|
+
if parent is None:
|
|
311
|
+
raise HTTPException(status_code=404, detail="run not found")
|
|
312
|
+
if parent.scenario != "text-to-image":
|
|
313
|
+
raise HTTPException(status_code=409, detail="not an image run")
|
|
314
|
+
advanced = parent.advanced or {}
|
|
315
|
+
try:
|
|
316
|
+
run = image_runner.create(
|
|
317
|
+
str((parent.inputs or {}).get("prompt") or ""),
|
|
318
|
+
model_id=str(advanced.get("model") or ""),
|
|
319
|
+
aspect=str(advanced.get("aspect") or "1:1"),
|
|
320
|
+
area=str(advanced.get("area") or "2K"),
|
|
321
|
+
parent=parent,
|
|
322
|
+
)
|
|
323
|
+
except ValueError as exc:
|
|
324
|
+
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
|
325
|
+
return JSONResponse(status_code=202, content={"run": run.model_dump()})
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
@app.get("/runs")
|
|
329
|
+
def list_runs(
|
|
330
|
+
offset: int = 0, limit: int = 20, q: str = "",
|
|
331
|
+
from_: str = Query("", alias="from"), to: str = "", status: str = "",
|
|
332
|
+
scenario: str = "", sort: str = "created_desc",
|
|
333
|
+
) -> dict[str, Any]:
|
|
334
|
+
rows, total = store.list_runs_page(
|
|
335
|
+
offset=offset, limit=limit, query=q, date_from=from_, date_to=to,
|
|
336
|
+
status=status, scenario=scenario, sort=sort,
|
|
337
|
+
)
|
|
338
|
+
return {"runs": rows, "total": total, "offset": max(0, offset), "limit": max(1, min(limit, 200)), "has_more": offset + len(rows) < total}
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
@app.post("/runs/export")
|
|
342
|
+
def export_runs(payload: dict[str, Any] = Body(...)) -> FileResponse:
|
|
343
|
+
ids = [str(x) for x in (payload.get("run_ids") or []) if str(x).strip()]
|
|
344
|
+
ids = list(dict.fromkeys(ids))[:50]
|
|
345
|
+
if not ids:
|
|
346
|
+
raise HTTPException(status_code=400, detail="run_ids is required")
|
|
347
|
+
export_dir = store.DATA / "exports"
|
|
348
|
+
export_dir.mkdir(parents=True, exist_ok=True)
|
|
349
|
+
target = export_dir / f"adplus-export-{int(time.time())}.zip"
|
|
350
|
+
included = []
|
|
351
|
+
with zipfile.ZipFile(target, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
|
352
|
+
for run_id in ids:
|
|
353
|
+
run = store.read_run(run_id)
|
|
354
|
+
if run is None or run.status != "succeeded":
|
|
355
|
+
continue
|
|
356
|
+
root = store.run_dir(run_id)
|
|
357
|
+
folder = run_id
|
|
358
|
+
zf.write(root / "run.json", arcname=f"{folder}/run.json")
|
|
359
|
+
for name in ("final.mp4", "image.jpg", "subtitles.srt", "qa.json", "script.json", "skeleton.json", "storyboard.json"):
|
|
360
|
+
path = root / "artifacts" / name
|
|
361
|
+
if path.is_file():
|
|
362
|
+
zf.write(path, arcname=f"{folder}/artifacts/{name}")
|
|
363
|
+
included.append(run_id)
|
|
364
|
+
if not included:
|
|
365
|
+
target.unlink(missing_ok=True)
|
|
366
|
+
raise HTTPException(status_code=409, detail="no succeeded runs to export")
|
|
367
|
+
return FileResponse(target, media_type="application/zip", filename=target.name)
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
@app.patch("/runs/{run_id}")
|
|
371
|
+
def rename_run(run_id: str, payload: dict[str, Any] = Body(...)) -> dict[str, Any]:
|
|
372
|
+
run = store.read_run(run_id)
|
|
373
|
+
if run is None:
|
|
374
|
+
raise HTTPException(status_code=404, detail="run not found")
|
|
375
|
+
title = str(payload.get("title") or "").strip()
|
|
376
|
+
if not title:
|
|
377
|
+
raise HTTPException(status_code=400, detail="title is required")
|
|
378
|
+
run.title = title[:120]
|
|
379
|
+
store.write_run(run)
|
|
380
|
+
return {"run": run.model_dump()}
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
@app.get("/runs/{run_id}")
|
|
384
|
+
def get_run(run_id: str) -> dict[str, Any]:
|
|
385
|
+
run = store.read_run(run_id)
|
|
386
|
+
if run is None:
|
|
387
|
+
raise HTTPException(status_code=404, detail="run not found")
|
|
388
|
+
data = run.model_dump()
|
|
389
|
+
storyboard = store.read_json(store.run_dir(run_id) / "artifacts" / "storyboard.json")
|
|
390
|
+
if storyboard:
|
|
391
|
+
data["storyboard"] = storyboard.get("segments") or []
|
|
392
|
+
return {"run": data}
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
@app.post("/runs/{run_id}/cancel")
|
|
396
|
+
def cancel_run(run_id: str) -> dict[str, Any]:
|
|
397
|
+
run = store.read_run(run_id)
|
|
398
|
+
if run is None:
|
|
399
|
+
raise HTTPException(status_code=404, detail="run not found")
|
|
400
|
+
run.cancel_requested = True
|
|
401
|
+
if run.status in {"queued", "running"}:
|
|
402
|
+
run.status = "canceled"
|
|
403
|
+
run.error = "已取消"
|
|
404
|
+
run.finished_at = store.now()
|
|
405
|
+
store.write_run(run)
|
|
406
|
+
return {"ok": True, "run": run.model_dump()}
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
@app.post("/runs/{run_id}/retry")
|
|
410
|
+
def retry_run(run_id: str) -> dict[str, Any]:
|
|
411
|
+
run = store.read_run(run_id)
|
|
412
|
+
if run is None:
|
|
413
|
+
raise HTTPException(status_code=404, detail="run not found")
|
|
414
|
+
new_run = _new_run(run.scenario, copy.deepcopy(run.inputs), copy.deepcopy(run.advanced), title=run.title, parent=run, changes=[])
|
|
415
|
+
return {"run": new_run.model_dump()}
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
@app.get("/runs/{run_id}/storyboard")
|
|
419
|
+
def storyboard(run_id: str) -> dict[str, Any]:
|
|
420
|
+
run = store.read_run(run_id)
|
|
421
|
+
if run is None:
|
|
422
|
+
raise HTTPException(status_code=404, detail="run not found")
|
|
423
|
+
data = store.read_json(store.run_dir(run_id) / "artifacts" / "storyboard.json")
|
|
424
|
+
if not data:
|
|
425
|
+
data = segment.build_storyboard(run_id)
|
|
426
|
+
return data
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def _apply_changes(advanced: dict[str, Any], inputs: dict[str, Any], changes: list[dict[str, Any]]) -> None:
|
|
430
|
+
for change in changes:
|
|
431
|
+
field = str(change.get("field") or "")
|
|
432
|
+
value = change.get("to")
|
|
433
|
+
if field.startswith("advanced."):
|
|
434
|
+
advanced[field.split(".", 1)[1]] = value
|
|
435
|
+
elif field.startswith("post."):
|
|
436
|
+
advanced[field.split(".", 1)[1]] = value
|
|
437
|
+
elif field.startswith("persona."):
|
|
438
|
+
persona = dict(advanced.get("persona") or {})
|
|
439
|
+
persona[field.split(".", 1)[1]] = value
|
|
440
|
+
advanced["persona"] = persona
|
|
441
|
+
elif field == "inputs.brief":
|
|
442
|
+
inputs["brief"] = value
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def _post_derive(parent: Run, changes: list[dict[str, Any]]) -> Run:
|
|
446
|
+
"""Apply post-only changes without calling any video model."""
|
|
447
|
+
run_id = store.new_id("run")
|
|
448
|
+
root = store.run_dir(run_id) / "artifacts"
|
|
449
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
450
|
+
src = store.run_dir(parent.id) / "artifacts"
|
|
451
|
+
for name in ("script.json", "skeleton.json", "storyboard.json", "qa.json", "subtitles.srt"):
|
|
452
|
+
source = src / name
|
|
453
|
+
if source.is_file():
|
|
454
|
+
shutil.copy2(source, root / name)
|
|
455
|
+
advanced = copy.deepcopy(parent.advanced)
|
|
456
|
+
inputs = copy.deepcopy(parent.inputs)
|
|
457
|
+
_apply_changes(advanced, inputs, changes)
|
|
458
|
+
bitrate = str(advanced.get("export_bitrate") or "8M")
|
|
459
|
+
fps = int(advanced.get("fps") or 30)
|
|
460
|
+
source_video = src / "final.mp4"
|
|
461
|
+
if not source_video.is_file():
|
|
462
|
+
raise HTTPException(status_code=409, detail="run has no final video to post-process")
|
|
463
|
+
target = root / "final.mp4"
|
|
464
|
+
ffmpeg.transcode(
|
|
465
|
+
source_video,
|
|
466
|
+
target,
|
|
467
|
+
bitrate=bitrate,
|
|
468
|
+
fps=fps,
|
|
469
|
+
trim_start=advanced.get("trim_start"),
|
|
470
|
+
trim_end=advanced.get("trim_end"),
|
|
471
|
+
)
|
|
472
|
+
notes = []
|
|
473
|
+
if any(str((c.get("field") or "")).startswith("post.subtitle") for c in changes):
|
|
474
|
+
notes.append("当前 ffmpeg 不支持烧录字幕,字幕仍以 SRT 旁挂文件交付")
|
|
475
|
+
if any(str((c.get("field") or "")).startswith("post.music") for c in changes):
|
|
476
|
+
notes.append("音乐替换尚未接入;本次未改动音轨")
|
|
477
|
+
run = Run(
|
|
478
|
+
id=run_id,
|
|
479
|
+
scenario=parent.scenario,
|
|
480
|
+
title=parent.title,
|
|
481
|
+
status="succeeded",
|
|
482
|
+
step="post",
|
|
483
|
+
progress=1.0,
|
|
484
|
+
generation_mode=parent.generation_mode,
|
|
485
|
+
inputs=inputs,
|
|
486
|
+
advanced=advanced,
|
|
487
|
+
persona_map=copy.deepcopy(parent.persona_map),
|
|
488
|
+
persona_snapshot=copy.deepcopy(parent.persona_snapshot),
|
|
489
|
+
steps=[RunStep(name="post", label="后期", status="done", progress=1.0, started_at=store.now(), finished_at=store.now())],
|
|
490
|
+
storyboard=copy.deepcopy(parent.storyboard),
|
|
491
|
+
result={
|
|
492
|
+
"video": store.rel_to_data(target),
|
|
493
|
+
"srt": store.rel_to_data(root / "subtitles.srt") if (root / "subtitles.srt").is_file() else None,
|
|
494
|
+
"width": (parent.result or {}).get("width"),
|
|
495
|
+
"height": (parent.result or {}).get("height"),
|
|
496
|
+
"duration_s": ffmpeg.probe(target)["duration_s"],
|
|
497
|
+
"qa": (parent.result or {}).get("qa"),
|
|
498
|
+
"demo": parent.demo,
|
|
499
|
+
"model": (parent.result or {}).get("model"),
|
|
500
|
+
"post_notes": notes,
|
|
501
|
+
"post_only": True,
|
|
502
|
+
},
|
|
503
|
+
demo=parent.demo,
|
|
504
|
+
created_at=store.now(),
|
|
505
|
+
finished_at=store.now(),
|
|
506
|
+
parent_run_id=parent.id,
|
|
507
|
+
version=parent.version + 1,
|
|
508
|
+
changes=changes,
|
|
509
|
+
)
|
|
510
|
+
store.write_run(run)
|
|
511
|
+
return run
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
@app.post("/runs/{run_id}/derive")
|
|
515
|
+
def derive_run(run_id: str, payload: dict[str, Any] = Body(...)) -> dict[str, Any]:
|
|
516
|
+
parent = store.read_run(run_id)
|
|
517
|
+
if parent is None:
|
|
518
|
+
raise HTTPException(status_code=404, detail="run not found")
|
|
519
|
+
changes = list(payload.get("changes") or [])
|
|
520
|
+
if changes and all(str(change.get("stage") or "") == "post" for change in changes):
|
|
521
|
+
return {"run": _post_derive(parent, changes).model_dump()}
|
|
522
|
+
advanced = copy.deepcopy(parent.advanced)
|
|
523
|
+
inputs = copy.deepcopy(parent.inputs)
|
|
524
|
+
_apply_changes(advanced, inputs, changes)
|
|
525
|
+
title = str(payload.get("title") or parent.title)
|
|
526
|
+
new_run = _new_run(parent.scenario, inputs, advanced, title=title, parent=parent, changes=changes)
|
|
527
|
+
return {"run": new_run.model_dump()}
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
@app.post("/runs/{run_id}/segments/{segment_id}/regenerate")
|
|
531
|
+
def regenerate_segment(run_id: str, segment_id: str, payload: dict[str, Any] = Body(default={})) -> dict[str, Any]:
|
|
532
|
+
parent = store.read_run(run_id)
|
|
533
|
+
if parent is None:
|
|
534
|
+
raise HTTPException(status_code=404, detail="run not found")
|
|
535
|
+
advanced = copy.deepcopy(parent.advanced)
|
|
536
|
+
inputs = copy.deepcopy(parent.inputs)
|
|
537
|
+
changes = list(payload.get("changes") or [])
|
|
538
|
+
_apply_changes(advanced, inputs, changes)
|
|
539
|
+
advanced["generation_mode"] = "fine"
|
|
540
|
+
new_run = _new_run(parent.scenario, inputs, advanced, title=parent.title, parent=parent, changes=[{"field": "selected_segment_id", "from": None, "to": segment_id, "stage": "generate", "requires_regeneration": True}] + changes)
|
|
541
|
+
return {"run": new_run.model_dump()}
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
@app.post("/runs/{run_id}/segments/{segment_id}/replace")
|
|
545
|
+
def replace_segment(run_id: str, segment_id: str) -> JSONResponse:
|
|
546
|
+
try:
|
|
547
|
+
run = segment_replace.start_replace(run_id, segment_id)
|
|
548
|
+
except ValueError as exc:
|
|
549
|
+
message = str(exc)
|
|
550
|
+
code = 404 if "not found" in message else 409
|
|
551
|
+
raise HTTPException(status_code=code, detail=message) from exc
|
|
552
|
+
return JSONResponse(status_code=202, content={"run": run.model_dump()})
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
@app.post("/runs/{run_id}/rollback")
|
|
556
|
+
def rollback_segment(run_id: str) -> dict[str, Any]:
|
|
557
|
+
try:
|
|
558
|
+
run = segment_replace.rollback(run_id)
|
|
559
|
+
except ValueError as exc:
|
|
560
|
+
message = str(exc)
|
|
561
|
+
code = 404 if "not found" in message else 409
|
|
562
|
+
raise HTTPException(status_code=code, detail=message) from exc
|
|
563
|
+
return {"run": run.model_dump()}
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
@app.get("/runs/{run_id}/export")
|
|
567
|
+
def export_run(run_id: str) -> FileResponse:
|
|
568
|
+
run = store.read_run(run_id)
|
|
569
|
+
if run is None:
|
|
570
|
+
raise HTTPException(status_code=404, detail="run not found")
|
|
571
|
+
root = store.run_dir(run_id)
|
|
572
|
+
target = root / "artifacts" / "export.zip"
|
|
573
|
+
with zipfile.ZipFile(target, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
|
574
|
+
for name in ("final.mp4", "subtitles.srt", "qa.json", "script.json", "skeleton.json", "storyboard.json"):
|
|
575
|
+
path = root / "artifacts" / name
|
|
576
|
+
if path.is_file():
|
|
577
|
+
zf.write(path, arcname=name)
|
|
578
|
+
return FileResponse(target, media_type="application/zip", filename=f"{run_id}.zip")
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
# ---------------------------------------------------------------------------
|
|
582
|
+
# stitch
|
|
583
|
+
# ---------------------------------------------------------------------------
|
|
584
|
+
def _resolve_clips(payload: StitchRequest) -> list[dict[str, Any]]:
|
|
585
|
+
rows = []
|
|
586
|
+
for clip in payload.clips:
|
|
587
|
+
record = store.get_asset(clip.asset_id)
|
|
588
|
+
if record is None:
|
|
589
|
+
raise HTTPException(status_code=404, detail=f"asset not found: {clip.asset_id}")
|
|
590
|
+
rows.append({"path": str(store.safe_media_path(record["path"])), "in_s": clip.in_s, "out_s": clip.out_s})
|
|
591
|
+
return rows
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
@app.post("/stitch/plan")
|
|
595
|
+
def stitch_plan(payload: StitchRequest) -> dict[str, Any]:
|
|
596
|
+
rows = _resolve_clips(payload)
|
|
597
|
+
output = payload.output or {}
|
|
598
|
+
width, height = params.size_for(str(output.get("aspect") or "9:16"), str(output.get("resolution") or "720p"))
|
|
599
|
+
try:
|
|
600
|
+
plan = stitch_mod.plan(rows, width=width, height=height, fit=payload.fit, transition=payload.transition, audio=payload.audio)
|
|
601
|
+
except (ValueError, FileNotFoundError) as exc:
|
|
602
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
603
|
+
return {"plan": plan, "output": output}
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
@app.post("/stitch")
|
|
607
|
+
def stitch(payload: StitchRequest) -> dict[str, Any]:
|
|
608
|
+
rows = _resolve_clips(payload)
|
|
609
|
+
output = payload.output or {}
|
|
610
|
+
width, height = params.size_for(str(output.get("aspect") or "9:16"), str(output.get("resolution") or "720p"))
|
|
611
|
+
try:
|
|
612
|
+
plan = stitch_mod.plan(rows, width=width, height=height, fit=payload.fit, transition=payload.transition, audio=payload.audio)
|
|
613
|
+
run_id = store.new_id("stitch")
|
|
614
|
+
target = store.run_dir(run_id) / "artifacts" / "final.mp4"
|
|
615
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
616
|
+
stitch_mod.execute(plan, target)
|
|
617
|
+
except Exception as exc: # noqa: BLE001
|
|
618
|
+
raise HTTPException(status_code=500, detail=f"stitch failed: {exc}") from exc
|
|
619
|
+
asset = store.create_asset(target.read_bytes(), f"stitch-{run_id}.mp4", "video", "video/mp4")
|
|
620
|
+
record = _probe_asset(asset.id)
|
|
621
|
+
run = Run(
|
|
622
|
+
id=run_id,
|
|
623
|
+
scenario="stitch",
|
|
624
|
+
title="拼接结果",
|
|
625
|
+
status="succeeded",
|
|
626
|
+
step="done",
|
|
627
|
+
progress=1.0,
|
|
628
|
+
inputs={"asset_ids": [clip.asset_id for clip in payload.clips]},
|
|
629
|
+
advanced={"stitch": payload.model_dump()},
|
|
630
|
+
steps=[],
|
|
631
|
+
result={"video": record["path"], "asset_id": record["id"], "duration_s": record.get("duration_s"), "warnings": plan.get("warnings") or []},
|
|
632
|
+
demo=True,
|
|
633
|
+
created_at=store.now(),
|
|
634
|
+
finished_at=store.now(),
|
|
635
|
+
)
|
|
636
|
+
store.write_run(run)
|
|
637
|
+
return {"run": run.model_dump(), "asset": record, "plan": plan}
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
# ---------------------------------------------------------------------------
|
|
641
|
+
# context / assistant
|
|
642
|
+
# ---------------------------------------------------------------------------
|
|
643
|
+
@app.get("/context")
|
|
644
|
+
def get_context() -> dict[str, Any]:
|
|
645
|
+
return {"context": store.read_context()}
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
@app.post("/context")
|
|
649
|
+
def set_context(payload: dict[str, Any] = Body(...)) -> dict[str, Any]:
|
|
650
|
+
return {"context": store.write_context(payload)}
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
@app.post("/assistant")
|
|
654
|
+
def assistant(req: AssistantRequest) -> dict[str, Any]:
|
|
655
|
+
return intents.analyze(req)
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
def main() -> None:
|
|
659
|
+
import uvicorn
|
|
660
|
+
|
|
661
|
+
parser = argparse.ArgumentParser()
|
|
662
|
+
parser.add_argument("--port", type=int, default=8787)
|
|
663
|
+
parser.add_argument("--host", default="127.0.0.1")
|
|
664
|
+
parser.add_argument("--graceful-timeout", type=int, default=5)
|
|
665
|
+
args = parser.parse_args()
|
|
666
|
+
uvicorn.run(
|
|
667
|
+
app,
|
|
668
|
+
host=args.host,
|
|
669
|
+
port=args.port,
|
|
670
|
+
log_level="warning",
|
|
671
|
+
timeout_graceful_shutdown=max(1, args.graceful_timeout),
|
|
672
|
+
)
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
if __name__ == "__main__":
|
|
676
|
+
main()
|