-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (85 loc) · 3.01 KB
/
script.js
File metadata and controls
96 lines (85 loc) · 3.01 KB
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
const key = "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";
const inputText = document.getElementById("input");
const image = document.getElementById("image");
const loading = document.getElementById("loading");
const generateBtn = document.getElementById("btn");
const resetBtn = document.getElementById("reset");
const downloadBtn = document.getElementById("download");
const darkModeToggle = document.getElementById("darkModeToggle");
// Dark Mode Toggle
darkModeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
if (document.body.classList.contains("dark-mode")) {
localStorage.setItem("darkMode", "enabled");
} else {
localStorage.setItem("darkMode", "disabled");
}
});
// Load Dark Mode Preference
if (localStorage.getItem("darkMode") === "enabled") {
document.body.classList.add("dark-mode");
}
// Function to generate an image
async function generateImage() {
const text = inputText.value.trim();
if (text === "") {
alert("Please enter a valid text prompt!");
return;
}
loading.style.display = "block"; // Show loading animation
image.style.display = "none"; // Hide previous image
resetBtn.classList.add("hidden");
downloadBtn.classList.add("hidden");
try {
const response = await fetch(
"https://router.huggingface.co/hf-inference/models/ZB-Tech/Text-to-Image",
{
headers: {
Authorization: `Bearer ${key}`,
},
method: "POST",
body: JSON.stringify({ "inputs": text }),
}
);
if (!response.ok) {
throw new Error("Failed to fetch the image. Try again.");
}
const result = await response.blob();
const objectUrl = URL.createObjectURL(result);
image.src = objectUrl;
image.style.display = "block";
image.classList.add("loaded"); // Add fade-in effect
resetBtn.classList.remove("hidden");
downloadBtn.classList.remove("hidden");
} catch (error) {
alert(error.message);
} finally {
loading.style.display = "none"; // Hide loading animation
}
}
// Event Listeners
generateBtn.addEventListener("click", generateImage);
inputText.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
generateImage();
}
});
resetBtn.addEventListener("click", () => {
inputText.value = "";
image.style.display = "none";
loading.style.display = "none";
resetBtn.classList.add("hidden");
downloadBtn.classList.add("hidden");
});
downloadBtn.addEventListener("click", () => {
if (!image.src || image.style.display === "none") {
alert("No image to download!");
return;
}
const a = document.createElement("a");
a.href = image.src;
a.download = "generated-image.png";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
});