@chad.dev/dsh-adclip 0.0.0-stage → 0.1.1
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 +201 -0
- package/NOTICE +4 -0
- package/README.md +28 -2
- package/client.js +2256 -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 +704 -0
- package/engine/src/adclip/catalog.py +341 -0
- package/engine/src/adclip/image_params.py +45 -0
- package/engine/src/adclip/intents.py +209 -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 +158 -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 +196 -0
- package/engine/src/adclip/pipeline/image_runner.py +146 -0
- package/engine/src/adclip/pipeline/persona.py +56 -0
- package/engine/src/adclip/pipeline/post.py +40 -0
- package/engine/src/adclip/pipeline/qa.py +44 -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/ark_image.py +114 -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 +89 -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,196 @@
|
|
|
1
|
+
"""Generation: capability-driven model choice plus the real provider adapters."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from .. import catalog, params, store
|
|
8
|
+
from ..media import ffmpeg
|
|
9
|
+
from ..providers.ark import ArkProvider
|
|
10
|
+
from ..providers.kling import KlingProvider
|
|
11
|
+
from ..providers.minimax import MiniMaxProvider
|
|
12
|
+
from . import persona as persona_mod
|
|
13
|
+
|
|
14
|
+
PROVIDERS = {"ark": ArkProvider, "kling": KlingProvider, "minimax": MiniMaxProvider}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _adapter(model_id: str) -> Any | None:
|
|
18
|
+
"""Adapter instance for a catalogue row, or None for the test-only model."""
|
|
19
|
+
row = catalog.get(model_id) or {}
|
|
20
|
+
provider = str(row.get("provider") or "")
|
|
21
|
+
if provider == "synthetic":
|
|
22
|
+
return None
|
|
23
|
+
factory = PROVIDERS.get(provider)
|
|
24
|
+
if factory is None:
|
|
25
|
+
raise RuntimeError(f"模型 {model_id} 没有可用的适配器(provider={provider or 'unknown'})")
|
|
26
|
+
return factory()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def resolve_model(run: Any) -> str:
|
|
30
|
+
"""The model id this run should use: explicit when valid, else capability-first."""
|
|
31
|
+
requested = str((run.advanced or {}).get("model") or "auto")
|
|
32
|
+
if requested not in {"", "auto"}:
|
|
33
|
+
model = catalog.get(requested)
|
|
34
|
+
if model is None:
|
|
35
|
+
raise RuntimeError(f"unknown model: {requested}")
|
|
36
|
+
if model.get("enabled") is False:
|
|
37
|
+
raise RuntimeError(f"模型 {requested} 未启用")
|
|
38
|
+
if not model.get("available"):
|
|
39
|
+
raise RuntimeError(f"模型 {requested} 不可用:{model.get('reason')}")
|
|
40
|
+
return requested
|
|
41
|
+
needs_dialogue = run.scenario == "viral-remake"
|
|
42
|
+
rows = catalog.with_capability("text_to_video", kind="video", usable=True)
|
|
43
|
+
if not rows:
|
|
44
|
+
raise RuntimeError("没有可用的视频模型:请在设置里配置对应厂商的 key")
|
|
45
|
+
order = {row["id"]: index for index, row in enumerate(catalog.rows())}
|
|
46
|
+
|
|
47
|
+
def rank(row: dict[str, Any]) -> tuple[int, int, int]:
|
|
48
|
+
# Real providers first, dialogue-capable models first when the scenario
|
|
49
|
+
# speaks, then catalogue order so the choice is deterministic.
|
|
50
|
+
return (
|
|
51
|
+
1 if row.get("provider") == "synthetic" else 0,
|
|
52
|
+
0 if (not needs_dialogue or row.get("native_audio")) else 1,
|
|
53
|
+
order.get(row["id"], 999),
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
return sorted(rows, key=rank)[0]["id"]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _cast_text(advanced: dict[str, Any]) -> str:
|
|
60
|
+
persona = (advanced.get("persona") or {}) if isinstance(advanced.get("persona"), dict) else {}
|
|
61
|
+
if not persona:
|
|
62
|
+
return ""
|
|
63
|
+
gender = {"male": "男性", "female": "女性", "any": "人物"}.get(persona.get("gender") or "any")
|
|
64
|
+
age = {"teenager": "18–24 岁", "youth": "25–34 岁", "middle_aged": "35–54 岁", "senior": "55 岁以上"}.get(persona.get("age") or "youth")
|
|
65
|
+
body = {"slim": "偏瘦", "standard": "标准", "athletic": "健硕", "chubby": "微胖"}.get(persona.get("body_type") or "standard")
|
|
66
|
+
return f"{age}{gender},{body}体型"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _segment_prompt(run: Any, script: dict[str, Any], seg: dict[str, Any]) -> str:
|
|
70
|
+
advanced = params.merged_advanced(run.advanced)
|
|
71
|
+
brief = str((run.inputs or {}).get("brief") or "").strip()
|
|
72
|
+
rows = script.get("beats") or []
|
|
73
|
+
index = int(seg.get("index") or 0)
|
|
74
|
+
beat = rows[index] if 0 <= index < len(rows) else {}
|
|
75
|
+
lines = ";".join(str(x.get("text") or "") for x in (beat.get("lines") or []) if str(x.get("text") or "").strip())
|
|
76
|
+
shot = str(seg.get("action") or beat.get("shot") or "").strip()
|
|
77
|
+
cast = _cast_text(advanced)
|
|
78
|
+
duration = max(0.5, float(seg.get("end_s") or 0) - float(seg.get("start_s") or 0))
|
|
79
|
+
parts = [
|
|
80
|
+
f"竖屏 {advanced.get('aspect', '9:16')},约 {duration:.1f} 秒,单个镜头,画面干净,自然光。",
|
|
81
|
+
f"人物:{cast}。" if cast else "",
|
|
82
|
+
persona_mod.persona_instruction(advanced),
|
|
83
|
+
f"场景/动作:{persona_mod.neutralize_people(shot, advanced)}。" if shot else "",
|
|
84
|
+
f"中文同期对白(只念以下句子,不要念画面描述或制作要求;按顺序说):{lines}。" if lines else "",
|
|
85
|
+
]
|
|
86
|
+
if not lines and brief:
|
|
87
|
+
parts.append(f"画面/产品:{brief[:300]}")
|
|
88
|
+
parts.append("要求:口型与台词同步;不要出现字幕、水印、logo;商品标签留白;不要念出这段说明。")
|
|
89
|
+
return "".join(part for part in parts if part).strip()[:1400]
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _script_prompt(run: Any, script: dict[str, Any]) -> str:
|
|
93
|
+
advanced = params.merged_advanced(run.advanced)
|
|
94
|
+
brief = str((run.inputs or {}).get("brief") or "").strip()
|
|
95
|
+
lines = []
|
|
96
|
+
actions = []
|
|
97
|
+
for beat in script.get("beats") or []:
|
|
98
|
+
spoken = " / ".join(str(x.get("text") or "") for x in beat.get("lines") or [])
|
|
99
|
+
if spoken:
|
|
100
|
+
lines.append(spoken)
|
|
101
|
+
if beat.get("shot"):
|
|
102
|
+
actions.append(str(beat["shot"]))
|
|
103
|
+
cast = _cast_text(advanced)
|
|
104
|
+
is_dialogue = run.scenario == "viral-remake"
|
|
105
|
+
parts = [
|
|
106
|
+
f"竖屏 {advanced.get('aspect', '9:16')},约 {advanced.get('duration', 12)} 秒,一镜到底,画面干净,自然光。",
|
|
107
|
+
f"人物:{cast}。" if cast else "",
|
|
108
|
+
persona_mod.persona_instruction(advanced),
|
|
109
|
+
f"场景/动作:{';'.join(persona_mod.neutralize_people(x, advanced) for x in actions[:4])}。" if actions else "",
|
|
110
|
+
]
|
|
111
|
+
if is_dialogue and lines:
|
|
112
|
+
parts.append(
|
|
113
|
+
"中文同期对白(只念以下句子,不要念画面描述、产品说明或制作要求;两个不同声部,按顺序说):"
|
|
114
|
+
+ ";".join(lines[:6]) + "。"
|
|
115
|
+
)
|
|
116
|
+
elif brief:
|
|
117
|
+
parts.append(f"画面/产品:{brief[:500]}")
|
|
118
|
+
parts.append("要求:口型与台词同步;画面不要出现字幕、水印、logo;商品标签留白;不要把这段说明念出来。")
|
|
119
|
+
return "".join(part for part in parts if part).strip()[:1800]
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _reference_images(run: Any) -> list[Path]:
|
|
123
|
+
paths: list[Path] = []
|
|
124
|
+
inputs = run.inputs or {}
|
|
125
|
+
for asset_id in list(inputs.get("product_asset_ids") or [])[:3]:
|
|
126
|
+
try:
|
|
127
|
+
paths.append(store.asset_media_path(str(asset_id)))
|
|
128
|
+
except Exception:
|
|
129
|
+
continue
|
|
130
|
+
ref = inputs.get("reference_image_asset_id")
|
|
131
|
+
if ref:
|
|
132
|
+
try:
|
|
133
|
+
paths.append(store.asset_media_path(str(ref)))
|
|
134
|
+
except Exception:
|
|
135
|
+
pass
|
|
136
|
+
return paths
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def generate_segment(run: Any, seg: dict[str, Any], output: Path) -> dict[str, Any]:
|
|
140
|
+
"""Generate only one storyboard segment (used by true shot replacement)."""
|
|
141
|
+
model = resolve_model(run)
|
|
142
|
+
advanced = params.merged_advanced(run.advanced)
|
|
143
|
+
width, height = params.size_for(str(advanced.get("aspect") or "9:16"), str(advanced.get("resolution") or "720p"))
|
|
144
|
+
seg_len = max(0.5, float(seg.get("end_s") or 0) - float(seg.get("start_s") or 0))
|
|
145
|
+
script = store.read_json(store.run_dir(run.id) / "artifacts" / "script.json", {}) or {}
|
|
146
|
+
ref_id = (run.inputs or {}).get("reference_asset_id")
|
|
147
|
+
source = store.asset_media_path(ref_id) if ref_id else None
|
|
148
|
+
prompt = _segment_prompt(run, script, seg)
|
|
149
|
+
provider = _adapter(model)
|
|
150
|
+
if provider is None:
|
|
151
|
+
ffmpeg.make_synthetic_clip(source, output, duration=seg_len, width=width, height=height, fps=int(advanced.get("fps") or 30))
|
|
152
|
+
return {"model": model, "path": str(output), "demo": True, "duration_s": seg_len}
|
|
153
|
+
allowed = list((params.get_model(model) or {}).get("durations") or [4, 5, 6, 10])
|
|
154
|
+
target = int(seg_len + 0.999)
|
|
155
|
+
gen_duration = next((d for d in sorted(allowed) if d >= target), max(allowed))
|
|
156
|
+
provider.generate(
|
|
157
|
+
prompt,
|
|
158
|
+
images=_reference_images(run),
|
|
159
|
+
duration=int(gen_duration),
|
|
160
|
+
resolution=str(advanced.get("resolution") or "720p"),
|
|
161
|
+
aspect=str(advanced.get("aspect") or "9:16"),
|
|
162
|
+
model=model,
|
|
163
|
+
output=output,
|
|
164
|
+
)
|
|
165
|
+
return {"model": model, "path": str(output), "demo": False, "duration_s": seg_len}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def generate(run_id: str) -> dict[str, Any]:
|
|
169
|
+
run = store.read_run(run_id)
|
|
170
|
+
if run is None:
|
|
171
|
+
raise FileNotFoundError(run_id)
|
|
172
|
+
model = resolve_model(run)
|
|
173
|
+
advanced = params.merged_advanced(run.advanced)
|
|
174
|
+
duration = max(1.0, float(advanced.get("duration") or 12))
|
|
175
|
+
width, height = params.size_for(str(advanced.get("aspect") or "9:16"), str(advanced.get("resolution") or "720p"))
|
|
176
|
+
ref_id = (run.inputs or {}).get("reference_asset_id")
|
|
177
|
+
source = store.asset_media_path(ref_id) if ref_id else None
|
|
178
|
+
result_path = store.run_artifact(run_id, "provider_raw.mp4")
|
|
179
|
+
provider = _adapter(model)
|
|
180
|
+
if provider is None:
|
|
181
|
+
# Test-only synthetic model: a local clip so the pipeline stays runnable
|
|
182
|
+
# without any provider key.
|
|
183
|
+
ffmpeg.make_synthetic_clip(source, result_path, duration=duration, width=width, height=height, fps=int(advanced.get("fps") or 30))
|
|
184
|
+
return {"model": model, "path": str(result_path), "demo": True, "duration_s": duration}
|
|
185
|
+
script = store.read_json(store.run_dir(run_id) / "artifacts" / "script.json", {}) or {}
|
|
186
|
+
prompt = _script_prompt(run, script)
|
|
187
|
+
provider.generate(
|
|
188
|
+
prompt,
|
|
189
|
+
images=_reference_images(run),
|
|
190
|
+
duration=int(duration),
|
|
191
|
+
resolution=str(advanced.get("resolution") or "720p"),
|
|
192
|
+
aspect=str(advanced.get("aspect") or "9:16"),
|
|
193
|
+
model=model,
|
|
194
|
+
output=result_path,
|
|
195
|
+
)
|
|
196
|
+
return {"model": model, "path": str(result_path), "demo": False, "duration_s": duration}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"""Asynchronous text-to-image run executor."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import traceback
|
|
5
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from .. import image_params, store
|
|
9
|
+
from ..models import Run, RunStep
|
|
10
|
+
from ..providers import ark_image, minimax_image
|
|
11
|
+
|
|
12
|
+
IMAGE_PROVIDERS = {"minimax": minimax_image.generate, "ark": ark_image.generate}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Manager:
|
|
16
|
+
def __init__(self, workers: int = 2) -> None:
|
|
17
|
+
self.pool = ThreadPoolExecutor(max_workers=workers, thread_name_prefix="adstudio-image")
|
|
18
|
+
|
|
19
|
+
def start(self, run_id: str) -> None:
|
|
20
|
+
self.pool.submit(execute, run_id)
|
|
21
|
+
|
|
22
|
+
def shutdown(self) -> None:
|
|
23
|
+
self.pool.shutdown(wait=False, cancel_futures=True)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
manager = Manager()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _update(run_id: str, **fields: Any) -> None:
|
|
30
|
+
run = store.read_run(run_id)
|
|
31
|
+
if run is None:
|
|
32
|
+
return
|
|
33
|
+
for key, value in fields.items():
|
|
34
|
+
setattr(run, key, value)
|
|
35
|
+
store.write_run(run)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def execute(run_id: str) -> None:
|
|
39
|
+
run = store.read_run(run_id)
|
|
40
|
+
if run is None:
|
|
41
|
+
return
|
|
42
|
+
run.status = "running"
|
|
43
|
+
run.step = "image"
|
|
44
|
+
run.progress = 0.2
|
|
45
|
+
run.error = None
|
|
46
|
+
run.steps = [RunStep(name="image", label="生成图片", status="running", progress=0.2)]
|
|
47
|
+
store.write_run(run)
|
|
48
|
+
try:
|
|
49
|
+
advanced = run.advanced or {}
|
|
50
|
+
model_id = str(advanced.get("model") or "")
|
|
51
|
+
model = image_params.get_model(model_id)
|
|
52
|
+
if model is None or not model.get("available"):
|
|
53
|
+
raise RuntimeError("图片模型不可用:" + model_id)
|
|
54
|
+
prompt = str((run.inputs or {}).get("prompt") or "").strip()
|
|
55
|
+
if not prompt:
|
|
56
|
+
raise RuntimeError("prompt 不能为空")
|
|
57
|
+
provider = IMAGE_PROVIDERS.get(str(model.get("provider") or ""))
|
|
58
|
+
if provider is None:
|
|
59
|
+
raise RuntimeError(f"图片模型 {model_id} 没有可用的适配器")
|
|
60
|
+
raw = store.run_artifact(run_id, "image.bin")
|
|
61
|
+
reference = None
|
|
62
|
+
ref_id = str((run.inputs or {}).get("reference_asset_id") or "")
|
|
63
|
+
if ref_id:
|
|
64
|
+
reference = store.asset_media_path(ref_id)
|
|
65
|
+
generated = provider(
|
|
66
|
+
prompt,
|
|
67
|
+
model=str(model["model"]),
|
|
68
|
+
aspect=str(advanced.get("aspect") or "1:1"),
|
|
69
|
+
area=str(advanced.get("area") or "2K"),
|
|
70
|
+
output=raw,
|
|
71
|
+
reference=reference,
|
|
72
|
+
)
|
|
73
|
+
suffix = ".png" if raw.read_bytes()[:4] == b"\x89PNG" else ".jpg"
|
|
74
|
+
image_path = raw.with_suffix(suffix)
|
|
75
|
+
raw.replace(image_path)
|
|
76
|
+
raw = image_path
|
|
77
|
+
width, height = generated.get("width"), generated.get("height")
|
|
78
|
+
if not width or not height:
|
|
79
|
+
try:
|
|
80
|
+
from PIL import Image
|
|
81
|
+
with Image.open(raw) as image:
|
|
82
|
+
width, height = image.size
|
|
83
|
+
except Exception:
|
|
84
|
+
width = height = None
|
|
85
|
+
mime = "image/png" if raw.suffix == ".png" else "image/jpeg"
|
|
86
|
+
asset = store.create_asset(raw.read_bytes(), f"image-{run_id}{raw.suffix}", "image", mime)
|
|
87
|
+
run = store.read_run(run_id) or run
|
|
88
|
+
run.status = "succeeded"
|
|
89
|
+
run.step = "done"
|
|
90
|
+
run.progress = 1.0
|
|
91
|
+
run.finished_at = store.now()
|
|
92
|
+
run.demo = False
|
|
93
|
+
run.steps = [RunStep(name="image", label="生成图片", status="done", progress=1.0, started_at=run.created_at, finished_at=store.now())]
|
|
94
|
+
run.result = {
|
|
95
|
+
"image": store.rel_to_data(raw),
|
|
96
|
+
"asset_id": asset.id,
|
|
97
|
+
"width": width,
|
|
98
|
+
"height": height,
|
|
99
|
+
"model": model_id,
|
|
100
|
+
"native_model": model.get("model"),
|
|
101
|
+
"aspect": str(advanced.get("aspect") or "1:1"),
|
|
102
|
+
"area": str(advanced.get("area") or "2K"),
|
|
103
|
+
"prompt": prompt,
|
|
104
|
+
}
|
|
105
|
+
store.write_run(run)
|
|
106
|
+
except Exception as exc: # noqa: BLE001
|
|
107
|
+
run = store.read_run(run_id)
|
|
108
|
+
if run:
|
|
109
|
+
run.status = "failed"
|
|
110
|
+
run.error = f"{type(exc).__name__}: {exc}"[:800]
|
|
111
|
+
run.finished_at = store.now()
|
|
112
|
+
run.steps = [RunStep(name="image", label="生成图片", status="failed", progress=1.0, error=run.error, finished_at=store.now())]
|
|
113
|
+
store.write_run(run)
|
|
114
|
+
traceback.print_exc()
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def create(prompt: str, model_id: str = "", aspect: str = "1:1", area: str = "2K", *, parent: Run | None = None, reference_asset_id: str = "") -> Run:
|
|
118
|
+
model = image_params.get_model(model_id or image_params.default_model())
|
|
119
|
+
if model is None:
|
|
120
|
+
raise ValueError("没有可用的图片模型;请在设置里配置对应厂商 key")
|
|
121
|
+
if not model.get("available"):
|
|
122
|
+
raise ValueError(f"图片模型不可用:{model.get('reason') or '请在设置里配置对应厂商 key'}")
|
|
123
|
+
aspect = aspect if aspect in (model.get("aspects") or []) else "1:1"
|
|
124
|
+
area = area if area in (model.get("areas") or []) else str(model.get("default_area") or "2K")
|
|
125
|
+
run_id = store.new_id("img")
|
|
126
|
+
store.run_dir(run_id).mkdir(parents=True, exist_ok=True)
|
|
127
|
+
(store.run_dir(run_id) / "artifacts").mkdir(parents=True, exist_ok=True)
|
|
128
|
+
head = prompt.strip().split("。")[0][:26]
|
|
129
|
+
run = Run(
|
|
130
|
+
id=run_id,
|
|
131
|
+
scenario="text-to-image",
|
|
132
|
+
title=("文生图片 · " + head) if head else "文生图片",
|
|
133
|
+
status="queued",
|
|
134
|
+
step="queued",
|
|
135
|
+
progress=0.0,
|
|
136
|
+
inputs={"prompt": prompt, **({"reference_asset_id": reference_asset_id} if reference_asset_id else {})},
|
|
137
|
+
advanced={"model": model["id"], "aspect": aspect, "area": area},
|
|
138
|
+
steps=[RunStep(name="image", label="生成图片")],
|
|
139
|
+
demo=False,
|
|
140
|
+
created_at=store.now(),
|
|
141
|
+
parent_run_id=parent.id if parent else None,
|
|
142
|
+
version=(parent.version + 1) if parent else 1,
|
|
143
|
+
)
|
|
144
|
+
store.write_run(run)
|
|
145
|
+
manager.start(run.id)
|
|
146
|
+
return run
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Persona text normalization for reference-based generation.
|
|
2
|
+
|
|
3
|
+
Reference analysis describes the people in the source video. When the operator
|
|
4
|
+
selects a persona, those descriptions are useful for blocking/actions but must
|
|
5
|
+
not override the selected gender/age. Keep that rule in one place so script and
|
|
6
|
+
provider prompts agree.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def persona(advanced: dict[str, Any] | None) -> dict[str, Any]:
|
|
15
|
+
value = (advanced or {}).get("persona")
|
|
16
|
+
return value if isinstance(value, dict) else {}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def persona_label(advanced: dict[str, Any] | None) -> str:
|
|
20
|
+
p = persona(advanced)
|
|
21
|
+
if not p:
|
|
22
|
+
return ""
|
|
23
|
+
gender = {"male": "男性", "female": "女性", "any": "人物"}.get(p.get("gender") or "any", "人物")
|
|
24
|
+
age = {"teenager": "18–24 岁", "youth": "25–34 岁", "middle_aged": "35–54 岁", "senior": "55 岁以上"}.get(p.get("age") or "youth", "")
|
|
25
|
+
body = {"slim": "偏瘦", "standard": "标准", "athletic": "健硕", "chubby": "微胖"}.get(p.get("body_type") or "standard", "")
|
|
26
|
+
ethnicity = {"asian": "亚洲", "caucasian": "欧美", "latino": "拉美", "african": "非洲", "middle_eastern": "中东", "any": "不限"}.get(p.get("ethnicity") or "any", "")
|
|
27
|
+
return ",".join(x for x in (age + gender, body + "体型", ethnicity) if x)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def persona_instruction(advanced: dict[str, Any] | None) -> str:
|
|
31
|
+
label = persona_label(advanced)
|
|
32
|
+
if not label:
|
|
33
|
+
return ""
|
|
34
|
+
return (
|
|
35
|
+
f"模特设定:{label}。参考片人物只用于动作、构图和人物关系,"
|
|
36
|
+
"不得沿用参考片中人物的性别、年龄或外貌;所有出镜人物必须符合模特设定。"
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def neutralize_people(text: str, advanced: dict[str, Any] | None) -> str:
|
|
41
|
+
"""Replace contradictory gender words in reference-derived actions."""
|
|
42
|
+
value = str(text or "")
|
|
43
|
+
p = persona(advanced)
|
|
44
|
+
gender = p.get("gender")
|
|
45
|
+
if not value or gender not in {"male", "female"}:
|
|
46
|
+
return value
|
|
47
|
+
if gender == "female":
|
|
48
|
+
value = re.sub(r"男子|男性|男人|男生|男演员|男孩", "女性", value)
|
|
49
|
+
value = re.sub(r"三男|两名男|几名男", "三名女性", value)
|
|
50
|
+
else:
|
|
51
|
+
value = re.sub(r"女子|女性|女人|女生|女演员|女孩", "男性", value)
|
|
52
|
+
value = re.sub(r"三女|两名女|几名女", "三名男性", value)
|
|
53
|
+
age = p.get("age")
|
|
54
|
+
if age in {"teenager", "youth"}:
|
|
55
|
+
value = re.sub(r"老年|老人|中年", "青年", value)
|
|
56
|
+
return value
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Post: normalize, SRT, optional loudness, final artifact."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from .. import params, store
|
|
8
|
+
from ..media import ffmpeg, subtitles
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def post(run_id: str) -> dict[str, Any]:
|
|
12
|
+
run = store.read_run(run_id)
|
|
13
|
+
if run is None:
|
|
14
|
+
raise FileNotFoundError(run_id)
|
|
15
|
+
advanced = params.merged_advanced(run.advanced)
|
|
16
|
+
script = store.read_json(store.run_dir(run_id) / "artifacts" / "script.json", {}) or {}
|
|
17
|
+
raw = store.run_artifact(run_id, "provider_raw.mp4")
|
|
18
|
+
if not raw.is_file():
|
|
19
|
+
raw = store.run_artifact(run_id, "demo_raw.mp4")
|
|
20
|
+
if not raw.is_file():
|
|
21
|
+
raise FileNotFoundError("generated clip missing")
|
|
22
|
+
width, height = params.size_for(str(advanced.get("aspect") or "9:16"), str(advanced.get("resolution") or "720p"))
|
|
23
|
+
final = store.run_artifact(run_id, "final.mp4")
|
|
24
|
+
ffmpeg.normalize(raw, final, width=width, height=height, fps=int(advanced.get("fps") or 30))
|
|
25
|
+
# Subtitles are a sidecar SRT, never burned in (the ffmpeg build has no
|
|
26
|
+
# libass/drawtext). The run's own setting decides whether we deliver one.
|
|
27
|
+
srt_path = store.run_artifact(run_id, "subtitles.srt")
|
|
28
|
+
if str(advanced.get("subtitle") or "off") == "sidecar":
|
|
29
|
+
subtitles.write_srt(script, srt_path)
|
|
30
|
+
srt_value = store.rel_to_data(srt_path)
|
|
31
|
+
else:
|
|
32
|
+
srt_path.unlink(missing_ok=True)
|
|
33
|
+
srt_value = None
|
|
34
|
+
return {
|
|
35
|
+
"video": store.rel_to_data(final),
|
|
36
|
+
"srt": srt_value,
|
|
37
|
+
"width": width,
|
|
38
|
+
"height": height,
|
|
39
|
+
"duration_s": ffmpeg.probe(final)["duration_s"],
|
|
40
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Minimal QA checks for the first slice."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from .. import params, store
|
|
7
|
+
from ..media import ffmpeg
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def check(run_id: str) -> dict[str, Any]:
|
|
11
|
+
run = store.read_run(run_id)
|
|
12
|
+
if run is None:
|
|
13
|
+
raise FileNotFoundError(run_id)
|
|
14
|
+
advanced = params.merged_advanced(run.advanced)
|
|
15
|
+
final = store.run_artifact(run_id, "final.mp4")
|
|
16
|
+
facts = ffmpeg.probe(final)
|
|
17
|
+
target = float(advanced.get("duration") or 12)
|
|
18
|
+
checks = []
|
|
19
|
+
|
|
20
|
+
def add(cid: str, label: str, status: str, value: Any, note: str = "") -> None:
|
|
21
|
+
checks.append({"id": cid, "label": label, "status": status, "value": value, "note": note})
|
|
22
|
+
|
|
23
|
+
delta = round(float(facts["duration_s"]) - target, 2)
|
|
24
|
+
add("duration", "时长", "pass" if abs(delta) <= max(0.5, target * 0.1) else "warn", {"final_s": facts["duration_s"], "target_s": target, "delta_s": delta})
|
|
25
|
+
add("aspect", "画幅", "pass" if facts.get("width") and facts.get("height") else "fail", {"width": facts.get("width"), "height": facts.get("height")})
|
|
26
|
+
add("audio", "音轨", "pass" if facts.get("has_audio") else "fail", {"has_audio": facts.get("has_audio")})
|
|
27
|
+
srt = store.run_artifact(run_id, "subtitles.srt")
|
|
28
|
+
if str(advanced.get("subtitle") or "off") == "sidecar":
|
|
29
|
+
add("subtitles", "字幕", "pass" if srt.is_file() and srt.stat().st_size > 0 else "warn", {"path": store.rel_to_data(srt)})
|
|
30
|
+
else:
|
|
31
|
+
add("subtitles", "字幕", "pass", {"enabled": False, "path": None}, "未启用(按配置)")
|
|
32
|
+
storyboard = store.read_json(store.run_dir(run_id) / "artifacts" / "storyboard.json", {}) or {}
|
|
33
|
+
beats = storyboard.get("segments") or []
|
|
34
|
+
add("segments", "分镜", "pass" if beats else "warn", {"count": len(beats)})
|
|
35
|
+
failed = [c for c in checks if c["status"] == "fail"]
|
|
36
|
+
warned = [c for c in checks if c["status"] == "warn"]
|
|
37
|
+
report = {
|
|
38
|
+
"version": 1,
|
|
39
|
+
"checks": checks,
|
|
40
|
+
"summary": {"pass": len(checks) - len(failed) - len(warned), "warn": len(warned), "fail": len(failed), "shippable": not failed},
|
|
41
|
+
"demo": bool(run.demo),
|
|
42
|
+
}
|
|
43
|
+
store.atomic_write_json(store.run_dir(run_id) / "artifacts" / "qa.json", report)
|
|
44
|
+
return report
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""True single-shot replacement and rollback.
|
|
2
|
+
|
|
3
|
+
Unlike the older /regenerate endpoint, this keeps the existing final clip and
|
|
4
|
+
only regenerates one storyboard slot, then splices it back with ffmpeg. The
|
|
5
|
+
previous final is kept under versions/ so the edit can be rolled back.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import shutil
|
|
11
|
+
import threading
|
|
12
|
+
import traceback
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from .. import params, store
|
|
17
|
+
from ..media import ffmpeg
|
|
18
|
+
from ..media import stitch as stitch_mod
|
|
19
|
+
from . import generate, qa, segment
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _storyboard(run: Any) -> list[dict[str, Any]]:
|
|
23
|
+
data = store.read_json(store.run_dir(run.id) / "artifacts" / "storyboard.json", {}) or {}
|
|
24
|
+
return list(data.get("segments") or run.storyboard or [])
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _find(run: Any, segment_id: str) -> dict[str, Any]:
|
|
28
|
+
for row in _storyboard(run):
|
|
29
|
+
if str(row.get("id")) == segment_id:
|
|
30
|
+
return row
|
|
31
|
+
raise ValueError(f"segment not found: {segment_id}")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _versions(run_id: str) -> Path:
|
|
35
|
+
path = store.run_dir(run_id) / "versions"
|
|
36
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
37
|
+
return path
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _backup(run: Any, final: Path) -> str:
|
|
41
|
+
name = f"v{run.version}_{store.new_id('pre')}.mp4"
|
|
42
|
+
target = _versions(run.id) / name
|
|
43
|
+
shutil.copy2(final, target)
|
|
44
|
+
return f"versions/{name}"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _refresh(run: Any) -> None:
|
|
48
|
+
final = store.run_artifact(run.id, "final.mp4")
|
|
49
|
+
segment.build_storyboard(run.id)
|
|
50
|
+
qa_report = qa.check(run.id)
|
|
51
|
+
facts = ffmpeg.probe(final)
|
|
52
|
+
result = dict(run.result or {})
|
|
53
|
+
result.update({
|
|
54
|
+
"video": store.rel_to_data(final),
|
|
55
|
+
"srt": store.rel_to_data(store.run_artifact(run.id, "subtitles.srt")) if store.run_artifact(run.id, "subtitles.srt").is_file() else None,
|
|
56
|
+
"width": facts.get("width"),
|
|
57
|
+
"height": facts.get("height"),
|
|
58
|
+
"duration_s": facts.get("duration_s"),
|
|
59
|
+
"qa": qa_report,
|
|
60
|
+
"demo": run.demo,
|
|
61
|
+
"model": result.get("model"),
|
|
62
|
+
})
|
|
63
|
+
run.result = result
|
|
64
|
+
run.status = "succeeded"
|
|
65
|
+
run.step = "qa"
|
|
66
|
+
run.progress = 1.0
|
|
67
|
+
run.error = None
|
|
68
|
+
run.finished_at = store.now()
|
|
69
|
+
store.write_run(run)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _run_replace(run_id: str, segment_id: str) -> None:
|
|
73
|
+
run = store.read_run(run_id)
|
|
74
|
+
if run is None:
|
|
75
|
+
return
|
|
76
|
+
try:
|
|
77
|
+
seg = _find(run, segment_id)
|
|
78
|
+
final = store.run_artifact(run_id, "final.mp4")
|
|
79
|
+
if not final.is_file():
|
|
80
|
+
raise FileNotFoundError("final video is missing")
|
|
81
|
+
advanced = params.merged_advanced(run.advanced)
|
|
82
|
+
width, height = params.size_for(str(advanced.get("aspect") or "9:16"), str(advanced.get("resolution") or "720p"))
|
|
83
|
+
fps = int(advanced.get("fps") or 30)
|
|
84
|
+
old_facts = ffmpeg.probe(final)
|
|
85
|
+
start = max(0.0, float(seg.get("start_s") or 0))
|
|
86
|
+
end = min(float(old_facts["duration_s"]), float(seg.get("end_s") or old_facts["duration_s"]))
|
|
87
|
+
slot_len = max(0.5, end - start)
|
|
88
|
+
raw = store.run_artifact(run_id, f"segment_replace_{segment_id}_raw.mp4")
|
|
89
|
+
normalized = store.run_artifact(run_id, f"segment_replace_{segment_id}.mp4")
|
|
90
|
+
generated = generate.generate_segment(run, seg, raw)
|
|
91
|
+
run = store.read_run(run_id) or run
|
|
92
|
+
run.progress = 0.65
|
|
93
|
+
run.step = "stitch"
|
|
94
|
+
store.write_run(run)
|
|
95
|
+
ffmpeg.loop_to_duration(raw, normalized, width=width, height=height, fps=fps, duration=slot_len)
|
|
96
|
+
|
|
97
|
+
before = start > 0.08
|
|
98
|
+
after = end < float(old_facts["duration_s"]) - 0.08
|
|
99
|
+
clips: list[dict[str, Any]] = []
|
|
100
|
+
if before:
|
|
101
|
+
clips.append({"path": str(final), "in_s": 0.0, "out_s": start})
|
|
102
|
+
clips.append({"path": str(normalized), "in_s": 0.0, "out_s": slot_len})
|
|
103
|
+
if after:
|
|
104
|
+
clips.append({"path": str(final), "in_s": end, "out_s": float(old_facts["duration_s"])})
|
|
105
|
+
target = store.run_artifact(run_id, "final.replace.mp4")
|
|
106
|
+
plan_data = stitch_mod.plan(clips, width=width, height=height, fit="black_pad", transition="cut", audio="keep")
|
|
107
|
+
stitch_mod.execute(plan_data, target)
|
|
108
|
+
run = store.read_run(run_id) or run
|
|
109
|
+
run.progress = 0.9
|
|
110
|
+
run.step = "qa"
|
|
111
|
+
store.write_run(run)
|
|
112
|
+
backup = _backup(run, final)
|
|
113
|
+
os.replace(target, final)
|
|
114
|
+
run = store.read_run(run_id) or run
|
|
115
|
+
run.version = int(run.version or 1) + 1
|
|
116
|
+
run.segment_edits = list(run.segment_edits or [])
|
|
117
|
+
run.segment_edits.append({
|
|
118
|
+
"segment_id": segment_id,
|
|
119
|
+
"index": seg.get("index"),
|
|
120
|
+
"from_version": int(run.version) - 1,
|
|
121
|
+
"to_version": int(run.version),
|
|
122
|
+
"backup": backup,
|
|
123
|
+
"model": generated.get("model"),
|
|
124
|
+
"created_at": store.now(),
|
|
125
|
+
})
|
|
126
|
+
run.changes = list(run.changes or [])
|
|
127
|
+
run.changes.append({
|
|
128
|
+
"field": "segment.replace",
|
|
129
|
+
"from": seg.get("index"),
|
|
130
|
+
"to": segment_id,
|
|
131
|
+
"stage": "generate",
|
|
132
|
+
"requires_regeneration": False,
|
|
133
|
+
"label": "替换分镜 #" + str(int(seg.get("index") or 0) + 1),
|
|
134
|
+
})
|
|
135
|
+
_refresh(run)
|
|
136
|
+
except Exception as exc: # noqa: BLE001
|
|
137
|
+
run = store.read_run(run_id)
|
|
138
|
+
if run:
|
|
139
|
+
run.status = "failed"
|
|
140
|
+
run.error = f"{type(exc).__name__}: {exc}"[:800]
|
|
141
|
+
run.finished_at = store.now()
|
|
142
|
+
store.write_run(run)
|
|
143
|
+
traceback.print_exc()
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def start_replace(run_id: str, segment_id: str) -> Any:
|
|
147
|
+
run = store.read_run(run_id)
|
|
148
|
+
if run is None:
|
|
149
|
+
raise ValueError("run not found")
|
|
150
|
+
if run.status == "running":
|
|
151
|
+
raise ValueError("run is busy")
|
|
152
|
+
seg = _find(run, segment_id)
|
|
153
|
+
if run.status != "succeeded":
|
|
154
|
+
raise ValueError("only a succeeded run can be edited")
|
|
155
|
+
run.status = "running"
|
|
156
|
+
run.step = "segment"
|
|
157
|
+
run.progress = 0.1
|
|
158
|
+
run.error = None
|
|
159
|
+
store.write_run(run)
|
|
160
|
+
threading.Thread(target=_run_replace, args=(run_id, segment_id), daemon=True, name="adstudio-segment-replace").start()
|
|
161
|
+
return run
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def rollback(run_id: str) -> Any:
|
|
165
|
+
run = store.read_run(run_id)
|
|
166
|
+
if run is None:
|
|
167
|
+
raise ValueError("run not found")
|
|
168
|
+
edits = list(run.segment_edits or [])
|
|
169
|
+
if not edits:
|
|
170
|
+
raise ValueError("no segment edits to roll back")
|
|
171
|
+
entry = edits[-1]
|
|
172
|
+
backup = store.run_dir(run_id) / str(entry.get("backup") or "")
|
|
173
|
+
final = store.run_artifact(run_id, "final.mp4")
|
|
174
|
+
if not backup.is_file():
|
|
175
|
+
raise ValueError("segment backup is missing")
|
|
176
|
+
current = _versions(run_id) / f"v{run.version}_{store.new_id('rollback')}.mp4"
|
|
177
|
+
shutil.copy2(final, current)
|
|
178
|
+
shutil.copy2(backup, final)
|
|
179
|
+
run.segment_edits = edits[:-1]
|
|
180
|
+
run.version = int(run.version or 1) + 1
|
|
181
|
+
run.changes = list(run.changes or [])
|
|
182
|
+
run.changes.append({
|
|
183
|
+
"field": "segment.rollback",
|
|
184
|
+
"from": entry.get("segment_id"),
|
|
185
|
+
"to": None,
|
|
186
|
+
"stage": "generate",
|
|
187
|
+
"requires_regeneration": False,
|
|
188
|
+
"label": "回滚分镜 #" + str(int(entry.get("index") or 0) + 1),
|
|
189
|
+
})
|
|
190
|
+
_refresh(run)
|
|
191
|
+
return run
|