Spaces:
Running
Running
Update script.js
Browse files
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 |
-
|
| 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 |
-
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) {
|