-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
68 lines (57 loc) · 2.17 KB
/
server.ts
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
import express from "express";
import * as tf from "@tensorflow/tfjs-node";
import data from "./data"; // Assume this is your training data module
const app = express();
const port = 3000;
// Load the model
const loadModel = async (): Promise<tf.LayersModel> => {
const model = await tf.loadLayersModel("file://./model/model.json");
return model;
};
// API endpoint to process a paragraph and return the color based on feelings
app.post("/analyze-feelings", express.json(), async (req, res) => {
const { paragraph } = req.body;
// Extract words (basic example, you might want to use a more sophisticated NLP tool)
const words = paragraph.split(/\s+/);
console.log(paragraph);
// Dummy method to detect feelings
// const detectedFeelings = words.filter((word) =>
// ["happy", "gloomy", "sad"].includes(word.toLowerCase())
// );
const detectedFeelings = words
.filter((word) => data.some((item) => item.word === word.toLowerCase()))
.map((word) => {
const matchedItem = data.find((item) => item.word === word.toLowerCase());
return matchedItem ? matchedItem.color : null;
})
.filter((color) => color !== null);
if (detectedFeelings.length === 0) {
return res.status(200).json({ color: "#FFFFFF" }); // Default or no emotion color
}
// Assuming a single detected feeling for simplicity
const model = await loadModel();
const inputVector = new Array(data.length).fill(0);
detectedFeelings.forEach((feeling) => {
const index = data.findIndex((item) => item.word === feeling.toLowerCase());
if (index !== -1) inputVector[index] = 1;
});
const input = tf.tensor2d([inputVector], [1, data.length]);
const prediction = model.predict(input) as tf.Tensor;
const colorArray = await prediction.array();
const colorHex = rgbToHex(
colorArray[0][0],
colorArray[0][1],
colorArray[0][2]
);
res.json({ color: colorHex });
});
function rgbToHex(r: number, g: number, b: number): string {
const toHex = (n: number) =>
Math.round(n * 255)
.toString(16)
.padStart(2, "0");
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
}
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});