File size: 3,985 Bytes
72c7698
 
6bbb741
72c7698
 
 
 
 
 
 
 
 
6bbb741
 
 
 
 
72c7698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bbb741
7605281
 
 
72c7698
 
 
 
 
 
 
6bbb741
 
72c7698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bbb741
 
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
import gradio as gr
import json
import os

class JSONLViewer:
    def __init__(self, file_path):
        self.file_path = file_path
        self.current_index = 0
        self.data = []
        self.load_data()

    def load_data(self):
        if os.path.exists(self.file_path):
            with open(self.file_path, 'r', encoding='utf-8') as file:
                self.data = [json.loads(line) for line in file]
        else:
            self.data = []

    def save_data(self):
        with open(self.file_path, 'w', encoding='utf-8') as file:
            for item in self.data:
                json.dump(item, file, ensure_ascii=False)
                file.write('\n')

    def get_current_record(self):
        if 0 <= self.current_index < len(self.data):
            return self.data[self.current_index]
        return None

    def update_status(self, status):
        current_record = self.get_current_record()
        if current_record:
            current_record['Status'] = status
            self.save_data()

    def move_prev(self):
        if self.current_index > 0:
            self.current_index -= 1
        return self.get_current_record()

    def move_next(self):
        if self.current_index < len(self.data) - 1:
            self.current_index += 1
        return self.get_current_record()

viewer = JSONLViewer('plik.jsonl')

def update_ui(record):
    if record:
        instruction = record['conversations'][0]['value'] if record['conversations'] else ""
        chosen = record['chosen']['value'] if 'chosen' in record else ""
        rejected = record['rejected']['value'] if 'rejected' in record else ""
        chosen_score = record['chosen_score'] if 'chosen_score' in record else ""
        rejected_score = record['rejected_score'] if 'rejected_score' in record else ""
        status = record.get('Status', "")
        return instruction, chosen, rejected, chosen_score, rejected_score, status
    return "", "", "", "", "", ""

def on_prev():
    record = viewer.move_prev()
    return update_ui(record)

def on_next():
    record = viewer.move_next()
    return update_ui(record)

def on_ok():
    viewer.update_status("ok")
    record = viewer.get_current_record()
    return update_ui(record)

def on_rejected():
    viewer.update_status("rejected")
    record = viewer.get_current_record()
    return update_ui(record)

with gr.Blocks(css="button.ok-button { background-color: #4CAF50 !important; }") as demo:
    instruction = gr.Textbox(label="INSTRUCTION", lines=8, max_lines=8)
    chosen = gr.Textbox(label="CHOSEN", lines=8, max_lines=8)
    rejected = gr.Textbox(label="REJECTED", lines=8, max_lines=8)
    chosen_score = gr.Number(label="CHOSEN SCORE")
    rejected_score = gr.Number(label="REJECTED SCORE")
    status = gr.Textbox(label="STATUS")

    with gr.Row():
        prev_btn = gr.Button("PREV")
        next_btn = gr.Button("NEXT")
        ok_btn = gr.Button("OK", elem_classes="ok-button")
        rejected_btn = gr.Button("REJECTED", variant="stop")

    prev_btn.click(on_prev, outputs=[instruction, chosen, rejected, chosen_score, rejected_score, status])
    next_btn.click(on_next, outputs=[instruction, chosen, rejected, chosen_score, rejected_score, status])
    ok_btn.click(on_ok, outputs=[instruction, chosen, rejected, chosen_score, rejected_score, status])
    rejected_btn.click(on_rejected, outputs=[instruction, chosen, rejected, chosen_score, rejected_score, status])

    # Initialize UI with first record
    initial_record = viewer.get_current_record()
    if initial_record:
        init_instruction, init_chosen, init_rejected, init_chosen_score, init_rejected_score, init_status = update_ui(initial_record)
        instruction.value = init_instruction
        chosen.value = init_chosen
        rejected.value = init_rejected
        chosen_score.value = init_chosen_score
        rejected_score.value = init_rejected_score
        status.value = init_status

if __name__ == "__main__":
    demo.launch()