-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (33 loc) · 1.28 KB
/
app.js
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
const API_KEY = "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"
const submitIcon = document.querySelector("#submit-icon")
const inputElement = document.querySelector("input")
const imageSection = document.querySelector('.images-section')
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.array.forEach(imageObject => {
const imageContainer = document.createElement("div")
imageContainer.classList.add('image-contaoiner')
const imageElement = document.createElement('img')
imageElement.setAttribute('src', imageObject.url)
imageContainer.append(imageElement)
imageSection.append(imageContainer)
})
}catch(error){
console.error(error)
}
}
submitIcon.addEventListener('click', getImages)