@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.
Files changed (49) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +4 -0
  3. package/README.md +28 -2
  4. package/client.js +2157 -0
  5. package/cordis.patch.yml +10 -0
  6. package/engine/requirements.txt +9 -0
  7. package/engine/src/adclip/__init__.py +0 -0
  8. package/engine/src/adclip/api.py +676 -0
  9. package/engine/src/adclip/image_params.py +64 -0
  10. package/engine/src/adclip/intents.py +190 -0
  11. package/engine/src/adclip/llm.py +142 -0
  12. package/engine/src/adclip/media/__init__.py +0 -0
  13. package/engine/src/adclip/media/asr.py +79 -0
  14. package/engine/src/adclip/media/ffmpeg.py +294 -0
  15. package/engine/src/adclip/media/stitch.py +124 -0
  16. package/engine/src/adclip/media/subtitles.py +45 -0
  17. package/engine/src/adclip/media/vision.py +105 -0
  18. package/engine/src/adclip/models.py +127 -0
  19. package/engine/src/adclip/params.py +363 -0
  20. package/engine/src/adclip/personas.py +60 -0
  21. package/engine/src/adclip/pipeline/__init__.py +0 -0
  22. package/engine/src/adclip/pipeline/analyze.py +133 -0
  23. package/engine/src/adclip/pipeline/generate.py +201 -0
  24. package/engine/src/adclip/pipeline/image_runner.py +131 -0
  25. package/engine/src/adclip/pipeline/persona.py +56 -0
  26. package/engine/src/adclip/pipeline/post.py +33 -0
  27. package/engine/src/adclip/pipeline/qa.py +41 -0
  28. package/engine/src/adclip/pipeline/replace.py +191 -0
  29. package/engine/src/adclip/pipeline/runner.py +138 -0
  30. package/engine/src/adclip/pipeline/script.py +226 -0
  31. package/engine/src/adclip/pipeline/segment.py +87 -0
  32. package/engine/src/adclip/providers/__init__.py +0 -0
  33. package/engine/src/adclip/providers/ark.py +122 -0
  34. package/engine/src/adclip/providers/base.py +30 -0
  35. package/engine/src/adclip/providers/discovery.py +56 -0
  36. package/engine/src/adclip/providers/kling.py +101 -0
  37. package/engine/src/adclip/providers/minimax.py +155 -0
  38. package/engine/src/adclip/providers/minimax_image.py +86 -0
  39. package/engine/src/adclip/settings.py +109 -0
  40. package/engine/src/adclip/store.py +378 -0
  41. package/host/dsh.js +60 -0
  42. package/host/engine.js +265 -0
  43. package/host/proxy.js +142 -0
  44. package/host/supervisor.js +251 -0
  45. package/icon.svg +6 -0
  46. package/index.js +39 -0
  47. package/locale/en.json +12 -0
  48. package/locale/zh.json +12 -0
  49. package/package.json +47 -4
@@ -0,0 +1,133 @@
1
+ """Reference analysis: probe, cut detection, beat grid, storyboard thumbnails."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+ from .. import store
9
+ from ..media import asr, ffmpeg, vision
10
+
11
+
12
+ def _beats(duration: float, cuts: list[float]) -> list[dict[str, Any]]:
13
+ points = [0.0] + [round(c, 3) for c in cuts if 0.4 < c < duration - 0.4] + [round(duration, 3)]
14
+ if len(points) <= 2:
15
+ step = max(2.0, duration / 4.0)
16
+ points = [0.0]
17
+ t = step
18
+ while t < duration - 1.0:
19
+ points.append(round(t, 3))
20
+ t += step
21
+ points.append(round(duration, 3))
22
+ # merge tiny beats into the next one
23
+ merged: list[float] = [points[0]]
24
+ for value in points[1:]:
25
+ if value - merged[-1] < 0.8 and value != points[-1]:
26
+ continue
27
+ merged.append(value)
28
+ if merged[-1] != points[-1]:
29
+ merged.append(points[-1])
30
+ # cap at 6 readable beats
31
+ while len(merged) > 7:
32
+ idx = min(range(1, len(merged) - 1), key=lambda i: merged[i] - merged[i - 1])
33
+ del merged[idx]
34
+ functions = ["hook", "setup", "escalation", "insult", "cta", "punchline", "beat"]
35
+ rows = []
36
+ for i in range(len(merged) - 1):
37
+ start, end = merged[i], merged[i + 1]
38
+ if end - start < 0.25:
39
+ continue
40
+ rows.append(
41
+ {
42
+ "id": f"b{len(rows)}",
43
+ "start_s": round(start, 3),
44
+ "end_s": round(end, 3),
45
+ "len_s": round(end - start, 2),
46
+ "function": functions[min(len(rows), len(functions) - 1)],
47
+ "function_source": "heuristic",
48
+ "confidence": 0.55,
49
+ "dialogue": [],
50
+ "visual": {"action": "", "shot_size": "中景", "product": {}},
51
+ }
52
+ )
53
+ return rows
54
+
55
+
56
+ def analyze(run_id: str) -> dict[str, Any]:
57
+ run = store.read_run(run_id)
58
+ if run is None:
59
+ raise FileNotFoundError(run_id)
60
+ root = store.run_dir(run_id)
61
+ root.mkdir(parents=True, exist_ok=True)
62
+ ref_id = (run.inputs or {}).get("reference_asset_id")
63
+ source = store.asset_media_path(ref_id) if ref_id else None
64
+ facts = ffmpeg.probe(source) if source else {
65
+ "duration_s": float((run.advanced or {}).get("duration") or 12),
66
+ "width": 720, "height": 1280, "fps": 30, "aspect": "9:16", "has_audio": False,
67
+ }
68
+ duration = float(facts["duration_s"])
69
+ cuts = ffmpeg.detect_cuts(source, duration) if source else []
70
+ beats = _beats(duration, cuts)
71
+ transcript: list[dict[str, Any]] = []
72
+ if source and facts.get("has_audio") and asr.available():
73
+ try:
74
+ transcript = asr.transcribe(source)
75
+ except Exception:
76
+ transcript = []
77
+ for beat in beats:
78
+ if transcript:
79
+ texts = [
80
+ item["text"]
81
+ for item in transcript
82
+ if float(item["start_s"]) < float(beat["end_s"]) and float(item["end_s"]) > float(beat["start_s"])
83
+ ]
84
+ beat["dialogue"] = texts
85
+ beat["dialogue_source"] = "asr"
86
+ else:
87
+ beat["dialogue"] = []
88
+ if source:
89
+ thumb = store.run_artifact(run_id, f"storyboard/{beat['id']}.jpg")
90
+ try:
91
+ ffmpeg.extract_frame(source, (beat["start_s"] + beat["end_s"]) / 2, thumb, width=360)
92
+ beat["thumbnail"] = store.rel_to_data(thumb)
93
+ except Exception:
94
+ beat["thumbnail"] = None
95
+ visual: dict[str, Any] = {}
96
+ if source and vision.available():
97
+ frames = [
98
+ {"id": beat["id"], "start_s": beat["start_s"], "end_s": beat["end_s"], "path": store.run_artifact(run_id, f"storyboard/{beat['id']}.jpg")}
99
+ for beat in beats
100
+ if store.run_artifact(run_id, f"storyboard/{beat['id']}.jpg").is_file()
101
+ ]
102
+ try:
103
+ visual = vision.analyze_frames(frames)
104
+ except Exception:
105
+ visual = {}
106
+ for beat in beats:
107
+ item = visual.get(beat["id"]) or {}
108
+ if item:
109
+ beat["visual"] = {
110
+ "action": item.get("action") or "",
111
+ "shot_size": item.get("shot_size") or "中景",
112
+ "product": {"name": item.get("product") or ""},
113
+ "people": item.get("people") or "",
114
+ "scene": item.get("scene") or "",
115
+ "on_screen_text": item.get("on_screen_text") or "",
116
+ "source": "vlm",
117
+ }
118
+ skeleton = {
119
+ "version": 2,
120
+ "partial": False,
121
+ "source": {**facts, "asset_id": ref_id},
122
+ "cuts_s": cuts,
123
+ "beats": beats,
124
+ "transcript": transcript,
125
+ "confidence": {"asr": 0.85 if transcript else 0.0, "vision": 0.75 if visual else 0.0, "speakers": 0.0},
126
+ "notes": [
127
+ "ASR transcript attached." if transcript else "No transcript: sound may be absent, ASR disabled, or speech not recognized.",
128
+ "M3 vision tags attached to keyframes." if visual else ("Vision unavailable or disabled." if not vision.available() else "Vision returned no usable tags."),
129
+ ],
130
+ }
131
+ path = root / "artifacts" / "skeleton.json"
132
+ store.atomic_write_json(path, skeleton)
133
+ return skeleton
@@ -0,0 +1,201 @@
1
+ """Generation: demo fallback plus the real Ark Seedance adapter."""
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
9
+ from ..media import stitch as stitch_mod
10
+ from ..providers.ark import ArkProvider
11
+ from ..providers.kling import KlingProvider
12
+ from ..providers.minimax import MiniMaxProvider
13
+ from . import persona as persona_mod
14
+
15
+
16
+ def resolve_model(run: Any) -> str:
17
+ requested = str((run.advanced or {}).get("model") or "auto")
18
+ if requested not in {"", "auto"}:
19
+ model = params.get_model(requested)
20
+ if model is None:
21
+ raise RuntimeError(f"unknown model: {requested}")
22
+ if model.get("enabled") is False:
23
+ raise RuntimeError(f"模型 {requested} 未启用")
24
+ if not model.get("available"):
25
+ raise RuntimeError(f"模型 {requested} 不可用:{model.get('reason')}")
26
+ return requested
27
+ needs_dialogue = run.scenario == "viral-remake"
28
+ for candidate in ("ark.seedance-2.5", "ark.seedance-2.0", "ark.seedance-2.0-fast", "ark.seedance-2.0-mini", "kling.v3", "minimax.h3", "minimax.hailuo-2.3-fast"):
29
+ model = params.get_model(candidate)
30
+ if not model or not model.get("available") or model.get("enabled") is False:
31
+ continue
32
+ if "text_to_video" not in (model.get("modes") or []):
33
+ continue
34
+ if needs_dialogue and not model.get("native_audio"):
35
+ continue
36
+ return candidate
37
+ return "demo"
38
+
39
+
40
+ def _cast_text(advanced: dict[str, Any]) -> str:
41
+ persona = (advanced.get("persona") or {}) if isinstance(advanced.get("persona"), dict) else {}
42
+ if not persona:
43
+ return ""
44
+ gender = {"male": "男性", "female": "女性", "any": "人物"}.get(persona.get("gender") or "any")
45
+ age = {"teenager": "18–24 岁", "youth": "25–34 岁", "middle_aged": "35–54 岁", "senior": "55 岁以上"}.get(persona.get("age") or "youth")
46
+ body = {"slim": "偏瘦", "standard": "标准", "athletic": "健硕", "chubby": "微胖"}.get(persona.get("body_type") or "standard")
47
+ return f"{age}{gender},{body}体型"
48
+
49
+
50
+ def _segment_prompt(run: Any, script: dict[str, Any], seg: dict[str, Any]) -> str:
51
+ advanced = params.merged_advanced(run.advanced)
52
+ brief = str((run.inputs or {}).get("brief") or "").strip()
53
+ rows = script.get("beats") or []
54
+ index = int(seg.get("index") or 0)
55
+ beat = rows[index] if 0 <= index < len(rows) else {}
56
+ lines = ";".join(str(x.get("text") or "") for x in (beat.get("lines") or []) if str(x.get("text") or "").strip())
57
+ shot = str(seg.get("action") or beat.get("shot") or "").strip()
58
+ cast = _cast_text(advanced)
59
+ duration = max(0.5, float(seg.get("end_s") or 0) - float(seg.get("start_s") or 0))
60
+ parts = [
61
+ f"竖屏 {advanced.get('aspect', '9:16')},约 {duration:.1f} 秒,单个镜头,画面干净,自然光。",
62
+ f"人物:{cast}。" if cast else "",
63
+ persona_mod.persona_instruction(advanced),
64
+ f"场景/动作:{persona_mod.neutralize_people(shot, advanced)}。" if shot else "",
65
+ f"中文同期对白(只念以下句子,不要念画面描述或制作要求;按顺序说):{lines}。" if lines else "",
66
+ ]
67
+ if not lines and brief:
68
+ parts.append(f"画面/产品:{brief[:300]}")
69
+ parts.append("要求:口型与台词同步;不要出现字幕、水印、logo;商品标签留白;不要念出这段说明。")
70
+ return "".join(part for part in parts if part).strip()[:1400]
71
+
72
+
73
+ def _script_prompt(run: Any, script: dict[str, Any]) -> str:
74
+ advanced = params.merged_advanced(run.advanced)
75
+ brief = str((run.inputs or {}).get("brief") or "").strip()
76
+ lines = []
77
+ actions = []
78
+ for beat in script.get("beats") or []:
79
+ spoken = " / ".join(str(x.get("text") or "") for x in beat.get("lines") or [])
80
+ if spoken:
81
+ lines.append(spoken)
82
+ if beat.get("shot"):
83
+ actions.append(str(beat["shot"]))
84
+ cast = _cast_text(advanced)
85
+ is_dialogue = run.scenario == "viral-remake"
86
+ parts = [
87
+ f"竖屏 {advanced.get('aspect', '9:16')},约 {advanced.get('duration', 12)} 秒,一镜到底,画面干净,自然光。",
88
+ f"人物:{cast}。" if cast else "",
89
+ persona_mod.persona_instruction(advanced),
90
+ f"场景/动作:{';'.join(persona_mod.neutralize_people(x, advanced) for x in actions[:4])}。" if actions else "",
91
+ ]
92
+ if is_dialogue and lines:
93
+ parts.append(
94
+ "中文同期对白(只念以下句子,不要念画面描述、产品说明或制作要求;两个不同声部,按顺序说):"
95
+ + ";".join(lines[:6]) + "。"
96
+ )
97
+ elif brief:
98
+ parts.append(f"画面/产品:{brief[:500]}")
99
+ parts.append("要求:口型与台词同步;画面不要出现字幕、水印、logo;商品标签留白;不要把这段说明念出来。")
100
+ return "".join(part for part in parts if part).strip()[:1800]
101
+
102
+
103
+ def _reference_images(run: Any) -> list[Path]:
104
+ paths: list[Path] = []
105
+ inputs = run.inputs or {}
106
+ for asset_id in list(inputs.get("product_asset_ids") or [])[:3]:
107
+ try:
108
+ paths.append(store.asset_media_path(str(asset_id)))
109
+ except Exception:
110
+ continue
111
+ ref = inputs.get("reference_image_asset_id")
112
+ if ref:
113
+ try:
114
+ paths.append(store.asset_media_path(str(ref)))
115
+ except Exception:
116
+ pass
117
+ return paths
118
+
119
+
120
+ def generate_segment(run: Any, seg: dict[str, Any], output: Path) -> dict[str, Any]:
121
+ """Generate only one storyboard segment (used by true shot replacement)."""
122
+ model = resolve_model(run)
123
+ advanced = params.merged_advanced(run.advanced)
124
+ width, height = params.size_for(str(advanced.get("aspect") or "9:16"), str(advanced.get("resolution") or "720p"))
125
+ seg_len = max(0.5, float(seg.get("end_s") or 0) - float(seg.get("start_s") or 0))
126
+ script = store.read_json(store.run_dir(run.id) / "artifacts" / "script.json", {}) or {}
127
+ ref_id = (run.inputs or {}).get("reference_asset_id")
128
+ source = store.asset_media_path(ref_id) if ref_id else None
129
+ if model == "demo":
130
+ ffmpeg.make_demo_clip(source, output, duration=seg_len, width=width, height=height, fps=int(advanced.get("fps") or 30))
131
+ return {"model": "demo", "path": str(output), "demo": True, "duration_s": seg_len}
132
+ prompt = _segment_prompt(run, script, seg)
133
+ if model.startswith("kling."):
134
+ provider = KlingProvider()
135
+ elif model.startswith("minimax."):
136
+ provider = MiniMaxProvider()
137
+ else:
138
+ provider = ArkProvider()
139
+ allowed = list((params.get_model(model) or {}).get("durations") or [4, 5, 6, 10])
140
+ target = int(seg_len + 0.999)
141
+ gen_duration = next((d for d in sorted(allowed) if d >= target), max(allowed))
142
+ provider.generate(
143
+ prompt,
144
+ images=_reference_images(run),
145
+ duration=int(gen_duration),
146
+ resolution=str(advanced.get("resolution") or "720p"),
147
+ aspect=str(advanced.get("aspect") or "9:16"),
148
+ model=model,
149
+ output=output,
150
+ )
151
+ return {"model": model, "path": str(output), "demo": False, "duration_s": seg_len}
152
+
153
+
154
+ def generate(run_id: str) -> dict[str, Any]:
155
+ run = store.read_run(run_id)
156
+ if run is None:
157
+ raise FileNotFoundError(run_id)
158
+ model = resolve_model(run)
159
+ advanced = params.merged_advanced(run.advanced)
160
+ duration = max(1.0, float(advanced.get("duration") or 12))
161
+ width, height = params.size_for(str(advanced.get("aspect") or "9:16"), str(advanced.get("resolution") or "720p"))
162
+ ref_id = (run.inputs or {}).get("reference_asset_id")
163
+ source = store.asset_media_path(ref_id) if ref_id else None
164
+ out = store.run_artifact(run_id, "demo_raw.mp4")
165
+
166
+ if model != "demo":
167
+ script = store.read_json(store.run_dir(run_id) / "artifacts" / "script.json", {}) or {}
168
+ prompt = _script_prompt(run, script)
169
+ if model.startswith("kling."):
170
+ provider = KlingProvider()
171
+ elif model.startswith("minimax."):
172
+ provider = MiniMaxProvider()
173
+ else:
174
+ provider = ArkProvider()
175
+ result_path = store.run_artifact(run_id, "provider_raw.mp4")
176
+ provider.generate(
177
+ prompt,
178
+ images=_reference_images(run),
179
+ duration=int(duration),
180
+ resolution=str(advanced.get("resolution") or "720p"),
181
+ aspect=str(advanced.get("aspect") or "9:16"),
182
+ model=model,
183
+ output=result_path,
184
+ )
185
+ return {"model": model, "path": str(result_path), "demo": False, "duration_s": duration}
186
+
187
+ mode = str(advanced.get("generation_mode") or "fast")
188
+ if mode == "fine" and duration > 8:
189
+ half = duration / 2
190
+ a = store.run_artifact(run_id, "demo_part0.mp4")
191
+ b = store.run_artifact(run_id, "demo_part1.mp4")
192
+ ffmpeg.make_demo_clip(source, a, duration=half, width=width, height=height, fps=int(advanced.get("fps") or 30))
193
+ ffmpeg.make_demo_clip(source, b, duration=half, width=width, height=height, fps=int(advanced.get("fps") or 30))
194
+ plan_data = stitch_mod.plan(
195
+ [{"path": str(a), "in_s": 0, "out_s": half}, {"path": str(b), "in_s": 0, "out_s": half}],
196
+ width=width, height=height, fit="black_pad", transition="cut", audio="keep",
197
+ )
198
+ stitch_mod.execute(plan_data, out)
199
+ else:
200
+ ffmpeg.make_demo_clip(source, out, duration=duration, width=width, height=height, fps=int(advanced.get("fps") or 30))
201
+ return {"model": "demo", "path": str(out), "demo": True, "duration_s": duration}
@@ -0,0 +1,131 @@
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 minimax_image
11
+
12
+
13
+ class Manager:
14
+ def __init__(self, workers: int = 2) -> None:
15
+ self.pool = ThreadPoolExecutor(max_workers=workers, thread_name_prefix="adstudio-image")
16
+
17
+ def start(self, run_id: str) -> None:
18
+ self.pool.submit(execute, run_id)
19
+
20
+ def shutdown(self) -> None:
21
+ self.pool.shutdown(wait=False, cancel_futures=True)
22
+
23
+
24
+ manager = Manager()
25
+
26
+
27
+ def _update(run_id: str, **fields: Any) -> None:
28
+ run = store.read_run(run_id)
29
+ if run is None:
30
+ return
31
+ for key, value in fields.items():
32
+ setattr(run, key, value)
33
+ store.write_run(run)
34
+
35
+
36
+ def execute(run_id: str) -> None:
37
+ run = store.read_run(run_id)
38
+ if run is None:
39
+ return
40
+ run.status = "running"
41
+ run.step = "image"
42
+ run.progress = 0.2
43
+ run.error = None
44
+ run.steps = [RunStep(name="image", label="生成图片", status="running", progress=0.2)]
45
+ store.write_run(run)
46
+ try:
47
+ advanced = run.advanced or {}
48
+ model_id = str(advanced.get("model") or "")
49
+ model = image_params.get_model(model_id)
50
+ if model is None or not model.get("available"):
51
+ raise RuntimeError("图片模型不可用:" + model_id)
52
+ prompt = str((run.inputs or {}).get("prompt") or "").strip()
53
+ if not prompt:
54
+ raise RuntimeError("prompt 不能为空")
55
+ raw = store.run_artifact(run_id, "image.jpg")
56
+ generated = minimax_image.generate(
57
+ prompt,
58
+ model=str(model["model"]),
59
+ aspect=str(advanced.get("aspect") or "1:1"),
60
+ area=str(advanced.get("area") or "2K"),
61
+ output=raw,
62
+ )
63
+ width, height = generated.get("width"), generated.get("height")
64
+ if not width or not height:
65
+ try:
66
+ from PIL import Image
67
+ with Image.open(raw) as image:
68
+ width, height = image.size
69
+ except Exception:
70
+ width = height = None
71
+ asset = store.create_asset(raw.read_bytes(), f"image-{run_id}.jpg", "image", "image/jpeg")
72
+ run = store.read_run(run_id) or run
73
+ run.status = "succeeded"
74
+ run.step = "done"
75
+ run.progress = 1.0
76
+ run.finished_at = store.now()
77
+ run.demo = False
78
+ run.steps = [RunStep(name="image", label="生成图片", status="done", progress=1.0, started_at=run.created_at, finished_at=store.now())]
79
+ run.result = {
80
+ "image": store.rel_to_data(raw),
81
+ "asset_id": asset.id,
82
+ "width": width,
83
+ "height": height,
84
+ "model": model_id,
85
+ "native_model": model.get("model"),
86
+ "aspect": str(advanced.get("aspect") or "1:1"),
87
+ "area": str(advanced.get("area") or "2K"),
88
+ "prompt": prompt,
89
+ }
90
+ store.write_run(run)
91
+ except Exception as exc: # noqa: BLE001
92
+ run = store.read_run(run_id)
93
+ if run:
94
+ run.status = "failed"
95
+ run.error = f"{type(exc).__name__}: {exc}"[:800]
96
+ run.finished_at = store.now()
97
+ run.steps = [RunStep(name="image", label="生成图片", status="failed", progress=1.0, error=run.error, finished_at=store.now())]
98
+ store.write_run(run)
99
+ traceback.print_exc()
100
+
101
+
102
+ def create(prompt: str, model_id: str = "", aspect: str = "1:1", area: str = "2K", *, parent: Run | None = None) -> Run:
103
+ model = image_params.get_model(model_id or image_params.default_model())
104
+ if model is None:
105
+ raise ValueError("没有可用的图片模型;请配置 MINIMAX_API_KEY")
106
+ if not model.get("available"):
107
+ raise ValueError("图片模型不可用;请配置 MINIMAX_API_KEY")
108
+ aspect = aspect if aspect in (model.get("aspects") or []) else "1:1"
109
+ area = area if area in (model.get("areas") or []) else str(model.get("default_area") or "2K")
110
+ run_id = store.new_id("img")
111
+ store.run_dir(run_id).mkdir(parents=True, exist_ok=True)
112
+ (store.run_dir(run_id) / "artifacts").mkdir(parents=True, exist_ok=True)
113
+ head = prompt.strip().split("。")[0][:26]
114
+ run = Run(
115
+ id=run_id,
116
+ scenario="text-to-image",
117
+ title=("文生图片 · " + head) if head else "文生图片",
118
+ status="queued",
119
+ step="queued",
120
+ progress=0.0,
121
+ inputs={"prompt": prompt},
122
+ advanced={"model": model["id"], "aspect": aspect, "area": area},
123
+ steps=[RunStep(name="image", label="生成图片")],
124
+ demo=False,
125
+ created_at=store.now(),
126
+ parent_run_id=parent.id if parent else None,
127
+ version=(parent.version + 1) if parent else 1,
128
+ )
129
+ store.write_run(run)
130
+ manager.start(run.id)
131
+ 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,33 @@
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
+ srt = store.run_artifact(run_id, "subtitles.srt")
26
+ subtitles.write_srt(script, srt)
27
+ return {
28
+ "video": store.rel_to_data(final),
29
+ "srt": store.rel_to_data(srt),
30
+ "width": width,
31
+ "height": height,
32
+ "duration_s": ffmpeg.probe(final)["duration_s"],
33
+ }
@@ -0,0 +1,41 @@
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
+ add("subtitles", "字幕", "pass" if srt.is_file() and srt.stat().st_size > 0 else "warn", {"path": store.rel_to_data(srt)})
29
+ storyboard = store.read_json(store.run_dir(run_id) / "artifacts" / "storyboard.json", {}) or {}
30
+ beats = storyboard.get("segments") or []
31
+ add("segments", "分镜", "pass" if beats else "warn", {"count": len(beats)})
32
+ failed = [c for c in checks if c["status"] == "fail"]
33
+ warned = [c for c in checks if c["status"] == "warn"]
34
+ report = {
35
+ "version": 1,
36
+ "checks": checks,
37
+ "summary": {"pass": len(checks) - len(failed) - len(warned), "warn": len(warned), "fail": len(failed), "shippable": not failed},
38
+ "demo": bool(run.demo),
39
+ }
40
+ store.atomic_write_json(store.run_dir(run_id) / "artifacts" / "qa.json", report)
41
+ return report