This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
63 lines (51 loc) · 1.84 KB
/
flask_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
from flask import Flask, request, session
from luis_sdk import LUISClient
import commands
import confirmations
from luis_command_names import LuisCommands
from luis_sdk.luis_response import LUISResponse
app = Flask(__name__)
app.secret_key = '\xe2\xd2\xfeSIM\x93\xad\x1b\x8bGgn{V\xd0\x00\x8f\x13\x95dr\xeeT'
application = app
# TODO get these, then hide them
APPID = "a754fc42-56f2-434a-a56b-8818c3df71f5"
APPKEY = "3423d600dd744266867e8ec0752b881b"
CLIENT = LUISClient(APPID, APPKEY, True)
@app.route("/")
def get_command():
command = request.args.get("command")
if not command:
print("No command parameter")
return
print(command)
reply = CLIENT.predict(command)
# error checking here
intent = camelCase(reply.get_top_intent().get_name())
print("Intent: ", intent)
if LuisCommands.is_confirmation(intent):
if session['prev_reply']:
prev_reply = LUISResponse(session['prev_reply'])
print("Previous Intent: ", prev_reply.get_top_intent().get_name())
else:
prev_reply = None
session['prev_reply'] = None
return confirm_cancel_request(intent, prev_reply)
elif LuisCommands.requires_confirm(intent):
confirm_reply = getattr(confirmations, intent)
session['prev_reply'] = reply.json
return confirm_reply(reply)
else:
session['prev_reply'] = None
action = getattr(commands, intent)
return action(reply)
def camelCase(str):
return str[0].lower() + str[1:]
def confirm_cancel_request(intent, prev_reply):
if prev_reply is None:
return commands.none(None)
if LuisCommands.is_confirm(intent):
prev_intent = camelCase(prev_reply.get_top_intent().get_name())
action = getattr(commands, prev_intent)
return action(prev_reply)
else:
return "Canceled request"