-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
126 lines (93 loc) · 3.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
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
from flask import Flask
from flask_restful import Resource, Api, reqparse
from datetime import datetime, timedelta
import json
import pymongo
import os, sys
import json
# cwd = os.path.dirname(__file__)
# sys.path.append(os.path.join(cwd, "../lib/birthday_parser"))
from active_ai import BirthdayParser
from active_ai import CityParser
app = Flask(__name__)
api = Api(app)
LOCAL_MONGO_HOST = '127.0.0.1'
LOCAL_MONGO_PORT = 27017
DB_NAME = 'QueMachine'
# mongo_server = pymongo.MongoClient(LOCAL_MONGO_HOST, LOCAL_MONGO_PORT)
parser = reqparse.RequestParser()
parser.add_argument('q_num', type=int)
parser.add_argument('session_id', type=int)
parser.add_argument('from', type=str)
parser.add_argument('timestamp', type=str)
parser.add_argument('content', type=str)
parser.add_argument('request_all', type=int)
class QuestionManger:
q_num = None
question_instance = None
def __init__(self, q_num):
self.q_num = q_num
if q_num == 1:
self.question_instance = BirthdayParser()
elif q_num == 2:
self.question_instance = CityParser()
def ask(self):
return self.question_instance.ask()
def answer(self, sentence):
# Send sentence to instance
return json.dumps(self.question_instance.reply(sentence))
class Chat(Resource):
# Session cache stores chat records in the layout below:
# { "id": int,
# "message": [
# {
# "from" : str,
# "timestamp" : int,
# "content" : str,
# },
# ...
# ]
# }
message_cache = {}
# live_session -> dict{ int(id) : QuestionManger}
live_session = {}
id = 1
def get(self):
args = parser.parse_args()
new_id = self.id
self.id += 1
new_session = QuestionManger(args['q_num'])
self.live_session[new_id] = new_session
try:
question = new_session.ask()
except Exception:
question = "Hello World!"
self.message_cache[new_id] = {"session_id": new_id, "message": []}
message_body = self.add_message(new_id, question, False)
return message_body, 200, {'Access-Control-Allow-Origin': '*'}
def post(self):
args = parser.parse_args()
if args['session_id'] in self.live_session:
message_body = self.add_message(args['session_id'], args['content'], True)
try:
current_session = self.live_session[args['session_id']]
answer = current_session.answer(args['content'])
except Exception as Error:
print(Error)
answer = "World Hello!"
message_body = self.add_message(args['session_id'], answer, False)
return message_body, 200, {'Access-Control-Allow-Origin': '*'}
else:
return None, 404, {'Access-Control-Allow-Origin': '*'}
def add_message(self, id, sentence, who, time=datetime.now()):
message_body = {
"session_id": id,
"from": who,
"timestamp": int(time.timestamp()),
"content": sentence,
}
self.message_cache[id]["message"].append(message_body)
return message_body
api.add_resource(Chat, '/api/')
if __name__ == "__main__":
app.run(debug=True)