This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheval_run_bots.py
296 lines (235 loc) · 10.1 KB
/
eval_run_bots.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#%%
import json
import time
try:
from eval_server_common import connect_to_redis
except ImportError:
print("HINT: copy example.eval_server_common.py to eval_server_common.py")
raise
import codraw_data
import episode
#%%
FAREWELL_MSG = "that's it, thanks!"
class Bot():
model_name = "model_generic"
agent_type = None
fns = None
# TODO(nikita): peek action for bot drawers is not supported
def __init__(self, id):
self.id = id
self.episode = episode.Episode()
self.role = "question" if self.agent_type == codraw_data.Agent.DRAWER else "answer"
self.handlers = {
'paired': self.on_paired,
'receive message': self.on_receive_message,
'server error': self.on_server_error, #TODO(nikita): Not emitted after I modified the server code
'disconnected partner': self.on_disconnected_partner,
}
self.disconnected = False
self.num_messages_sent = 0
def disconnect(self):
if self.id in type(self).active_bots:
assert type(self).active_bots[self.id] == self
del type(self).active_bots[self.id]
if not self.disconnected:
self.disconnected = True
self.emit('disconnect')
def emit(self, event, **msg):
obj = {
'botId': self.id,
'event': event,
'msg': msg,
}
self.redis.publish('visdial_server', json.dumps(obj))
def send_msg(self, msg):
self.num_messages_sent += 1
self.emit('chat message', msg=msg, role=self.role, seqId=self.num_messages_sent)
print("Sent chat message:", msg)
def send_scene_log(self, scene):
self.emit('scene log', scene=scene.stringify(), role=self.role, seqId=self.num_messages_sent)
# TODO(nikita): implement drawer bots, including "send_scene_log" which is sent by drawer
# socket.emit('scene log', {scene: Abs.resultAMT(), hitId: hitId, assignmentId: assignmentId, workerId: workerId, role: workerRole, seqId: noOfMsg});
def run_model_actions(self, must_trigger=True):
old_len = len(self.episode)
terminated = self._run_model_actions()
if terminated:
print("No action taking. Disconnecting")
if INTERACTIVE:
display(self.episode.get_true_scene())
self.send_msg(FAREWELL_MSG)
self.disconnect()
return
if must_trigger:
if len(self.episode) == old_len:
self.disconnect()
assert False, f"No response for event: {type(self.episode[-1]).__name__}"
msg_to_send = None
do_send_scene_log = False
for event in self.episode[old_len:]:
# TODO(nikita): log latent actions, such as SelectClipart
if isinstance(event, codraw_data.TellGroup):
assert msg_to_send is None, "Multiple TellGroup events added in a single round!"
msg_to_send = event.msg
elif isinstance(event, codraw_data.ReplyGroup):
assert msg_to_send is None, "Multiple ReplyGroup events added in a single round!"
msg_to_send = event.msg
elif isinstance(event, (codraw_data.DrawClipart, codraw_data.DrawGroup)):
do_send_scene_log = True
if do_send_scene_log:
assert self.agent_type == codraw_data.Agent.DRAWER
self.send_scene_log(self.episode.reconstruct())
if self.agent_type == codraw_data.Agent.TELLER:
assert msg_to_send is not None, "No message to send"
# Empty message is a signal for the drawer to begin the conversation
if msg_to_send == "" and len([x for x in self.episode if isinstance(x, codraw_data.TellGroup)]) == 1:
msg_to_send = None
print("Model expects the human drawer to start the conversation.")
else:
assert msg_to_send is not None or isinstance(self.episode[-1], codraw_data.ObserveTruth), "No message to send, and not the start"
if msg_to_send is not None:
self.send_msg(msg_to_send)
def _run_model_actions(self):
while True:
for fn in self.fns:
if type(self.episode[-1]) in fn._trigger_types:
old_len = len(self.episode)
fn(self.episode)
if len(self.episode) == old_len:
return True # terminated
break
else:
# print('no trigger for', type(self.episode[-1]))
return False
def on_paired(self, partnerId=None, key=None, image_url=None, role=None, caption=None):
if self.disconnected:
print("[ERROR] Disconnected bot was paired!")
return
print("Paired wih human partner!")
print("image_url:", image_url)
print("partner role:", role) # Yes, the role sent in the message is for the partner
assigned_role = "question" if role == "answer" else "answer"
assert assigned_role == self.role, "Wrong role assigned to bot!"
true_scene = codraw_data.AbstractScene(image_url)
self.episode.append(codraw_data.ObserveTruth(true_scene))
self.run_model_actions(must_trigger=False)
def on_receive_message(self, message=None, noOfMsg=None):
if self.disconnected:
print("[ERROR] Disconnected bot received a message!")
return
print(f"Got human message {noOfMsg}: {message}")
assert message is not None
if self.agent_type == codraw_data.Agent.TELLER:
self.episode.append(codraw_data.ReplyGroup(message))
else:
self.episode.append(codraw_data.TellGroup(message))
self.run_model_actions()
def on_disconnected_partner(self, disable='_unused'):
print("Partner disconnected from bot! Cleanining up the bot")
self.disconnect()
def on_server_error(self, errorMsg='[no errorMsg specified]'):
print("Error from server:", errorMsg)
self.disconnect()
# %%
def run_loop(classes):
active_bots = {}
channel_to_cls = {}
for cls in classes:
assert cls.agent_type in (codraw_data.Agent.TELLER, codraw_data.Agent.DRAWER), "Invalid agent_type for bot!"
channel = f'visdial_models.{cls.model_name}'.encode('utf-8')
assert channel not in channel_to_cls, f"Duplicate model name {cls.model_name}"
channel_to_cls[channel] = cls
if not hasattr(cls, 'redis'):
cls.redis = connect_to_redis()
if not hasattr(cls, 'active_bots'):
cls.active_bots = active_bots
p = cls.redis.pubsub()
for channel in channel_to_cls:
p.subscribe(channel)
for redis_msg in p.listen():
print("Got redis msg", redis_msg)
if redis_msg['type'] != 'message':
continue
if redis_msg['channel'] not in channel_to_cls:
print(f"WARNING: unrecognized channel {redis_msg['channel']}")
continue
data = json.loads(redis_msg['data'])
id = data['botId']
event = data['event']
msg = data['msg']
if event == 'paired':
active_bots[id] = channel_to_cls[redis_msg['channel']](id)
if id in active_bots:
handler = active_bots[id].handlers.get(event, None)
if handler is None:
print(f"No handler for event '{event}'")
else:
active_bots[id].handlers[event](**msg)
# %%
def make_script_teller_class():
import model
class ScriptTellerBot(Bot):
model_name = 'teller_script'
agent_type = codraw_data.Agent.TELLER
fns = [model.scripted_tell_before_peek]
scene_to_script = {}
def _run_model_actions(self):
if not hasattr(self.episode, 'script'):
script = self.scene_to_script[self.episode.get_last(codraw_data.ObserveTruth).scene.stringify()]
self.episode.script = script
self.episode.script_index = 0
return super()._run_model_actions()
for scene, script in codraw_data.get_scenes_and_scripts('all'):
ScriptTellerBot.scene_to_script[scene.stringify()] = script
return ScriptTellerBot
# %%
def model_to_bot_class(model_name, model, model_agent_type=codraw_data.Agent.TELLER):
model_name_ = model_name
class TheBot(Bot):
model_name = model_name_
agent_type = model_agent_type
fns = model.get_action_fns()
TheBot.__name__ = type(model).__name__ + 'Bot'
TheBot.__qualname__ = TheBot.__qualname__.replace('TheBot', TheBot.__name__)
return TheBot
# %%
def run_model_pairs(tellers, drawers=[], include_script_teller=True):
classes = []
if include_script_teller:
classes.append(make_script_teller_class())
for teller_name, (a, b) in tellers:
if a is not None:
classes.append(model_to_bot_class(teller_name + '_a', a, codraw_data.Agent.TELLER))
if b is not None:
classes.append(model_to_bot_class(teller_name + '_b', b, codraw_data.Agent.TELLER))
for drawer_name, (a, b) in drawers:
if a is not None:
classes.append(model_to_bot_class(drawer_name + '_a', a, codraw_data.Agent.DRAWER))
if b is not None:
classes.append(model_to_bot_class(drawer_name + '_b', b, codraw_data.Agent.DRAWER))
run_loop(classes)
#%%
if __name__ == '__main__':
from saved_models import load_models, make_pairs
models = load_models()
models['teller_scene2seq_a'].max_rounds = 20
models['teller_scene2seq_aux2_a'].max_rounds = 20
models['teller_rl_a'].max_rounds = 20
# TODO(nikita): change max_rounds for partition-b tellers, too
tellers = make_pairs(models,
'teller_nn',
'teller_pragmaticnn',
'teller_scene2seq',
'teller_scene2seq_aux2',
'teller_rl',
)
drawers = make_pairs(models,
'drawer_nn',
'drawer_bowcanvas2bce',
'drawer_lstmaddonly',
)
run_model_pairs(tellers, drawers)