-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (87 loc) · 3.19 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require('dotenv').config();
const express = require('express');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;
const OpenAI = require('openai').default;
const openai = new OpenAI();
const multer = require('multer');
const AWS = require('aws-sdk');
const bodyParser = require('body-parser');
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION
});
const s3 = new AWS.S3();
// Set up multer for handling file uploads
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
// Serve static files from the "static" directory
app.use(express.static(path.join(__dirname, 'static')));
app.use(bodyParser.json({ limit: '10mb' }));
app.listen(port, () => {
console.log(`App is running at http://localhost:${port}`);
});
app.post('/upload-image', async (req, res) => {
const { image } = req.body;
if (!image) {
return res.status(400).send('No image uploaded.');
}
const buffer = Buffer.from(image, 'base64');
console.log('Received Base64 Image:', image);
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "user",
content: [
{ type: "text", text: "Talk to a baby as a teddy bear as if this picture is what the bear sees at the moment. if the picture has some person holding something, tell the baby what that object is. Maybe spell the object so that the baby can learn. maybe tell what color the object is. if the picture has some dangerous items involved, warn the baby. keep response under 30 words. don't return any emojis. only text" },
{
type: "image_url",
image_url: {
"url": "data:image/jpeg;base64," + image
},
},
],
},
],
});
console.log(response.choices[0]);
res.json({ message: response.choices[0] });
const params = {
Bucket: process.env.S3_BUCKET_NAME,
Key: `images/${Date.now()}.jpg`, // Unique file name
Body: buffer,
ContentType: 'image/jpeg'
};
s3.upload(params, (err, data) => {
if (err) {
console.error('Error uploading to S3:', err);
}
});
});
app.get('/stream-audio', async (req, res) => {
try {
const { text } = req.query;
if (!text) {
return res.status(400).send('Text input is required');
}
// Generate speech using OpenAI
const mp3 = await openai.audio.speech.create({
model: 'tts-1',
voice: 'alloy',
input: text,
});
const buffer = Buffer.from(await mp3.arrayBuffer());
// Stream the audio back
res.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': buffer.length,
});
res.end(buffer);
} catch (error) {
console.error('Error generating speech:', error);
res.status(500).send('Error generating speech');
}
});