Remek commited on
Commit
37b927a
1 Parent(s): 04f9469

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -1,37 +1,56 @@
1
  import gradio as gr
2
  import json
3
  import os
 
4
 
5
  class JSONLViewer:
6
- def __init__(self, file_path):
7
- self.file_path = file_path
 
8
  self.current_index = 0
9
  self.data = []
 
10
  self.load_data()
 
11
 
12
  def load_data(self):
13
- if os.path.exists(self.file_path):
14
- with open(self.file_path, 'r', encoding='utf-8') as file:
15
  self.data = [json.loads(line) for line in file]
16
  else:
17
  self.data = []
18
 
19
- def save_data(self):
20
- with open(self.file_path, 'w', encoding='utf-8') as file:
21
- for item in self.data:
22
- json.dump(item, file, ensure_ascii=False)
23
- file.write('\n')
 
 
 
 
 
24
 
25
  def get_current_record(self):
26
  if 0 <= self.current_index < len(self.data):
27
  return self.data[self.current_index]
28
  return None
29
 
 
 
 
 
 
30
  def update_status(self, status):
31
  current_record = self.get_current_record()
32
  if current_record:
33
- current_record['Status'] = status
34
- self.save_data()
 
 
 
 
 
35
 
36
  def move_prev(self):
37
  if self.current_index > 0:
@@ -43,7 +62,7 @@ class JSONLViewer:
43
  self.current_index += 1
44
  return self.get_current_record()
45
 
46
- viewer = JSONLViewer('plik.jsonl')
47
 
48
  def update_ui(record):
49
  if record:
@@ -52,7 +71,7 @@ def update_ui(record):
52
  rejected = record['rejected']['value'] if 'rejected' in record else ""
53
  chosen_score = record['chosen_score'] if 'chosen_score' in record else ""
54
  rejected_score = record['rejected_score'] if 'rejected_score' in record else ""
55
- status = record.get('Status', "")
56
  return instruction, chosen, rejected, chosen_score, rejected_score, status
57
  return "", "", "", "", "", ""
58
 
@@ -75,9 +94,9 @@ def on_rejected():
75
  return update_ui(record)
76
 
77
  with gr.Blocks(css="button.ok-button { background-color: #4CAF50 !important; }") as demo:
78
- instruction = gr.Textbox(label="INSTRUCTION", lines=2, max_lines=2)
79
- chosen = gr.Textbox(label="CHOSEN", lines=12, max_lines=12)
80
- rejected = gr.Textbox(label="REJECTED", lines=12, max_lines=12)
81
  chosen_score = gr.Number(label="CHOSEN SCORE")
82
  rejected_score = gr.Number(label="REJECTED SCORE")
83
  status = gr.Textbox(label="STATUS")
 
1
  import gradio as gr
2
  import json
3
  import os
4
+ import hashlib
5
 
6
  class JSONLViewer:
7
+ def __init__(self, data_file_path, state_file_path):
8
+ self.data_file_path = data_file_path
9
+ self.state_file_path = state_file_path
10
  self.current_index = 0
11
  self.data = []
12
+ self.state = {}
13
  self.load_data()
14
+ self.load_state()
15
 
16
  def load_data(self):
17
+ if os.path.exists(self.data_file_path):
18
+ with open(self.data_file_path, 'r', encoding='utf-8') as file:
19
  self.data = [json.loads(line) for line in file]
20
  else:
21
  self.data = []
22
 
23
+ def load_state(self):
24
+ if os.path.exists(self.state_file_path):
25
+ with open(self.state_file_path, 'r', encoding='utf-8') as file:
26
+ self.state = json.load(file)
27
+ else:
28
+ self.state = {}
29
+
30
+ def save_state(self):
31
+ with open(self.state_file_path, 'w', encoding='utf-8') as file:
32
+ json.dump(self.state, file, ensure_ascii=False, indent=2)
33
 
34
  def get_current_record(self):
35
  if 0 <= self.current_index < len(self.data):
36
  return self.data[self.current_index]
37
  return None
38
 
39
+ def get_record_id(self, record):
40
+ # Create a unique identifier for the record
41
+ record_str = json.dumps(record, sort_keys=True)
42
+ return hashlib.md5(record_str.encode()).hexdigest()
43
+
44
  def update_status(self, status):
45
  current_record = self.get_current_record()
46
  if current_record:
47
+ record_id = self.get_record_id(current_record)
48
+ self.state[record_id] = status
49
+ self.save_state()
50
+
51
+ def get_status(self, record):
52
+ record_id = self.get_record_id(record)
53
+ return self.state.get(record_id, "")
54
 
55
  def move_prev(self):
56
  if self.current_index > 0:
 
62
  self.current_index += 1
63
  return self.get_current_record()
64
 
65
+ viewer = JSONLViewer('plik.jsonl', 'state.json')
66
 
67
  def update_ui(record):
68
  if record:
 
71
  rejected = record['rejected']['value'] if 'rejected' in record else ""
72
  chosen_score = record['chosen_score'] if 'chosen_score' in record else ""
73
  rejected_score = record['rejected_score'] if 'rejected_score' in record else ""
74
+ status = viewer.get_status(record)
75
  return instruction, chosen, rejected, chosen_score, rejected_score, status
76
  return "", "", "", "", "", ""
77
 
 
94
  return update_ui(record)
95
 
96
  with gr.Blocks(css="button.ok-button { background-color: #4CAF50 !important; }") as demo:
97
+ instruction = gr.Textbox(label="INSTRUCTION", lines=8, max_lines=8)
98
+ chosen = gr.Textbox(label="CHOSEN", lines=8, max_lines=8)
99
+ rejected = gr.Textbox(label="REJECTED", lines=8, max_lines=8)
100
  chosen_score = gr.Number(label="CHOSEN SCORE")
101
  rejected_score = gr.Number(label="REJECTED SCORE")
102
  status = gr.Textbox(label="STATUS")