-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathserver.mjs
305 lines (251 loc) · 10.2 KB
/
server.mjs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import express from 'express';
import multer from 'multer';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { Client } from '@gradio/client';
import bodyParser from 'body-parser';
import nodemailer from 'nodemailer';
// Define __dirname for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Create uploads directory if it does not exist
const uploadsDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir);
}
// Initialize the Gradio client
const client = await Client.connect("Vinit710/GMED"); // Replace with your actual Gradio app
const skinClient = await Client.connect("Vinit710/Skin_Disease");
const chatClient = await Client.connect("Vinit710/Chatbot");
const symtodieClient = await Client.connect('Vinit710/symtodise');
// Connect to the Hugging Face model via Gradio Client
// const xrayClient = await Client.connect("darksoule26/fracture");
const app = express();
const port = 3001;
app.use(bodyParser.json());
app.use(express.static('public'));
// Set up multer for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadsDir); // Save files to the uploads directory
},
filename: (req, file, cb) => {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage: storage });
// Serve static files
app.use('/static', express.static(path.join(__dirname, 'static')))
// Serve the index.html file
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'index.html'));
});
app.get('/about', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'about.html'));
});
app.get('/chatbot', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'chatbot.html'));
});
app.get('/contact', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'contact.html'));
});
app.get('/ocular', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'ocular.html'));
});
app.get('/skin_disease', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'skin_disease.html'));
});
app.get('/docs', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'docs.html'));
});
app.get('/symtodie', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'symtodie.html'));
});
app.get('/booking', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'booking.html'));
});
app.get('/xray', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'xray.html'));
});
// Handle image uploads and predictions for ocular
app.post('/predict', upload.fields([{ name: 'left_image' }, { name: 'right_image' }]), async (req, res) => {
try {
const leftImagePath = req.files['left_image'][0].path;
const rightImagePath = req.files['right_image'][0].path;
// Log paths to ensure they are correct
console.log(`Left image path: ${leftImagePath}`);
console.log(`Right image path: ${rightImagePath}`);
// Read the input images as buffers
const leftEyeBuffer = fs.readFileSync(leftImagePath);
const rightEyeBuffer = fs.readFileSync(rightImagePath);
// Send both left and right eye images for prediction
console.log('Making prediction...');
const result = await client.predict("/predict", {
left_image: new Blob([leftEyeBuffer]), // Wrap buffer in Blob
right_image: new Blob([rightEyeBuffer]), // Wrap buffer in Blob
});
// Log the prediction result
console.log('Prediction result:', result);
// Extract the label from the result data
if (result && result.data && result.data[0] && result.data[0].label) {
console.log(`Predicted label: ${result.data[0].label}`);
res.json(result); // Return the result as a JSON response
} else {
console.error('No valid prediction label received.');
res.status(500).json({ error: 'No valid prediction label received.' });
}
// Clean up uploaded files
fs.unlinkSync(leftImagePath);
fs.unlinkSync(rightImagePath);
} catch (error) {
// Catch and log any errors that occur during the process
console.error('Error:', error.message);
console.error('Stack Trace:', error.stack);
res.status(500).json({ error: error.message });
}
});
// Handle skin disease image upload and prediction
app.post('/predict_skin', upload.single('input_image'), async (req, res) => {
try {
// Get the image path
const imagePath = req.file.path;
console.log(`Image path: ${imagePath}`);
// Read the uploaded image as a buffer
const imageBuffer = fs.readFileSync(imagePath);
// Convert the buffer into a Blob object to send to the prediction API
const imageBlob = new Blob([imageBuffer], { type: 'image/png' }); // Change MIME type if necessary
console.log('Making prediction...');
// Send the image for prediction using the skinClient
const result = await skinClient.predict("/predict", {
img: imageBlob // Correctly send the image as 'img' parameter
});
// Log the prediction result
console.log('Prediction result:', result);
// Check if the result contains the expected label
if (result && result.data && result.data[0]) {
const predictedLabel = result.data[0];
console.log(`Predicted label: ${predictedLabel}`);
// Return the prediction result as JSON
res.status(200).json({ data: [predictedLabel] });
} else {
console.error('No valid prediction label received.');
res.status(500).json({ error: 'No valid prediction label received.' });
}
} catch (error) {
// Log the error for debugging
console.error('Error:', error.message);
console.error('Stack Trace:', error.stack);
// Return an error response
res.status(500).json({ error: 'Prediction failed. ' + error.message });
} finally {
// Clean up the uploaded image file
if (fs.existsSync(req.file.path)) {
fs.unlinkSync(req.file.path); // Ensure the file is deleted even in case of errors
}
}
});
app.post('/chatbot', async (req, res) => {
const userMessage = req.body.message;
try {
// Call the API to get the chatbot's response
const result = await chatClient.predict('/chat', {
message: userMessage,
system_message: "You are a friendly chatbot.", // Custom system message
max_tokens: 512, // Define the max tokens for the response
temperature: 0.7, // Control the randomness of the response
top_p: 0.95 // Control diversity via nucleus sampling
});
// Return the chatbot response to the front-end
res.json({ reply: result.data });
} catch (error) {
console.error('Error communicating with the API:', error);
res.status(500).json({ reply: "Sorry, something went wrong with the chatbot." });
}
});
// Symptom to Disease Prediction API endpoint
app.post('/predict_symtodie', async (req, res) => {
try {
const {
age, gender, fever, cough, fatigue,
difficulty_breathing, blood_pressure, cholesterol_level
} = req.body;
// Make the prediction using the symptom-to-disease Gradio client
const result = await symtodieClient.predict('/predict', {
age: age,
gender: gender,
fever: fever,
cough: cough,
fatigue: fatigue,
difficulty_breathing: difficulty_breathing,
blood_pressure: blood_pressure,
cholesterol_level: cholesterol_level
});
// Return the predicted result
res.json({ prediction: result.data });
} catch (error) {
console.error('Error during symptom-to-disease prediction:', error.message);
res.status(500).json({ error: 'Prediction failed.' });
}
});
// // Handle X-ray image upload and prediction
// app.post('/predict_xray', upload.single('input_image'), async (req, res) => {
// try {
// const imagePath = req.file.path;
// console.log(`Uploaded X-ray image path: ${imagePath}`);
// // Read the image as a Blob
// const imageBuffer = fs.readFileSync(imagePath);
// const imageBlob = new Blob([imageBuffer], { type: 'image/jpeg' }); // Set correct MIME type
// // Make the prediction call to the Hugging Face API
// const result = await xrayClient.predict("/predict", {
// img: imageBlob // Use the Blob format as required by the API
// });
// console.log('Prediction result:', result);
// // Extract the prediction text from the API response
// const prediction = result.data[0];
// // Return the prediction result as JSON
// res.status(200).json({ prediction });
// } catch (error) {
// console.error('Error during prediction:', error.message);
// res.status(500).json({ error: 'Prediction failed. ' + error.message });
// } finally {
// // Clean up the uploaded image file
// if (fs.existsSync(req.file.path)) {
// fs.unlinkSync(req.file.path);
// }
// }
// });
// Endpoint to handle email sending
app.post('/sendEmail', async (req, res) => {
const { name, phone, email, hospital, date } = req.body;
// Set up transporter for Nodemailer (using Gmail as an example)
const transporter = nodemailer.createTransport({
service: 'gmail',
secure: true,
port: 465,
auth: {
user: '[email protected]', // replace with your email
pass: 'lbynfuyroyherynf', // replace with your email password or app password
},
});
// Define the email options
const mailOptions = {
from: '[email protected]',
to: email, // Send to the user's email
subject: 'Appointment Confirmation',
text: `Dear ${name},\n\nYour appointment has been booked at ${hospital}.\n\nDate and Time: ${date}\n\nThank you for using our service.\n\nPhone: ${phone}`,
};
try {
// Send the email
const info = await transporter.sendMail(mailOptions);
console.log('Email sent:', info.response);
res.status(200).json({ success: true, message: 'Email sent successfully!' });
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({ success: false, message: 'Failed to send email' });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});