-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (76 loc) · 3.66 KB
/
script.js
File metadata and controls
90 lines (76 loc) · 3.66 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
document.addEventListener("DOMContentLoaded", () => {
const generateForm = document.querySelector(".generate-form");
const generateBtn = generateForm.querySelector(".generate-btn");
const imageGallery = document.querySelector(".image-gallery");
const HUGGING_FACE_API_KEY = "HUGGING_FACE_API_KEY"; // Your Hugging Face API key here
let isImageGenerating = false;
const updateImageCard = (imgDataArray) => {
imgDataArray.forEach((imgObject) => {
const imgCard = document.createElement("div");
imgCard.classList.add("img-card", "loading");
const imgElement = document.createElement("img");
imgCard.appendChild(imgElement);
const downloadBtn = document.createElement("a");
downloadBtn.classList.add("download-btn");
downloadBtn.innerHTML = '<img src="images/download.svg" alt="download">';
imgCard.appendChild(downloadBtn);
// Set the image source to the AI-generated image data
const aiGeneratedImage = `data:image/png;base64,${imgObject.image}`;
imgElement.src = aiGeneratedImage;
// When the image is loaded, remove the loading class and set download attributes
imgElement.onload = () => {
imgCard.classList.remove("loading");
downloadBtn.setAttribute("href", aiGeneratedImage);
downloadBtn.setAttribute("download", `${new Date().getTime()}.png`);
};
imageGallery.appendChild(imgCard);
});
};
const generateAiImages = async (userPrompt, userImgQuantity) => {
try {
generateBtn.setAttribute("disabled", true);
generateBtn.innerText = "Generating...";
isImageGenerating = true;
// Clear existing images
imageGallery.innerHTML = "";
// Send a request to the Hugging Face API to generate images based on user inputs
const response = await fetch("https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${HUGGING_FACE_API_KEY}`,
},
body: JSON.stringify({
inputs: userPrompt,
options: {
wait_for_model: true,
n: parseInt(userImgQuantity),
},
}),
});
// Throw an error message if the API response is unsuccessful
if (!response.ok) throw new Error("Failed to generate AI images. Make sure your API key is correct and you have enough quota.");
const responseData = await response.json();
// Ensure the responseData is in the expected format
if (responseData && Array.isArray(responseData)) {
updateImageCard(responseData);
} else {
throw new Error("Unexpected response format.");
}
} catch (error) {
alert(error.message);
} finally {
generateBtn.removeAttribute("disabled");
generateBtn.innerText = "Generate";
isImageGenerating = false;
}
};
generateForm.addEventListener("submit", (event) => {
event.preventDefault();
const userPrompt = generateForm.querySelector(".prompt-input").value.trim();
const userImgQuantity = generateForm.querySelector(".img-quantity").value;
if (userPrompt && !isImageGenerating) {
generateAiImages(userPrompt, userImgQuantity);
}
});
});