@chad.dev/dsh-adclip 0.1.0 → 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 +0 -1
- package/README.md +9 -9
- package/client.js +190 -91
- package/engine/src/adclip/api.py +42 -14
- package/engine/src/adclip/catalog.py +341 -0
- package/engine/src/adclip/image_params.py +30 -49
- package/engine/src/adclip/intents.py +30 -11
- package/engine/src/adclip/media/ffmpeg.py +4 -4
- package/engine/src/adclip/media/subtitles.py +1 -1
- package/engine/src/adclip/models.py +2 -2
- package/engine/src/adclip/params.py +19 -224
- package/engine/src/adclip/pipeline/generate.py +56 -61
- package/engine/src/adclip/pipeline/image_runner.py +23 -8
- package/engine/src/adclip/pipeline/post.py +10 -3
- package/engine/src/adclip/pipeline/qa.py +4 -1
- package/engine/src/adclip/providers/ark.py +1 -1
- package/engine/src/adclip/providers/ark_image.py +114 -0
- package/engine/src/adclip/providers/minimax_image.py +3 -0
- package/engine/src/adclip/store.py +1 -1
- package/host/dsh.js +1 -1
- package/host/proxy.js +1 -1
- package/locale/en.json +1 -1
- package/locale/zh.json +2 -2
- package/package.json +2 -2
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Volcengine Ark image generation (Seedream 4.x).
|
|
2
|
+
|
|
3
|
+
One POST to `/images/generations`, then the returned URL (or inline base64) is
|
|
4
|
+
written to the run's artifact path. The request shape follows Ark's image API:
|
|
5
|
+
`size` takes explicit pixel dimensions computed from the UI's area + aspect, and
|
|
6
|
+
`watermark` is off because the output is a product image.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import base64
|
|
11
|
+
import mimetypes
|
|
12
|
+
import os
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
import httpx
|
|
16
|
+
|
|
17
|
+
BASE_URL = os.environ.get("ARK_BASE_URL", "https://ark.cn-beijing.volces.com/api/v3").rstrip("/")
|
|
18
|
+
MODEL_ENV = "ARK_MODEL_SEEDREAM_4_0"
|
|
19
|
+
DEFAULT_MODEL = "doubao-seedream-4-0-20260415"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _api_key() -> str:
|
|
23
|
+
key = os.environ.get("ARK_API_KEY", "").strip()
|
|
24
|
+
if key.lower().startswith("bearer "):
|
|
25
|
+
key = key[7:].strip()
|
|
26
|
+
return key
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _model_id(model: str) -> str:
|
|
30
|
+
return os.environ.get(MODEL_ENV, "").strip() or model or DEFAULT_MODEL
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _data_uri(path: Path) -> str:
|
|
34
|
+
mime = mimetypes.guess_type(path.name)[0] or "image/png"
|
|
35
|
+
return f"data:{mime};base64,{base64.b64encode(path.read_bytes()).decode('ascii')}"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _error_message(response: httpx.Response) -> str:
|
|
39
|
+
message = response.text[:400]
|
|
40
|
+
try:
|
|
41
|
+
payload = response.json()
|
|
42
|
+
error = payload.get("error") if isinstance(payload, dict) else None
|
|
43
|
+
if isinstance(error, dict):
|
|
44
|
+
message = str(error.get("message") or message)
|
|
45
|
+
elif isinstance(payload, dict) and payload.get("message"):
|
|
46
|
+
message = str(payload["message"])
|
|
47
|
+
except Exception:
|
|
48
|
+
pass
|
|
49
|
+
return message
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def generate(
|
|
53
|
+
prompt: str,
|
|
54
|
+
*,
|
|
55
|
+
model: str = DEFAULT_MODEL,
|
|
56
|
+
aspect: str = "1:1",
|
|
57
|
+
area: str = "2K",
|
|
58
|
+
output: Path,
|
|
59
|
+
reference: Path | None = None,
|
|
60
|
+
) -> dict:
|
|
61
|
+
"""Generate one image; returns its pixel size."""
|
|
62
|
+
from .. import image_params
|
|
63
|
+
|
|
64
|
+
key = _api_key()
|
|
65
|
+
if not key:
|
|
66
|
+
raise RuntimeError("未配置 ARK_API_KEY")
|
|
67
|
+
body: dict = {
|
|
68
|
+
"model": _model_id(model),
|
|
69
|
+
"prompt": prompt,
|
|
70
|
+
"size": image_params.size_for(area, aspect),
|
|
71
|
+
"response_format": "url",
|
|
72
|
+
"watermark": False,
|
|
73
|
+
"sequential_image_generation": "disabled",
|
|
74
|
+
}
|
|
75
|
+
if reference is not None and reference.is_file():
|
|
76
|
+
body["image"] = _data_uri(reference)
|
|
77
|
+
try:
|
|
78
|
+
with httpx.Client(timeout=httpx.Timeout(300.0, connect=15.0)) as client:
|
|
79
|
+
response = client.post(
|
|
80
|
+
f"{BASE_URL}/images/generations",
|
|
81
|
+
headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"},
|
|
82
|
+
json=body,
|
|
83
|
+
)
|
|
84
|
+
if response.status_code >= 400:
|
|
85
|
+
raise RuntimeError(f"Ark 图片生成失败 HTTP {response.status_code}: {_error_message(response)}")
|
|
86
|
+
payload = response.json()
|
|
87
|
+
rows = payload.get("data") or []
|
|
88
|
+
if not rows:
|
|
89
|
+
raise RuntimeError("Ark 图片接口没有返回 data")
|
|
90
|
+
item = rows[0]
|
|
91
|
+
if item.get("error"):
|
|
92
|
+
error = item["error"]
|
|
93
|
+
raise RuntimeError(f"Ark 图片生成失败:{error.get('message') if isinstance(error, dict) else error}")
|
|
94
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
95
|
+
if item.get("b64_json"):
|
|
96
|
+
output.write_bytes(base64.b64decode(item["b64_json"]))
|
|
97
|
+
elif item.get("url"):
|
|
98
|
+
download = client.get(str(item["url"]))
|
|
99
|
+
if download.status_code >= 400:
|
|
100
|
+
raise RuntimeError(f"Ark 图片下载失败 HTTP {download.status_code}")
|
|
101
|
+
output.write_bytes(download.content)
|
|
102
|
+
else:
|
|
103
|
+
raise RuntimeError("Ark 图片接口没有返回 url 或 b64_json")
|
|
104
|
+
except httpx.HTTPError as exc:
|
|
105
|
+
raise RuntimeError(f"Ark 图片请求失败:{exc}") from exc
|
|
106
|
+
result: dict = {}
|
|
107
|
+
try:
|
|
108
|
+
from PIL import Image
|
|
109
|
+
|
|
110
|
+
with Image.open(output) as image:
|
|
111
|
+
result["width"], result["height"] = image.size
|
|
112
|
+
except Exception:
|
|
113
|
+
result["width"] = result["height"] = None
|
|
114
|
+
return result
|
|
@@ -23,10 +23,13 @@ def generate(
|
|
|
23
23
|
aspect: str = "1:1",
|
|
24
24
|
area: str = "2K",
|
|
25
25
|
output: Path,
|
|
26
|
+
reference: Path | None = None,
|
|
26
27
|
) -> dict:
|
|
27
28
|
key = _api_key()
|
|
28
29
|
if not key:
|
|
29
30
|
raise RuntimeError("未配置 MINIMAX_API_KEY")
|
|
31
|
+
# H3 Image is text-to-image only; `reference` exists so the runner can call
|
|
32
|
+
# every adapter with one signature.
|
|
30
33
|
body = {
|
|
31
34
|
"model": model,
|
|
32
35
|
"prompt": prompt,
|
package/host/dsh.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Read the host's configured LLM catalogue and, optionally, run a text call.
|
|
2
2
|
*
|
|
3
|
-
* DSH already owns model routing and credentials.
|
|
3
|
+
* DSH already owns model routing and credentials. AdClip should not ask the user
|
|
4
4
|
* to re-enter an LLM key for copy/analysis when a DSH model is configured.
|
|
5
5
|
* Video generation is different: DSH exposes LLM calls, not video tasks.
|
|
6
6
|
*/
|
package/host/proxy.js
CHANGED
|
@@ -55,7 +55,7 @@ export function registerProxy(ctx, supervisor, dsh) {
|
|
|
55
55
|
// without one so a freshly installed bundle works before the page refresh;
|
|
56
56
|
// a wrong token is always rejected.
|
|
57
57
|
if (token && supervisor.token && token !== supervisor.token) {
|
|
58
|
-
writeJson(res, 401, { error: { code: 'unauthorized', message: 'refresh the page to refresh the
|
|
58
|
+
writeJson(res, 401, { error: { code: 'unauthorized', message: 'refresh the page to refresh the AdClip token' } })
|
|
59
59
|
return
|
|
60
60
|
}
|
|
61
61
|
if (!token && !loopback) {
|
package/locale/en.json
CHANGED
package/locale/zh.json
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chad.dev/dsh-adclip",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "AdClip: AI ad clips up to 30 seconds — viral remakes, text to video, storyboard edits, personas and stitching inside DSH.",
|
|
5
|
+
"description": "AdClip: AI ad clips up to 30 seconds — viral remakes, text to video, text to image, storyboard edits, personas and stitching inside DSH.",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": "./index.js",
|
|
8
8
|
"./client": "./client.js",
|