This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fancy-nlp-server.js
101 lines (75 loc) · 2.95 KB
/
fancy-nlp-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
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
const express = require('express')
const bodyParser = require('body-parser')
const { NlpManager } = require('node-nlp');
const http = require('http')
const https = require('https')
const app = express()
let port = (process.argv[2] === undefined) ? 4443 : process.argv[2]
const fs = require('fs')
const path = require('path')
var cors = require('cors')
const handles = []
const messagesFilePath = path.join(`${__dirname}`, 'operational-data/messages.json')
let readyForMore = true
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/train', async (req, res) => {
if (readyForMore) {
const handle = {
clientId: (req.body.wallet === undefined) ? Date.now().toString() : req.body.wallet,
manager: new NlpManager({ languages: req.body.languageCodes })
}
for (const document of req.body.documents) {
handle.manager.addDocument(document.languageCode, document.input, document.intent);
}
for (const answer of req.body.answers) {
handle.manager.addAnswer(answer.languageCode, answer.intent, answer.output);
}
await handle.manager.train()
handles.push(handle)
limitNumberOfActiveClients() // shall be optimized soon
res.send({ clientId: handle.clientId })
} else {
res.send('give me a break')
}
})
app.get('/process/input/:input/languageCode/:languageCode/clientId/:clientId', async (req, res) => {
const handle = handles.filter((e) => e.clientId === req.params.clientId)[0]
if (handle === undefined) {
res.send(`Are you sure you submitted a valid clientId?: ${req.params.clientId} - try training again and save the return value`)
}
const nlpResult = await handle.manager.process(req.params.languageCode, req.params.input)
const message = {
clientId: req.params.clientId,
timeStamp: Date.now().toString(),
languageCode: req.params.languageCode,
input: req.params.input,
output: nlpResult.answer
}
const messages = JSON.parse(fs.readFileSync(messagesFilePath, 'utf-8'))
messages.push(message)
fs.writeFileSync(messagesFilePath, JSON.stringify(messages))
res.send({ nlpResult })
})
app.get('/getMessages/clientId/:clientId', async (req, res) => {
const fileContent = JSON.parse(fs.readFileSync(messagesFilePath, 'utf8'))
res.send(fileContent.filter((e) => e.clientId === req.params.clientId))
})
function limitNumberOfActiveClients() {
if (handles.length > 100) {
handles.splice(0, 1)
readyForMore = false
setTimeout(() => {
readyForMore = true
}, 60 * 1000)
}
}
if (port === 4443) {
https.createServer({
cert: fs.readFileSync('/etc/letsencrypt/live/fancy-chats.com/fullchain.pem'),
key: fs.readFileSync('/etc/letsencrypt/live/fancy-chats.com/privkey.pem')
}, app).listen(port)
} else {
http.createServer(app).listen(port)
}