@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,138 @@
|
|
|
1
|
+
"""Sequential run executor with cancellation and step persistence."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import traceback
|
|
5
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from .. import store
|
|
9
|
+
from ..models import RunStep
|
|
10
|
+
from ..media import ffmpeg
|
|
11
|
+
from . import analyze, generate, post, qa, script, segment
|
|
12
|
+
|
|
13
|
+
STEPS = [
|
|
14
|
+
("analyze", "拆解参考片", 0.1),
|
|
15
|
+
("gag", "抽梗", 0.2),
|
|
16
|
+
("script", "改编脚本", 0.3),
|
|
17
|
+
("generate", "生成原生对白视频", 0.6),
|
|
18
|
+
("post", "后期", 0.85),
|
|
19
|
+
("qa", "检查", 0.95),
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Canceled(RuntimeError):
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _set_step(run_id: str, name: str, status: str, progress: float, error: str | None = None) -> None:
|
|
28
|
+
run = store.read_run(run_id)
|
|
29
|
+
if run is None:
|
|
30
|
+
return
|
|
31
|
+
for step in run.steps:
|
|
32
|
+
if step.name == name:
|
|
33
|
+
step.status = status # type: ignore[assignment]
|
|
34
|
+
step.progress = progress
|
|
35
|
+
if status == "running":
|
|
36
|
+
step.started_at = store.now()
|
|
37
|
+
if status in {"done", "failed", "skipped"}:
|
|
38
|
+
step.finished_at = store.now()
|
|
39
|
+
if error:
|
|
40
|
+
step.error = error
|
|
41
|
+
break
|
|
42
|
+
run.step = name
|
|
43
|
+
run.progress = progress
|
|
44
|
+
store.write_run(run)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _check_cancel(run_id: str) -> None:
|
|
48
|
+
run = store.read_run(run_id)
|
|
49
|
+
if run and run.cancel_requested:
|
|
50
|
+
raise Canceled("canceled by user")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def execute(run_id: str) -> None:
|
|
54
|
+
run = store.read_run(run_id)
|
|
55
|
+
if run is None:
|
|
56
|
+
return
|
|
57
|
+
run.status = "running"
|
|
58
|
+
run.error = None
|
|
59
|
+
run.steps = [RunStep(name=name, label=label) for name, label, _ in STEPS]
|
|
60
|
+
store.write_run(run)
|
|
61
|
+
try:
|
|
62
|
+
_check_cancel(run_id)
|
|
63
|
+
_set_step(run_id, "analyze", "running", 0.05)
|
|
64
|
+
skeleton = analyze.analyze(run_id)
|
|
65
|
+
_set_step(run_id, "analyze", "done", 0.1)
|
|
66
|
+
_check_cancel(run_id)
|
|
67
|
+
|
|
68
|
+
_set_step(run_id, "gag", "running", 0.15)
|
|
69
|
+
run = store.read_run(run_id)
|
|
70
|
+
gag = script.build_gag(run, skeleton)
|
|
71
|
+
store.atomic_write_json(store.run_dir(run_id) / "artifacts" / "gag.json", gag)
|
|
72
|
+
_set_step(run_id, "gag", "done", 0.2)
|
|
73
|
+
_check_cancel(run_id)
|
|
74
|
+
|
|
75
|
+
_set_step(run_id, "script", "running", 0.25)
|
|
76
|
+
run = store.read_run(run_id)
|
|
77
|
+
script_doc = script.build_script(run, skeleton, gag)
|
|
78
|
+
store.atomic_write_json(store.run_dir(run_id) / "artifacts" / "script.json", script_doc)
|
|
79
|
+
_set_step(run_id, "script", "done", 0.3)
|
|
80
|
+
_check_cancel(run_id)
|
|
81
|
+
|
|
82
|
+
_set_step(run_id, "generate", "running", 0.35)
|
|
83
|
+
generated = generate.generate(run_id)
|
|
84
|
+
run = store.read_run(run_id)
|
|
85
|
+
if run:
|
|
86
|
+
run.demo = bool(generated.get("demo", True))
|
|
87
|
+
store.write_run(run)
|
|
88
|
+
_set_step(run_id, "generate", "done", 0.6)
|
|
89
|
+
_check_cancel(run_id)
|
|
90
|
+
|
|
91
|
+
_set_step(run_id, "post", "running", 0.7)
|
|
92
|
+
result = post.post(run_id)
|
|
93
|
+
_set_step(run_id, "post", "done", 0.85)
|
|
94
|
+
_check_cancel(run_id)
|
|
95
|
+
|
|
96
|
+
segment.build_storyboard(run_id)
|
|
97
|
+
_set_step(run_id, "qa", "running", 0.9)
|
|
98
|
+
qa_report = qa.check(run_id)
|
|
99
|
+
_set_step(run_id, "qa", "done", 1.0)
|
|
100
|
+
|
|
101
|
+
run = store.read_run(run_id)
|
|
102
|
+
if run:
|
|
103
|
+
run.status = "succeeded"
|
|
104
|
+
run.progress = 1.0
|
|
105
|
+
run.finished_at = store.now()
|
|
106
|
+
run.demo = bool(generated.get("demo", True))
|
|
107
|
+
run.result = {**result, "qa": qa_report, "demo": bool(generated.get("demo", True)), "model": generated.get("model")}
|
|
108
|
+
store.write_run(run)
|
|
109
|
+
store.write_context({"last_run_id": run.id, "last_reference_asset_id": (run.inputs or {}).get("reference_asset_id"), "last_product_ids": (run.inputs or {}).get("product_asset_ids"), "last_params": run.advanced, "last_brief": (run.inputs or {}).get("brief")})
|
|
110
|
+
except Canceled:
|
|
111
|
+
run = store.read_run(run_id)
|
|
112
|
+
if run:
|
|
113
|
+
run.status = "canceled"
|
|
114
|
+
run.error = "已取消"
|
|
115
|
+
run.finished_at = store.now()
|
|
116
|
+
store.write_run(run)
|
|
117
|
+
except Exception as exc: # noqa: BLE001
|
|
118
|
+
run = store.read_run(run_id)
|
|
119
|
+
if run:
|
|
120
|
+
run.status = "failed"
|
|
121
|
+
run.error = f"{type(exc).__name__}: {exc}"[:800]
|
|
122
|
+
run.finished_at = store.now()
|
|
123
|
+
store.write_run(run)
|
|
124
|
+
traceback.print_exc()
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class Manager:
|
|
128
|
+
def __init__(self, workers: int = 2) -> None:
|
|
129
|
+
self.pool = ThreadPoolExecutor(max_workers=workers, thread_name_prefix="adstudio-run")
|
|
130
|
+
|
|
131
|
+
def start(self, run_id: str) -> None:
|
|
132
|
+
self.pool.submit(execute, run_id)
|
|
133
|
+
|
|
134
|
+
def shutdown(self) -> None:
|
|
135
|
+
self.pool.shutdown(wait=False, cancel_futures=True)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
manager = Manager()
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""Deterministic demo script and gag artefacts.
|
|
2
|
+
|
|
3
|
+
The real LLM/VLM extraction lands after the provider bake-off. This module
|
|
4
|
+
keeps the run shape complete so the UI, storyboard, QA and edits can be built
|
|
5
|
+
and tested without API keys.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import re
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from .. import llm
|
|
13
|
+
from . import persona as persona_mod
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _text_model(run: Any) -> dict[str, Any] | None:
|
|
17
|
+
value = (run.advanced or {}).get("text_model")
|
|
18
|
+
return value if isinstance(value, dict) else None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _fallback_gag(skeleton: dict[str, Any]) -> dict[str, Any]:
|
|
22
|
+
beats = skeleton.get("beats") or []
|
|
23
|
+
must_keep = []
|
|
24
|
+
if beats:
|
|
25
|
+
must_keep.append({"id": "m1", "text": "开头 2 秒内出现人物和商品", "beat": beats[0]["id"], "confidence": 0.6})
|
|
26
|
+
if len(beats) > 2:
|
|
27
|
+
must_keep.append({"id": "m2", "text": "中段出现价格/卖点信息", "beat": beats[len(beats) // 2]["id"], "confidence": 0.55})
|
|
28
|
+
if beats:
|
|
29
|
+
must_keep.append({"id": "m3", "text": "结尾有明确的行动或结果", "beat": beats[-1]["id"], "confidence": 0.55})
|
|
30
|
+
return {
|
|
31
|
+
"version": 2,
|
|
32
|
+
"mechanism": "demo_recreate",
|
|
33
|
+
"mechanism_label": "Demo:结构保真占位梗卡",
|
|
34
|
+
"template_type": "剧情对话",
|
|
35
|
+
"confidence": 0.55,
|
|
36
|
+
"must_keep": must_keep,
|
|
37
|
+
"timing": {"punchline_beat": beats[-1]["id"] if beats else None, "punchline_at_s": beats[-1]["end_s"] if beats else None},
|
|
38
|
+
"transferable": True,
|
|
39
|
+
"replicability": 0.6,
|
|
40
|
+
"risk_tags": ["demo 占位"],
|
|
41
|
+
"source": {"degraded": True, "reason": "demo pipeline"},
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def build_gag(run: Any, skeleton: dict[str, Any]) -> dict[str, Any]:
|
|
46
|
+
"""Ask the MiniMax text model for a structured gag; fall back locally."""
|
|
47
|
+
brief = str((run.inputs or {}).get("brief") or "").strip()
|
|
48
|
+
beats = skeleton.get("beats") or []
|
|
49
|
+
transcript = (skeleton.get("transcript") or [])[:24]
|
|
50
|
+
transcript_text = "\n".join(f"- {x.get('start_s')}-{x.get('end_s')}s {x.get('text')}" for x in transcript)[:1600]
|
|
51
|
+
summary = "\n".join(
|
|
52
|
+
f"- {b['id']} {b['start_s']:.1f}-{b['end_s']:.1f}s [{b.get('function')}] 画面:{((b.get('visual') or {}).get('action') or '')[:90]}"
|
|
53
|
+
for b in beats
|
|
54
|
+
)
|
|
55
|
+
prompt = f"""参考片结构:
|
|
56
|
+
{summary}
|
|
57
|
+
|
|
58
|
+
参考片台词/ASR:
|
|
59
|
+
{transcript_text or '(无可用台词)'}
|
|
60
|
+
|
|
61
|
+
用户/产品需求:
|
|
62
|
+
{brief or '无'}
|
|
63
|
+
|
|
64
|
+
{persona_mod.persona_instruction(run.advanced)}
|
|
65
|
+
|
|
66
|
+
请判断这条参考片靠什么机制成立,并给出 3-6 条必须保留、可被第三人检查的具体要求。
|
|
67
|
+
只输出 JSON:
|
|
68
|
+
{{
|
|
69
|
+
"mechanism": "price_compare_punchline|status_contrast|misunderstanding|exaggeration|repetition|snatch|callout|testimonial|unboxing|none",
|
|
70
|
+
"mechanism_label": "一句中文说明",
|
|
71
|
+
"must_keep": [{{"text": "具体要求", "beat": "b0", "confidence": 0.0}}],
|
|
72
|
+
"punchline_beat": "b0 或 null",
|
|
73
|
+
"confidence": 0.0,
|
|
74
|
+
"risk_tags": ["合规或执行风险"]
|
|
75
|
+
}}
|
|
76
|
+
must_keep 每条必须挂到上面真实存在的 beat;不要写空话。不要编造用户没有提供的原价、对比价、销量、功效或绝对化宣传。没有提供对比价时,只写活动价,不要自己补一个原价数字。"""
|
|
77
|
+
data = llm.chat_json("你是广告片结构分析师。只输出 JSON,不要解释。", prompt, max_tokens=2400, temperature=0.2, text_model=_text_model(run))
|
|
78
|
+
if not data:
|
|
79
|
+
return _fallback_gag(skeleton)
|
|
80
|
+
valid_beats = {b["id"] for b in beats}
|
|
81
|
+
keeps = []
|
|
82
|
+
for i, item in enumerate(data.get("must_keep") or []):
|
|
83
|
+
beat = str(item.get("beat") or "")
|
|
84
|
+
text = str(item.get("text") or "").strip()
|
|
85
|
+
if not text or beat not in valid_beats:
|
|
86
|
+
continue
|
|
87
|
+
keeps.append({"id": f"m{i+1}", "text": persona_mod.neutralize_people(text, run.advanced)[:120], "beat": beat, "confidence": float(item.get("confidence") or 0.6)})
|
|
88
|
+
if len(keeps) >= 6:
|
|
89
|
+
break
|
|
90
|
+
if not keeps:
|
|
91
|
+
fallback = _fallback_gag(skeleton)
|
|
92
|
+
fallback["mechanism"] = str(data.get("mechanism") or fallback["mechanism"])
|
|
93
|
+
fallback["mechanism_label"] = str(data.get("mechanism_label") or fallback["mechanism_label"])
|
|
94
|
+
fallback["confidence"] = float(data.get("confidence") or fallback["confidence"])
|
|
95
|
+
fallback["source"] = {"model": llm.model_label(), "degraded": True, "reason": "LLM must_keep invalid"}
|
|
96
|
+
return fallback
|
|
97
|
+
if len(keeps) < 3:
|
|
98
|
+
fallback = _fallback_gag(skeleton)
|
|
99
|
+
for item in fallback["must_keep"]:
|
|
100
|
+
if len(keeps) >= 3:
|
|
101
|
+
break
|
|
102
|
+
if all(item["beat"] != existing["beat"] for existing in keeps):
|
|
103
|
+
keeps.append(item)
|
|
104
|
+
return {
|
|
105
|
+
"version": 2,
|
|
106
|
+
"mechanism": str(data.get("mechanism") or "none"),
|
|
107
|
+
"mechanism_label": str(data.get("mechanism_label") or ""),
|
|
108
|
+
"template_type": "剧情对话",
|
|
109
|
+
"confidence": float(data.get("confidence") or 0.6),
|
|
110
|
+
"must_keep": keeps[:6],
|
|
111
|
+
"timing": {"punchline_beat": data.get("punchline_beat"), "punchline_at_s": None},
|
|
112
|
+
"transferable": True,
|
|
113
|
+
"replicability": float(data.get("confidence") or 0.6),
|
|
114
|
+
"risk_tags": list(data.get("risk_tags") or []),
|
|
115
|
+
"source": {"model": llm.model_label(), "degraded": False},
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _spoken_lines(brief: str) -> list[str]:
|
|
120
|
+
"""Turn a mixed brief into short, speakable lines for the demo pipeline.
|
|
121
|
+
|
|
122
|
+
The real LLM script adapter will replace this. Until then, never let the
|
|
123
|
+
model read the whole brief (including production instructions) as dialogue.
|
|
124
|
+
"""
|
|
125
|
+
clauses = [part.strip(" ::,,;;。\n") for part in re.split(r"[。;;,,\n]", brief)]
|
|
126
|
+
keep = []
|
|
127
|
+
for clause in clauses:
|
|
128
|
+
if not clause or len(clause) < 2:
|
|
129
|
+
continue
|
|
130
|
+
# Production instructions are not dialogue.
|
|
131
|
+
if re.search(r"保留|不要|要求|改成|更生活化|结构|动作|镜头|台词|信息|修改|参考", clause):
|
|
132
|
+
continue
|
|
133
|
+
if len(clause) > 24:
|
|
134
|
+
clause = clause[:24]
|
|
135
|
+
keep.append(clause)
|
|
136
|
+
if not keep:
|
|
137
|
+
keep = ["这个产品现在活动价 19.9", "不油腻,追剧吃刚好", "真的可以试试"]
|
|
138
|
+
return keep[:3]
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _fallback_script(run: Any, skeleton: dict[str, Any], gag: dict[str, Any]) -> dict[str, Any]:
|
|
142
|
+
brief = str((run.inputs or {}).get("brief") or "").strip()
|
|
143
|
+
spoken = _spoken_lines(brief)
|
|
144
|
+
beats = skeleton.get("beats") or []
|
|
145
|
+
rows = []
|
|
146
|
+
for i, beat in enumerate(beats):
|
|
147
|
+
texts = spoken[:3] if len(beats) == 1 else [spoken[min(i, len(spoken) - 1)]] if spoken else ["这个产品现在活动价 19.9"]
|
|
148
|
+
lines = []
|
|
149
|
+
for j, text in enumerate(texts[:3]):
|
|
150
|
+
speaker = "A" if j % 2 == 0 else "B"
|
|
151
|
+
lines.append({"speaker": speaker, "text": text[:24], "chars": len(text[:24])})
|
|
152
|
+
rows.append({
|
|
153
|
+
"id": beat["id"], "function": beat.get("function"), "len_s": beat.get("len_s"),
|
|
154
|
+
"cps": 4.0, "max_chars": max(12, int(float(beat.get("len_s") or 2) * 4.0)),
|
|
155
|
+
"chars": sum(line["chars"] for line in lines), "fit": "ok", "lines": lines,
|
|
156
|
+
"shot": (persona_mod.persona_label(run.advanced) + "在中景里自然交流,商品可见") if persona_mod.persona_label(run.advanced) else "人物在中景里自然交流,商品可见", "start_s": beat.get("start_s"), "end_s": beat.get("end_s"),
|
|
157
|
+
})
|
|
158
|
+
return {"version": 2, "beats": rows, "totals": {"duration_s": (skeleton.get("source") or {}).get("duration_s"), "chars": sum(r["chars"] for r in rows)}, "must_keep_map": {i["id"]: i.get("beat") for i in gag.get("must_keep") or []}, "compliance_flags": ["demo 文案,价格/功效需人工核对"], "source": {"degraded": True, "reason": "demo pipeline"}}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def build_script(run: Any, skeleton: dict[str, Any], gag: dict[str, Any]) -> dict[str, Any]:
|
|
162
|
+
brief = str((run.inputs or {}).get("brief") or "").strip()
|
|
163
|
+
advanced = run.advanced or {}
|
|
164
|
+
beats = skeleton.get("beats") or []
|
|
165
|
+
target = float(advanced.get("duration") or (skeleton.get("source") or {}).get("duration_s") or 8)
|
|
166
|
+
transcript = (skeleton.get("transcript") or [])[:24]
|
|
167
|
+
transcript_text = "\n".join(f"- {x.get('start_s')}-{x.get('end_s')}s {x.get('text')}" for x in transcript)[:1600]
|
|
168
|
+
beat_text = "\n".join(
|
|
169
|
+
f"- {b['id']} {b['start_s']:.1f}-{b['end_s']:.1f}s [{b.get('function')}] 画面:{((b.get('visual') or {}).get('action') or '')[:90]}"
|
|
170
|
+
for b in beats
|
|
171
|
+
)
|
|
172
|
+
keeps = "\n".join(f"- [{k['beat']}] {k['text']}" for k in gag.get("must_keep") or [])
|
|
173
|
+
prompt = f"""目标时长:{target:g}s;参考片 beat:
|
|
174
|
+
{beat_text}
|
|
175
|
+
|
|
176
|
+
参考片台词/ASR:
|
|
177
|
+
{transcript_text or '(无可用台词)'}
|
|
178
|
+
|
|
179
|
+
必须保留:
|
|
180
|
+
{keeps}
|
|
181
|
+
|
|
182
|
+
产品/需求:
|
|
183
|
+
{brief or '产品展示,突出卖点,最后引导购买'}
|
|
184
|
+
|
|
185
|
+
{persona_mod.persona_instruction(run.advanced)}
|
|
186
|
+
|
|
187
|
+
写一段可直接交给原生对白视频模型的中文脚本。要求:
|
|
188
|
+
- 只输出 JSON;
|
|
189
|
+
- 每个 beat 给出 1-3 行短台词,每行不超过 18 个汉字;
|
|
190
|
+
- 台词自然、像人说话,不要把产品说明/制作要求整段念出来;
|
|
191
|
+
- 保留用户给出的价格数字和 must_keep 的动作/信息;
|
|
192
|
+
- 不要编造未提供的原价、对比价、销量、功效或绝对化宣传;没有对比价就只写活动价;
|
|
193
|
+
- 尽量用 A/B 两个说话人,除非明显是单人场景;
|
|
194
|
+
- 如果存在模特设定,参考片人物只用于动作/构图,action 不得沿用原片性别/年龄/外貌;
|
|
195
|
+
- 每个 beat 给一个画面 action。
|
|
196
|
+
JSON:
|
|
197
|
+
{{"beats":[{{"id":"b0","lines":[{{"speaker":"A","text":"..."}}],"shot":"..."}}]}}"""
|
|
198
|
+
data = llm.chat_json("你是广告片编剧。只输出 JSON,不要解释。", prompt, max_tokens=3600, temperature=0.45, text_model=_text_model(run))
|
|
199
|
+
if not data or not isinstance(data.get("beats"), list):
|
|
200
|
+
fallback = _fallback_script(run, skeleton, gag)
|
|
201
|
+
fallback["source"] = {"degraded": True, "reason": "LLM script failed: " + (llm.LAST_ERROR or "empty response")}
|
|
202
|
+
return fallback
|
|
203
|
+
by_id = {str(item.get("id")): item for item in data["beats"] if isinstance(item, dict)}
|
|
204
|
+
rows = []
|
|
205
|
+
for beat in beats:
|
|
206
|
+
item = by_id.get(beat["id"]) or {}
|
|
207
|
+
lines = []
|
|
208
|
+
for j, line in enumerate((item.get("lines") or [])[:3]):
|
|
209
|
+
text = str((line or {}).get("text") or "").strip()
|
|
210
|
+
if not text:
|
|
211
|
+
continue
|
|
212
|
+
text = text[:24]
|
|
213
|
+
speaker = str((line or {}).get("speaker") or ("A" if j % 2 == 0 else "B"))
|
|
214
|
+
lines.append({"speaker": speaker if speaker in {"A", "B"} else "A", "text": text, "chars": len(text)})
|
|
215
|
+
if not lines:
|
|
216
|
+
fallback = _fallback_script(run, skeleton, gag)
|
|
217
|
+
fallback_row = next((r for r in fallback["beats"] if r["id"] == beat["id"]), None)
|
|
218
|
+
lines = fallback_row["lines"] if fallback_row else [{"speaker": "A", "text": "这个产品现在活动价 19.9", "chars": 13}]
|
|
219
|
+
rows.append({
|
|
220
|
+
"id": beat["id"], "function": beat.get("function"), "len_s": beat.get("len_s"),
|
|
221
|
+
"cps": 4.0, "max_chars": max(12, int(float(beat.get("len_s") or 2) * 4.0)),
|
|
222
|
+
"chars": sum(line["chars"] for line in lines), "fit": "ok", "lines": lines,
|
|
223
|
+
"shot": persona_mod.neutralize_people(str(item.get("shot") or "人物在中景里自然交流,商品可见"), run.advanced)[:120],
|
|
224
|
+
"start_s": beat.get("start_s"), "end_s": beat.get("end_s"),
|
|
225
|
+
})
|
|
226
|
+
return {"version": 2, "beats": rows, "totals": {"duration_s": target, "chars": sum(r["chars"] for r in rows)}, "must_keep_map": {i["id"]: i.get("beat") for i in gag.get("must_keep") or []}, "compliance_flags": ["LLM 文案,价格/功效需人工核对"], "source": {"model": llm.model_label(), "degraded": False}}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Storyboard derivation from the final clip and the script."""
|
|
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 _segments_for(run: Any, skeleton: dict[str, Any], script: dict[str, Any], final_s: float) -> list[dict[str, Any]]:
|
|
11
|
+
advanced = params.merged_advanced(run.advanced)
|
|
12
|
+
target = float(advanced.get("duration") or final_s or 6)
|
|
13
|
+
source_s = float((skeleton.get("source") or {}).get("duration_s") or target)
|
|
14
|
+
scale = (target / source_s) if source_s > 0 else 1.0
|
|
15
|
+
script_beats = {row.get("id"): row for row in script.get("beats") or []}
|
|
16
|
+
beats = skeleton.get("beats") or []
|
|
17
|
+
rows: list[dict[str, Any]] = []
|
|
18
|
+
for i, beat in enumerate(beats):
|
|
19
|
+
start = float(beat.get("start_s") or 0) * scale
|
|
20
|
+
end = float(beat.get("end_s") or target) * scale
|
|
21
|
+
start = min(max(0.0, start), max(0.0, target - 0.3))
|
|
22
|
+
end = min(max(start + 0.3, end), target)
|
|
23
|
+
line = " / ".join(str(x.get("text") or "") for x in (script_beats.get(beat.get("id")) or {}).get("lines") or [])
|
|
24
|
+
rows.append({
|
|
25
|
+
"id": f"s{len(rows)}",
|
|
26
|
+
"index": len(rows),
|
|
27
|
+
"start_s": round(start, 3),
|
|
28
|
+
"end_s": round(end, 3),
|
|
29
|
+
"function": beat.get("function") or "beat",
|
|
30
|
+
"source": "generated" if run.generation_mode == "fine" else "one_shot_slice",
|
|
31
|
+
"thumbnail": None,
|
|
32
|
+
"dialogue": line,
|
|
33
|
+
"action": (script_beats.get(beat.get("id")) or {}).get("shot") or (beat.get("visual") or {}).get("action") or "",
|
|
34
|
+
"product": "",
|
|
35
|
+
"status": "ok",
|
|
36
|
+
"asset_id": None,
|
|
37
|
+
"take": 1,
|
|
38
|
+
"note": "demo analysis segment",
|
|
39
|
+
})
|
|
40
|
+
# Fine mode must expose the actual generation split even when the reference
|
|
41
|
+
# is shorter than the target. Keep two readable segments for 8s+ targets.
|
|
42
|
+
if run.generation_mode == "fine" and len(rows) < 2 and target >= 8:
|
|
43
|
+
rows = [{
|
|
44
|
+
"id": "s0", "index": 0, "start_s": 0.0, "end_s": round(target / 2, 3),
|
|
45
|
+
"function": "hook", "source": "generated", "thumbnail": None,
|
|
46
|
+
"dialogue": rows[0]["dialogue"] if rows else "", "action": rows[0]["action"] if rows else "",
|
|
47
|
+
"product": "", "status": "ok", "asset_id": None, "take": 1, "note": "demo fine segment",
|
|
48
|
+
}, {
|
|
49
|
+
"id": "s1", "index": 1, "start_s": round(target / 2, 3), "end_s": round(target, 3),
|
|
50
|
+
"function": "punchline", "source": "generated", "thumbnail": None,
|
|
51
|
+
"dialogue": "", "action": "产品展示/行动落点", "product": "",
|
|
52
|
+
"status": "ok", "asset_id": None, "take": 1, "note": "demo fine segment",
|
|
53
|
+
}]
|
|
54
|
+
if not rows:
|
|
55
|
+
rows = [{
|
|
56
|
+
"id": "s0", "index": 0, "start_s": 0.0, "end_s": round(target, 3), "function": "beat",
|
|
57
|
+
"source": "one_shot_slice", "thumbnail": None, "dialogue": "", "action": "",
|
|
58
|
+
"product": "", "status": "ok", "asset_id": None, "take": 1, "note": "demo",
|
|
59
|
+
}]
|
|
60
|
+
return rows
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def build_storyboard(run_id: str) -> dict[str, Any]:
|
|
64
|
+
run = store.read_run(run_id)
|
|
65
|
+
if run is None:
|
|
66
|
+
raise FileNotFoundError(run_id)
|
|
67
|
+
skeleton = store.read_json(store.run_dir(run_id) / "artifacts" / "skeleton.json", {}) or {}
|
|
68
|
+
script = store.read_json(store.run_dir(run_id) / "artifacts" / "script.json", {}) or {}
|
|
69
|
+
final = store.run_artifact(run_id, "final.mp4")
|
|
70
|
+
final_s = 0.0
|
|
71
|
+
if final.is_file():
|
|
72
|
+
try:
|
|
73
|
+
final_s = float(ffmpeg.probe(final)["duration_s"])
|
|
74
|
+
except Exception:
|
|
75
|
+
pass
|
|
76
|
+
rows = _segments_for(run, skeleton, script, final_s)
|
|
77
|
+
for i, seg in enumerate(rows):
|
|
78
|
+
if final.is_file():
|
|
79
|
+
thumb = store.run_artifact(run_id, f"storyboard/seg_{i}.jpg")
|
|
80
|
+
try:
|
|
81
|
+
ffmpeg.extract_frame(final, (float(seg["start_s"]) + float(seg["end_s"])) / 2, thumb, width=360)
|
|
82
|
+
seg["thumbnail"] = store.rel_to_data(thumb)
|
|
83
|
+
except Exception:
|
|
84
|
+
pass
|
|
85
|
+
payload = {"version": 1, "generation_mode": run.generation_mode, "segments": rows}
|
|
86
|
+
store.atomic_write_json(store.run_dir(run_id) / "artifacts" / "storyboard.json", payload)
|
|
87
|
+
return payload
|
|
File without changes
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""Minimal Volcengine Ark Seedance adapter (native audio).
|
|
2
|
+
|
|
3
|
+
This is intentionally small: create a content-generation task, poll it, download
|
|
4
|
+
the result. It is implemented against the official Ark REST shape used by the
|
|
5
|
+
existing tooling, but is marked unverified until a real key is available.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import base64
|
|
10
|
+
import mimetypes
|
|
11
|
+
import os
|
|
12
|
+
import time
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
import httpx
|
|
16
|
+
|
|
17
|
+
from .base import Provider, ProviderError, ProviderNotConfigured
|
|
18
|
+
|
|
19
|
+
BASE_URL = os.environ.get("ARK_BASE_URL", "https://ark.cn-beijing.volces.com/api/v3")
|
|
20
|
+
MODELS = {
|
|
21
|
+
"ark.seedance-2.5": "doubao-seedance-2-5-260628",
|
|
22
|
+
"ark.seedance-2.0": "doubao-seedance-2-0-260128",
|
|
23
|
+
"ark.seedance-2.0-mini": "doubao-seedance-2-0-mini-260615",
|
|
24
|
+
"ark.seedance-2.0-fast": "doubao-seedance-2-0-fast-260128",
|
|
25
|
+
}
|
|
26
|
+
MODEL_ENV = {
|
|
27
|
+
"ark.seedance-2.5": "ARK_MODEL_SEEDANCE_2_5",
|
|
28
|
+
"ark.seedance-2.0": "ARK_MODEL_SEEDANCE_2_0",
|
|
29
|
+
"ark.seedance-2.0-mini": "ARK_MODEL_SEEDANCE_2_0_MINI",
|
|
30
|
+
"ark.seedance-2.0-fast": "ARK_MODEL_SEEDANCE_2_0_FAST",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _model_id(model: str) -> str:
|
|
35
|
+
env_name = MODEL_ENV.get(model)
|
|
36
|
+
return (os.environ.get(env_name, "").strip() if env_name else "") or MODELS.get(model, "")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _api_key() -> str:
|
|
40
|
+
key = os.environ.get("ARK_API_KEY", "").strip()
|
|
41
|
+
if key.lower().startswith("bearer "):
|
|
42
|
+
key = key[7:].strip()
|
|
43
|
+
return key
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _data_uri(path: Path) -> str:
|
|
47
|
+
mime = mimetypes.guess_type(path.name)[0] or "image/png"
|
|
48
|
+
return f"data:{mime};base64,{base64.b64encode(path.read_bytes()).decode('ascii')}"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class ArkProvider(Provider):
|
|
52
|
+
id = "ark"
|
|
53
|
+
|
|
54
|
+
def generate(
|
|
55
|
+
self,
|
|
56
|
+
prompt: str,
|
|
57
|
+
*,
|
|
58
|
+
images: list[Path] | None = None,
|
|
59
|
+
duration: int = 8,
|
|
60
|
+
resolution: str = "720p",
|
|
61
|
+
aspect: str = "9:16",
|
|
62
|
+
model: str = "ark.seedance-2.5",
|
|
63
|
+
output: Path,
|
|
64
|
+
) -> Path:
|
|
65
|
+
key = _api_key()
|
|
66
|
+
if not key:
|
|
67
|
+
raise ProviderNotConfigured("未配置 ARK_API_KEY")
|
|
68
|
+
model_id = _model_id(model)
|
|
69
|
+
if model_id is None:
|
|
70
|
+
raise ProviderError(f"未知 Ark 模型:{model}")
|
|
71
|
+
content: list[dict] = [{"type": "text", "text": prompt}]
|
|
72
|
+
for path in (images or [])[:9]:
|
|
73
|
+
content.append({
|
|
74
|
+
"type": "image_url",
|
|
75
|
+
"image_url": {"url": _data_uri(path)},
|
|
76
|
+
"role": "reference_image",
|
|
77
|
+
})
|
|
78
|
+
payload = {
|
|
79
|
+
"model": model_id,
|
|
80
|
+
"content": content,
|
|
81
|
+
"duration": max(4, min(30, int(duration))),
|
|
82
|
+
"resolution": resolution,
|
|
83
|
+
"ratio": aspect,
|
|
84
|
+
"generate_audio": True,
|
|
85
|
+
"watermark": False,
|
|
86
|
+
"return_last_frame": False,
|
|
87
|
+
}
|
|
88
|
+
headers = {"Authorization": f"Bearer {key}", "Content-Type": "application/json"}
|
|
89
|
+
try:
|
|
90
|
+
with httpx.Client(timeout=httpx.Timeout(60.0, connect=15.0)) as client:
|
|
91
|
+
created = client.post(f"{BASE_URL}/contents/generations/tasks", headers=headers, json=payload)
|
|
92
|
+
if created.status_code >= 400:
|
|
93
|
+
raise ProviderError(f"Ark 创建任务失败 HTTP {created.status_code}: {created.text[:300]}")
|
|
94
|
+
task_id = created.json().get("id")
|
|
95
|
+
if not task_id:
|
|
96
|
+
raise ProviderError("Ark 没有返回 task id")
|
|
97
|
+
deadline = time.monotonic() + 1500
|
|
98
|
+
while time.monotonic() < deadline:
|
|
99
|
+
time.sleep(3)
|
|
100
|
+
status_res = client.get(f"{BASE_URL}/contents/generations/tasks/{task_id}", headers=headers)
|
|
101
|
+
if status_res.status_code >= 400:
|
|
102
|
+
raise ProviderError(f"Ark 查询失败 HTTP {status_res.status_code}: {status_res.text[:300]}")
|
|
103
|
+
task = status_res.json()
|
|
104
|
+
status = str(task.get("status") or "").lower()
|
|
105
|
+
if status in {"failed", "cancelled", "expired"}:
|
|
106
|
+
raise ProviderError(f"Ark 任务 {status}: {task.get('error') or task}")
|
|
107
|
+
if status == "succeeded":
|
|
108
|
+
url = ((task.get("content") or {}).get("video_url"))
|
|
109
|
+
if not url:
|
|
110
|
+
raise ProviderError("Ark 成功但没有 video_url")
|
|
111
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
112
|
+
with client.stream("GET", url, timeout=120.0) as download:
|
|
113
|
+
download.raise_for_status()
|
|
114
|
+
with output.open("wb") as handle:
|
|
115
|
+
for chunk in download.iter_bytes():
|
|
116
|
+
handle.write(chunk)
|
|
117
|
+
if not output.is_file() or output.stat().st_size == 0:
|
|
118
|
+
raise ProviderError("Ark 下载结果为空")
|
|
119
|
+
return output
|
|
120
|
+
raise ProviderError("Ark 任务超时")
|
|
121
|
+
except httpx.HTTPError as exc:
|
|
122
|
+
raise ProviderError(f"Ark 网络错误:{exc}") from exc
|