-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-http.js
More file actions
45 lines (37 loc) · 1.27 KB
/
Copy pathai-http.js
File metadata and controls
45 lines (37 loc) · 1.27 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
const http = require('http');
const querystring = require('querystring');
const url = require('url');
// Define the MyClassificationPipeline class
class MyClassificationPipeline {
static async getInstance() {
const { pipeline } = await transformersPromise;
return pipeline('text-classification', 'bigcode/starcoder2-7b');
}
}
// Define the HTTP server
const server = http.createServer();
const hostname = '0.0.0.0';
const port = 3000;
// Listen for requests made to the server
server.on('request', async (req, res) => {
// Parse the request URL
const parsedUrl = url.parse(req.url);
// Extract the query parameters
const { text } = querystring.parse(parsedUrl.query);
// Set the response headers
res.setHeader('Content-Type', 'application/json');
let response;
if (parsedUrl.pathname === '/classify' && text) {
const classifier = await MyClassificationPipeline.getInstance();
response = await classifier(text);
res.statusCode = 200;
} else {
response = { 'error': 'Bad request' }
res.statusCode = 400;
}
// Send the JSON response
res.end(JSON.stringify(response));
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});