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)