-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
538 lines (446 loc) · 16.4 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Stable Diffusion 3.0 图片生成</title>
<style>
/* 添加CSS样式 */
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 10px;
background-image: none;
/* 移除之前的背景图片 */
background-color: #f9f9f9;
/* 设置页面背景颜色 */
}
.footer {
text-align: center;
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid #ccc;
}
h1 {
text-align: center;
color: #196cea;
margin-top: 1px;
font-size: 24px;
font-family: 'Courier New', Courier, monospace;
}
#account-points {
position: fixed;
top: 70px;
/* 调整积分信息位置 */
right: 10px;
background-color: #333;
color: white;
padding: 5px 10px;
border-radius: 5px;
}
#result {
overflow: auto;
}
label {
font-weight: bold;
}
textarea,
select,
input[type="text"],
input[type="number"],
input[type="file"] {
width: 100%;
padding: 10px;
/* 增加内边距 */
margin-bottom: 15px;
/* 调整外边距 */
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
font-size: 16px;
/* 增加字体大小 */
line-height: 1.2;
/* 调整行高,使文本更加紧凑 */
}
button {
padding: 12px 24px;
/* 调整按钮内边距 */
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.negativePrompt-form {
display: block;
}
.image-options {
display: none;
}
.style_preset-options {
display: none;
}
.form-group {
margin-bottom: 2px;
}
.left-column {
width: 20%;
float: left;
}
.right-column {
width: 75%;
float: right;
/* display: flex; */
flex-wrap: wrap;
padding-left: 1%;
overflow-y: auto;
/* 添加垂直滚动条 */
justify-content: center;
/* 在主轴上居中显示 */
align-items: flex-start;
/* 在交叉轴上顶部对齐 */
}
#result img {
transition: transform 0.25s ease;
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
/* 添加2像素偏移的阴影,颜色为黑色,透明度为0.2 */
margin-right: 5px;
margin-bottom: 5px;
}
#result img:hover {
transform: scale(1.2);
cursor: pointer;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="title">
<h1>Stable Diffusion 3.0 图片生成</h1>
</div>
<div id="account-points"></div>
<div class="left-column">
<div class="form-group">
<label for="api_key">API密钥(api Key):</label><a href="https://platform.stability.ai/account/keys"
target="_blank">点击获取API密钥(Click here to get the API key)</a>
<input type="text" id="api_key" value="">
</div>
<div class="form-group">
<label for="model">模型(model):</label>
<select id="model">
<option value="sd3" selected>Stable Diffusion 3.0(消耗6.5积分)</option>
<option value="sd3-turbo">Stable Diffusion 3.0-turbo(消耗4积分)</option>
<option value="core">Stable Image Core(消耗3积分)</option>
</select>
</div>
<div class="form-group">
<label for="aspect_ratio">图片比例(aspect Ratio):</label>
<select id="aspect_ratio">
<option value="1:1" selected>1:1</option>
<option value="21:9">21:9</option>
<option value="2:3">2:3</option>
<option value="3:2">3:2</option>
<option value="4:5">4:5</option>
<option value="5:4">5:4</option>
<option value="9:16">9:16</option>
<option value="9:21">9:21</option>
<option value="16:9">16:9</option>
</select>
</div>
<div class="form-group">
<label for="mode">模式(mode):</label>
<select id="mode" onchange="toggleImageOptions()">
<option value="text-to-image" selected>文本到图像(text-to-image)</option>
<option value="image-to-image">图像到图像(image-to-image)</option>
</select>
</div>
<!-- 文件输入框用于增加image和strength -->
<div class="image-options">
<div class="form-group">
<label for="image">图像文件(image):</label>
<input type="file" id="image">
</div>
<div class="form-group">
<label for="strength">调整强度(strength) (0.00 - 1.00):</label>
<input type="number" id="strength" step="0.01" min="0" max="1">
</div>
</div>
<div class="form-group">
<label for="seed">随机种子(seed):</label>
<input type="number" id="seed" value="0" min="0" max="4294967294">
</div>
<div class="style_preset-options">
<div class="form-group">
<label for="style_preset">样式预设(style Preset):</label>
<select id="style_preset">
<option value="3d-model">3d模型(3d-model)</option>
<option value="analog-film">模拟电影(analog-film)</option>
<option value="anime">动画(anime)</option>
<option value="cinematic">电影(cinematic)</option>
<option value="comic-book">漫画书(comic-book)</option>
<option value="digital-art">数字艺术(digital-art)</option>
<option value="enhance" selected>增强(enhance)</option>
<option value="fantasy-art">幻想艺术(fantasy-art)</option>
<option value="isometric">等距(isometric)</option>
<option value="line-art">线艺术(line-art)</option>
<option value="low-poly">低聚(low-poly)</option>
<option value="modeling-compound">建模复合(modeling-compound)</option>
<option value="neon-punk">立体朋克(neon-punk)</option>
<option value="origami">折纸(origami)</option>
<option value="photographic">摄影(photographic)</option>
<option value="pixel-art">像素艺术(pixel-art)</option>
<option value="tile-texture">瓷砖纹理(tile-texture)</option>
</select>
</div>
</div>
<div class="form-group">
<label for="output_format">输出格式(output Format):</label>
<select id="output_format">
<option value="jpeg" selected>JPEG</option>
<option value="png">PNG</option>
</select>
</div>
<div class="form-group">
<label for="prompt">正面描述词(prompt):</label>
<textarea id="prompt" rows="5" cols="50" required maxlength="10000"
placeholder="要控制给定单词的权重,请使用格式(word:weight),其中word是您想要控制权重的单词,weight是介于0和1之间的值。example:The sky was a crisp (blue:0.3) and (green:0.8) would convey a sky that was blue and green, but more green than blue."></textarea>
</div>
<div class="negativePrompt-form">
<div class="form-group">
<label for="negativePrompt">负面描述词(negative Prompt):</label>
<textarea id="negativePrompt" rows="5" cols="50" maxlength="10000"
placeholder="example:lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality,jpeg artifacts, signature, watermark, username, blurry, bad feet"></textarea>
</div>
</div>
<!-- 新增输入框,用于选择生成图片数量 -->
<div class="form-group">
<label for="num_images">绘图数量(Number of drawings):</label>
<input type="number" id="num_images" value="1" min="1" max="4">
</div>
<button id="generateButton" onclick="generateImages()">一键生成(generate Image)</button>
</div>
<div class="right-column">
<div class="form-group">
<label class="resultLabel" id="resultLabel">生成结果(generate Result):</label>
<div id="result"></div>
</div>
</div>
<script>
const config = {
apiKey: '',
apiUrl: {
core: 'https://api.stability.ai/v2beta/stable-image/generate/core',
sd3: 'https://api.stability.ai/v2beta/stable-image/generate/sd3'
},
balanceUrl: 'https://api.stability.ai/v1/user/balance',
pointsElem: '#account-points',
};
function getFormData(index) {
const prompt = $('#prompt').val();
const negativePrompt = $('#negativePrompt').val();
const aspectRatio = $('#aspect_ratio').val();
const mode = $('#mode').val();
const model = $('#model').val();
const stylePreset = $('#style_preset').val();
let seed = $('#seed').val();
const outputFormat = $('#output_format').val();
const formData = new FormData();
formData.append('prompt', prompt);
if (mode !== 'image-to-image') {
formData.append('aspect_ratio', aspectRatio);
}
if (model === 'sd3' || model === 'sd3-turbo') {
formData.append('mode', mode);
formData.append('model', model);
if (mode === 'image-to-image') {
const imageFile = $('#image').prop('files')[0];
const strength = $('#strength').val();
formData.append('image', imageFile);
formData.append('strength', strength);
}
}
if (model === 'sd3' || model === 'core') {
formData.append('negative_prompt', negativePrompt);
}
if (model === 'core') {
formData.append('style_preset', stylePreset);
}
if (seed !== '0') {
seed = parseInt(seed, 10) + index; // 使用循环变量index调整seed值
formData.append('seed', seed);
}
// 添加输出格式参数
formData.append('output_format', outputFormat);
return formData;
}
function saveApiKey(apiKey) {
if (apiKey.trim()) localStorage.setItem('stable_diffusion_3_api_key', apiKey);
}
function checkPoints(apiKey) {
if (apiKey.trim() === '') {
return
}
$.ajax({
url: 'https://api.stability.ai/v1/user/balance', // 替换为实际的 API URL
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
}
})
.done(function (data) {
if (data.credits < 6.5) {
alert('您的 API 密钥积分不足,请充值!');
} else {
console.log('当前积分:' + data.credits);
saveApiKey(apiKey); // 调用 saveApiKey 函数
}
const points = data.credits;
$('#account-points').text('剩余积分:' + points);
})
.fail(function () {
$('#account-points').text('Api密钥错误,无法获取积分信息');
console.error('检查余额失败,请检查您的网络连接并重新尝试。');
});
}
function toggleImageOptions() {
const mode = $('#mode').val();
const imageOptionsDiv = $('.image-options');
imageOptionsDiv.css('display', mode === 'text-to-image' ? 'none' : 'block');
// 根据模式选择修改模型选项
if (mode === 'image-to-image') {
$('#model option[value="core"]').prop('disabled', true);
if ($('#model').val() === 'core') {
$('#model').val('sd3');
}
} else {
$('#model option[value="core"]').prop('disabled', false);
}
}
function toggleStylePresetOptions() {
const model = $('#model').val();
const stylePresetOptionsDiv = $('.style_preset-options');
stylePresetOptionsDiv.css('display', model === 'core' ? 'block' : 'none');
}
function toggleModelOptions() {
const model = $('#model').val();
// 根据模型选择禁用或启用 negativePrompt
const negativePromptFormDiv = document.querySelector('.negativePrompt-form');
if (model === 'sd3-turbo') {
negativePromptFormDiv.style.display = 'none';
} else {
negativePromptFormDiv.style.display = 'block';
}
}
function handleApiKeyBlur() {
const apiKey = $(this).val();
if (apiKey) {
checkPoints(apiKey);
}
}
async function generateImages() {
const generateButton = $('#generateButton'); // 获取生成图片按钮
generateButton.prop('disabled', true); // 在请求开始前禁用按钮
const numImages = $('#num_images').val();
const promises = []; // 创建一个空数组用于存储所有的 Promise
for (let i = 0; i < numImages; i++) {
promises.push(generateImage(i)); // 将每个请求的 Promise 添加到数组
}
await Promise.all(promises); // 使用 Promise.all 等待所有请求完成
generateButton.prop('disabled', false); // 当所有请求都完成后,重新启用按钮
}
function generateImage(index) {
const formData = getFormData(index);
const model = $('#model').val();
const apiKey = $('#api_key').val();
if (apiKey === "") {
alert("请先输入API密钥");
return;
}
let apiUrl = config.apiUrl.sd3; // 默认为sd3接口
if (model === 'core') {
apiUrl = config.apiUrl.core;
}
axios({
method: 'post',
url: apiUrl,
data: formData,
headers: {
'Authorization': "Bearer " + apiKey,
'Accept': 'image/*'
},
responseType: 'blob'
})
.then(function (response) {
console.log('Image response:', response); // 打印出图片response
const imageUrl = URL.createObjectURL(response.data);
const img = document.createElement('img');
img.src = imageUrl;
img.style.width = '20%'; // 设置图片统一宽度
img.style.height = 'auto'; // 根据宽度自动调整高度
// 按比例缩放图片
const scale = 0.2; // 设置缩放比例
img.style.width = (scale * 100) + '%'; // 根据缩放比例调整宽度
img.style.height = 'auto'; // 根据宽度自动调整高度
// 添加鼠标悬停事件监听器
img.addEventListener('mouseover', function () {
img.style.width = '40%'; // 鼠标悬停时放大显示
});
// 添加鼠标移出事件监听器
img.addEventListener('mouseout', function () {
img.style.width = '20%'; // 鼠标移出时恢复原始大小
});
const resultDiv = document.getElementById('result');
resultDiv.appendChild(img);
img.onclick = function () {
const modal = document.createElement('div');
modal.style.position = 'fixed';
modal.style.top = '0';
modal.style.left = '0';
modal.style.width = '100%';
modal.style.height = '100%';
modal.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
modal.style.display = 'flex';
modal.style.justifyContent = 'center';
modal.style.alignItems = 'center';
modal.onclick = function () {
document.body.removeChild(modal);
};
const modalImg = document.createElement('img');
modalImg.src = imageUrl;
modalImg.style.maxWidth = '90%';
modalImg.style.maxHeight = '90%';
modal.appendChild(modalImg);
document.body.appendChild(modal);
};
// 每次成功生成图片后,都重新检查积分
checkPoints(apiKey);
})
.catch(function (error) {
let errorMessage = error.message;
if (error.response && error.response.data) {
errorMessage = error.response.data.message || errorMessage;
}
console.error('图片生成出错:', errorMessage);
alert('图片生成失败, ' + errorMessage);
});
}
$(document).ready(function () {
const savedApiKey = localStorage.getItem('stable_diffusion_3_api_key');
if (savedApiKey) {
$('#api_key').val(savedApiKey);
checkPoints(savedApiKey);
}
$('#api_key').on('blur', handleApiKeyBlur);
$('#mode').on('change', toggleImageOptions);
$('#model').on('change', toggleStylePresetOptions);
// 在模型选择改变时调用 toggleModelOptions 函数
$('#model').on('change', toggleModelOptions);
// 初始化时也调用一次,以应对初始模型为 'sd3-turbo' 的情况
toggleModelOptions();
});
</script>
</body>
</html>