-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
232 lines (188 loc) · 6.96 KB
/
server.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const morgan = require('morgan');
const { Connection, Request, TYPES } = require('tedious');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
const { GoogleGenerativeAI } = require("@google/generative-ai");
const fs = require("fs");
const API_KEY = 'AIzaSyCtn7lNbzVQusWCB3WCxz66FkQkCxA8Shs'
// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(API_KEY);
// Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(morgan('dev'));
app.use(cors({
origin: process.env.REACT_APP_API_URL,
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
}));
// Test route
app.get('/*', (req, res) => {
res.send('Express on Vercel!');
});
// MS SQL Server configuration
var config = require('tedious').Connection;
var dbConfig = {
server: 'basedatospoliticalpower.database.windows.net',
authentication: {
type: 'default',
options: {
userName: 'administrador',
password: 'Te$91827364'
}
},
options: {
// Microsoft Azure so we need encryption:
//encrypt: true,
database: 'PoliticalPowerDataBase'
}
};
var connection = new Connection(dbConfig);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
// executeStatement();
});
connection.connect();
// Login route
app.post('/api/login', (req, res) => {
const { email, password } = req.body;
const connection = new Connection(dbConfig);
connection.on('connect', (err) => {
if (err) {
console.error('Database connection error:', err);
return res.status(500).json({ message: 'Internal server error' });
}
const query = 'SELECT * FROM Usuarios WHERE correo = @correo AND contrasenia = @contrasenia';
const request = new Request(query, (err, rowCount, rows) => {
if (err) {
console.error('SQL error:', err);
return res.status(500).json({ message: 'Internal server error' });
}
if (rowCount > 0) {
res.json({ message: 'Login successful' });
} else {
res.status(401).json({ message: 'Invalid email or password' });
}
connection.close();
});
request.addParameter('correo', TYPES.VarChar, email);
request.addParameter('contrasenia', TYPES.VarChar, password);
connection.execSql(request);
});
connection.connect();
});
// Register route
app.post('/api/register', (req, res) => {
const { name, email, password } = req.body;
const connection = new Connection(dbConfig);
connection.on('connect', (err) => {
if (err) {
console.error('Database connection error:', err);
return res.status(500).json({ message: 'Internal server error' });
}
const query = 'INSERT INTO Usuarios (nombre, contrasenia, correo) VALUES ( @nombre, @contrasenia, @correo) ';
const request = new Request(query, (err) => {
if (err) {
console.error('SQL error:', err);
return res.status(500).json({ message: 'Internal server error' });
}
res.json({ message: 'Registration successful' });
connection.close();
});
request.addParameter('nombre', TYPES.VarChar, name);
request.addParameter('correo', TYPES.VarChar, email);
request.addParameter('contrasenia', TYPES.VarChar, password);
connection.execSql(request);
});
connection.connect();
});
// Converts local file information to a GoogleGenerativeAI.Part object.
function fileToGenerativePart(path, mimeType) {
return {
inlineData: {
data: Buffer.from(fs.readFileSync(path)).toString("base64"),
mimeType
},
};
}
async function run() {
// The Gemini 1.5 models are versatile and work with both text-only and multimodal prompts
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const prompt = "Do you know who are these mexican politicians are? What's different between these pictures?";
const imageParts = [
fileToGenerativePart('./src/images/candidata-fuerzycorazonxmexico-xochitl-presidencial.jpg', "image/jpg"),
fileToGenerativePart('./src/images/candidata-morena-sheinbaum-presidencial.jpg', "image/jpg"),
fileToGenerativePart('./src/images/candidato-mc-maynez-presidencial.jpg', "image/jpg")
];
const result = await model.generateContent([prompt, ...imageParts]);
const response = await result.response;
const texto = response.text();
console.log(texto);
}
run();
// Endpoint to interpret multiple images and return the result
app.post('/api/interpret-images', async (req, res) => {
try {
const { imagePaths, prompt } = req.body;
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const imageParts = imagePaths.map(imagePath => {
const imageBuffer = fs.readFileSync(imagePath.replace('src/', ''));
const imageBase64 = Buffer.from(imageBuffer).toString('base64');
return {
inlineData: {
data: imageBase64,
mimeType: 'image/jpg', // Adjust according to your image type
},
};
});
const result = await model.generateContent([prompt, ...imageParts]);
const text = await result.response.text();
res.json({ text });
} catch (error) {
console.error('Error interpreting images:', error);
res.status(500).json({ message: 'Error interpreting images', error: error.message });
}
});
// Endpoint to fetch data from MySQL database
app.get('/api/data', (req, res) => {
const connection = mysql.createConnection(dbConfig);
connection.connect((err) => {
if (err) {
console.error('Database connection error:', err);
return res.status(500).json({ message: 'Internal server error' });
}
const sql = `
SELECT CP.ApellidoCandidato, COUNT(*) AS TotalVotos
FROM EleccionCandidatos EC
JOIN CandidatosPresidenciales CP ON EC.idCandidato = CP.idCandidato
GROUP BY CP.ApellidoCandidato
ORDER BY TotalVotos DESC
`;
connection.query(sql, (err, results) => {
if (err) {
console.error('SQL error:', err);
return res.status(500).json({ message: 'Internal server error' });
}
res.json(results);
});
connection.end();
});
});
//const sql = 'SELECT CP.ApellidoCndidato, COUNT(*) AS TotalVotos FROM EleccionCandidtos EC JOIN CandidatosPresidenciales CP ON EC.idCandidato = CP.idCandidato';
// db.query(sql, (err, result) => {
// if (err) {
// res.status(500).json({ error: 'Error fetching data from database' });
// return;
// }
// res.json(result);
// });
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
module.exports = app;