File size: 1,501 Bytes
8c7fe43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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)