-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist-image-generator.js
45 lines (40 loc) · 1.67 KB
/
playlist-image-generator.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
function generateArt(prompt) {
let DALLE_API_KEY;
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
const config = JSON.parse(xhr.responseText);
DALLE_API_KEY = config.DALLE_KEY;
const apiUrl = 'https://api.openai.com/v1/images/generations';
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${DALLE_API_KEY}`
},
body: JSON.stringify({
"model": "image-alpha-001",
"prompt": prompt,
"num_images": 1,
"size": "1024x1024",
"response_format": "url"
})
})
.then(response => response.json())
.then(data => {
const imageSource = data.data[0].url;
console.log(data);
console.log(imageSource); // log image source to console
document.getElementById("playlist-art").src = imageSource;
return imageSource;
})
.catch(error => console.error(error));
} else {
console.error('Error loading configuration file:', xhr.statusText);
}
}
};
xhr.open('GET', 'config.json', true);
xhr.send();
}