-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (72 loc) · 2.85 KB
/
script.js
File metadata and controls
86 lines (72 loc) · 2.85 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
// Variables
const promptBar = document.querySelector("#prompt_bar");
const imageresult = document.querySelector("#image_result");
const downloadBtn = document.querySelector(".download-btn");
// Replace 'your-api-key' with your actual OpenAI API key
const openaiAPIKey = "your-api-key";
let isImgGen = false;
// Function to update image boxes with generated images
const updateImgBoxes = (imgBoxArray) => {
imgBoxArray.forEach((imgObject, index) => {
const imgBox = imageresult.querySelectorAll(".img_box")[index];
const imgElement = imgBox.querySelector("img");
const downloadBtn = imgBox.querySelector(".download-btn");
const aiImgGenerate = `data:image/jpeg;base64,${imgObject.b64_json}`;
imgElement.src = aiImgGenerate;
imgElement.onload = () => {
imgBox.classList.remove("loading");
downloadBtn.setAttribute("href", aiImgGenerate);
downloadBtn.setAttribute("download", `Cosas_Learning_${new Date().getTime()}.jpg`);
}
})
}
// Function to generate AI images
const generateAIImages = async (userPrompt, imgQuntity, imgSize) => {
try {
const response = await fetch('https://api.openai.com/v1/images/generations', {
method : "POST",
headers : {
"Content-Type": "application/json",
"Authorization": `Bearer ${openaiAPIKey}`
},
body:JSON.stringify({
prompt : userPrompt,
n: parseInt(imgQuntity),
size : imgSize,
response_format :"b64_json"
})
});
if(!response.ok) throw new Error("Failed to generate AI Images! Please try again.")
const { data } = await response.json();
console.log(data);
updateImgBoxes([...data]);
} catch (error) {
alert(error.message);
imageresult.style.display = "none";
} finally {
isImgGen = false;
}
}
// Function to handle the form submission
const handlePrompt = (e) => {
e.preventDefault();
if(isImgGen) return;
isImgGen = true;
imageresult.style.display = "flex";
const userPrompt = e.srcElement[0].value;
const imgQuntity = e.srcElement[1].value;
const imgSize = e.srcElement[2].value;
// Create loading placeholders for images
const imgBoxes = Array.from({length: imgQuntity}, () =>
`<div class="img_box loading">
<img src="images/loader.gif">
<a href="#" class="download-btn">
<i class="fa-solid fa-download"></i>
</a>
</div>`
).join("");
imageresult.innerHTML = imgBoxes;
generateAIImages(userPrompt, imgQuntity, imgSize);
}
// Add event listener for form submission
promptBar.addEventListener("submit", handlePrompt);