Remek commited on
Commit
2c4ffd2
1 Parent(s): 7e788c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -39
app.py CHANGED
@@ -1,36 +1,29 @@
1
  import gradio as gr
2
  import json
3
- import os
4
  import hashlib
5
- import tempfile
 
6
 
7
  class JSONLViewer:
8
- def __init__(self, data_file_path):
9
  self.data_file_path = data_file_path
10
- self.state_file_path = os.path.join(tempfile.gettempdir(), 'app_state.json')
11
  self.current_index = 0
12
  self.data = []
13
- self.state = {}
14
  self.load_data()
15
- self.load_state()
16
 
17
  def load_data(self):
18
- if os.path.exists(self.data_file_path):
19
- with open(self.data_file_path, 'r', encoding='utf-8') as file:
20
- self.data = [json.loads(line) for line in file]
21
- else:
22
- self.data = []
23
-
24
- def load_state(self):
25
- if os.path.exists(self.state_file_path):
26
- with open(self.state_file_path, 'r', encoding='utf-8') as file:
27
- self.state = json.load(file)
28
- else:
29
- self.state = {}
30
-
31
- def save_state(self):
32
- with open(self.state_file_path, 'w', encoding='utf-8') as file:
33
- json.dump(self.state, file, ensure_ascii=False, indent=2)
34
 
35
  def get_current_record(self):
36
  if 0 <= self.current_index < len(self.data):
@@ -41,16 +34,21 @@ class JSONLViewer:
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,7 +60,7 @@ class JSONLViewer:
62
  self.current_index += 1
63
  return self.get_current_record()
64
 
65
- viewer = JSONLViewer('plik.jsonl')
66
 
67
  def update_ui(record):
68
  if record:
@@ -71,7 +69,8 @@ def update_ui(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
 
@@ -84,19 +83,23 @@ def on_next():
84
  return update_ui(record)
85
 
86
  def on_ok():
87
- viewer.update_status("ok")
88
  record = viewer.get_current_record()
 
 
 
89
  return update_ui(record)
90
 
91
  def on_rejected():
92
- viewer.update_status("rejected")
93
  record = viewer.get_current_record()
 
 
 
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=2, max_lines=2)
98
- chosen = gr.Textbox(label="CHOSEN", lines=12, max_lines=12)
99
- rejected = gr.Textbox(label="REJECTED", lines=12, max_lines=12)
100
  chosen_score = gr.Number(label="CHOSEN SCORE")
101
  rejected_score = gr.Number(label="REJECTED SCORE")
102
  status = gr.Textbox(label="STATUS")
 
1
  import gradio as gr
2
  import json
 
3
  import hashlib
4
+ import sqlite3
5
+ import os
6
 
7
  class JSONLViewer:
8
+ def __init__(self, data_file_path, db_path):
9
  self.data_file_path = data_file_path
10
+ self.db_path = db_path
11
  self.current_index = 0
12
  self.data = []
 
13
  self.load_data()
14
+ self.init_db()
15
 
16
  def load_data(self):
17
+ with open(self.data_file_path, 'r', encoding='utf-8') as file:
18
+ self.data = [json.loads(line) for line in file]
19
+
20
+ def init_db(self):
21
+ conn = sqlite3.connect(self.db_path)
22
+ c = conn.cursor()
23
+ c.execute('''CREATE TABLE IF NOT EXISTS states
24
+ (record_id TEXT PRIMARY KEY, status TEXT)''')
25
+ conn.commit()
26
+ conn.close()
 
 
 
 
 
 
27
 
28
  def get_current_record(self):
29
  if 0 <= self.current_index < len(self.data):
 
34
  record_str = json.dumps(record, sort_keys=True)
35
  return hashlib.md5(record_str.encode()).hexdigest()
36
 
37
+ def get_status(self, record_id):
38
+ conn = sqlite3.connect(self.db_path)
39
+ c = conn.cursor()
40
+ c.execute("SELECT status FROM states WHERE record_id = ?", (record_id,))
41
+ result = c.fetchone()
42
+ conn.close()
43
+ return result[0] if result else ""
44
+
45
+ def set_status(self, record_id, status):
46
+ conn = sqlite3.connect(self.db_path)
47
+ c = conn.cursor()
48
+ c.execute("INSERT OR REPLACE INTO states (record_id, status) VALUES (?, ?)",
49
+ (record_id, status))
50
+ conn.commit()
51
+ conn.close()
52
 
53
  def move_prev(self):
54
  if self.current_index > 0:
 
60
  self.current_index += 1
61
  return self.get_current_record()
62
 
63
+ viewer = JSONLViewer('plik.jsonl', 'states.db')
64
 
65
  def update_ui(record):
66
  if record:
 
69
  rejected = record['rejected']['value'] if 'rejected' in record else ""
70
  chosen_score = record['chosen_score'] if 'chosen_score' in record else ""
71
  rejected_score = record['rejected_score'] if 'rejected_score' in record else ""
72
+ record_id = viewer.get_record_id(record)
73
+ status = viewer.get_status(record_id)
74
  return instruction, chosen, rejected, chosen_score, rejected_score, status
75
  return "", "", "", "", "", ""
76
 
 
83
  return update_ui(record)
84
 
85
  def on_ok():
 
86
  record = viewer.get_current_record()
87
+ if record:
88
+ record_id = viewer.get_record_id(record)
89
+ viewer.set_status(record_id, "ok")
90
  return update_ui(record)
91
 
92
  def on_rejected():
 
93
  record = viewer.get_current_record()
94
+ if record:
95
+ record_id = viewer.get_record_id(record)
96
+ viewer.set_status(record_id, "rejected")
97
  return update_ui(record)
98
 
99
  with gr.Blocks(css="button.ok-button { background-color: #4CAF50 !important; }") as demo:
100
+ instruction = gr.Textbox(label="INSTRUCTION", lines=8, max_lines=8)
101
+ chosen = gr.Textbox(label="CHOSEN", lines=8, max_lines=8)
102
+ rejected = gr.Textbox(label="REJECTED", lines=8, max_lines=8)
103
  chosen_score = gr.Number(label="CHOSEN SCORE")
104
  rejected_score = gr.Number(label="REJECTED SCORE")
105
  status = gr.Textbox(label="STATUS")