This repository was archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.py
More file actions
138 lines (126 loc) · 4.37 KB
/
server.py
File metadata and controls
138 lines (126 loc) · 4.37 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
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
128
129
130
131
132
133
134
135
136
137
138
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Imports
import fasttext
import flask
from flask import request
from flask import jsonify
import json
import requests
import random
app = flask.Flask(__name__)
model_name = 'model'
train_file_name = 'train_data.txt'
response_file_name = 'story.json'
def train():
print('--- training started ---')
classifier = fasttext.supervised(train_file_name, model_name)
print('--- training ended ---')
return True
def predict(query):
texts = [query]
classifier = fasttext.load_model(model_name+'.bin')
prediction = json.dumps(classifier.predict(texts, k=3))
return prediction
def utter(prediction):
response_file = open(response_file_name,'r')
response = json.loads(response_file.read())
prediction = json.loads(prediction)[0][0]
utter_list = response[prediction]
return random.choice(utter_list)
@app.route('/demo/predict/<query>',methods=['POST','GET'])
def ask(query):
prediction = predict(query)
utterance = utter(prediction)
return utterance
@app.route('/demo/example',methods=['GET'])
def example():
return '''
[{
"questions": ["hello", "Hi", "namaste"],
"answers": ["hey", "hi"]
},
{
"questions": ["how do you do", "How are you"],
"answers": ["I' m good ", "Feeling great!"]
}]
'''
@app.route('/demo',methods=['POST','GET'])
def demo():
if request.method == 'POST':
QnAdata = json.loads(request.get_json()["data"])
train_file = open(train_file_name,'w')
response_file = open(response_file_name,'w')
train_data = ''
response_data = {}
counter = 0
for intent in QnAdata:
questions = intent["questions"]
answers = intent["answers"]
for question in questions:
train_data += '__label__QnA'+str(counter)+' '+question+'\n'
response_data['__label__QnA'+str(counter)] = answers
counter += 1
train_file.write(train_data)
response_file.write(json.dumps(response_data))
train_file.close()
response_file.close()
train()
return jsonify('{"status":"success"}')
return '''
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
function send(){
var textdata = $('#textArea').val()
$("#text").html('training agent..')
$.ajax({
type: 'POST',
url: '/demo',
data: JSON.stringify({"data":textdata}),
success: function(resp) {$("#text").html('training completed.')},
error: function (jqXHR, exception){console.log('error')},
contentType: "application/json",
dataType: 'json'
})
}
function test(){
var testarea = $('#testarea').val()
console.log(testarea)
$.ajax({
type: 'GET',
url: '/demo/predict/'+testarea,
success: function(resp) {$("#testresult").html(resp)},
error: function (jqXHR, exception){console.log('error')}
})
}
</script>
</head>
<body>
<p> paste your FAQ here: <a href="/demo/example" target="new">see an example training data</a></p>
<textarea id="textArea" rows="10"></textarea>
<button onclick="send()">train</button>
<p id="text"></p>
<input type="text" id="testarea" placeholder="enter your query" />
<button onclick="test()">test</button>
<p id="testresult"></p>
</body>
<style>
textarea,input {
width: 99.7%;
resize: none;
border: 1px solid;
}
button {
margin-top: 10px;
}
</style>
</html>
'''
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)