-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (70 loc) · 2.09 KB
/
script.js
File metadata and controls
81 lines (70 loc) · 2.09 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
const openAiAPI = "YOUR_API_HERE";
async function generateImage() {
const text = document.getElementById("input-text").value;
if (!text) {
alert("Please enter some text");
return;
}
try {
const response = await fetch(
"https://api.openai.com/v1/images/generations",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${openAiAPI}`,
},
body: JSON.stringify({
prompt: text,
model: "dall-e-3",
n: 1,
size: "1024x1024",
}),
}
);
const data = await response.json();
// console.log(data);
if (!data || !data.data || !data.data[0] || !data.data[0].url) {
throw new Error("No image found in the response");
}
const imageUrl = data.data[0].url;
const outputElement = document.getElementById("output");
outputElement.innerHTML = `<img src="${imageUrl}" alt="Generated Image">`;
} catch (error) {
console.error("Error generating image:", error);
alert("An error occurred while generating the image. Please try again.");
}
}
async function generateText() {
const text = document.querySelector("#input-text");
if (!text) {
alert("Please enter some text");
return;
}
try {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${openAiAPI}`,
},
body: JSON.stringify({
messages: [
{
role: "user",
content: `${text.value}`,
},
],
model: "gpt-3.5-turbo",
}),
});
const textData = await response.json();
// console.log(textData);
const textResponse = textData.choices[0].message.content;
const outputElement = document.getElementById("output");
outputElement.innerHTML = `<p>${textResponse}<p>`;
} catch (error) {
console.error("Error generating text:", error);
alert("An error occurred while generating the text. Please try again.");
}
}