# This is a Gradio app that generates random images and adds them to a gallery. import gradio as gr import numpy as np # Function to generate a new image def generate_image(): # Generate a random solid color image image = np.ones((100, 100, 3), dtype=np.uint8) * np.random.randint(0, 255, 3) return image # Function to add a new image to the gallery def add_image_to_gallery(new_image, gallery): # Append the new image to the existing gallery gallery.append(new_image) return gallery # Initialize the gallery with no images initial_gallery = [] # Create the Gradio interface with gr.Blocks() as demo: # Create a state variable to store the gallery gallery_state = gr.State(initial_gallery) # Create the gallery component gallery = gr.Gallery( label="Generated Images", show_label=True, columns=[1], rows=[10], height="400px", allow_preview=True ) # Create a button to generate a new image generate_button = gr.Button("Generate Image") # When the button is clicked, generate a new image and add it to the gallery generate_button.click( fn=lambda: add_image_to_gallery(generate_image(), gallery_state.value), outputs=[gallery_state] ) # Update the gallery component with the new state gallery_state.change( fn=lambda x: x, inputs=gallery_state, outputs=gallery ) # Launch the interface demo.launch(show_error=True)