-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
860 lines (731 loc) · 26.5 KB
/
app.js
File metadata and controls
860 lines (731 loc) · 26.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
// Moark Web (Cloudflare Pages/Workers)
// - Calls ai.gitee.com via same-origin proxy: /api/* (Pages Functions) to avoid CORS.
// - Downloads images/videos via /dl?url=... (Pages Function) to avoid cross-origin blocks.
const BASE_V1 = "https://ai.gitee.com/v1"; // for reference only (proxied)
const $ = (id) => document.getElementById(id);
const Z_RESOLUTIONS = {
"1:1 (2048x2048)": [2048, 2048],
"1:1 (1024x1024)": [1024, 1024],
"3:4 (768x1024)": [768, 1024],
"4:3 (1024x768)": [1024, 768],
"16:9 (1024x576)": [1024, 576],
"9:16 (576x1024)": [576, 1024],
};
const EDIT_TASK_TYPES = ["id", "style", "pose", "layout", "color", "background"];
const WAN_RES_PRESETS = {
"480p 横屏 / 832x480 (推荐 / Recommended)": [832, 480],
"480p 竖屏 / 480x832": [480, 832],
"720p 横屏 / 1280x720": [1280, 720],
"720p 竖屏 / 720x1280": [720, 1280],
"1024 方图 / 1024x1024": [1024, 1024],
"2048 方图 / 2048x2048 (高成本 / Expensive)": [2048, 2048],
};
function nowTs() {
const d = new Date();
const pad = (n) => String(n).padStart(2, "0");
return `${d.getFullYear()}${pad(d.getMonth()+1)}${pad(d.getDate())}_${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`;
}
function setStatus(text, kind="info") {
const badge = $("statusBadge");
if (!badge) return;
badge.textContent = text;
badge.style.borderColor =
kind === "ok" ? "rgba(37,194,160,.7)" :
kind === "err" ? "rgba(255,84,112,.75)" :
"rgba(255,255,255,.10)";
badge.style.background =
kind === "ok" ? "rgba(37,194,160,.10)" :
kind === "err" ? "rgba(255,84,112,.10)" :
"rgba(255,255,255,.06)";
}
function waitingStatusText(label, tick, elapsedMs, extra="") {
const sec = Math.floor(elapsedMs / 1000);
const extraText = extra ? ` • ${extra}` : "";
return `${label} 轮询中... 已等待 ${sec}s • 第 ${tick} 次检查${extraText} • 正常等待,并非卡死`;
}
function getApiKey() {
const key = $("apiKey").value.trim();
if (!key) throw new Error("请输入 API Key / Please enter API Key");
return key;
}
function rememberKeyMaybe() {
const key = $("apiKey").value.trim();
if ($("rememberKey").checked && key) {
localStorage.setItem("moark_api_key", key);
}
}
function loadRememberedKey() {
const key = localStorage.getItem("moark_api_key") || "";
if (key) {
$("apiKey").value = key;
$("rememberKey").checked = true;
}
}
function clearRememberedKey() {
localStorage.removeItem("moark_api_key");
$("apiKey").value = "";
$("rememberKey").checked = false;
}
function showPanel(model) {
$("panelZ").style.display = model === "z-image" ? "block" : "none";
$("panelEdit").style.display = model === "Edit-2511" ? "block" : "none";
$("panelWan").style.display = model === "Wan2.2-I2V-A14B" ? "block" : "none";
$("panelHunyuan").style.display = model === "HunyuanVideo-1.5" ? "block" : "none";
}
function addOutputItem({title, kind="info", meta="", element=null, rawJson=null, download=null, openUrl=null}) {
const out = $("output");
const box = document.createElement("div");
box.className = "item";
const h = document.createElement("h3");
h.textContent = title;
box.appendChild(h);
if (meta) {
const m = document.createElement("div");
m.className = "meta";
m.textContent = meta;
box.appendChild(m);
}
if (element) box.appendChild(element);
if (rawJson) {
const pre = document.createElement("pre");
pre.textContent = JSON.stringify(rawJson, null, 2);
box.appendChild(pre);
const btns = document.createElement("div");
btns.className = "row";
const b = document.createElement("button");
b.className = "btn";
b.textContent = "下载 JSON / Download JSON";
b.onclick = () => downloadBlob(new Blob([pre.textContent], {type:"application/json"}), `${title}_${nowTs()}.json`);
btns.appendChild(b);
box.appendChild(btns);
}
if (download) {
const btn = document.createElement("a");
btn.className = "btn";
btn.textContent = "下载 / Download";
btn.href = download.href;
btn.download = download.filename || "";
btn.target = "_blank";
btn.rel = "noopener";
const row = document.createElement("div");
row.className = "row";
row.appendChild(btn);
if (openUrl) {
const b2 = document.createElement("a");
b2.className = "btn";
b2.textContent = "打开 file_url";
b2.href = openUrl;
b2.target = "_blank";
b2.rel = "noopener";
row.appendChild(b2);
}
box.appendChild(row);
} else if (openUrl) {
const row = document.createElement("div");
row.className = "row";
const b2 = document.createElement("a");
b2.className = "btn";
b2.textContent = "打开 file_url";
b2.href = openUrl;
b2.target = "_blank";
b2.rel = "noopener";
row.appendChild(b2);
box.appendChild(row);
}
out.prepend(box);
return box;
}
function clearOutput() {
$("output").innerHTML = "";
}
// Same-origin proxy to ai.gitee.com/v1
async function apiFetch(path, {method="GET", headers={}, body=null, signal=null}={}) {
const res = await fetch(`/api/${path.replace(/^\/+/, "")}`, {
method,
headers,
body,
signal,
});
return res;
}
// Download proxy for arbitrary file_url/image urls to avoid CORS
async function dlFetch(url, {signal=null}={}) {
const u = `/dl?url=${encodeURIComponent(url)}`;
const res = await fetch(u, {method:"GET", signal});
return res;
}
async function readJsonSafely(res) {
const text = await res.text();
try {
return JSON.parse(text);
} catch {
return { _text: text };
}
}
function clampInt(v, lo, hi, defv) {
const n = Number.parseInt(String(v), 10);
if (Number.isFinite(n)) return Math.max(lo, Math.min(hi, n));
return defv;
}
function clampFloat(v, lo, hi, defv) {
const n = Number.parseFloat(String(v));
if (Number.isFinite(n)) return Math.max(lo, Math.min(hi, n));
return defv;
}
function downloadBlob(blob, filename) {
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
setTimeout(() => URL.revokeObjectURL(url), 1500);
}
async function fetchAsBlob(url, kindHint="file") {
const r = await dlFetch(url);
if (!r.ok) {
const j = await readJsonSafely(r);
throw new Error(`下载失败 / Download failed (${r.status}): ${JSON.stringify(j).slice(0, 240)}`);
}
const blob = await r.blob();
const objUrl = URL.createObjectURL(blob);
return { blob, objUrl };
}
// Poll task status
async function pollTask(taskId, apiKey, {timeoutMs=30*60*1000, intervalMs=6000, onTick=null}={}) {
const start = Date.now();
let tick = 0;
while (Date.now() - start < timeoutMs) {
tick++;
const elapsedMs = Date.now() - start;
if (onTick) {
onTick({
tick,
elapsedMs,
});
}
const res = await apiFetch(`task/${encodeURIComponent(taskId)}`, {
method: "GET",
headers: { "Authorization": `Bearer ${apiKey}` },
});
const j = await readJsonSafely(res);
const st = j.status || "unknown";
if (st === "success" || st === "failed" || st === "cancelled") {
return { status: st, raw: j };
}
await new Promise(r => setTimeout(r, intervalMs));
}
return { status: "timeout", raw: { status:"timeout", message:"maximum wait time exceeded" } };
}
// -------- HunyuanVideo-1.5 (Text-to-Video) --------
async function runHunyuanVideo() {
const apiKey = getApiKey();
rememberKeyMaybe();
const prompt = $("hyPrompt").value.trim();
if (!prompt) throw new Error("请输入提示词 / Please input prompt");
const negative_prompt = $("hyNeg").value.trim();
const aspect_ratio = $("hyAspect").value;
const num_inferenece_steps = clampInt($("hySteps").value, 1, 10, 10);
const num_frames = clampInt($("hyFrames").value, 81, 241, 241);
// seed must be positive integer
const seedRaw = $("hySeed").value;
const seed = Number.parseInt(String(seedRaw), 10);
if (!Number.isFinite(seed) || seed <= 0) {
throw new Error("seed 必须是正整数 / seed must be a positive integer");
}
const fps = clampInt($("hyFps").value, 1, 24, 24);
const openAfter = $("hyOpenUrl").checked;
// Compose payload (keep server field name: num_inferenece_steps)
const payload = {
prompt,
model: "HunyuanVideo-1.5",
aspect_ratio,
negative_prompt,
num_inferenece_steps,
num_frames,
seed,
fps,
};
setStatus("HunyuanVideo 创建任务... / Creating task...");
const res = await apiFetch("async/videos/generations", {
method: "POST",
headers: {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
const j = await readJsonSafely(res);
if (!res.ok) {
setStatus("HunyuanVideo 失败 / Failed", "err");
addOutputItem({
title: "HunyuanVideo 创建任务失败 / Create task failed",
meta: `HTTP ${res.status}`,
rawJson: j,
});
throw new Error(`API 错误 / API Error (${res.status})`);
}
const taskId = j.task_id;
if (!taskId) {
setStatus("HunyuanVideo 失败 / Failed", "err");
addOutputItem({
title: "HunyuanVideo 未返回 task_id / Missing task_id",
rawJson: j,
});
throw new Error("Task ID not found in response");
}
addOutputItem({
title: "HunyuanVideo 任务已创建 / Task created",
meta: `task_id=${taskId} • aspect_ratio=${aspect_ratio} • frames=${num_frames} • fps=${fps} • steps=${num_inferenece_steps} • seed=${seed}`,
rawJson: j,
openUrl: openAfter ? `https://ai.gitee.com/v1/task/${encodeURIComponent(taskId)}` : null,
});
setStatus("HunyuanVideo 任务已创建,开始轮询...");
const result = await pollTask(taskId, apiKey, {
intervalMs: 10 * 1000,
timeoutMs: 30 * 60 * 1000,
onTick: (info) => {
setStatus(waitingStatusText("HunyuanVideo", info.tick, info.elapsedMs));
},
});
const st = result.status;
const raw = result.raw || {};
if (st !== "success") {
setStatus(`HunyuanVideo ${st} / ${st}`, st === "failed" ? "err" : "info");
addOutputItem({
title: `HunyuanVideo 任务结束:${st} / Task ended: ${st}`,
rawJson: raw,
meta: `task_id=${taskId}`,
});
return;
}
// success
const fileUrl = raw?.output?.file_url;
const textRes = raw?.output?.text_result;
if (fileUrl) {
const blobInfo = await fetchAsBlob(fileUrl, "video");
const video = document.createElement("video");
video.src = blobInfo.objUrl;
video.controls = true;
video.playsInline = true;
addOutputItem({
title: "HunyuanVideo 输出 / Output",
meta: `task_id=${taskId} • file_url=${fileUrl}`,
element: video,
rawJson: raw,
download: { href: blobInfo.objUrl, filename: `hunyuan-video-${nowTs()}.mp4` },
openUrl: openAfter ? fileUrl : null,
});
setStatus("HunyuanVideo 成功 / Success", "ok");
} else if (textRes) {
addOutputItem({
title: "HunyuanVideo 文本输出 / Text output",
meta: `task_id=${taskId}`,
rawJson: raw,
});
setStatus("HunyuanVideo 成功 / Success", "ok");
} else {
addOutputItem({
title: "HunyuanVideo 成功但无输出 / Success but no output",
meta: `task_id=${taskId}`,
rawJson: raw,
});
setStatus("HunyuanVideo 成功 / Success", "ok");
}
}
// -------- z-image --------
async function runZImage() {
const apiKey = getApiKey();
rememberKeyMaybe();
const prompt = $("zPrompt").value.trim();
if (!prompt) throw new Error("请输入提示词 / Please input prompt");
const n = clampInt($("zN").value, 1, 4, 1);
const [w, h] = Z_RESOLUTIONS[$("zRes").value];
const size = `${w}x${h}`;
setStatus("z-image 生成中... / Generating...");
const payload = { prompt, model: "z-image-turbo", n, size };
const res = await apiFetch("images/generations", {
method: "POST",
headers: {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
const j = await readJsonSafely(res);
if (!res.ok) {
setStatus("z-image 失败 / Failed", "err");
addOutputItem({ title: "z-image 生成失败 / Failed", rawJson: j, meta: `HTTP ${res.status}` });
throw new Error(`API 错误 / API Error (${res.status})`);
}
// Expect OpenAI-like: { data: [ { url | b64_json } ] }
const data = Array.isArray(j.data) ? j.data : [];
if (!data.length) {
addOutputItem({ title: "z-image 返回无数据 / Empty response", rawJson: j });
setStatus("z-image 失败 / Failed", "err");
return;
}
for (let i = 0; i < data.length; i++) {
const item = data[i] || {};
let blobInfo = null;
if (item.url) {
blobInfo = await fetchAsBlob(item.url, "image");
} else if (item.b64_json) {
const byteChars = atob(item.b64_json);
const bytes = new Uint8Array(byteChars.length);
for (let k = 0; k < byteChars.length; k++) bytes[k] = byteChars.charCodeAt(k);
const blob = new Blob([bytes], { type: "image/png" });
blobInfo = { blob, objUrl: URL.createObjectURL(blob) };
} else {
addOutputItem({ title: `z-image 第${i+1}张无数据 / No image data`, rawJson: item });
continue;
}
const img = document.createElement("img");
img.src = blobInfo.objUrl;
const filename = `z-image-${nowTs()}-${i+1}.png`;
addOutputItem({
title: `z-image 输出 #${i+1}`,
meta: `size=${size}, n=${n}`,
element: img,
download: { href: blobInfo.objUrl, filename },
});
}
setStatus("z-image 成功 / Success", "ok");
}
// -------- Edit-2511 --------
async function runEdit() {
const apiKey = getApiKey();
rememberKeyMaybe();
const f1 = $("editImg1").files?.[0];
const f2 = $("editImg2").files?.[0];
const prompt = $("editPrompt").value.trim();
if (!f1 || !f2 || !prompt) throw new Error("请上传2张图片并输入提示词 / Please provide 2 images and prompt");
const taskTypes = Array.from(document.querySelectorAll("input[name='editTaskType']:checked")).map(x => x.value);
if (!taskTypes.length) throw new Error("至少选择一个 task_types / Choose at least one task type");
const steps = clampInt($("editSteps").value, 1, 50, 4);
const guidance = clampFloat($("editGuidance").value, 0, 10, 1.0);
const fd = new FormData();
fd.append("prompt", prompt);
fd.append("model", "Qwen-Image-Edit-2511");
fd.append("num_inference_steps", String(steps));
fd.append("guidance_scale", String(guidance));
for (const t of taskTypes) fd.append("task_types", t);
fd.append("image", f1, f1.name);
fd.append("image", f2, f2.name);
setStatus("Edit-2511 创建任务中... / Creating task...");
const res = await apiFetch("async/images/edits", {
method: "POST",
headers: { "Authorization": `Bearer ${apiKey}` },
body: fd,
});
const j = await readJsonSafely(res);
if (!res.ok || !j.task_id) {
setStatus("Edit-2511 创建失败 / Create failed", "err");
addOutputItem({
title: "Edit-2511 创建任务失败 / Create failed",
meta: `HTTP ${res.status}`,
rawJson: j,
});
throw new Error("创建任务失败 / Create failed");
}
const taskId = j.task_id;
setStatus(`Edit-2511 任务已创建,开始轮询... (${taskId.slice(0,8)})`);
const result = await pollTask(taskId, apiKey, {
intervalMs: 6000,
onTick: (info) => {
setStatus(
waitingStatusText(
"Edit-2511",
info.tick,
info.elapsedMs,
`task=${taskId.slice(0,8)}`
)
);
},
});
addOutputItem({ title: `Edit-2511 任务结果 task=${taskId.slice(0,8)}`, rawJson: result.raw });
if (result.status !== "success") {
setStatus("Edit-2511 失败 / Failed", "err");
throw new Error(`任务失败 / Task failed: ${result.status}`);
}
const fileUrl = result.raw?.output?.file_url;
if (!fileUrl) throw new Error("success 但没有 file_url / no file_url");
setStatus("Edit-2511 下载中... / Downloading...");
const { objUrl } = await fetchAsBlob(fileUrl, "image");
const img = document.createElement("img");
img.src = objUrl;
addOutputItem({
title: "Edit-2511 输出图片",
meta: `task_id=${taskId}`,
element: img,
download: { href: objUrl, filename: `edit-2511-${nowTs()}.png` },
openUrl: $("editOpenUrl").checked ? fileUrl : null,
});
setStatus("Edit-2511 成功 / Success", "ok");
}
// -------- Wan2.2 I2V --------
function applyWanResolution() {
const key = $("wanResPreset").value;
const [w, h] = WAN_RES_PRESETS[key];
$("wanW").value = String(w);
$("wanH").value = String(h);
}
function applyWanPreset() {
const p = $("wanPreset").value;
let steps = 30;
let guidance = 5.0;
let fps = 24;
if (p.includes("更清晰")) { steps = 60; guidance = 6.0; }
else if (p.includes("更动感")) { steps = 40; guidance = 5.0; fps = 30; }
else if (p.includes("更快")) { steps = 20; guidance = 4.0; }
$("wanSteps").value = String(steps);
$("wanGuidance").value = String(guidance);
$("wanFps").value = String(fps);
if ($("wanAutoFrames").checked) {
$("wanFrames").value = String(Math.max(1, Math.min(300, fps * 5)));
}
}
function buildWanFormData({
imageFile, prompt, model, numInferenceSteps, numFrames, guidanceScale,
width, height, negativePrompt, seed, watermark, promptExtend, useTypoField=false
}) {
const fd = new FormData();
fd.append("prompt", prompt);
fd.append("model", model);
fd.append("num_frames", String(numFrames));
fd.append("guidance_scale", String(guidanceScale));
fd.append("height", String(height));
fd.append("width", String(width));
if (negativePrompt?.trim()) fd.append("negative_prompt", negativePrompt.trim());
if (seed !== null && seed !== undefined) fd.append("seed", String(seed));
if (watermark !== null && watermark !== undefined) fd.append("watermark", watermark ? "true" : "false");
if (promptExtend !== null && promptExtend !== undefined) fd.append("prompt_extend", promptExtend ? "true" : "false");
fd.append(useTypoField ? "num_inferenece_steps" : "num_inference_steps", String(numInferenceSteps));
fd.append("image", imageFile, imageFile.name);
return fd;
}
async function createWanTask(apiKey, params) {
// Try correct field name first
let fd = buildWanFormData({ ...params, useTypoField:false });
let res = await apiFetch("async/videos/image-to-video", {
method: "POST",
headers: { "Authorization": `Bearer ${apiKey}` },
body: fd,
});
let j = await readJsonSafely(res);
if (res.ok && j.task_id) return { ok:true, res, json:j, tried:"num_inference_steps" };
// Fallback to typo
fd = buildWanFormData({ ...params, useTypoField:true });
res = await apiFetch("async/videos/image-to-video", {
method: "POST",
headers: { "Authorization": `Bearer ${apiKey}` },
body: fd,
});
const j2 = await readJsonSafely(res);
if (res.ok && j2.task_id) return { ok:true, res, json:j2, tried:"num_inferenece_steps" };
return { ok:false, res, json:{ _try1: j, _try2: j2 }, tried:"both" };
}
// Optional: zip segments via JSZip loaded dynamically when needed
async function ensureJsZip() {
if (window.JSZip) return window.JSZip;
const script = document.createElement("script");
script.src = "https://cdn.jsdelivr.net/npm/jszip@3.10.1/dist/jszip.min.js";
script.crossOrigin = "anonymous";
document.head.appendChild(script);
await new Promise((resolve, reject) => {
script.onload = resolve;
script.onerror = () => reject(new Error("加载 JSZip 失败 / Failed to load JSZip"));
});
return window.JSZip;
}
async function zipAndDownloadMp4s(files, zipName) {
const JSZip = await ensureJsZip();
const zip = new JSZip();
for (const f of files) {
zip.file(f.name, f.blob);
}
const blob = await zip.generateAsync({ type: "blob" });
downloadBlob(blob, zipName);
}
async function runWan() {
const apiKey = getApiKey();
rememberKeyMaybe();
const img = $("wanImg").files?.[0];
if (!img) throw new Error("请选择有效图片 / Please select a valid image");
const prompt = $("wanPrompt").value.trim();
if (!prompt) throw new Error("请输入提示词 / Please input prompt");
const neg = $("wanNeg").value.trim();
const width = clampInt($("wanW").value, 64, 2048, 832);
const height = clampInt($("wanH").value, 64, 2048, 480);
const steps = clampInt($("wanSteps").value, 1, 100, 30);
const guidance = clampFloat($("wanGuidance").value, 0, 20, 5.0);
const fps = clampInt($("wanFps").value, 1, 60, 24);
const duration = clampFloat($("wanDuration").value, 0.5, 60, 5.0);
let numFrames;
if ($("wanAutoFrames").checked) {
numFrames = Math.max(1, Math.min(300, fps * 5));
$("wanFrames").value = String(numFrames);
} else {
numFrames = clampInt($("wanFrames").value, 1, 300, 30);
}
const seedVal = clampInt($("wanSeed").value, -1, 2147483647, -1);
const seed = seedVal < 0 ? null : seedVal;
const watermark = $("wanWatermark").checked;
const promptExtend = $("wanPromptExtend").checked;
// segment logic (same spirit as desktop): assume backend returns 5s per segment
const segmentLen = 5.0;
const segCount = Math.max(1, Math.ceil(duration / segmentLen));
const segments = []; // {name, blob, objUrl, fileUrl, taskId}
for (let i = 0; i < segCount; i++) {
setStatus(`Wan2.2 分段 ${i+1}/${segCount} 创建中... / Segment ${i+1}/${segCount} creating...`);
const create = await createWanTask(apiKey, {
imageFile: img,
prompt,
model: "Wan2_2-I2V-A14B",
numInferenceSteps: steps,
numFrames,
guidanceScale: guidance,
width,
height,
negativePrompt: neg,
seed,
watermark,
promptExtend,
});
if (!create.ok) {
setStatus("Wan2.2 创建失败 / Create failed", "err");
addOutputItem({
title: `Wan2.2 创建任务失败(分段 ${i+1})`,
meta: `tried=${create.tried}, HTTP ${create.res.status}`,
rawJson: create.json,
});
throw new Error("创建任务失败 / Create failed");
}
const taskId = create.json.task_id;
setStatus(`Wan2.2 分段 ${i+1}/${segCount} 任务已创建,开始轮询... (${taskId.slice(0,8)})`);
const result = await pollTask(taskId, apiKey, {
timeoutMs: 60*60*1000,
intervalMs: 8000,
onTick: (info) => {
setStatus(
waitingStatusText(
`Wan2.2 分段 ${i+1}/${segCount}`,
info.tick,
info.elapsedMs,
`task=${taskId.slice(0,8)}`
)
);
},
});
addOutputItem({ title: `Wan2.2 分段 ${i+1} 任务结果`, rawJson: result.raw, meta: `task_id=${taskId}` });
if (result.status !== "success") {
setStatus("Wan2.2 失败 / Failed", "err");
throw new Error(`任务失败 / Task failed: ${result.status}`);
}
const fileUrl = result.raw?.output?.file_url;
if (!fileUrl) throw new Error("success 但没有 file_url / no file_url");
setStatus(`Wan2.2 分段 ${i+1}/${segCount} 下载中... / Downloading...`);
const dl = await fetchAsBlob(fileUrl, "video");
const name = `wan_seg${i+1}_${nowTs()}.mp4`;
segments.push({ name, blob: dl.blob, objUrl: dl.objUrl, fileUrl, taskId });
// Show each segment playable + download
const video = document.createElement("video");
video.controls = true;
video.src = dl.objUrl;
addOutputItem({
title: `Wan2.2 输出视频(分段 ${i+1}/${segCount})`,
meta: `width=${width}, height=${height}, frames=${numFrames}, steps=${steps}, guidance=${guidance}`,
element: video,
download: { href: dl.objUrl, filename: name },
openUrl: $("wanOpenUrl").checked ? fileUrl : null,
});
}
// Optional zip download
if (segCount > 1 && $("wanZipSegments").checked) {
try {
setStatus("Wan2.2 打包 zip 中... / Zipping...");
await zipAndDownloadMp4s(segments, `wan_segments_${nowTs()}.zip`);
setStatus("Wan2.2 成功 / Success", "ok");
} catch (e) {
addOutputItem({ title: "Wan2.2 zip 打包失败 / Zip failed", meta: String(e) });
setStatus("Wan2.2 成功(但 zip 失败)/ Success (zip failed)", "ok");
}
} else {
setStatus("Wan2.2 成功 / Success", "ok");
}
}
// ---- init UI ----
function initUi() {
// fill selects
const zRes = $("zRes");
for (const k of Object.keys(Z_RESOLUTIONS)) {
const o = document.createElement("option");
o.value = k; o.textContent = k;
zRes.appendChild(o);
}
zRes.value = Object.keys(Z_RESOLUTIONS)[0];
const wanRes = $("wanResPreset");
for (const k of Object.keys(WAN_RES_PRESETS)) {
const o = document.createElement("option");
o.value = k; o.textContent = k;
wanRes.appendChild(o);
}
wanRes.value = Object.keys(WAN_RES_PRESETS)[0];
applyWanResolution();
// task type checkboxes
const box = $("editTaskTypes");
for (const t of EDIT_TASK_TYPES) {
const label = document.createElement("label");
label.className = "chk";
const input = document.createElement("input");
input.type = "checkbox";
input.name = "editTaskType";
input.value = t;
input.checked = (t === "id" || t === "style");
label.appendChild(input);
label.appendChild(document.createTextNode(" " + t));
box.appendChild(label);
}
// model selection
$("modelSel").addEventListener("change", (e) => showPanel(e.target.value));
showPanel($("modelSel").value);
// buttons
$("btnZRun").onclick = async () => {
try { await runZImage(); }
catch (e) { addOutputItem({ title:"z-image 错误 / Error", meta:String(e) }); }
};
$("btnEditRun").onclick = async () => {
try { await runEdit(); }
catch (e) { addOutputItem({ title:"Edit-2511 错误 / Error", meta:String(e) }); }
};
$("btnWanRun").onclick = async () => {
try { await runWan(); }
catch (e) { addOutputItem({ title:"Wan2.2 错误 / Error", meta:String(e) }); }
};
$("btnHyRun").onclick = async () => {
try { await runHunyuanVideo(); }
catch (e) { addOutputItem({ title:"HunyuanVideo 错误 / Error", meta:String(e) }); }
};
$("btnClearOutput").onclick = clearOutput;
$("btnWanApplyPreset").onclick = applyWanPreset;
$("wanResPreset").addEventListener("change", applyWanResolution);
$("wanAutoFrames").addEventListener("change", () => {
if ($("wanAutoFrames").checked) {
const fps = clampInt($("wanFps").value, 1, 60, 24);
$("wanFrames").value = String(Math.max(1, Math.min(300, fps * 5)));
}
});
$("wanFps").addEventListener("change", () => {
if ($("wanAutoFrames").checked) {
const fps = clampInt($("wanFps").value, 1, 60, 24);
$("wanFrames").value = String(Math.max(1, Math.min(300, fps * 5)));
}
});
$("btnClearKey").onclick = clearRememberedKey;
loadRememberedKey();
}
window.addEventListener("DOMContentLoaded", () => {
initUi();
setStatus("准备就绪 / Ready");
});