Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
386 lines (339 loc) · 10.7 KB
/
Copy pathindex.html
File metadata and controls
386 lines (339 loc) · 10.7 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
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image2 API 请求测试器</title>
<style>
:root {
color-scheme: dark;
--bg: #0b0f14;
--panel: rgba(17, 24, 34, 0.88);
--panel-strong: #131c28;
--text: #edf4ff;
--muted: #8fa2ba;
--line: rgba(142, 163, 190, 0.2);
--accent: #7ee787;
--accent-2: #58a6ff;
--danger: #ff7b72;
--shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
background:
radial-gradient(circle at 20% 0%, rgba(88, 166, 255, 0.22), transparent 34rem),
radial-gradient(circle at 84% 10%, rgba(126, 231, 135, 0.16), transparent 30rem),
linear-gradient(135deg, #06090f 0%, var(--bg) 55%, #101820 100%);
color: var(--text);
font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
}
main {
width: min(1180px, calc(100% - 32px));
margin: 0 auto;
padding: 48px 0;
}
.hero {
display: grid;
grid-template-columns: 1.1fr 0.9fr;
gap: 24px;
align-items: end;
margin-bottom: 24px;
}
h1 {
margin: 0 0 12px;
font-size: clamp(36px, 7vw, 84px);
line-height: 0.92;
letter-spacing: -0.07em;
}
.subtitle {
max-width: 720px;
margin: 0;
color: var(--muted);
font-size: 17px;
line-height: 1.7;
}
.notice {
border: 1px solid rgba(126, 231, 135, 0.28);
border-radius: 24px;
padding: 18px;
background: rgba(126, 231, 135, 0.08);
color: #cfffd4;
line-height: 1.6;
}
.grid {
display: grid;
grid-template-columns: 420px 1fr;
gap: 24px;
}
.card {
border: 1px solid var(--line);
border-radius: 28px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
}
form,
.result-panel {
padding: 24px;
}
label {
display: grid;
gap: 8px;
margin-bottom: 16px;
color: var(--muted);
font-size: 13px;
}
input,
textarea,
select {
width: 100%;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel-strong);
color: var(--text);
padding: 13px 14px;
font: inherit;
outline: none;
}
textarea {
min-height: 110px;
resize: vertical;
}
input:focus,
textarea:focus,
select:focus {
border-color: rgba(88, 166, 255, 0.85);
box-shadow: 0 0 0 4px rgba(88, 166, 255, 0.13);
}
.row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
button {
width: 100%;
border: 0;
border-radius: 18px;
padding: 15px 18px;
background: linear-gradient(135deg, var(--accent), var(--accent-2));
color: #061018;
font-weight: 800;
cursor: pointer;
}
button:disabled {
cursor: wait;
filter: grayscale(0.5);
opacity: 0.7;
}
.status {
margin: 0 0 18px;
padding: 14px 16px;
border-radius: 16px;
background: rgba(88, 166, 255, 0.1);
color: #c9e2ff;
}
.status.error {
background: rgba(255, 123, 114, 0.12);
color: #ffd2ce;
}
.preview {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 16px;
margin-bottom: 18px;
}
.preview img {
width: 100%;
border: 1px solid var(--line);
border-radius: 20px;
background: #05070a;
}
details {
border: 1px solid var(--line);
border-radius: 18px;
background: rgba(5, 8, 12, 0.42);
margin-top: 14px;
overflow: hidden;
}
summary {
cursor: pointer;
padding: 14px 16px;
color: var(--accent);
font-weight: 700;
}
pre {
margin: 0;
padding: 0 16px 16px;
overflow: auto;
color: #d8e8ff;
white-space: pre-wrap;
word-break: break-word;
}
@media (max-width: 900px) {
.hero,
.grid,
.row {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<main>
<section class="hero">
<div>
<h1>Image2 API Lab</h1>
<p class="subtitle">
本地请求测试页:填入中转站地址、API Key、模型名和 prompt,验证文生图接口的请求体、鉴权方式和响应结构。
</p>
</div>
<div class="notice">
如果浏览器提示 CORS 错误,说明中转站不允许网页跨域直连。页面会生成等价 curl 命令,可先用终端验证接口。
</div>
</section>
<section class="grid">
<form class="card" id="testerForm">
<label>
Base URL
<input id="baseUrl" value="https://www.micuapi.ai/v1" autocomplete="off" />
</label>
<label>
Endpoint
<input id="endpoint" value="/images/generations" autocomplete="off" />
</label>
<label>
API Key
<input id="apiKey" type="password" placeholder="sk-..." autocomplete="off" />
</label>
<div class="row">
<label>
Model
<input id="model" value="image2" autocomplete="off" />
</label>
<label>
Size
<select id="size">
<option>1024x1024</option>
<option>1024x1792</option>
<option>1792x1024</option>
<option>512x512</option>
</select>
</label>
</div>
<label>
Response Format
<select id="responseFormat">
<option value="url">url</option>
<option value="b64_json">b64_json</option>
</select>
</label>
<label>
请求实验模式
<select id="experimentMode">
<option value="chat_messages">图生图 Chat Messages:content text + image_url data URL</option>
</select>
</label>
<label>
参考图文件(可多选)
<input id="imageFiles" type="file" accept="image/png,image/jpeg" multiple />
</label>
<label>
Prompt
<textarea id="prompt">一只穿着宇航服的橘猫,站在月球表面,电影感光照</textarea>
</label>
<button id="submitButton" type="submit">发送请求并解析响应</button>
</form>
<div class="card result-panel">
<p class="status" id="statusText">等待请求。先填入 API Key,然后点击发送。</p>
<div class="preview" id="imagePreview"></div>
<details open>
<summary>请求预览</summary>
<pre id="requestPreview">{}</pre>
</details>
<details open>
<summary>响应原文</summary>
<pre id="responsePreview">{}</pre>
</details>
<details>
<summary>等价 curl(已脱敏 Key)</summary>
<pre id="curlPreview"></pre>
</details>
</div>
</section>
</main>
<script src="./app.js"></script>
<script>
const form = document.querySelector("#testerForm");
const submitButton = document.querySelector("#submitButton");
const statusText = document.querySelector("#statusText");
const requestPreview = document.querySelector("#requestPreview");
const responsePreview = document.querySelector("#responsePreview");
const curlPreview = document.querySelector("#curlPreview");
const imagePreview = document.querySelector("#imagePreview");
function readFormData() {
return {
baseUrl: document.querySelector("#baseUrl").value,
endpoint: document.querySelector("#endpoint").value,
apiKey: document.querySelector("#apiKey").value,
model: document.querySelector("#model").value,
size: document.querySelector("#size").value,
responseFormat: document.querySelector("#responseFormat").value,
experimentMode: document.querySelector("#experimentMode").value,
imageFiles: document.querySelector("#imageFiles").files,
prompt: document.querySelector("#prompt").value,
};
}
function renderImages(results) {
imagePreview.innerHTML = "";
for (const result of results) {
const img = document.createElement("img");
img.alt = "生成结果";
img.src =
result.type === "base64"
? `data:image/png;base64,${result.value}`
: result.value;
imagePreview.append(img);
}
}
form.addEventListener("submit", async (event) => {
event.preventDefault();
statusText.classList.remove("error");
statusText.textContent = "请求中...";
submitButton.disabled = true;
const formData = readFormData();
const request =
await ImageApiTester.buildImageExperimentRequest(formData);
requestPreview.textContent = JSON.stringify(
ImageApiTester.toDisplayRequest(request, formData.apiKey),
null,
2,
);
curlPreview.textContent = ImageApiTester.buildCurlCommand(
request,
formData.apiKey,
);
try {
const result = await ImageApiTester.sendImageGenerationRequest(formData);
responsePreview.textContent = JSON.stringify(result.responseBody, null, 2);
renderImages(result.imageResults);
statusText.textContent = result.ok
? `请求成功:HTTP ${result.status},解析到 ${result.imageResults.length} 张图片。`
: `请求失败:HTTP ${result.status} ${result.statusText}`;
statusText.classList.toggle("error", !result.ok);
} catch (error) {
responsePreview.textContent = String(error?.stack || error);
statusText.textContent =
"请求被浏览器拦截或网络失败。常见原因是 CORS 限制,可复制 curl 到终端继续验证。";
statusText.classList.add("error");
imagePreview.innerHTML = "";
} finally {
submitButton.disabled = false;
}
});
</script>
</body>
</html>