@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,64 @@
|
|
|
1
|
+
"""Image-generation model catalogue.
|
|
2
|
+
|
|
3
|
+
Deliberately separate from the video catalogue: video models are not image
|
|
4
|
+
models, and image models do not participate in the video model enable list.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import time
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
IMAGE_MODELS: list[dict[str, Any]] = [
|
|
13
|
+
{
|
|
14
|
+
"id": "minimax.h3-image",
|
|
15
|
+
"provider": "minimax",
|
|
16
|
+
"model": "MiniMax-H3-Image",
|
|
17
|
+
"label": "MiniMax H3 Image",
|
|
18
|
+
"aspects": ["1:1", "3:4", "4:3", "9:16", "16:9"],
|
|
19
|
+
"areas": ["1K", "2K", "4K"],
|
|
20
|
+
"default_area": "2K",
|
|
21
|
+
"requires": ["MINIMAX_API_KEY"],
|
|
22
|
+
"available": False,
|
|
23
|
+
"reason": "配置 MINIMAX_API_KEY 后可用",
|
|
24
|
+
"tags": ["文生图", "MiniMax"],
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"id": "minimax.h3-image-highspeed",
|
|
28
|
+
"provider": "minimax",
|
|
29
|
+
"model": "MiniMax-H3-Image-Highspeed",
|
|
30
|
+
"label": "MiniMax H3 Image Highspeed",
|
|
31
|
+
"aspects": ["1:1", "3:4", "4:3", "9:16", "16:9"],
|
|
32
|
+
"areas": ["1K", "2K", "4K"],
|
|
33
|
+
"default_area": "2K",
|
|
34
|
+
"requires": ["MINIMAX_API_KEY"],
|
|
35
|
+
"available": False,
|
|
36
|
+
"reason": "配置 MINIMAX_API_KEY 后可用",
|
|
37
|
+
"tags": ["文生图", "快速"],
|
|
38
|
+
},
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def catalog() -> list[dict[str, Any]]:
|
|
43
|
+
available = bool(os.environ.get("MINIMAX_API_KEY", "").strip())
|
|
44
|
+
rows = []
|
|
45
|
+
for row in IMAGE_MODELS:
|
|
46
|
+
item = dict(row)
|
|
47
|
+
item["available"] = available
|
|
48
|
+
if available:
|
|
49
|
+
item["reason"] = "MiniMax key 已配置;生成时按账号权限为准"
|
|
50
|
+
item["checked_at"] = time.strftime("%Y-%m-%dT%H:%M:%S%z")
|
|
51
|
+
rows.append(item)
|
|
52
|
+
return rows
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def get_model(model_id: str) -> dict[str, Any] | None:
|
|
56
|
+
for row in catalog():
|
|
57
|
+
if row["id"] == model_id:
|
|
58
|
+
return row
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def default_model() -> str:
|
|
63
|
+
rows = [row for row in catalog() if row["available"]]
|
|
64
|
+
return rows[0]["id"] if rows else ""
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Rule-first assistant changes.
|
|
2
|
+
|
|
3
|
+
The assistant is deliberately a form copilot: it returns changes, never starts
|
|
4
|
+
a run. The common phrases from the operator playbook are handled here.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from .models import AssistantRequest
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _change(field: str, old: Any, new: Any, stage: str, label: str) -> dict[str, Any]:
|
|
15
|
+
return {
|
|
16
|
+
"field": field,
|
|
17
|
+
"from": old,
|
|
18
|
+
"to": new,
|
|
19
|
+
"stage": stage,
|
|
20
|
+
"requires_regeneration": stage == "generate",
|
|
21
|
+
"label": label,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def analyze(req: AssistantRequest) -> dict[str, Any]:
|
|
26
|
+
text = req.message.strip()
|
|
27
|
+
context = req.context or {}
|
|
28
|
+
advanced = dict(context.get("advanced") or {})
|
|
29
|
+
persona = dict(context.get("persona") or {})
|
|
30
|
+
post = dict(context.get("post") or {})
|
|
31
|
+
changes: list[dict[str, Any]] = []
|
|
32
|
+
questions: list[str] = []
|
|
33
|
+
actions: list[str] = []
|
|
34
|
+
|
|
35
|
+
# aspect
|
|
36
|
+
aspect = None
|
|
37
|
+
if re.search(r"横屏|16\s*[::]\s*9", text):
|
|
38
|
+
aspect = "16:9"
|
|
39
|
+
elif re.search(r"竖屏|9\s*[::]\s*16", text):
|
|
40
|
+
aspect = "9:16"
|
|
41
|
+
elif re.search(r"方形|1\s*[::]\s*1", text):
|
|
42
|
+
aspect = "1:1"
|
|
43
|
+
if aspect and aspect != advanced.get("aspect"):
|
|
44
|
+
changes.append(_change("advanced.aspect", advanced.get("aspect"), aspect, "generate", "比例"))
|
|
45
|
+
advanced["aspect"] = aspect
|
|
46
|
+
|
|
47
|
+
# resolution
|
|
48
|
+
resolution = None
|
|
49
|
+
if re.search(r"1080|高清", text):
|
|
50
|
+
resolution = "1080p"
|
|
51
|
+
elif re.search(r"720|标清", text):
|
|
52
|
+
resolution = "720p"
|
|
53
|
+
elif re.search(r"480|草稿", text):
|
|
54
|
+
resolution = "480p"
|
|
55
|
+
if resolution and resolution != advanced.get("resolution"):
|
|
56
|
+
changes.append(_change("advanced.resolution", advanced.get("resolution"), resolution, "generate", "清晰度"))
|
|
57
|
+
advanced["resolution"] = resolution
|
|
58
|
+
|
|
59
|
+
# duration
|
|
60
|
+
match = re.search(r"(\d+(?:\.\d+)?)\s*(?:秒|s|S)", text)
|
|
61
|
+
if match:
|
|
62
|
+
duration = int(round(float(match.group(1))))
|
|
63
|
+
duration = max(1, min(30, duration))
|
|
64
|
+
if duration != advanced.get("duration"):
|
|
65
|
+
changes.append(_change("advanced.duration", advanced.get("duration"), duration, "generate", "时长"))
|
|
66
|
+
advanced["duration"] = duration
|
|
67
|
+
|
|
68
|
+
# model
|
|
69
|
+
if re.search(r"可灵|kling", text, re.I):
|
|
70
|
+
changes.append(_change("advanced.model", advanced.get("model"), "kling.v3", "generate", "模型"))
|
|
71
|
+
elif re.search(r"seedance|方舟", text, re.I):
|
|
72
|
+
changes.append(_change("advanced.model", advanced.get("model"), "ark.seedance-2.5", "generate", "模型"))
|
|
73
|
+
elif re.search(r"minimax|海螺", text, re.I):
|
|
74
|
+
changes.append(_change("advanced.model", advanced.get("model"), "minimax.hailuo-2.3-fast", "generate", "模型"))
|
|
75
|
+
elif re.search(r"demo|演示", text, re.I):
|
|
76
|
+
changes.append(_change("advanced.model", advanced.get("model"), "demo", "generate", "模型"))
|
|
77
|
+
|
|
78
|
+
# generation mode
|
|
79
|
+
if re.search(r"精细|逐镜|分段", text):
|
|
80
|
+
changes.append(_change("advanced.generation_mode", advanced.get("generation_mode"), "fine", "generate", "生成模式"))
|
|
81
|
+
elif re.search(r"快速|整条", text):
|
|
82
|
+
changes.append(_change("advanced.generation_mode", advanced.get("generation_mode"), "fast", "generate", "生成模式"))
|
|
83
|
+
|
|
84
|
+
# persona gender
|
|
85
|
+
gender = None
|
|
86
|
+
if re.search(r"换成女性|女性|女生|女主", text):
|
|
87
|
+
gender = "female"
|
|
88
|
+
elif re.search(r"换成男性|男性|男生|男主", text):
|
|
89
|
+
gender = "male"
|
|
90
|
+
if gender and gender != persona.get("gender"):
|
|
91
|
+
changes.append(_change("persona.gender", persona.get("gender"), gender, "generate", "模特性别"))
|
|
92
|
+
persona["gender"] = gender
|
|
93
|
+
|
|
94
|
+
# persona age
|
|
95
|
+
age = None
|
|
96
|
+
if re.search(r"年龄大|老一点|成熟", text):
|
|
97
|
+
age = {"teenager": "youth", "youth": "middle_aged", "middle_aged": "senior", "senior": "senior"}.get(persona.get("age") or "youth")
|
|
98
|
+
elif re.search(r"年轻|年龄小|小几岁", text):
|
|
99
|
+
age = {"teenager": "teenager", "youth": "teenager", "middle_aged": "youth", "senior": "middle_aged"}.get(persona.get("age") or "youth")
|
|
100
|
+
if age and age != persona.get("age"):
|
|
101
|
+
changes.append(_change("persona.age", persona.get("age"), age, "generate", "模特年龄"))
|
|
102
|
+
persona["age"] = age
|
|
103
|
+
|
|
104
|
+
# persona body
|
|
105
|
+
body = None
|
|
106
|
+
if re.search(r"壮|健硕|肌肉", text):
|
|
107
|
+
body = "athletic"
|
|
108
|
+
elif re.search(r"瘦|纤细", text):
|
|
109
|
+
body = "slim"
|
|
110
|
+
elif re.search(r"微胖|丰满", text):
|
|
111
|
+
body = "chubby"
|
|
112
|
+
elif re.search(r"标准|匀称", text):
|
|
113
|
+
body = "standard"
|
|
114
|
+
if body and body != persona.get("body_type"):
|
|
115
|
+
changes.append(_change("persona.body_type", persona.get("body_type"), body, "generate", "模特体型"))
|
|
116
|
+
persona["body_type"] = body
|
|
117
|
+
|
|
118
|
+
# persona ethnicity
|
|
119
|
+
ethnicity = None
|
|
120
|
+
if re.search(r"欧美|白人|caucasian", text, re.I):
|
|
121
|
+
ethnicity = "caucasian"
|
|
122
|
+
elif re.search(r"亚洲|asian", text, re.I):
|
|
123
|
+
ethnicity = "asian"
|
|
124
|
+
elif re.search(r"拉美|latino", text, re.I):
|
|
125
|
+
ethnicity = "latino"
|
|
126
|
+
elif re.search(r"非洲|african", text, re.I):
|
|
127
|
+
ethnicity = "african"
|
|
128
|
+
elif re.search(r"中东|middle", text, re.I):
|
|
129
|
+
ethnicity = "middle_eastern"
|
|
130
|
+
if ethnicity and ethnicity != persona.get("ethnicity"):
|
|
131
|
+
changes.append(_change("persona.ethnicity", persona.get("ethnicity"), ethnicity, "generate", "模特族裔"))
|
|
132
|
+
persona["ethnicity"] = ethnicity
|
|
133
|
+
|
|
134
|
+
# post-only
|
|
135
|
+
if re.search(r"字幕", text) and re.search(r"大|放大", text):
|
|
136
|
+
size = int(post.get("subtitle_size") or 32) + 4
|
|
137
|
+
changes.append(_change("post.subtitle_size", post.get("subtitle_size"), size, "post", "字幕字号"))
|
|
138
|
+
post["subtitle_size"] = size
|
|
139
|
+
if re.search(r"字幕", text) and re.search(r"小", text):
|
|
140
|
+
size = max(16, int(post.get("subtitle_size") or 32) - 4)
|
|
141
|
+
changes.append(_change("post.subtitle_size", post.get("subtitle_size"), size, "post", "字幕字号"))
|
|
142
|
+
post["subtitle_size"] = size
|
|
143
|
+
if re.search(r"轻音乐|背景音乐|音乐", text):
|
|
144
|
+
changes.append(_change("post.music", post.get("music", "off"), "light", "post", "音乐"))
|
|
145
|
+
post["music"] = "light"
|
|
146
|
+
if re.search(r"文件.*(小|太大)|码率|体积", text):
|
|
147
|
+
changes.append(_change("post.export_bitrate", post.get("export_bitrate", "8M"), "4M", "post", "导出码率"))
|
|
148
|
+
post["export_bitrate"] = "4M"
|
|
149
|
+
|
|
150
|
+
# stitch intent
|
|
151
|
+
stitch_words = re.search(r"拼|合并|接起来|插入|中间加|加个广告|串起来", text)
|
|
152
|
+
if stitch_words:
|
|
153
|
+
actions.append("stitch")
|
|
154
|
+
questions.append("请选择至少两个视频片段,或在素材库里勾选后再打开拼接计划。")
|
|
155
|
+
|
|
156
|
+
# reroll / revert
|
|
157
|
+
if re.search(r"再来一条|换一条|重生成一条", text):
|
|
158
|
+
actions.append("retry")
|
|
159
|
+
if re.search(r"上一版|回退|恢复", text):
|
|
160
|
+
actions.append("version")
|
|
161
|
+
|
|
162
|
+
# price / product text -> brief changes
|
|
163
|
+
price = re.search(r"价格.*?(\d+(?:\.\d+)?)|(\d+(?:\.\d+)?)\s*(?:块|元|块九|\.9)", text)
|
|
164
|
+
if price and re.search(r"价格|活动价|原价", text):
|
|
165
|
+
value = price.group(1) or price.group(2)
|
|
166
|
+
changes.append(_change("inputs.brief", context.get("brief"), f"价格调整为 {value},其余结构不变", "generate", "价格台词"))
|
|
167
|
+
actions.append("needs_brief_merge")
|
|
168
|
+
|
|
169
|
+
requires_regen = any(c["stage"] == "generate" for c in changes)
|
|
170
|
+
post_only = bool(changes) and not requires_regen
|
|
171
|
+
if not changes and not questions and actions:
|
|
172
|
+
reply = {"retry": "好,按同参数再生成一条。", "version": "可以切换到上一版。", "stitch": "请选择片段后打开拼接计划。"}.get(actions[0], "我整理成一个动作了,请确认。")
|
|
173
|
+
elif not changes and not questions:
|
|
174
|
+
reply = "我理解为你想调整这条片子。请说具体一点:改台词、换商品/模特、改比例/清晰度、拼接,或只改字幕/音乐。"
|
|
175
|
+
questions.append("你想改内容、画面,还是只改后期?")
|
|
176
|
+
elif post_only:
|
|
177
|
+
reply = "这些是后期修改,不用重生成。"
|
|
178
|
+
elif stitch_words:
|
|
179
|
+
reply = "这需要走拼接计划:选择片段、插入点、转场和音频后,再应用拼接。"
|
|
180
|
+
else:
|
|
181
|
+
reply = "我整理成变更卡了。生成参数变化需要重生成整条或选中分镜;请确认后应用。"
|
|
182
|
+
return {
|
|
183
|
+
"reply": reply,
|
|
184
|
+
"changes": changes,
|
|
185
|
+
"questions": questions,
|
|
186
|
+
"actions": actions,
|
|
187
|
+
"requires_regeneration": requires_regen,
|
|
188
|
+
"post_only": post_only,
|
|
189
|
+
"context_patch": {"advanced": advanced, "persona": persona, "post": post},
|
|
190
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""MiniMax text helper for structured gag/script generation.
|
|
2
|
+
|
|
3
|
+
This reuses the same MiniMax key as the video adapter; it is not a second
|
|
4
|
+
provider configuration. When the key is absent or the call fails, callers fall
|
|
5
|
+
back to deterministic demo content rather than blocking generation.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import re
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
import httpx
|
|
15
|
+
|
|
16
|
+
BASE = os.environ.get("MINIMAX_API_BASE", "https://api.minimaxi.com").rstrip("/")
|
|
17
|
+
MODEL = os.environ.get("ADSTUDIO_TEXT_MODEL", "MiniMax-M3")
|
|
18
|
+
DSH_URL = (os.environ.get("ADSTUDIO_DSH_CHAT_URL") or (os.environ.get("DSH_WEB_URL", "http://127.0.0.1:63215").rstrip("/") + "/adplus/api/dsh/chat")).strip()
|
|
19
|
+
TEXT_PROVIDER = os.environ.get("ADSTUDIO_TEXT_PROVIDER", "auto").strip().lower()
|
|
20
|
+
LAST_ERROR = ""
|
|
21
|
+
LAST_MODEL = ""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def available() -> bool:
|
|
25
|
+
if TEXT_PROVIDER in {"dsh", "auto"} and DSH_URL:
|
|
26
|
+
return True
|
|
27
|
+
return bool(os.environ.get("MINIMAX_API_KEY", "").strip())
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def model_label() -> str:
|
|
31
|
+
return LAST_MODEL or MODEL
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def provider_label() -> str:
|
|
35
|
+
if TEXT_PROVIDER in {"dsh", "auto"} and DSH_URL:
|
|
36
|
+
return "dsh"
|
|
37
|
+
if os.environ.get("MINIMAX_API_KEY", "").strip():
|
|
38
|
+
return "minimax"
|
|
39
|
+
return "none"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _normalize_text_model(value: Any) -> dict[str, str] | None:
|
|
43
|
+
if not isinstance(value, dict):
|
|
44
|
+
return None
|
|
45
|
+
provider = str(value.get("provider") or "").strip()
|
|
46
|
+
model = str(value.get("model") or "").strip()
|
|
47
|
+
if not provider or not model:
|
|
48
|
+
return None
|
|
49
|
+
return {"provider": provider, "model": model}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _strip_reasoning(text: str) -> str:
|
|
53
|
+
text = re.sub(r"<think>.*?</think>", "", text or "", flags=re.S)
|
|
54
|
+
return text.strip()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _chat_dsh(system: str, user: str, *, max_tokens: int, temperature: float, text_model: dict[str, str] | None = None) -> str:
|
|
58
|
+
global LAST_ERROR, LAST_MODEL
|
|
59
|
+
payload = {
|
|
60
|
+
"system": system,
|
|
61
|
+
"messages": [{"role": "user", "content": [{"type": "text", "text": user}]}],
|
|
62
|
+
"maxTokens": max_tokens,
|
|
63
|
+
}
|
|
64
|
+
if text_model:
|
|
65
|
+
payload["provider"] = text_model["provider"]
|
|
66
|
+
payload["model"] = text_model["model"]
|
|
67
|
+
with httpx.Client(timeout=httpx.Timeout(45.0, connect=8.0)) as client:
|
|
68
|
+
response = client.post(DSH_URL, json=payload)
|
|
69
|
+
if response.status_code >= 400:
|
|
70
|
+
raise RuntimeError(f"DSH bridge HTTP {response.status_code}: {response.text[:300]}")
|
|
71
|
+
data = response.json()
|
|
72
|
+
dsh = data.get("dsh") or {}
|
|
73
|
+
text = _strip_reasoning(str(dsh.get("text") or ""))
|
|
74
|
+
if not text:
|
|
75
|
+
raise RuntimeError("DSH bridge returned empty text")
|
|
76
|
+
LAST_MODEL = str(dsh.get("model") or "dsh")
|
|
77
|
+
return text
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _chat_minimax(system: str, user: str, *, max_tokens: int, temperature: float) -> str:
|
|
81
|
+
global LAST_ERROR, LAST_MODEL
|
|
82
|
+
key = os.environ.get("MINIMAX_API_KEY", "").strip()
|
|
83
|
+
if not key:
|
|
84
|
+
raise RuntimeError("MINIMAX_API_KEY 未配置")
|
|
85
|
+
body = {
|
|
86
|
+
"model": MODEL,
|
|
87
|
+
"messages": [
|
|
88
|
+
{"role": "system", "content": system},
|
|
89
|
+
{"role": "user", "content": user},
|
|
90
|
+
],
|
|
91
|
+
"max_tokens": max_tokens,
|
|
92
|
+
"temperature": temperature,
|
|
93
|
+
}
|
|
94
|
+
with httpx.Client(timeout=httpx.Timeout(90.0, connect=15.0)) as client:
|
|
95
|
+
response = client.post(f"{BASE}/v1/text/chatcompletion_v2", headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"}, json=body)
|
|
96
|
+
if response.status_code >= 400:
|
|
97
|
+
raise RuntimeError(f"MiniMax text HTTP {response.status_code}: {response.text[:300]}")
|
|
98
|
+
data = response.json()
|
|
99
|
+
base = data.get("base_resp") or {}
|
|
100
|
+
if base.get("status_code") not in (0, None):
|
|
101
|
+
raise RuntimeError(f"MiniMax text rejected: {base.get('status_msg')}")
|
|
102
|
+
content = ((data.get("choices") or [{}])[0].get("message") or {}).get("content") or ""
|
|
103
|
+
text = _strip_reasoning(str(content))
|
|
104
|
+
LAST_MODEL = MODEL
|
|
105
|
+
return text
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def chat(system: str, user: str, *, max_tokens: int = 1600, temperature: float = 0.3, text_model: dict[str, str] | None = None) -> str:
|
|
109
|
+
global LAST_ERROR
|
|
110
|
+
LAST_ERROR = ""
|
|
111
|
+
errors = []
|
|
112
|
+
if TEXT_PROVIDER != "minimax" and DSH_URL:
|
|
113
|
+
try:
|
|
114
|
+
return _chat_dsh(system, user, max_tokens=max_tokens, temperature=temperature, text_model=_normalize_text_model(text_model))
|
|
115
|
+
except Exception as exc: # noqa: BLE001
|
|
116
|
+
errors.append(f"DSH: {type(exc).__name__}: {exc}")
|
|
117
|
+
if TEXT_PROVIDER != "dsh" and os.environ.get("MINIMAX_API_KEY", "").strip():
|
|
118
|
+
try:
|
|
119
|
+
return _chat_minimax(system, user, max_tokens=max_tokens, temperature=temperature)
|
|
120
|
+
except Exception as exc: # noqa: BLE001
|
|
121
|
+
errors.append(f"MiniMax: {type(exc).__name__}: {exc}")
|
|
122
|
+
LAST_ERROR = " | ".join(errors)[:500] or "no text provider configured"
|
|
123
|
+
raise RuntimeError(LAST_ERROR)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def chat_json(system: str, user: str, *, max_tokens: int = 1600, temperature: float = 0.25, text_model: dict[str, str] | None = None) -> dict[str, Any] | None:
|
|
127
|
+
global LAST_ERROR
|
|
128
|
+
LAST_ERROR = ""
|
|
129
|
+
for attempt in range(2):
|
|
130
|
+
try:
|
|
131
|
+
text = chat(system, user, max_tokens=max_tokens + attempt * 800, temperature=temperature, text_model=text_model)
|
|
132
|
+
start, end = text.find("{"), text.rfind("}")
|
|
133
|
+
if start < 0 or end <= start:
|
|
134
|
+
LAST_ERROR = f"attempt {attempt + 1}: no JSON in {len(text)} chars"
|
|
135
|
+
continue
|
|
136
|
+
value = json.loads(text[start:end + 1])
|
|
137
|
+
if isinstance(value, dict):
|
|
138
|
+
return value
|
|
139
|
+
LAST_ERROR = f"attempt {attempt + 1}: JSON root is not an object"
|
|
140
|
+
except Exception as exc: # noqa: BLE001
|
|
141
|
+
LAST_ERROR = f"attempt {attempt + 1}: {type(exc).__name__}: {exc}"[:300]
|
|
142
|
+
return None
|
|
File without changes
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Lightweight Chinese ASR for reference clips.
|
|
2
|
+
|
|
3
|
+
faster-whisper is optional: if it is not installed (or ADSTUDIO_ASR=0), the
|
|
4
|
+
analysis pipeline falls back to the heuristic transcript-free path. Audio is
|
|
5
|
+
decoded by ffmpeg to PCM first because the installed PyAV/Python combination
|
|
6
|
+
does not accept faster-whisper's container options directly.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import importlib.util
|
|
11
|
+
import os
|
|
12
|
+
import subprocess
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from .ffmpeg import FFMPEG
|
|
17
|
+
|
|
18
|
+
_MODEL = None
|
|
19
|
+
_MODEL_NAME = ""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def available() -> bool:
|
|
23
|
+
if os.environ.get("ADSTUDIO_ASR", "1") == "0":
|
|
24
|
+
return False
|
|
25
|
+
return importlib.util.find_spec("faster_whisper") is not None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _load_model():
|
|
29
|
+
global _MODEL, _MODEL_NAME
|
|
30
|
+
name = os.environ.get("ADSTUDIO_ASR_MODEL", "small")
|
|
31
|
+
if _MODEL is not None and _MODEL_NAME == name:
|
|
32
|
+
return _MODEL
|
|
33
|
+
from faster_whisper import WhisperModel
|
|
34
|
+
|
|
35
|
+
_MODEL = WhisperModel(name, device="cpu", compute_type="int8")
|
|
36
|
+
_MODEL_NAME = name
|
|
37
|
+
return _MODEL
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _pcm(path: str | Path) -> bytes:
|
|
41
|
+
proc = subprocess.run(
|
|
42
|
+
[FFMPEG, "-v", "error", "-i", str(path), "-vn", "-ac", "1", "-ar", "16000", "-f", "f32le", "-"],
|
|
43
|
+
capture_output=True,
|
|
44
|
+
timeout=120,
|
|
45
|
+
)
|
|
46
|
+
if proc.returncode != 0:
|
|
47
|
+
raise RuntimeError(f"audio decode failed: {proc.stderr[-300:]!r}")
|
|
48
|
+
return proc.stdout
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def transcribe(path: str | Path, *, max_seconds: float = 60.0) -> list[dict[str, Any]]:
|
|
52
|
+
if not available() or not Path(path).is_file():
|
|
53
|
+
return []
|
|
54
|
+
try:
|
|
55
|
+
import numpy as np
|
|
56
|
+
|
|
57
|
+
audio = np.frombuffer(_pcm(path), dtype=np.float32)
|
|
58
|
+
if audio.size < 1600:
|
|
59
|
+
return []
|
|
60
|
+
max_samples = int(max_seconds * 16000)
|
|
61
|
+
if audio.size > max_samples:
|
|
62
|
+
audio = audio[:max_samples]
|
|
63
|
+
model = _load_model()
|
|
64
|
+
segments, _info = model.transcribe(
|
|
65
|
+
audio,
|
|
66
|
+
language=os.environ.get("ADSTUDIO_ASR_LANGUAGE", "zh"),
|
|
67
|
+
vad_filter=True,
|
|
68
|
+
beam_size=1,
|
|
69
|
+
condition_on_previous_text=False,
|
|
70
|
+
)
|
|
71
|
+
rows = []
|
|
72
|
+
for seg in segments:
|
|
73
|
+
text = str(seg.text or "").strip()
|
|
74
|
+
if not text:
|
|
75
|
+
continue
|
|
76
|
+
rows.append({"start_s": round(float(seg.start), 3), "end_s": round(float(seg.end), 3), "text": text})
|
|
77
|
+
return rows
|
|
78
|
+
except Exception:
|
|
79
|
+
return []
|