// JavaScript code
function generateImage() {
const apiKey = "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";
const textInput = document.getElementById("text-input").value;
const url = "https://api.openai.com/v1/images/generations";
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
},
body: JSON.stringify({
"model": "image-alpha-001",
"prompt": textInput,
"num_images": 1
})
};
fetch(url, requestOptions)
.then(response => response.json())
.then(data => {
const imageUrl = data.data[0].url;
const image = document.getElementById("generated-image");
image.src = imageUrl;
})
.catch(error => console.log(error));
}