forked from ilirt123/ECE528-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (41 loc) · 1.41 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
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
// Set up the Express.js app
const app = express();
const port = 3000;
// Serve static files from the public directory
app.use(express.static('public'));
// Parse incoming request bodies
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Set up the POST endpoint for predictions
app.post('/predict', (req, res) => {
// Extract the feature data from the request body
const features = req.body.features;
// Set up the request options
const options = {
method: 'POST',
url: `https://us-central1-aiplatform.googleapis.com/v1/projects/${process.env.PROJECT_ID}/locations/us-central1/endpoints/${process.env.ENDPOINT_ID}:predict`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`
},
body: JSON.stringify({ instances: features })
};
// Send the request to the Vertex AI endpoint
request(options, (error, response, body) => {
if (error) {
console.error(error);
res.status(500).send('Internal server error');
} else {
// Return the prediction result to the client
const result = JSON.parse(body).predictions[0].output;
res.status(200).send(result);
}
});
});
// Start the server
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});