-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (76 loc) · 2.65 KB
/
Copy pathserver.js
File metadata and controls
92 lines (76 loc) · 2.65 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
82
83
84
85
86
87
88
89
90
91
92
const express = require('express');
const fs = require('fs');
const OpenAI = require('openai');
const app = express();
const port = 3000;
app.use(express.static('public'));
app.use(express.json({ limit: '50mb' }));
app.set('view engine', 'ejs');
app.use(express.static('public'));
const openai = new OpenAI({
apiKey: 'Api-Key',
});
app.get('/', (req, res) => {
res.render('homepage');
});
app.get('/index', (req, res) => {
res.render('homepage');
});
app.get('/signin', (req, res) => {
res.render('signin');
});
app.get('/flux', (req, res) => {
res.render('flux');
});
app.get('/analytics', (req, res) => {
res.render('analytics');
});
app.get('/insights', (req, res) => {
res.render('insights');
});
app.get('/landing', (req, res) => {
res.render('landing');
});
app.get('/logout', (req, res) => {
res.render('logout');
});
app.post('/calculate-carbon', async (req, res) => {
try {
const base64Image = req.body.image;
const completion = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{
role: 'system',
content: [{
type: 'text',
text: `You are a helpful assistant that can help users calculate their carbon footprint based on their daily activities and lifestyle choices. You will provide personalized insights on how they can reduce their carbon emissions.
NO EXTRA TEXT, JUST THE INSIGHT
NO unnessasary symbols backticks or quotes or any markdown symbols`,
}],
role: 'user',
content: [
{
type: 'text',
text: '274 Units consumed in the last month calculate the carbon footprint i live in India i us coal as my energy source and give 2 insights on how to reduce my carbon footprint',
},
{
type: 'image_url',
image_url: {
url: `data:image/jpeg;base64,${base64Image}`,
},
},
],
}],
temperature: 0.3,
max_tokens: 200,
});
const response = completion.choices[0].message.content;
res.json({ result: response });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'An error occurred' });
}
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});