-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (74 loc) · 2.64 KB
/
server.js
File metadata and controls
83 lines (74 loc) · 2.64 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
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
const { readJson } = require("./public/read-files.js");
// const { connectToDb } = require("./public/use-db.js");
const { runShellCommand } = require("./public/shell.js");
const {
getClosest,
embedAllSentences,
embed1Sentence,
// compareSentences,
compareEmbeddings,
} = require("./public/tfjs.js");
const express = require("express");
const app = express();
const port = process.env.PORT || 8000;
app.listen(port, function () {
console.log(`port ${port}`);
});
app.use(express.json());
app.use("/public", express.static(process.cwd() + "/public"));
app.use("/styles", express.static(process.cwd() + "/styles"));
app.use("/views", express.static(process.cwd() + "/views"));
app.route("/").get(function (req, res) {
console.log("GET");
res.sendFile(process.cwd() + "/views/index.html");
readJson("takeaways.json");
// connectToDb();
});
app.route("/shell").post(function (req, res) {
const command = req.body.command;
if (!command) return;
console.log(`shell command: ${command}`);
const output = runShellCommand(command);
console.log(`Result: ${output}`);
res.send({ result: output });
});
app.route("/python").post(function (req, res) {
const scriptPath = req.body.scriptPath;
if (!scriptPath) return;
console.log(`python script: ${scriptPath}`);
const spawn = require("child_process").spawn;
const process = spawn("python", [scriptPath]);
process.stdout.on("data", function (data) {
const output = data.toString();
console.log(`Result: ${output}`);
res.send({ result: output });
});
});
app.route("/embed1Sentence").post(async function (req, res) {
const sentence = req.body.sentence;
if (!sentence) return;
console.log(`getting embedding for this sentence: ${sentence}`);
const embeddingObject = await embed1Sentence(sentence);
const embedding = embeddingObject[sentence];
console.log("Embedding retrieved.");
res.send({ embedding });
});
app.route("/embedAllSentences").post(async function (req, res) {
const sentences = req.body.sentences;
if (!sentences) return;
console.log(`getting embeddings for ${sentences.length} sentences.`);
const embeddingsObject = await embedAllSentences(sentences);
console.log("Embeddings retrieved.");
res.send({ embeddingsObject });
});
app.route("/getClosest").post(async function (req, res) {
const sentence = req.body.sentence;
const embeddingsObject = req.body.embeddingsObject;
if (!sentence) return;
console.log(
`getting sentence with closest embedding to this sentence: ${sentence}`
);
const closestSentence = await getClosest(sentence, embeddingsObject);
console.log(`Closest sentence: ${closestSentence}`);
res.send({ closestSentence });
});