-
Notifications
You must be signed in to change notification settings - Fork 5
/
step3.html
74 lines (67 loc) · 2.49 KB
/
step3.html
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
<!DOCTYPE html>
<html>
<head>
<title>OpenAI Web Project</title>
</head>
<body>
<h1>Using OpenAI </h1>
<p>OpenAI API Key:</p>
<input type="text" id="apiKey">
<br><br>
<p>Keyword:</p>
<input type="text" id="keyword">
<br><br>
<button onclick="generateTitle()">Create Title</button>
<br><br>
<p id="result"></p>
<img id="pic1" src="" width="256px" height="256px"/>
<script>
let API_KEY = "";
async function generateTitle() {
API_KEY = document.getElementById("apiKey").value;
const keyword = document.getElementById("keyword").value;
const prompt = `Generate an article title related to "${keyword}`;
const model = "gpt-3.5-turbo";
try {
const response = await fetch(`https://api.openai.com/v1/chat/completions`,
{
body: JSON.stringify({"model": model, "messages": [{"role": "user", "content": prompt}], "max_tokens": 20}),
method: "POST",
headers: {
"content-type": "application/json",
Authorization: `Bearer ${API_KEY}`,
},
}
)
let data = await response.json();
console.log(`Title: ${JSON.stringify(data)}`);
const title = data.choices[0].message.content.trim();
document.getElementById("result").innerHTML = title;
await generateImage()
} catch(error) {
console.log(`Error: ${error}`);
}
}
async function generateImage(){
try {
const prompt = document.getElementById('result').textContent;
const response = await fetch(`https://api.openai.com/v1/images/generations`,
{
body: JSON.stringify({"prompt":prompt, n:1, size:"256x256"}),
method: "POST",
headers: {
"content-type": "application/json",
Authorization: `Bearer ${API_KEY}`,
},
}
)
let data = await response.json();
console.log(`Image: ${JSON.stringify(data)}`);
document.getElementById("pic1").src= data.data[0].url;
} catch(error) {
console.log(`Error: ${error}`);
}
}
</script>
</body>
</html>