import gradio as gr import os # Get all images in the folder, sorted by filename image_dir = "/tmp/video/frames" image_paths = sorted([ os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith(".jpg") ]) def get_image(index): # Make sure the index is within bounds if 0 <= index < len(image_paths): return image_paths[index], index return None, index # No change if out of bounds def next_image(current_index): next_index = current_index + 1 return get_image(next_index) def previous_image(current_index): prev_index = current_index - 1 return get_image(prev_index) # with gr.Blocks() as demo: # gr.Markdown("### Browse Keyframes") # image = gr.Image() # index_state = gr.State(0) # Track current index # with gr.Row(): # prev_btn = gr.Button("Previous") # next_btn = gr.Button("Next") # next_btn.click(fn=next_image, inputs=index_state, outputs=[image, index_state]) # prev_btn.click(fn=previous_image, inputs=index_state, outputs=[image, index_state]) # # Initialize with first image # demo.load(fn=get_image, inputs=index_state, outputs=[image, index_state]) # demo.launch()