-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (59 loc) · 1.61 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import express from "express";
import bodyParser from "body-parser";
import axios from 'axios'
import 'dotenv/config'
import multer from "multer";
const app = express();
const port = 3000;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
const upload = multer({ storage: multer.memoryStorage() });
app.get("/", (req, res) => {
res.render("index.ejs")
});
const config = {}
app.post("/submit", upload.single("image"), async (req, res) => {
try {
// req.file contains the uploaded file
if (!req.file) {
return res.status(400).send("No file uploaded.");
}
// Example: Converting the file buffer to a Base64 string (if needed)
const imageBase64 = req.file.buffer.toString('base64');
// console.log(req.file.originalname, imageBase64);
// Prepare data for OpenAI API
const dataUrl = `data:image/png;base64,${imageBase64}`;
const response = await axios.post("https://api.openai.com/v1/chat/completions", {
model: "gpt-4o-mini",
messages: [
{
role: "user",
content: [
{ type: "text", text: "What's in this image"},
{
type: "image_url",
image_url: {
"url": dataUrl,
},
},
],
},
],
store: true
},
{
headers: {
'Authorization': process.env.API_KEY,
'Content-Type': 'application/json'
}
}
);
console.log(response.data.choices[0].message.content);
} catch (error) {
console.error("Error:", error.response ? error.response.data : error.message);
res.status(500).send("Internal Server Error");
}
});
app.listen(port, () => {
console.log("Server listening on port");
})