Spaces:
Running
Running
Create script.js
Browse files
script.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const token = huggingface.variables.tokenID
|
| 2 |
+
const imageBox_el = document.getElementById("imageBox");
|
| 3 |
+
const promptText_el = document.getElementById("promptText");
|
| 4 |
+
const btn_gen_el = document.getElementById("btn-gen");
|
| 5 |
+
const btn_clear_el = document.getElementById("btn-clear");
|
| 6 |
+
const loadingMessage_el = document.getElementById("loadingMessage");
|
| 7 |
+
const btn_download_el = document.getElementById("btn-download");
|
| 8 |
+
const download_el = document .getElementById("download");
|
| 9 |
+
|
| 10 |
+
let promptText = "";
|
| 11 |
+
|
| 12 |
+
const modelLinks = ["https://api-inference.huggingface.co/models/Shakker-Labs/FLUX.1-dev-LoRA-add-details",
|
| 13 |
+
"https://api-inference.huggingface.co/models/glif/90s-anime-art",
|
| 14 |
+
"https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev",
|
| 15 |
+
"https://api-inference.huggingface.co/models/Jovie/Midjourney"]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
async function query(data) {
|
| 19 |
+
const response = await fetch(
|
| 20 |
+
|
| 21 |
+
modelLinks[3],
|
| 22 |
+
// "https://api-inference.huggingface.co/models/glif/90s-anime-art",
|
| 23 |
+
{
|
| 24 |
+
headers: {
|
| 25 |
+
Authorization: `Bearer ${token}`,
|
| 26 |
+
"Content-Type": "application/json",
|
| 27 |
+
},
|
| 28 |
+
method: "POST",
|
| 29 |
+
body: JSON.stringify(data),
|
| 30 |
+
}
|
| 31 |
+
);
|
| 32 |
+
const result = await response.blob();
|
| 33 |
+
return result;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
btn_clear_el.addEventListener("click", () => {
|
| 37 |
+
promptText_el.value = "";
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
btn_gen_el.addEventListener("click", (event) => {
|
| 41 |
+
promptText = promptText_el.value;
|
| 42 |
+
|
| 43 |
+
if (promptText === "") {
|
| 44 |
+
alert("Please Enter Description of the image you want to generate");
|
| 45 |
+
} else {
|
| 46 |
+
console.log(promptText);
|
| 47 |
+
|
| 48 |
+
// Show the loading message and hide the image
|
| 49 |
+
loadingMessage_el.style.display = "block";
|
| 50 |
+
imageBox_el.style.display = "none";
|
| 51 |
+
btn_download_el.style.display = "none";
|
| 52 |
+
|
| 53 |
+
query({ inputs: promptText }).then((response) => {
|
| 54 |
+
const imageUrl = URL.createObjectURL(response);
|
| 55 |
+
imageBox_el.src = imageUrl;
|
| 56 |
+
|
| 57 |
+
// Hide the loading message and show the image once it's ready
|
| 58 |
+
loadingMessage_el.style.display = "none";
|
| 59 |
+
imageBox_el.style.display = "block";
|
| 60 |
+
// Update the download button
|
| 61 |
+
download_el.style.display = "block";
|
| 62 |
+
btn_download_el.style.display = "block";
|
| 63 |
+
btn_download_el.href = imageUrl;
|
| 64 |
+
btn_download_el.download = "generated_image.png";
|
| 65 |
+
}).catch(error => {
|
| 66 |
+
loadingMessage_el.style.display = "none";
|
| 67 |
+
alert("Failed to fetch the image. Please try again.");
|
| 68 |
+
console.error("Error fetching image:", error);
|
| 69 |
+
});
|
| 70 |
+
}
|
| 71 |
+
});
|
| 72 |
+
console.log(btn_download_el)
|
| 73 |
+
btn_download_el.addEventListener("click", (event) => {
|
| 74 |
+
// Optional: Log or perform any action before the download starts
|
| 75 |
+
console.log("Download initiated for the generated image.");
|
| 76 |
+
|
| 77 |
+
// You can also perform checks or modify the behavior here if needed.
|
| 78 |
+
if (!btn_download_el.href) {
|
| 79 |
+
event.preventDefault();
|
| 80 |
+
alert("No image available for download.");
|
| 81 |
+
}
|
| 82 |
+
});
|