-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (54 loc) · 1.8 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
import express from 'express';
import fs from 'fs';
import path, { dirname } from 'path';
import cors from 'cors';
import similarity from 'compute-cosine-similarity';
import pkg from 'node-gzip';
const { ungzip } = pkg;
const PORT = 3005;
const app = express();
app.use(express.json());
app.use(cors());
app.use(express.static('./dist'));
let existingDescriptors = [];
ungzip(fs.readFileSync('data/embeddings.json.gz')).then((data) => {
existingDescriptors = JSON.parse(data);
console.log(existingDescriptors.length);
existingDescriptors = existingDescriptors.map((item) => {
item.descriptor = Object.keys(item.descriptor).map((key) => item.descriptor[key]);
return item;
});
});
// create an express route that returns a json with all the descriptors
app.post('/api/text', async (req, res) => {
// get the `embedding` data from the request
const embedding = req.body;
const distances = [];
// loop through existingDataset and compare the descriptor distance
for (let existingDescriptor of existingDescriptors) {
const dist = similarity(existingDescriptor.descriptor, embedding);
// console.log(existingDescriptor.descriptor.length, embedding.length)
if (dist < .9) {
distances.push({
name: existingDescriptor.file,
distance: dist,
});
}
console.log('Distance between ' + existingDescriptor.file + ' and upload is ' + dist);
}
// sort distances by lowest distance
distances.sort((a, b) => {
return b.distance - a.distance;
});
res.json({ msg: "Embed processed successfully", distances: distances.splice(0, 8) })
});
app.use('*', async (req, res) => {
const template = fs.readFileSync(
'./dist/index.html',
'utf-8'
);
res.status(200).set({ 'Content-Type': 'text/html' }).end(template);
});
app.listen(PORT, () => {
console.log(`Listening on http://localhost:${PORT}`);
});