DucLai commited on
Commit
88c0f6a
·
verified ·
1 Parent(s): f96e00b

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +35 -48
script.js CHANGED
@@ -213,55 +213,42 @@ MÔ TẢ: ["A cute [object] in line-art style, with soft pastel colors, minimali
213
  }
214
 
215
  async generateImageFromDescription(description) {
216
- const prompt = description.trim();
217
- const negative_prompt = ""; // Có thể truyền vào nếu cần filter
218
- const seed = Math.floor(Math.random() * 1e9); // seed 32-bit
219
- const url = "https://api.stability.ai/v2beta/stable-image/generate/sd3";
220
- const STABILITY_API_KEY = 'sk-JzOAcde6bqHRjfInzdrNmNjjrCs0LFAvB838xi14OhhEQ06T ';
221
-
222
- const res = await fetch(url, {
223
- method: "POST",
224
- headers: {
225
- "Content-Type": "application/json",
226
- Accept: "application/json",
227
- Authorization: `Bearer ${STABILITY_API_KEY}`,
228
- },
229
- body: JSON.stringify({
230
- prompt,
231
- negative_prompt,
232
- seed,
233
- cfg_scale: 7,
234
- steps: 30,
235
- width: 1024,
236
- height: 1024,
237
- samples: 1,
238
- model: "sd3.5-medium",
239
- }),
240
- // Thêm timeout nếu cần: fetch-with-timeout wrapper
241
- });
242
-
243
- if (!res.ok) {
244
- const { status, statusText } = res;
245
- const errBody = await res.text();
246
- throw new Error(`HTTP ${status} ${statusText}: ${errBody}`);
247
- }
248
-
249
- const data = await res.json();
250
- if (!data.artifacts || !data.artifacts.length) {
251
- throw new Error("No artifacts returned from API.");
252
- }
253
- const artifact = data.artifacts[0];
254
- if (artifact.finish_reason === "CONTENT_FILTERED") {
255
- throw new Error("NSFW content was filtered out by the model.");
256
- }
257
-
258
- // format từ response có thể là base64 hoặc mảng bytes
259
- const base64 = artifact.base64 ?? artifact.base64_string;
260
- if (!base64) {
261
- throw new Error("No base64 image data found in artifact.");
262
- }
263
- return `data:image/png;base64,${base64}`;
264
  }
 
 
 
 
 
 
 
 
265
 
266
 
267
  // async generateImageFromDescription(description) {
 
213
  }
214
 
215
  async generateImageFromDescription(description) {
216
+ const prompt = description.trim();
217
+ const seed = Math.floor(Math.random() * 1e9);
218
+ const url = "https://api.stability.ai/v2beta/stable-image/generate/sd3";
219
+
220
+ const form = new FormData();
221
+ form.append("prompt", prompt);
222
+ form.append("model", "sd3.5-medium");
223
+ form.append("seed", seed.toString());
224
+ form.append("cfg_scale", "7");
225
+ form.append("steps", "30");
226
+ form.append("width", "1024");
227
+ form.append("height", "1024");
228
+ form.append("samples", "1");
229
+ form.append("output_format", "png");
230
+
231
+ const res = await fetch(url, {
232
+ method: "POST",
233
+ headers: {
234
+ "Authorization": `Bearer ${STABILITY_API_KEY}`,
235
+ "Accept": "application/json" // để nhận hình dạng base64 JSON
236
+ },
237
+ body: form // multipart/form-data tự xử với boundary
238
+ });
239
+
240
+ if (!res.ok) {
241
+ const err = await res.json().catch(() => ({ message: await res.text() }));
242
+ throw new Error(`HTTP ${res.status}: ${JSON.stringify(err)}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  }
244
+
245
+ const data = await res.json();
246
+ if (!data.artifacts?.[0]?.base64) {
247
+ throw new Error("Không nhận được image từ API.");
248
+ }
249
+ return `data:image/png;base64,${data.artifacts[0].base64}`;
250
+ }
251
+
252
 
253
 
254
  // async generateImageFromDescription(description) {