File size: 2,262 Bytes
ff753c0
 
 
b19335d
 
ff753c0
 
 
 
 
 
 
 
 
 
 
b19335d
ff753c0
 
 
 
 
 
86d103c
 
 
638348e
 
 
 
ff753c0
 
 
b19335d
ff753c0
 
7b4983c
ff753c0
 
 
 
 
86d103c
 
 
 
 
 
 
 
 
 
 
 
 
ff753c0
 
 
 
 
 
b19335d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff753c0
 
b19335d
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
import gradio as gr
import json
import random


# Function to parse the file
def parse_file(filepath):
    with open(filepath) as f:
        lines = f.readlines()
    cleaned_lines = []
    for line in lines:
        if len(line) > 1:
            cleaned_lines.append(line)
    return cleaned_lines


data = parse_file("concatenated_quality-control_011223.json")


def update_index(direction, current_index, output_text):
    if direction == 0:
        # Random number
        new_index = random.randint(0, len(data) - 1)
    elif direction == 10:
        # Next
        new_index = current_index
    else:
        new_index = current_index + direction
        # Ensure the new index is within bounds
        new_index = max(0, min(new_index, len(data) - 1))
    output_text = data[int(new_index)]
    return new_index, output_text


with gr.Blocks() as demo:
    gr.Markdown("# Navigate through the contents of different files")
    output_text = gr.Textbox(label="File Content", value=data[1], show_copy_button=True)
    btn_previous = gr.Button(value="Previous")
    btn_next = gr.Button(value="Next")
    btn_random = gr.Button(value="Random index")

    # Initialize the state
    current_index = gr.Number(
        1,
        label="Current Index",
        visible=True,
    )

    direction_constant = gr.Number(10, visible=False)

    current_index.change(
        update_index,
        inputs=[direction_constant, current_index, output_text],
        outputs=[current_index, output_text],
    )

    # Invisible Number components to pass direction
    direction_previous = gr.Number(-1, visible=False)
    direction_next = gr.Number(1, visible=False)
    random_number = gr.Number(0, label="Random Number", visible=False)

    btn_previous.click(
        update_index,
        inputs=[direction_previous, current_index, output_text],
        outputs=[current_index, output_text],
    )
    btn_next.click(
        update_index,
        inputs=[direction_next, current_index, output_text],
        outputs=[current_index, output_text],
    )
    btn_random.click(
        update_index,
        inputs=[random_number, current_index, output_text],
        outputs=[current_index, output_text],
    )

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