-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (48 loc) · 1.61 KB
/
Copy pathapp.js
File metadata and controls
59 lines (48 loc) · 1.61 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
const submitIcon = document.querySelector("#submit-icon")
const inputElement = document.querySelector("input")
const imageSection = document.querySelector('.image-section')
var API_key = ""
// Process the keys from the json file
async function fetchData() {
try {
const response = await fetch('keys.json');
const key_data = await response.json();
// Process the data from the JSON file
console.log(key_data.keys);
API_Key = key_data.keys;
} catch (error) {
// Handle any errors that occur during the fetch
console.error('Error:', error);
}
}
fetchData();
const getImages = async () => {
const options = {
method: "POST",
headers: {
"Authorization": `Bearer ${API_Key}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
prompt: inputElement.value,
n: 4,
size: "1024x1024"
})
}
try {
const response = await fetch('https://api.openai.com/v1/images/generations', options)
const data = await response.json()
console.log(data)
data?.data.foreach(ImageObject => {
const imageContainer = document.createElement('div')
imageContainer.classList.add('image-container')
const imageElement = document.createElement('img')
imageElement.setAttribute('src',ImageObject.url)
imageContainer.append(imageElement)
imageSection.append(ImageContainer)
})
} catch (error){
console.log(error)
}
}
submitIcon.addEventListener('click', getImages)