-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (50 loc) · 1.49 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
const express = require('express');
const request = require('request');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/generate_image', (req, res) => {
const description = req.body.description;
const apiKey = process.env.OPENAI_API_KEY;
const options = {
url: 'https://api.openai.com/v1/images/generations',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
prompt: description,
model: 'image-alpha-001'
})
};
request.post(options, (err, resp, body) => {
if (err) {
return res.status(500).send(err);
}
const response = JSON.parse(body);
if (!response.data) {
console.error('Error: No data returned from OpenAI API');
} else if (!response.data[0]) {
console.error('Error: No image returned from OpenAI API');
} else {
res.send(`
<html>
<body>
<form action="/generate_image" method="post">
<input type="text" name="description" placeholder="Enter a description" />
<button type="submit">Generate Image</button>
</form>
<br />
<img src="${response.data[0].url}" />
</body>
</html>
`);
}
});
});
app.listen(3000, () => {
console.log('Image generation service listening on port 3000!');
});