-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflask-socket.py
More file actions
executable file
·231 lines (189 loc) · 7.67 KB
/
Copy pathflask-socket.py
File metadata and controls
executable file
·231 lines (189 loc) · 7.67 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
# This is a basic webserver that will wrap the emo20q questioner
# agent and implement the dialog by representing user input by http
# request and system response by http response
import pickle # for saving python objects
import os # for paths
import sys # set the python path to find emo20q module
import time # for sleep
import uuid # for creating a random name for the pickled agent
from gevent import monkey
monkey.patch_all()
# import the web application factory class and instantiate it
from flask import Flask
print(__name__)
app = Flask(__name__)
app.config['SECRET_KEY'] = "don't use this in prod"
# import flask session for cookies
from flask import session
# for static files (todo: for performance, these should be served by
# the webserver
from flask import send_from_directory
# import request for getting query params from user response
from flask import request
# templates
from flask import render_template
# socket.io
from flask_socketio import SocketIO, emit
socketio = SocketIO(app, logger=True, engineio_logger=True)
# import emo20q agent: need to set the path
path = "./emo20q"
if path not in sys.path:
sys.path.insert(0,path)
sys.path.insert(0,path)
import emo20q
from emo20q.gpdaquestioner import QuestionerAgent
# it would be nice to just pickle the whole agent, but because of
# lambdas we need to reconstruct the agent from episodic buffer (short
# term memory) and lexical access (word knowledge)
from emo20q.gpdaquestioner import LexicalAccess
from emo20q.gpdaquestioner import SemanticKnowledge
from emo20q.gpdaquestioner import EpisodicBuffer
# create an endpoint/route for the app ( '/' is the root of the
# server)
@app.route('/')
def index():
""" the main webpage """
#session['sessionid'] = uuid.uuid4()
#session['ip'] = request.remote
# we will assume that if the page is reloaded, that the user
# intends to start a new game so we'll remove the session info
if 'agent_id' in session:
del session['agent_id']
app.logger.debug("new session", extra={"ip": request.remote_addr})
return render_template('emo20q_chat.html')
@app.route('/css/<path:filename>')
def css(filename):
""" to get css """
directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "css"))
print(directory)
return send_from_directory(directory, filename)
@app.route('/img/<path:filename>')
def img(filename):
""" images """
directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "img"))
return send_from_directory(directory, filename)
# socket connection events
@socketio.on('connect', namespace='/pbot')
def pbot_connect():
""" first connection where the dialog system starts"""
# create agent
# first get agent file name if it exists
if 'agent_id' in session: # if the user has already visited
agent_id = session['agent_id']
else:
agent_id = uuid.uuid4()
print(agent_id)
session['agent_id'] = agent_id
# try top open and load the pickled agent
pickled_path = str(agent_id)
print(pickled_path)
if os.path.exists(pickled_path):
try:
""" this part is a bit verbose and error prone"""
episodic_buffer, state_name, belief = \
pickle.load(open(str(pickled_path), "rb"))
agent = QuestionerAgent(episodicBuffer=episodic_buffer,
lexicalAccess=LexicalAccess(),
semanticKnowledge=SemanticKnowledge())
#import pdb; pdb.set_trace()
agent.set_state(getattr(agent, state_name))
agent.belief = belief
except Exception as ex: # normally a bare except is not good but this is just
# for illustration
print(ex)
agent = QuestionerAgent()
else: # this should occur when it is the users first visit
agent = QuestionerAgent()
#time.sleep(1)
emit('loguser', {'data': '[user enters]'})
time.sleep(1)
# give the user input to the agent, start with just empty to start
try:
agent_output = agent("")
except emo20q.gpda.GPDAError: # if there's an error, reset the session
del session['agent_id']
agent_id = uuid.uuid4()
print(agent_id)
session['agent_id'] = agent_id
agent = QuestionerAgent()
agent_output = agent("")
print(agent_output)
# pickle the agent
pickle.dump([agent.episodicBuffer, agent.state.name,
agent.belief], open(pickled_path, "wb"))
#return('Agent says: ' + agent_output)
for line in agent_output.strip().split("\n"):
time.sleep(1)
emit('logagent', {'data': "%s" % line})
# emit('logagent', {'data': '[emo20q agent enters the universe of discourse]'})
# time.sleep(1)
# emit('logagent', {'data': 'Hi I\'m the Emotion Twenty Questions Agent.'})
# time.sleep(1)
# emit('logagent', {'data': 'In the game of emotion twenty questions (EMO20Q), you will pick an emotion, then I\'ll try to guess it.'})
# time.sleep(1)
# emit('logagent', {'data': 'I\'m not a real person, but you can talk to me like you would talk to a normal person. Let me know when you have picked an emotion word and are ready to start!'})
#time.sleep(1)
#sessionid = session['sessionid']
# socket message events
@socketio.on('UserUtt', namespace='/pbot')
def pbot_message(message):
"""When the user enters input, echo UserUtt as confirmation and
display on client/browser
"""
user_input = message['data']
session['receive_count'] = session.get('receive_count', 0) + 1
emit('loguser',
{'data':"%s" % user_input})
# create agent
# first get agent file name if it exists
if 'agent_id' in session: # if the user has already visited
agent_id = session['agent_id']
else:
agent_id = uuid.uuid4()
print(agent_id)
session['agent_id'] = agent_id
# try top open and load the pickled agent
pickled_path = str(agent_id)
print(pickled_path)
if os.path.exists(pickled_path):
try:
""" this part is a bit verbose and error prone"""
episodic_buffer, state_name, belief = \
pickle.load(open(str(pickled_path), "rb"))
agent = QuestionerAgent(episodicBuffer=episodic_buffer,
lexicalAccess=LexicalAccess(),
semanticKnowledge=SemanticKnowledge())
#import pdb; pdb.set_trace()
agent.set_state(getattr(agent, state_name))
agent.belief = belief
except Exception as ex: # normally a bare except is not good but this is just
# for illustration
print(ex)
agent = QuestionerAgent()
else: # this should occur when it is the users first visit
agent = QuestionerAgent()
# give the user input to the agent
try:
agent_output = agent(user_input)
except emo20q.gpda.GPDAError:
agent_output = ("You can close the window/tab now.\n"
"If you want to play again, please refresh the page.")
print(agent_output)
# pickle the agent
pickle.dump([agent.episodicBuffer, agent.state.name,
agent.belief], open(pickled_path, "wb"))
#return('Agent says: ' + agent_output)
for line in agent_output.strip().split("\n"):
time.sleep(1)
emit('logagent', {'data': "%s" % line})
@socketio.on('disconnect', namespace='/pbot')
def pbot_disconnect():
session.clear()
@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
return request.remote_addr
if __name__ == '__main__':
socketio.run(app, host="0.0.0.0",
#debug=True,
port=5000)