-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolictom_mm.py
580 lines (486 loc) · 29.7 KB
/
symbolictom_mm.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import argparse
import copy
import json
import os
import traceback
import re
from collections import Counter
import matplotlib.pyplot as plt
import networkx as nx
from allennlp.predictors.predictor import Predictor
from precomputed_resulting_states import precomputed_resulting_states_all_models_with_regex, get_resulting_state
from utils_mm import load_model, run_inference, OpenIEPredictor, WANLIPredictor, \
loadFileWithCleanQuestionsAndQuestionTypes, final_answer_prompt_formatting, model_specific_cleaning_main_inference
ner_predictor = Predictor.from_path("https://storage.googleapis.com/allennlp-public-models/ner-elmo.2021-02-12.tar.gz")
openie = OpenIEPredictor()
wanli = WANLIPredictor(cache_dir = '/gscratch/xlab/olo126/.cache')
TOMI_QUESTION_TYPES = [
'reality', 'memory',
'first_order_0_tom', 'first_order_1_tom', 'first_order_0_no_tom', 'first_order_1_no_tom',
'second_order_0_tom', 'second_order_1_tom', 'second_order_0_no_tom', 'second_order_1_no_tom'
]
class Graph:
def __init__(self):
self.sentences = []
self.original_sentences = [] # text without any modification, only for final QA purposes
self.sentence_id_mapping_to_edge_ids = {} # maps edge to text that generated it
self.edge_id_to_sentence_id = []
self.nodes = []
self.edges = [] # (node_id, verb, node_id)
self.node_id_to_edge_ids = {}
def __str__(self):
result = ""
result += f"sentences: [{' || '.join(self.sentences)}]\n"
result += f"edges: [{' || '.join([str(e) for e in self.edges])}]\n"
return result
def to_dict(self):
return {
'sentences': self.sentences,
'original_sentences': self.original_sentences,
'sentence_id_mapping_to_edge_ids': self.sentence_id_mapping_to_edge_ids,
'nodes': self.nodes,
'edges': self.edges
}
def plot(self, filename, plots_dir):
G = nx.MultiGraph()
for edge in self.edges:
if not edge:
continue
node0, verb, node1 = edge
G.add_nodes_from([self.nodes[node0], self.nodes[node1]])
G.add_edge(self.nodes[node0], self.nodes[node1], r=verb)
pos = nx.spring_layout(G, scale=2)
nx.draw(G, with_labels=True, connectionstyle='arc3, rad = 0.1')
# edge_labels = nx.get_edge_attributes(G, 'r')
# nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
os.makedirs(plots_dir, exist_ok=True)
plt.savefig(os.path.join(plots_dir, filename + '.png'), format="PNG")
plt.clf()
plt.close('all')
def add_edges(self, text_sentence, original_text_sentence):
"""
Add new edges, one by OpenIE triple detected on the resulting state sentence.
Along with the edge, we store the original sentences to restore the text for
when we want to feed a linearized version of the graph to an LLM.
Note that we are implicitly also storing the order in which each edge was inserted,
so that we can restore the sentences in the same order as they were inserted to the graph.
"""
sentences = re.split(r"\b[0-9]{1,}. ", text_sentence)[1:]
triples = []
for s in range(len(sentences)):
sentences[s] = sentences[s].strip()
triples.extend(openie.get_triples(sentences[s]))
#triples = openie.get_triples(text_sentence)
if not triples:
return
#self.sentences.append(text_sentence)
self.sentences.append(sentences[s])
# EDIT: append a copy of the original sentence for each new sentence in self.sentences
self.original_sentences.append(original_text_sentence)
sentence_id = len(self.sentences) - 1
self.sentence_id_mapping_to_edge_ids[sentence_id] = []
for arg0, verb, arg1 in triples:
node_id_arg0 = next(iter(i for i in range(len(self.nodes)) if self.nodes[i] == arg0), None)
if node_id_arg0 is None:
self.nodes.append(arg0)
node_id_arg0 = len(self.nodes) - 1
node_id_arg1 = next(iter(i for i in range(len(self.nodes)) if self.nodes[i] == arg1), None)
if node_id_arg1 is None:
self.nodes.append(arg1)
node_id_arg1 = len(self.nodes) - 1
self.edges.append((node_id_arg0, verb, node_id_arg1))
self.edge_id_to_sentence_id.append(sentence_id)
edge_id = len(self.edges) - 1
self.sentence_id_mapping_to_edge_ids[len(self.sentences) - 1].append(edge_id)
for node_id in [node_id_arg0, node_id_arg1]:
if node_id not in self.node_id_to_edge_ids:
self.node_id_to_edge_ids[node_id] = []
self.node_id_to_edge_ids[node_id].append(edge_id)
# for tracking openIE
return triples
def remove_edges(self, sentence_id_to_remove):
"""
Removes edges described by the text sentence.
WARNING FOR FUTURE WORK: partial contradictions are not considered. We removed everything that a sentence said.
In a later version, we should only removed the parts of the sentence that caused an NLI contradiction.
"""
self.sentences[sentence_id_to_remove] = None # we don't want to pop anything and break the ids in the process
self.original_sentences[
sentence_id_to_remove] = None # we don't want to pop anything and break the ids in the process
for edge_id in self.sentence_id_mapping_to_edge_ids[sentence_id_to_remove]:
arg0, verb, arg1 = self.edges[edge_id]
self.edges[edge_id] = None
self.edge_id_to_sentence_id[edge_id] = None
self.node_id_to_edge_ids[arg0] = [i for i in self.node_id_to_edge_ids[arg0] if i != edge_id]
self.node_id_to_edge_ids[arg1] = [i for i in self.node_id_to_edge_ids[arg1] if i != edge_id]
self.sentence_id_mapping_to_edge_ids[sentence_id_to_remove] = None
def detect_contradicting_edges(self, sentence, return_wanli_scores=False):
"""
Remove WANLI contradictions
"""
wanli_scores = {sentence: {}}
sentence_ids_to_remove = []
# WANLI brittleness is sometimes triggered by lack of punctuation,
# so solely for linguistic diversity experiments we included checking for contradictions
# also including a final dot if not originally there
for add_final_punctuation_function in [lambda x: x,
lambda x: x + '.' if x[-1] != '.' else x]:
for i, ctxt in enumerate(self.sentences):
if not ctxt:
continue
# (sentence, ctxt) works better than (ctxt, sentence) since
# "Nathan moved the t-shirt to the fridge.</s></s>The t-shirt is in the basket."
# is marked as contradiction
predictions, score = wanli.predict(
add_final_punctuation_function(sentence),
add_final_punctuation_function(ctxt)
)
wanli_scores[sentence][ctxt] = score
if predictions[0] == 'contradiction' and i not in sentence_ids_to_remove:
sentence_ids_to_remove.append(i)
if return_wanli_scores:
return sentence_ids_to_remove, wanli_scores
return sentence_ids_to_remove
def get_nodes_in_connected_component(self, node_name):
node_id = next(iter(i for i in range(len(self.nodes)) if self.nodes[i] == node_name), None)
if node_id is None:
print(f'WARNING: node {node_name} does not exist. Nodes are: {self.nodes}')
return [], []
visited = [node_id]
queue = [node_id]
edges = set([])
all_edges = set([])
while queue:
cur_node_id = queue.pop(0)
for edge_id in self.node_id_to_edge_ids[cur_node_id]:
node0, verb, node1 = self.edges[edge_id]
if node0 not in visited:
visited.append(node0)
queue.append(node0)
edges.add(edge_id)
if node1 not in visited:
visited.append(node1)
queue.append(node1)
edges.add(edge_id)
all_edges.add(edge_id)
return [self.nodes[v] for v in visited], all_edges # used to be edges
@classmethod
def retain_only_entities_referring_to_people(cls, entities_list):
"""
Retain only entities that refer to people.
Implemented in an EXTREMELY HACKY WAY, by checking if the entity is capitalized.
FIXME: Implement a robust way of detecting actors (as opposed to objects).
"""
return [e for e in entities_list if e.istitle()]
def get_witnesses(self, sentence):
"""
Here's where the inference happens!
A exited B. Who saw this? People in B!
A entered B. Who saw this? People in B!
A moved B to C. Who saw this? People in the same room as A!
"""
# We used to restrict this to people, but actually "The broccoli is in the bucket" is implicitly learned
# by everyone in the same room as the broccoli (or the bucket)
# all entities should be in the same connected component since they've already been added to the graph by now
triples = openie.get_triples(sentence)
entities = [n0 for n0, _, n1 in triples] + [n1 for n0, _, n1 in triples]
if not entities:
print(f'WARNING: Possible OpenIE error, since no entities were found for sentence {sentence}')
return []
entities_in_cc, _ = self.get_nodes_in_connected_component(entities[0])
return Graph.retain_only_entities_referring_to_people(entities_in_cc)
class GraphsContainer:
"""
Structure that contains all belief graphs (local contexts) and recursively updates them all.
"""
def __init__(self, tom_level):
self.local_context = {}
self.tom_level = tom_level
def __str__(self):
return "\n".join([f"LocalContext for person {p}\n" + str(self.local_context[p]) for p in self.local_context])
def to_dict(self):
result = {'tom_level': self.tom_level,
'local_context': {p: self.local_context[p].to_dict() for p in self.local_context}}
return result
def plot(self, filename, plots_dir):
for p in self.local_context:
self.local_context[p].plot(filename + f'_local_{p}', plots_dir)
def add_person(self, person):
"""
When a new person appears in a story, add them to the structure of belief graphs.
"""
if person not in self.local_context:
self.local_context[person] = GraphsContainer(tom_level=self.tom_level - 1) if self.tom_level > 1 else Graph()
if self.tom_level > 1:
for p in self.local_context:
self.local_context[p].add_person(person)
def recursively_update_all_graphs(self, global_context, witnesses, current_state_sentence, sent):
if self.tom_level > 1:
for p in witnesses:
self.add_person(p)
self.local_context[p].recursively_update_all_graphs(global_context, witnesses, current_state_sentence, sent)
else:
for p in witnesses:
# below is the localContextUpdate() functionality mentioned in the manuscript
self.add_person(p)
sentence_ids_to_remove = self.local_context[p].detect_contradicting_edges(sent)
self.local_context[p].add_edges(current_state_sentence, sent)
global_context, self.local_context[p] = self.propagate_knowledge(global_context, p)
for s_id in sentence_ids_to_remove:
self.local_context[p].remove_edges(s_id)
def propagate_knowledge(self, global_context, active_person):
"""
# Update what the active person in sent implicitly learns when they perform the action
# this involves more than adding an edge, but rather adding a whole subgraph from global_context
# Here we are implicitly simplifying the problem: the truth is fully accessible to all in the same room
Example: John entered the bedroom. Now John gains everything in his new connected component (simplification here!)
FIXME: it might also involve removing stuff from a subgraph
"""
local_context_of_active_person = self.local_context[active_person]
_, edge_ids_in_global_context = global_context.get_nodes_in_connected_component(active_person)
for edge_id in edge_ids_in_global_context:
sentence = global_context.sentences[global_context.edge_id_to_sentence_id[edge_id]]
original_sentence = global_context.original_sentences[global_context.edge_id_to_sentence_id[edge_id]]
if sentence not in local_context_of_active_person.sentences:
local_context_of_active_person.add_edges(sentence, original_sentence)
return global_context, local_context_of_active_person
def create_all_symbolictom_graphs(story, model, tokenizer, tom_level=2):
"""
Processes story and returns all belief graphs (local context graphs)
plus the global context (true world state graph).
"""
witnesses_history = []
global_context_history = []
local_contexts_history = []
# for resulting state tracking
resulting_states = []
global_context = Graph()
local_context = GraphsContainer(tom_level=tom_level)
print(story)
for j, sent in enumerate(story):
sentence_ids_to_remove, wanli_scores = global_context.detect_contradicting_edges(sent, return_wanli_scores=True)
#current_state_sentence = get_resulting_state(precomputed_resulting_states_with_regex, sent)
shots = "[Winston commented, pointing to the open drawer.] What is the resulting state after this action? Do not add new information. The resulting state after this action is that: No update |\n[\"It was no secret I was neck-deep in debt and Emma was my only salvation,\" he conceded, \"I did seek help from her, but Emma\u2026\" he trailed off, avoiding eye contact.] What is the resulting state after this action? Do not add new information. The resulting state after this action is that: 1. Warren is in debt. 2. Warren asked for Emma’s help. 3. Warren is hiding something regarding Emma. |\n[Winston got the impression that Gregory was worried about something\u2026 He had learned through his observations that Gregory often stayed behind after auctions to go over the inventory.] What is the resulting state after this action? Do not add new information. The resulting state after this action is that: 1. Gregory checks the inventory after auctions. 2. Gregory is worried. |\n[As he walked into the bustling fitness center, he was immediately directed towards Sergio, who was idly flicking through a book on anatomy.] What is the resulting state after this action? Do not add new information. The resulting state after this action is that: 1. Sergio is reading an anatomy book. 2. Winston is in the fitness center. 3. Sergio is in the fitness center. |\n[In the picturesque world of paragliding, hidden secrets tumble as Detective Winston investigates the brutal hatchet murder of Travis, with only Bryan and Everett in the center of the storm.] What is the resulting state after this action? Do not add new information. The resulting state after this action is that: 1. Winston investigates Travis’ murder. 2. Bryan is a suspect. 3. Everett is a suspect. 4. Travis was murdered with a hatchet. |\n"
resulting_state_prompt = shots + "[" + sent + "]" + ". What is the resulting state after this action? Do not add new information. The resulting state after this action is that: "
#print(resulting_state_prompt)
# for resulting state tracking
input_ids = tokenizer.encode(resulting_state_prompt, return_tensors="pt")
output = model.generate(input_ids, max_new_tokens=50)
current_state_sentence = tokenizer.batch_decode(output, skip_special_tokens=True)[0]
current_state_sentence = current_state_sentence[len(resulting_state_prompt):].split('|')[0].strip()
print(current_state_sentence)
# for tracking resulting state generation
pair = {"original": sent, "resulting state": current_state_sentence}
resulting_states.append(pair)
# for tracking openIE triples
pair["triples"] = global_context.add_edges(current_state_sentence, sent)
people = global_context.get_witnesses(sent)
for s_id in sentence_ids_to_remove:
global_context.remove_edges(s_id)
local_context.recursively_update_all_graphs(global_context, people, current_state_sentence, sent)
witnesses_history.append(list(people))
tmp = copy.deepcopy(global_context.to_dict())
tmp['wanli_scores'] = wanli_scores
global_context_history.append(tmp)
local_contexts_history.append(copy.deepcopy(local_context.to_dict()))
if args.plot_graphs:
global_context.plot(f'example_{i}_sentence_{j}_global', 'plots_graph')
local_context.plot(f'example_{i}_sentence_{j}', 'plots_graph')
return witnesses_history, global_context_history, local_contexts_history, resulting_states
class QuestionProcessingModule:
def __init__(self, question, question_type):
self.question = question
self.question_type = question_type
def _get_entities_in_question(self):
"""
Extract people involved in a question. For experiments we used NER
but it is a *complete overkill*: we could have manually extracted the entities just like we do
for memory questions.
"""
entities = ner_predictor.predict(sentence=self.question)
if set(entities['tags']) == set(['O']):
return []
# sometimes people are tagged as U-ORG
if not any(set(entities['tags']) != set([t, 'O']) for t in ['U-PER', 'U-ORG', 'U-LOC']):
print('WARNING: No named entity found', self.question, entities)
entities = [w for w, tag in zip(entities['words'], entities['tags']) if tag in ['U-PER', 'U-ORG', 'U-LOC']]
return entities
def get_relevant_context(self, global_context, local_context):
assert self.question_type != 'memory', "This function may not be called with memory questions"
entities = self._get_entities_in_question()
if len(entities) > local_context['tom_level']:
assert False, 'We cannot respond to this depth of question!'
if len(entities) == 0:
context = global_context['original_sentences']
else:
# if we do not have enough entities, that means we can repeat the last entity "Oliver think that Oliver thinks ..."
diff_entities_tom_depth = local_context['tom_level'] - len(entities)
for _ in range(diff_entities_tom_depth):
entities.append(entities[-1])
for e in entities:
# this shouldn't happen, but we add a fallback to not crash
if e not in local_context['local_context']:
local_context = {'local_context': [], 'original_sentences': []}
print("WARNING: THIS SHOULD NOT HAPPEN IF ACTION_TO_RESULTING_STATE IS HIGH QUALITY")
else:
local_context = local_context['local_context'][e]
context = local_context['original_sentences']
return [s for s in context if s]
def get_relevant_context_for_memory_questions_and_update_question(self, global_context_history):
"""
Get the earliest global graph that mentions the object being asked about and ask a reality question there.
If no graph has a node referring to this object, we return the latest global graph and the original question
by default.
Assumes memory question has the format "Where was the X at the beginning?".
"""
assert self.question_type == 'memory', "This function may only be called with memory questions"
assert self.question.startswith('Where was the ') and self.question.endswith(' at the beginning?')
object_node = self.question[len('Where was the '):-len(' at the beginning?')]
for g in global_context_history:
matched_object = object_node if object_node in g['nodes'] else None
if matched_object is not None:
self.question = f'Where is the {matched_object}?'
return [s for s in g['original_sentences'] if s]
print('WARNING: we did not find a suitable context, defaulting to the last global context available.')
return [s for s in global_context_history[-1] if s]
def rephrase_question_to_be_factual(self):
"""
Heuristically rephrase the question to ask a factual question. In ToMi, questions are phrased as follows:
- First Order Questions: Where will PersonX look for the Object1?
- Second Order Questions: Where does PersonX think that PersonY searches for the Object1?
- Reality Questions: Where is the Object1 really?
- Memory Questions: Where was the Object1 at the beginning?
Reality + Memory Questions stay the same, and First + Second Order Questions
are changed to "Where is the Object1?" (the factual question).
"""
tmp = self.question.split('for')
self.question = 'Where is' + tmp[-1] if len(tmp) > 1 else self.question
def process_question_and_retrieve_relevant_context(self, global_context_history, local_contexts_history):
if self.question_type == 'memory':
relevant_context = self.get_relevant_context_for_memory_questions_and_update_question(global_context_history)
else:
relevant_context = self.get_relevant_context(global_context_history[-1], local_contexts_history[-1])
self.rephrase_question_to_be_factual()
return relevant_context
def main(args):
df1 = loadFileWithCleanQuestionsAndQuestionTypes(args.input_file)
model, tokenizer = load_model(model_name=args.model, cache_dir=args.cache_dir)
#precomputed_resulting_states_with_regex = precomputed_resulting_states_all_models_with_regex[args.resulting_state_model]
logs_directory = f"logs_model_{args.model.split('/')[-1]}_{args.input_file.split('/')[-2]}"
logs_directory += f"_resulting_state_model_{args.resulting_state_model.split('/')[-1]}"
logs_directory += (
'_do_not_filter_sentences_before_answering'
if args.do_not_filter_sentences_before_answering
else '_filter_sentences_before_answering_openIE_split'
)
remaining_questions_by_type = {
question_type: args.max_questions_per_type * (1 if 'tom' in question_type else 2)
for question_type in TOMI_QUESTION_TYPES
}
correct_per_question_type = Counter()
total_per_question_type = Counter()
output_file = []
for i, row in df1.iterrows():
if i % args.max_questions_per_type == 0:
print('STORY #', i)
if remaining_questions_by_type.get(row['qTypeRaw'], 0) == 0:
continue
remaining_questions_by_type[row['qTypeRaw']] -= 1
total_per_question_type[row['qTypeRaw']] += 1
logs_filename = f'example_{i}.json'
# for tracking resulting states
rs_filename = f'resulting_states_{i}.json'
try:
if args.run_symbolictom:
story = [s.strip() for s in row['story'].split('.') if s]
# 0. Precompute all graphs in a story
witnesses_history, global_context_history, local_contexts_history, resulting_states = \
create_all_symbolictom_graphs(story, model, tokenizer)
# 1. Detect entities in the question, Retrieve the relevant belief graph, Perform recursion over the question
question_processing_module = QuestionProcessingModule(row['question'], row['qTypeRaw'])
relevant_local_context = question_processing_module.process_question_and_retrieve_relevant_context(
global_context_history, local_contexts_history)
rephrased_question = question_processing_module.question
# 2. Retrieve sentences captured by the graph
# (Optional) Filter sentences based on entities mentioned in question
if not args.do_not_filter_sentences_before_answering:
stopwords_appearing_in_question = set(['the', 'for'])
words = set(row['question'].strip('?').split()) - stopwords_appearing_in_question
relevant_local_context = [s for s in relevant_local_context if any(w in words for w in s.split())]
reconstructed_story = '. '.join(relevant_local_context)
elif args.load_symbolictom_from_logs:
# Use this param only when you already computed all symbolictom representations and
# you just want to change the final LLM being called without having to recompute everything
all_logs = json.load(open(os.path.join(args.load_symbolictom_from_logs, logs_filename), 'r'))
reconstructed_story = all_logs['reconstructed_story']
rephrased_question = all_logs['rephrased_question']
else:
# Baseline runs
reconstructed_story = row['story'].strip('.')
rephrased_question = row['question']
# 3. Feed to Language Model
prompt, max_length = final_answer_prompt_formatting(args.model, reconstructed_story, rephrased_question)
generation = run_inference(prompt, args.model, model, tokenizer, max_length=max_length)
generated_answer = model_specific_cleaning_main_inference(args.model, generation)
correct_per_question_type[row['qTypeRaw']] += row['answer'] in generated_answer
if args.run_symbolictom:
all_logs = {
'story': story,
'reconstructed_story': reconstructed_story,
'witnesses': witnesses_history,
'global_context': global_context_history,
'local_contexts': local_contexts_history,
'question': row['question'],
'rephrased_question': rephrased_question,
'expected_answer': row['answer'],
'generated_answer': generated_answer,
'relevant_local_context': relevant_local_context,
'question_type': row['qTypeRaw']
}
os.makedirs(logs_directory, exist_ok=True)
json.dump(all_logs, open(os.path.join(logs_directory, logs_filename), 'w'))
json.dump(resulting_states, open(os.path.join(logs_directory, rs_filename), 'w', encoding='utf-8'), ensure_ascii=False, indent=4)
elif args.load_symbolictom_from_logs:
all_logs['generated_answer'] = generated_answer
os.makedirs(logs_directory, exist_ok=True)
json.dump(all_logs, open(os.path.join(logs_directory, logs_filename), 'w'))
except KeyboardInterrupt:
import sys
sys.exit()
except:
print(f'Skipped datapoint #{i}')
traceback.print_exc()
print('Overall Accuracy:', sum(correct_per_question_type.values()) / sum(total_per_question_type.values()))
print('Final Scores by Question Type:')
for k in sorted(total_per_question_type.keys()):
print(k, correct_per_question_type[k] / total_per_question_type[k], total_per_question_type[k])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--run_symbolictom', action='store_true', help='Flag to run SymbolicToM.')
parser.add_argument('--load_symbolictom_from_logs', default=None,
help="""If a path is given, we load SymbolicToM from file instead of computing it."""
"""Useful for fast experimentation when we only want to change --model""")
parser.add_argument('--plot_graphs', action='store_true')
parser.add_argument('--max_questions_per_type', type=int, default=50)
parser.add_argument('--input_file', type=str,
default='data_50k_post_omni_fixed_with_underscores_linguistic_diversity_sent_question/test')
parser.add_argument('--do_not_filter_sentences_before_answering', action='store_true')
parser.add_argument('--model', type=str, default='allenai/macaw-3b',
choices=['text-curie-001', 'text-davinci-002', 'gpt-3.5-turbo', 'gpt-4',
"allenai/macaw-3b", "google/flan-t5-xl", "google/flan-t5-xxl",
"/gscratch/argon/tianxing/llama/converted/7B",
"/gscratch/argon/tianxing/llama/converted/13B", "meta-llama/Llama-2-13b-hf",
"meta-llama/Llama-2-70b-hf"],
help='Model to use to answer final question.'
)
parser.add_argument('--cache_dir', type=str, default='/gscratch/xlab/olo126/.cache')
parser.add_argument('--resulting_state_model', required=True,
choices=['text-curie-001', 'text-davinci-002', 'gpt-3.5-turbo', 'gpt-4',
"allenai/macaw-3b", "google/flan-t5-xl", "google/flan-t5-xxl",
"/gscratch/argon/tianxing/llama/converted/7B",
"/gscratch/argon/tianxing/llama/converted/13B", "meta-llama/Llama-2-13b-hf",
"meta-llama/Llama-2-70b-hf"])
args = parser.parse_args()
assert not (args.load_symbolictom_from_logs and args.run_symbolictom), \
'You cannot simultaneously ask to run symbolictom and load it from a file.'
main(args)