-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
127 lines (99 loc) · 3.62 KB
/
routes.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import express, { response } from 'express';
import { ModelResponder } from './Controller/ModelController.js';
import { Allergies } from './Config/Allergies.js';
const router = express.Router();
router.post('/identify', async (req, res) => {
const body = req.body;
const content = body.content;
try {
const modelResponse = await ModelResponder('identify',{ content : content });
res.json({ response: modelResponse });
} catch (error) {
res.status(400);
res.json({ error : 'failed'});
}
});
router.post('/recommend', async (req, res) => {
try {
const content = req.body.content.replace(/\n/g, ' ');
const allergies = req.body.allergies;
let inputString = content;
if(allergies)
{
inputString = content + " " + 'The person has' + allergies;
}
let identifiedElements = await ModelResponder('identify',{ content : content });
console.log('identifiedElements are ===== ' + identifiedElements);
// check if identifiedElements has text "None"
const regex = /\bNone\b/;
const regex1 = /\bI can't provide\b/;
if(regex.test(identifiedElements) || regex1.test(identifiedElements))
{
res.status(400);
res.json({ error : "Invalid element provided"});
return;
}
const modelResponse = await ModelResponder('recommend',{ content : identifiedElements });
console.log(modelResponse);
res.json({ response: JSON.parse(modelResponse) });
} catch (error) {
console.log(error);
res.status(400);
res.json({ error : 'failed'});
}
});
router.post('/recommend2', async (req, res) => {
try {
const content = req.body.content.replace(/\n/g, ' ');
const allergies = req.body.allergies;
const age = req.body.age;
const gender = req.body.gender;
let identifiedElements = await ModelResponder('identify',{ content : content });
console.log('identifiedElements are ===== ' + identifiedElements);
// check if identifiedElements has text "None"
const regex = /\bNone\b/;
const regex1 = /\bI can't provide\b/;
if(regex.test(identifiedElements) || regex1.test(identifiedElements))
{
res.status(400);
res.json({ error : "Invalid element provided"});
return;
}
let inputString = "Identified elements are " + identifiedElements;
if(allergies)
{
inputString = inputString + " " + ' The person has ' + allergies;
}
if(age)
{
inputString = inputString + " " + ' The persons age is ' + age;
}
if(gender)
{
inputString = inputString + " " + ' The persons gender is ' + gender;
}
const modelResponse = await ModelResponder('recommend2',{ content : inputString });
console.log(modelResponse);
res.json({ response: JSON.parse(modelResponse) });
} catch (error) {
console.log(error);
res.status(400);
res.json({ error : 'failed'});
}
});
router.post('/why', async (req, res) => {
try {
const body = req.body;
const content = body.content;
const modelResponse = await ModelResponder('why',content);
console.log(modelResponse);
res.json({ response: JSON.parse(modelResponse) });
} catch (error) {
res.status(400);
res.json({ error : 'failed'});
}
});
router.get('/test', (req, res) => {
res.json({ test : 'test' });
});
export default router;