Spaces:
Running
Running
ABCASDFG98765432
commited on
Commit
•
d83b3a8
1
Parent(s):
658974f
Update index.js
Browse files
index.js
CHANGED
@@ -1,79 +1,57 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
});
|
23 |
-
|
24 |
-
fileUpload.addEventListener('change', function (e) {
|
25 |
-
const file = e.target.files[0];
|
26 |
-
if (!file) {
|
27 |
-
return;
|
28 |
}
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
});
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
imageContainer.innerHTML = '';
|
42 |
-
imageContainer.style.backgroundImage = `url(${img})`;
|
43 |
-
|
44 |
-
status.textContent = 'Analysing...';
|
45 |
-
const output = await detector(img, {
|
46 |
-
threshold: 0.5,
|
47 |
-
percentage: true,
|
48 |
-
});
|
49 |
-
status.textContent = '';
|
50 |
-
output.forEach(renderBox);
|
51 |
-
}
|
52 |
-
|
53 |
-
// Render a bounding box and label on the image
|
54 |
-
function renderBox({ box, label }) {
|
55 |
-
const { xmax, xmin, ymax, ymin } = box;
|
56 |
-
|
57 |
-
// Generate a random color for the box
|
58 |
-
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
59 |
-
|
60 |
-
// Draw the box
|
61 |
-
const boxElement = document.createElement('div');
|
62 |
-
boxElement.className = 'bounding-box';
|
63 |
-
Object.assign(boxElement.style, {
|
64 |
-
borderColor: color,
|
65 |
-
left: 100 * xmin + '%',
|
66 |
-
top: 100 * ymin + '%',
|
67 |
-
width: 100 * (xmax - xmin) + '%',
|
68 |
-
height: 100 * (ymax - ymin) + '%',
|
69 |
-
})
|
70 |
-
|
71 |
-
// Draw label
|
72 |
-
const labelElement = document.createElement('span');
|
73 |
-
labelElement.textContent = label;
|
74 |
-
labelElement.className = 'bounding-box-label';
|
75 |
-
labelElement.style.backgroundColor = color;
|
76 |
-
|
77 |
-
boxElement.appendChild(labelElement);
|
78 |
-
imageContainer.appendChild(boxElement);
|
79 |
-
}
|
|
|
1 |
+
const http = require('http');
|
2 |
+
const querystring = require('querystring');
|
3 |
+
const url = require('url');
|
4 |
+
|
5 |
+
class MyClassificationPipeline {
|
6 |
+
static task = 'text-classification';
|
7 |
+
static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english';
|
8 |
+
static instance = null;
|
9 |
+
|
10 |
+
static async getInstance(progress_callback = null) {
|
11 |
+
if (this.instance === null) {
|
12 |
+
// Dynamically import the Transformers.js library
|
13 |
+
let { pipeline, env } = await import('@xenova/transformers');
|
14 |
+
|
15 |
+
// NOTE: Uncomment this to change the cache directory
|
16 |
+
// env.cacheDir = './.cache';
|
17 |
+
|
18 |
+
this.instance = pipeline(this.task, this.model, { progress_callback });
|
19 |
+
}
|
20 |
+
|
21 |
+
return this.instance;
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
}
|
23 |
+
}
|
24 |
+
|
25 |
+
// Define the HTTP server
|
26 |
+
const server = http.createServer();
|
27 |
+
const hostname = '127.0.0.1';
|
28 |
+
const port = 3000;
|
29 |
+
|
30 |
+
// Listen for requests made to the server
|
31 |
+
server.on('request', async (req, res) => {
|
32 |
+
// Parse the request URL
|
33 |
+
const parsedUrl = url.parse(req.url);
|
34 |
+
|
35 |
+
// Extract the query parameters
|
36 |
+
const { text } = querystring.parse(parsedUrl.query);
|
37 |
+
|
38 |
+
// Set the response headers
|
39 |
+
res.setHeader('Content-Type', 'application/json');
|
40 |
+
|
41 |
+
let response;
|
42 |
+
if (parsedUrl.pathname === '/classify' && text) {
|
43 |
+
const classifier = await MyClassificationPipeline.getInstance();
|
44 |
+
response = await classifier(text);
|
45 |
+
res.statusCode = 200;
|
46 |
+
} else {
|
47 |
+
response = { 'error': 'Bad request' }
|
48 |
+
res.statusCode = 400;
|
49 |
+
}
|
50 |
+
|
51 |
+
// Send the JSON response
|
52 |
+
res.end(JSON.stringify(response));
|
53 |
});
|
54 |
|
55 |
+
server.listen(port, hostname, () => {
|
56 |
+
console.log(`Server running at http://${hostname}:${port}/`);
|
57 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|