Remek commited on
Commit
72c7698
1 Parent(s): 47a8967

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+
4
+ class JSONLViewer:
5
+ def __init__(self, file_path):
6
+ self.file_path = file_path
7
+ self.current_index = 0
8
+ self.data = []
9
+ self.load_data()
10
+
11
+ def load_data(self):
12
+ with open(self.file_path, 'r', encoding='utf-8') as file:
13
+ self.data = [json.loads(line) for line in file]
14
+
15
+ def save_data(self):
16
+ with open(self.file_path, 'w', encoding='utf-8') as file:
17
+ for item in self.data:
18
+ json.dump(item, file, ensure_ascii=False)
19
+ file.write('\n')
20
+
21
+ def get_current_record(self):
22
+ if 0 <= self.current_index < len(self.data):
23
+ return self.data[self.current_index]
24
+ return None
25
+
26
+ def update_status(self, status):
27
+ current_record = self.get_current_record()
28
+ if current_record:
29
+ current_record['Status'] = status
30
+ self.save_data()
31
+
32
+ def move_prev(self):
33
+ if self.current_index > 0:
34
+ self.current_index -= 1
35
+ return self.get_current_record()
36
+
37
+ def move_next(self):
38
+ if self.current_index < len(self.data) - 1:
39
+ self.current_index += 1
40
+ return self.get_current_record()
41
+
42
+ viewer = JSONLViewer('plik.jsonl')
43
+
44
+ def update_ui(record):
45
+ if record:
46
+ instruction = record['conversations'][0]['value'] if record['conversations'] else ""
47
+ chosen = record['chosen']['value'] if 'chosen' in record else ""
48
+ rejected = record['rejected']['value'] if 'rejected' in record else ""
49
+ chosen_score = record['chosen_score'] if 'chosen_score' in record else ""
50
+ rejected_score = record['rejected_score'] if 'rejected_score' in record else ""
51
+ status = record.get('Status', "")
52
+ return instruction, chosen, rejected, chosen_score, rejected_score, status
53
+ return "", "", "", "", "", ""
54
+
55
+ def on_prev():
56
+ record = viewer.move_prev()
57
+ return update_ui(record)
58
+
59
+ def on_next():
60
+ record = viewer.move_next()
61
+ return update_ui(record)
62
+
63
+ def on_ok():
64
+ viewer.update_status("ok")
65
+ record = viewer.get_current_record()
66
+ return update_ui(record)
67
+
68
+ def on_rejected():
69
+ viewer.update_status("rejected")
70
+ record = viewer.get_current_record()
71
+ return update_ui(record)
72
+
73
+ with gr.Blocks() as demo:
74
+ instruction = gr.Markdown(label="Instruction")
75
+ chosen = gr.Markdown(label="CHOSEN")
76
+ rejected = gr.Markdown(label="REJECTED")
77
+ chosen_score = gr.Number(label="CHOSEN SCORE")
78
+ rejected_score = gr.Number(label="REJECTED SCORE")
79
+ status = gr.Textbox(label="STATUS")
80
+
81
+ with gr.Row():
82
+ prev_btn = gr.Button("PREV")
83
+ next_btn = gr.Button("NEXT")
84
+ ok_btn = gr.Button("OK")
85
+ rejected_btn = gr.Button("REJECTED")
86
+
87
+ prev_btn.click(on_prev, outputs=[instruction, chosen, rejected, chosen_score, rejected_score, status])
88
+ next_btn.click(on_next, outputs=[instruction, chosen, rejected, chosen_score, rejected_score, status])
89
+ ok_btn.click(on_ok, outputs=[instruction, chosen, rejected, chosen_score, rejected_score, status])
90
+ rejected_btn.click(on_rejected, outputs=[instruction, chosen, rejected, chosen_score, rejected_score, status])
91
+
92
+ # Initialize UI with first record
93
+ initial_record = viewer.get_current_record()
94
+ if initial_record:
95
+ init_instruction, init_chosen, init_rejected, init_chosen_score, init_rejected_score, init_status = update_ui(initial_record)
96
+ instruction.value = init_instruction
97
+ chosen.value = init_chosen
98
+ rejected.value = init_rejected
99
+ chosen_score.value = init_chosen_score
100
+ rejected_score.value = init_rejected_score
101
+ status.value = init_status
102
+
103
+ demo.launch()