forked from gksoriginals/Voicebulb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
66 lines (63 loc) · 2.27 KB
/
app.py
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
from ghee.action import action_predict
from ghee.ner import ner_predict
from flask import Flask,request
import json
import sys
app = Flask(__name__)
app.config['SECRET_KEY'] = '22334455'
@app.route('/bulb',methods=['GET'])
def send_recieve():
control = {'status':None, 'text':None, 'blue_state':0, 'red_state':0}
if request.method=='GET':
try:
data = json.loads(request.data.decode('utf-8'))
log(data)
except (ValueError,TypeError,KeyError):
print("Error caught")
control['status'] = 0
return json.dumps(control)
control['status'] = 1
data = request.args.get('text')
intent = action_predict(str(data), 'bulb')
ent = ner_predict(str(data), 'bulb')
entities = []
for i in ent:
entities.append(i[1])
log(entities)
if intent == 'ON':
control['text'] = "intent recognised ON \n"
if 'blue' in entities:
control['text'] = control['text']+'entity recognised Blue light \n'
control['blue_state'] = 1
elif 'red' in entities:
control['text'] = control['text']+'entity recognised Red light \n'
control['red_state'] = 1
elif 'lights' in entities:
control['text'] = control['text']+'entity recognised Blue light and Red light\n'
control['red_state'] = 1
control['blue_state'] = 1
elif intent == 'OFF':
control['text'] = "intent recognised OFF \n"
if 'blue' in entities:
control['text'] = control['text']+'entity recognised Blue light \n'
control['blue_state'] = 2
elif 'red' in entities:
control['text'] = control['text']+'entity recognised Red light \n'
control['red_state'] = 2
elif 'lights' in entities:
control['text'] = control['text']+'entity recognised Blue light and Blue light\n'
control['red_state'] = 2
control['blue_state'] = 2
control = json.dumps(control)
return control
def log(message):
if message:
print(str(message))
sys.stdout.flush()
else:
print("NULL")
sys.stdout.flush()
#port_=int(sys.argv[1])
if __name__ == '__main__':
app.debug = True
app.run()