-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequiresRerwrite.py
684 lines (542 loc) · 25.2 KB
/
requiresRerwrite.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
from utils import (
JsonFunctions,
ProgressIndicator,
FontSizeChanger,
EnoughContext,
RequireRewriteCheckBox,
LoadingScreen,
DialogFrame,
MongoData,
NeedsClarificationCheckBox
)
import tkinter as tk
from tkinter import font
import platform
import os
class RequiresRewriteApp:
def __init__(
self,
root,
version,
login={},
):
# Main windows settings
self.root = root
self.root.title("OneAI ReWrite Annotation Software - Only Requires Rewrite")
# Set the minimum size of the window
root.minsize(1200, 800)
self.root.update()
self.fields_check = True
self.save_before_exit = False
# Create a Top Panel Frame for options
top_panel_frame = tk.Frame(root)
top_panel_frame.pack(side=tk.TOP, fill=tk.X)
version_label = tk.Label(top_panel_frame, text=f"Version {version}")
version_label.pack(side=tk.RIGHT, padx=10, pady=10)
# Create Main PanedWindow
main_pane = tk.PanedWindow(root, orient=tk.VERTICAL)
main_pane.pack(fill=tk.BOTH, expand=True)
# "<" (Previous) and ">" (Next) buttons next to each other
prev_button = tk.Button(top_panel_frame, text="<", command=self.prev_turn)
prev_button.pack(side=tk.LEFT, padx=(10, 0), pady=10)
next_button = tk.Button(top_panel_frame, text=">", command=self.next_turn)
next_button.pack(side=tk.LEFT)
# "<<" (Previous Dialog) and ">>" (Next Dialog) buttons
prev_dialog_button = tk.Button(
top_panel_frame, text="<<", command=self.prev_dialog
)
prev_dialog_button.pack(side=tk.LEFT, padx=(10, 0), pady=10)
next_dialog_button = tk.Button(
top_panel_frame, text=">>", command=self.next_dialog
)
next_dialog_button.pack(side=tk.LEFT)
# Save Button at the top
self.save_button = tk.Button(
top_panel_frame, text="Save", command=self.save_json
)
self.save_button.pack(side=tk.RIGHT, pady=10, padx=(0, 10))
# Create status bar frame with a darker background color
self.status_bar = tk.Frame(self.root, bd=1, relief=tk.SUNKEN, height=25, bg="#ebebeb")
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
self.dialog_text = tk.Text(self.status_bar, wrap=tk.WORD, height=1, bg="#ebebeb", bd=0)
self.dialog_text.pack(side=tk.LEFT, fill=tk.X, expand=True)
self.dialog_text.config(state=tk.DISABLED)
# Create a label for the status bar
self.dialog_label = tk.Label(self.status_bar, text="", bg="#ebebeb")
self.dialog_label.pack(side=tk.LEFT, padx=10)
# Next Button at the bottom
self.bottom_next_button = tk.Button(
root, text="Next Turn", command=self.next_turn
)
self.bottom_next_button.pack(side=tk.BOTTOM, pady=10)
self.root.bind("<Return>", self.next_turn)
# Override the window close protocol
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
# Load JSON data
connection_string = "mongodb+srv://ori:CqxF0bLlZoX2OQoD@cluster0.agjlk.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
self.mongo = MongoData(self.root, connection_string, login)
self.json_data = self.mongo.load_file()
self.progress = ProgressIndicator(top_panel_frame, dialog_change_function=self.change_dialog)
self.dialog_frame = DialogFrame(main_pane, root)
self.font = FontSizeChanger(top_panel_frame, root)
self.font.add_exclude_widget(self.dialog_label)
self.require_rewrite = RequireRewriteCheckBox(
main_pane, root, self.update_enough_focus_needs_clarification_state
)
self.enough_context = EnoughContext(main_pane, root)
self.needs_clarification = None
self.quick_annotation = self.quick_annotation_no_clarification_version
if self.mongo.get_needs_clarification():
self.needs_clarification = NeedsClarificationCheckBox(main_pane, root)
self.quick_annotation = self.quick_annotation_needs_clarification_version
self.LoadingScreen = LoadingScreen(root)
if self.json_data == None or self.json_data == "":
raise Exception(f"The json files is Null.\n JSON={self.json_data}")
self.root.bind('<KeyPress>', self.quick_annotation) # Bind the key press event to the quick annotation function
# Load JSON and display data
self.save_counter = 15
self.max_dialog_num = 0
self.current_dialog_num = 0
self.current_turn_num = 0
self.find_next_empty_turn()
self.init_turn()
def change_dialog(self, dialog_num):
"""
Change the current dialog to the specified dialog number.
Args:
dialog_num (int): The dialog number to change to.
Returns:
bool: True if the dialog was successfully changed, False otherwise.
"""
dialog_num -= 1
if int(dialog_num) > len(self.json_data):
tk.messagebox.showerror(
"Error",
f"Dialog {dialog_num + 1} does not exist in the file. Please enter a valid dialog number.",
)
self.update_progress_bar()
return False
elif dialog_num > self.max_dialog_num:
tk.messagebox.showerror(
"Error",
f"Dialog {dialog_num + 1} is not available yet. Please annotate the previous dialogs first.",
)
self.update_progress_bar()
return False
elif dialog_num < 0:
tk.messagebox.showerror(
"Error",
f"Dialog {dialog_num + 1} does not exist in the file. Please enter a valid dialog number.",
)
self.update_progress_bar()
return False
self.current_dialog_num = dialog_num
self.current_turn_num = self.get_first_turn_index()
self.init_turn()
def update_status_bar(self, dialog_id):
"""
Update the status bar with the provided dialog_id.
Parameters:
- dialog_id (str): The ID of the dialog.
Returns:
- None
"""
myfont = font.Font(family="Helvetica", size=9, weight="bold")
# Enable the text widget to update its content
self.dialog_text.config(state=tk.NORMAL)
# Clear the previous content
self.dialog_text.delete(1.0, tk.END)
# Insert the new content
status_text = (
f"Dialog: {dialog_id} | Dialog Index: {self.current_dialog_num} | Turn Index: {self.current_turn_num} | "
f"Total Turns: {self.count_turns_in_dialog()} | Total Dialogs: {self.count_dialogs_in_batch()} | "
f"Next Unfilled Dialog Index: {self.max_dialog_num} | username: {self.mongo.get_username()} | "
f"file: {self.mongo.get_filename()}"
)
self.dialog_text.insert(tk.END, status_text)
# Apply the font
self.dialog_text.tag_configure("font", font=myfont)
self.dialog_text.tag_add("font", 1.0, tk.END)
# Disable the text widget to make it read-only
self.dialog_text.config(state=tk.DISABLED)
def update_progress_bar(self):
"""
Updates the progress bar with the current turn dialog labels.
Parameters:
- json_data (dict): The JSON data containing the dialog information.
- current_dialog_num (int): The current dialog number.
- dialog_id (str): The ID of the dialog.
- current_turn_num (int): The current turn number.
- total_turns (int): The total number of turns in the dialog.
Returns:
None
"""
self.progress.update_current_turn_dialog_labels(
self.json_data,
self.current_dialog_num,
self.get_dialog_id(),
self.current_turn_num,
JsonFunctions.count_turns_in_dialog(self.json_data, self.get_dialog_id()),
)
def other_quick_actions(self, event):
keycodes = []
if platform.system() == "Darwin":
keycodes = {"z": 97, "x": 16777331, "c": 33554532, "left": 2063660802, "right": 2080438019, "up": 2113992448, "down": 2097215233, "q": 201326705}
elif platform.system() == "Windows":
keycodes = {"z": 65, "x": 83, "c": 68, "left": 37, "right": 39, "up": 38, "down": 40, "q": 81}
if event.keycode == keycodes["left"]: # Keycode for left arrow
self.prev_turn()
elif event.keycode == keycodes["right"]: # Keycode for right arrow
self.next_turn()
elif event.keycode == keycodes["up"]: # Keycode for up arrow
self.dialog_frame.scroll_up()
elif event.keycode == keycodes["down"]: # Keycode for down arrow
self.dialog_frame.scroll_down()
def quick_annotation_no_clarification_version(self, event):
"""
Handles quick annotation based on the key pressed.
Args:
event (Event): The event object containing information about the key press.
Returns:
None
"""
keycodes = []
if platform.system() == "Darwin":
keycodes = {"a": 97, "s": 16777331, "d": 33554532, "left": 2063660802, "right": 2080438019, "up": 2113992448, "down": 2097215233, "q": 201326705}
elif platform.system() == "Windows":
keycodes = {"a": 65, "s": 83, "d": 68, "left": 37, "right": 39, "up": 38, "down": 40, "q": 81}
if event.keycode == keycodes["a"]: # Keycode for 'a' on many keyboards
self.require_rewrite.set_requires_rewrite(1)
self.enough_context.set_context(1)
self.next_turn()
elif event.keycode == keycodes["s"]: # Keycode for 's' on many keyboards
self.require_rewrite.set_requires_rewrite(0)
self.enough_context.set_context(1)
self.next_turn()
elif event.keycode == keycodes["d"]: # Keycode for 'd' on many keyboards
self.require_rewrite.set_requires_rewrite(1)
self.enough_context.set_context(0)
self.next_turn()
else:
self.other_quick_actions(event)
def quick_annotation_needs_clarification_version(self, event):
"""
Handles quick annotation based on the key pressed.
Args:
event (Event): The event object containing information about the key press.
Returns:
None
"""
keycodes = []
if platform.system() == "Darwin":
keycodes = {"a": 97, "s": 16777331, "d": 33554532, "left": 2063660802, "right": 2080438019, "up": 2113992448, "down": 2097215233, "q": 201326705}
elif platform.system() == "Windows":
keycodes = {"a": 65, "s": 83, "d": 68, "left": 37, "right": 39, "up": 38, "down": 40, "q": 81}
if event.keycode == keycodes["q"]: # Keycode for 'q' on many keyboards
self.require_rewrite.set_requires_rewrite(0)
self.next_turn()
if event.keycode == keycodes["a"]: # Keycode for 'a' on many keyboards
self.require_rewrite.set_requires_rewrite(1)
self.enough_context.set_context(1)
self.needs_clarification.set_needs_clarification(0)
self.next_turn()
elif event.keycode == keycodes["s"]: # Keycode for 's' on many keyboards
self.require_rewrite.set_requires_rewrite(1)
self.enough_context.set_context(1)
self.needs_clarification.set_needs_clarification(1)
self.next_turn()
elif event.keycode == keycodes["d"]: # Keycode for 'd' on many keyboards
self.require_rewrite.set_requires_rewrite(1)
self.enough_context.set_context(0)
self.needs_clarification.set_needs_clarification(0)
self.next_turn()
else:
self.other_quick_actions(event)
def on_closing(self):
"""This function is called when the user tries to close the program. It checks if the user has saved the file, and if not, it asks the user if they want to save it."""
if self.save_before_exit == False:
return self.root.destroy()
if self.mongo.get_saving_status() == True:
if self.LoadingScreen.is_active() == False:
self.LoadingScreen.show_loading_screen(
message="Program will close automatically after saving is done. Please wait."
)
self.root.after(1000, self.on_closing) # Check again in 1 second
else:
self.mongo.client.close()
self.root.destroy()
def find_next_empty_turn(self):
"""goes through the json_file and finds the next turn which is not filled already, then sets the program to show the turn"""
for dialog_index, dialog_id in enumerate(self.json_data):
dialog_data = self.json_data[dialog_id]
turns = JsonFunctions.get_turns(self.json_data, dialog_id)
for key in turns.keys():
if key.isdigit():
if (
JsonFunctions.get_require_rewrite(
self.json_data, dialog_id, key
)
== None
):
self.current_dialog_num = dialog_index
self.max_dialog_num = dialog_index
self.current_turn_num = int(key)
return
self.current_dialog_num = self.count_dialogs_in_batch() - 1
self.current_turn_num = self.count_turns_in_dialog()
self.max_dialog_num = self.count_dialogs_in_batch() - 1
def are_all_fields_filled(self):
"""check if the turn the annotator is currently on is saved comletly, used before moving to the next turn
Returns:
boolean: True if everything is filled, False if not.
"""
missing_fields = []
if self.require_rewrite.is_empty():
missing_fields.append("Requires-Rewrite")
if self.require_rewrite.get_requires_rewrite() != 0:
if self.enough_context.is_empty():
missing_fields.append("Enough-Context")
if self.needs_clarification:
if self.needs_clarification.is_empty():
missing_fields.append("Needs-Clarification")
if missing_fields and self.fields_check:
tk.messagebox.showwarning(
"Warning",
"The following fields are missing: "
+ ", ".join(missing_fields)
+ ". Please fill them in before proceeding.",
)
return False
return True
def save_json(self):
self.LoadingScreen.show_loading_screen(message="Saving your progress...")
self.update_json()
finished = False
self.LoadingScreen.close_loading_screen()
if self.mongo.save_to_mongo(self.json_data, self.get_dialog_id()) == False:
tk.messagebox.showerror(
"Error",
"An error occurred while saving the file. Do not close the app, and contact Ori.",
)
else:
finished = True
if finished == True:
tk.messagebox.showinfo("Success", "The file was saved successfully.")
def update_json(self, prev=False):
"""updates the json_file inside the Data class (MongoDB or JsonHandler), to be saved later
Raises:
MemoryError: Raises when using online mode, and the annotation was not saved correctly in MongoDB
Returns:
boolean: Return True if opertion was successful, False if not
"""
self.json_data = self.require_rewrite.update_json_data(
self.get_dialog_id(), self.current_turn_num, self.json_data
)
self.json_data = self.enough_context.update_json_data(
self.get_dialog_id(), self.current_turn_num, self.json_data
)
if self.needs_clarification:
self.json_data = self.needs_clarification.update_json_data(
self.get_dialog_id(), self.current_turn_num, self.json_data
)
return True
def get_dialog_id(self):
"""simply gets the string of the dialog_id using the current num of the dialog in the batch file
Returns:
string: the dialog_id
"""
return JsonFunctions.get_dialog_id(self.json_data, self.current_dialog_num)
def init_turn(self):
"""
Initializes a new turn in the GUI application.
This method performs the following tasks:
- Prints the progress string indicating the current turn and dialog number.
- Updates the current turn and dialog labels in the progress bar.
- Displays the dialog in the dialog frame.
- Updates the entry text for the "require rewrite" field.
- Updates the entry text for the "enough context" field.
- Updates the font size.
- Sets focus on the "require rewrite" field.
- Updates the maximum dialog number if necessary.
- Updates the status bar.
Parameters:
None
Returns:
None
"""
progress_string = (
f"Turn={self.current_turn_num+1} | Dialog={self.current_dialog_num+1}"
)
print(progress_string)
self.save_counter += 1
self.progress.update_current_turn_dialog_labels(
self.json_data,
self.current_dialog_num,
self.get_dialog_id(),
self.current_turn_num,
JsonFunctions.count_turns_in_dialog(self.json_data, self.get_dialog_id()),
)
self.dialog_frame.display_dialog(
self.get_dialog_id(), self.current_turn_num, self.json_data
)
self.require_rewrite.update_entry_text(
self.get_dialog_id(), self.current_turn_num, self.json_data
)
self.enough_context.update_entry_text(
self.get_dialog_id(), self.current_turn_num, self.json_data
)
if self.needs_clarification: # If the needs_clarification field is present
self.needs_clarification.update_entry_text(
self.get_dialog_id(), self.current_turn_num, self.json_data
)
self.font.update_font_size_wrapper()
self.require_rewrite.focus_on()
if self.current_dialog_num > self.max_dialog_num:
self.max_dialog_num = self.current_dialog_num
self.update_status_bar(self.get_dialog_id())
def get_first_turn_index(self):
"""
Returns the index of the first turn in the JSON data for the current dialog.
Returns:
int: The index of the first turn.
"""
return JsonFunctions.first_turn(self.json_data, self.get_dialog_id())
def get_original_question(self):
"""
Retrieves the original question from the dialog data based on the current turn number.
Returns:
str: The original question from the dialog data.
"""
return JsonFunctions.get_original_question(
self.json_data, self.get_dialog_id(), self.current_turn_num
)
def count_turns_in_dialog(self):
"""count the number of turn in the dialog
Returns:
int: number of turns in dialog
"""
return JsonFunctions.count_turns_in_dialog(self.json_data, self.get_dialog_id())
def count_dialogs_in_batch(self):
"""count the number of dialogs in the batch file
Returns:
int: number of dialogs in batch
"""
return JsonFunctions.count_dialogs_in_batch(self.json_data)
def prev_turn(self):
"""goes to the previous turn in the dialog
if there are no more turns, go to the prev dialog,
if there are no more dialogs and using mongo, goes to prev batch (if offline need to manually change target.json)
Returns:
boolean: Return True if opertion was successful, False if not
"""
if not self.update_json(prev=True):
return False
if self.current_turn_num > JsonFunctions.first_turn(
self.json_data, self.get_dialog_id()
):
self.current_turn_num -= 1
self.init_turn()
else:
self.prev_dialog()
return True
def next_turn(self, event=None):
"""goes to the previous turn in the dialog
if there are no more turns, go to the next dialog,
if there are no more dialogs and using mongo, goes to next batch (if offline need to manually change target.json)
Returns:
boolean: Return True if opertion was successful, False if not
"""
focused_widget = self.root.focus_get()
if focused_widget == self.progress.get_widget():
self.root.focus_set()
return True
if not self.are_all_fields_filled():
return False
elif not self.update_json():
return False
self.mongo.save_json(json_data=self.json_data, dialog_id=self.get_dialog_id())
if self.current_turn_num < JsonFunctions.last_turn(
self.json_data, self.get_dialog_id()
):
self.current_turn_num += 1
self.init_turn()
else:
self.next_dialog()
return True
def prev_dialog(self):
"""used in the prev dialog button to go to prev dialog"""
if self.current_dialog_num > 0:
if not self.require_rewrite.is_empty():
self.update_json()
self.current_dialog_num -= 1
self.current_turn_num = JsonFunctions.last_turn(
self.json_data, self.get_dialog_id()
)
self.init_turn()
self.font.update_font_size_wrapper()
else:
tk.messagebox.showwarning("Warning", "This is the first dialog")
def next_dialog(self):
"""used in the next dialog button to go to prev dialog"""
if self.current_dialog_num < len(self.json_data) - 1:
if self.fields_check:
if self.are_all_turns_filled():
if not self.require_rewrite.is_empty():
self.update_json()
self.current_dialog_num += 1
self.current_turn_num = self.get_first_turn_index()
self.init_turn()
else:
tk.messagebox.showwarning(
"Warning", "Not all turns in this dialog are filled"
)
else:
self.update_json()
self.current_dialog_num += 1
self.current_turn_num = self.get_first_turn_index()
self.init_turn()
else:
tk.messagebox.showinfo(
title="Finished Annotating!", message="No More Annotations", icon="info"
)
def are_all_turns_filled(self):
"""when going to the next dialog using the button, checks if all the turns in the dialog are filled
Returns:
boolean: Return True if opertion was successful, False if not
"""
turns = JsonFunctions.get_turns(self.json_data, self.get_dialog_id())
for turn in turns.values():
if (
JsonFunctions.get_require_rewrite(
self.json_data, self.get_dialog_id(), self.current_turn_num
)
is None
):
return False
return True
def update_enough_focus_needs_clarification_state(self):
"""
Update the focus state of the 'enough_context' based on the value of 'require_rewrite' choice variable.
If the 'require_rewrite' choice variable is 0, the 'enough_context' is set to a focused state.
Otherwise, the 'enough_context' is set to a normal state.
Args:
None
Returns:
None
"""
if self.require_rewrite.choice_var.get() == 0:
self.enough_context.choice_var.set(-1)
self.enough_context.circle1.config(state="disabled")
self.enough_context.circle2.config(state="disabled")
if self.needs_clarification:
self.needs_clarification.choice_var.set(-1)
self.needs_clarification.circle1.config(state="disabled")
self.needs_clarification.circle2.config(state="disabled")
else:
self.enough_context.circle1.config(state="normal")
self.enough_context.circle2.config(state="normal")
self.enough_context.choice_var.set(-1)
if self.needs_clarification:
self.needs_clarification.circle1.config(state="normal")
self.needs_clarification.circle2.config(state="normal")
self.needs_clarification.choice_var.set(-1)