-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
204 lines (177 loc) · 6.03 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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Ai 图像生成器</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 20px;
margin: 0;
}
.container {
max-width: 600px;
margin: auto;
}
.input-group {
display: flex;
flex-direction: column;
align-items: center;
}
input,
select,
button,
.input-link {
width: 50%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
}
.input-link {
background-color: transparent;
color: blue;
text-decoration: none;
}
footer {
text-align: center;
margin-top: 20px;
color: gray;
}
footer ul {
list-style-type: none;
padding-left: 0;
margin-top: 5px; /* 将 PS: 和下面的文字靠拢 */
}
button,
.button-link {
margin: 5px;
padding: 10px 15px;
border: none;
color: white;
cursor: pointer;
width: 50%;
text-align: center;
display: inline-block;
text-decoration: none;
border-radius: 4px;
}
#generateButton {
background-color: black;
}
#generateButton:disabled {
background-color: gray;
cursor: not-allowed;
}
#deleteButton {
background-color: red;
}
#deleteButton:hover,
.button-link:hover {
opacity: 0.8;
}
.button-link {
background-color: #3498db;
}
#imageResult img {
max-width: 100%;
height: auto;
margin-top: 20px;
}
@media (max-width: 600px) {
.container {
width: 100%;
padding: 10px;
}
input,
select,
button,
.input-link {
width: 80%;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Ai 图像生成器</h1>
<div class="input-group">
<input type="text" id="apiKey" placeholder="在此输入您的OpenAI API密钥" required>
<a class="input-link" href="https://faka.kokoo.cc/buy/2" target="_blank">点击购买API</a>
</div>
<input type="text" id="prompt" placeholder="输入图像提示(例如:'一只白色的暹罗猫')" required>
<select id="imageSize">
<option value="1024x1024">1024x1024 像素</option>
<option value="1024x1792">1024x1792 像素</option>
<option value="1792x1024">1792x1024 像素</option>
</select>
<button id="generateButton" onclick="generateImage()">生成图像</button>
<div id="imageResult"></div>
</div>
<footer>
PS
<ul>
<li>错误 - 429:因为提交太快了。</li>
<li>错误 - 401:API 错误。</li>
<li>错误 - 400:额度用完了。</li>
<li>每生成一张图片约0.04刀</li>
<li>本程序使用的是DALL·E 3</li>
<li>本程序不会收集API</li>
</ul>
</footer>
<script>
async function generateImage() {
const generateButton = document.getElementById("generateButton");
generateButton.disabled = true;
const imageContainer = document.getElementById("imageResult");
imageContainer.innerHTML = '<p style="color: gray;">图像生成中,请稍等...</p>';
const apiKey = document.getElementById("apiKey").value;
const prompt = document.getElementById("prompt").value;
const imageSize = document.getElementById("imageSize").value;
if (!apiKey || !prompt) {
alert("请填写API密钥和图像提示。");
generateButton.disabled = false;
imageContainer.innerHTML = '';
return;
}
const api_url = 'https://api.openai.com/v1/images/generations';
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
};
const data = {
"model": "dall-e-3",
"prompt": prompt,
"n": 1,
"size": imageSize
};
try {
const response = await fetch(api_url, {
method: 'POST',
headers: headers,
body: JSON.stringify(data)
});
if (response.ok) {
const result = await response.json();
imageContainer.innerHTML = '<p style="color: green;">图像生成完毕</p>';
imageContainer.innerHTML += `<img src="${result.data[0].url}" alt="生成的图像">`;
imageContainer.innerHTML += `
<button id="deleteButton">清除图像</button>
<a href="${result.data[0].url}" target="_blank" class="button-link">下载</a>
`;
} else {
console.error("错误:", response.status, response.statusText);
imageContainer.innerHTML = `<p style="color: red;">图像生成失败 - ${response.status} ${response.statusText}</p>`;
}
} catch (error) {
console.error("请求失败:", error);
imageContainer.innerHTML = '<p style="color: red;">发生错误,请检查网络连接或稍后重试。</p>';
} finally {
generateButton.disabled = false;
}
}
</script>
</body>
</html>