Change the following python gradio app to use a gallery instead of a simple image since…
- User
Change the following python gradio app to use a gallery instead of a simple image since the API now returns multiple image urls like so for example: 'result': {'1': 'https://cdn.leonardo.ai/users/414c5ec8-7021-4736-841b-d5dff9de261f/generations/42c63388-aa8b-483d-b1f5-a3d1ce143f41/Default_french_maid_1.jpg', '2': 'https://cdn.leonardo.ai/users/414c5ec8-7021-4736-841b-d5dff9de261f/generations/42c63388-aa8b-483d-b1f5-a3d1ce143f41/Default_french_maid_0.jpg'}
import requests import gradio as gr # Define the API function def generate_image(prompt, nprompt, style, steps, gd, width, height): api_url = "https://l-d83c.onrender.com/leonardo" payload = { "prompt": prompt, "nprompt": nprompt, "steps": steps, "gd": gd, "width": width, "height": height, "style": style, "alchemy": True, "pr": True } headers = { "accept": "application/json", "content-type": "application/json" } response = requests.post(api_url, json=payload, headers=headers) response.raise_for_status() data = response.json() image_result = data.get("result") print(data) if image_result: return image_result else: return "Failed to generate image" # Create a Gradio interface iface = gr.Interface( fn=generate_image, inputs=[ gr.Textbox(label="Prompt", placeholder="Enter a description for the image"), gr.Textbox(label="Negative Prompt", placeholder="Enter a description to avoid in the image"), gr.Dropdown(["PHOTOGRAPHY", "CINEMATIC", "CREATIVE", "FILM", "LEONARDO"], label="Style", value="LEONARDO"), gr.Slider(label="Steps", minimum=1, maximum=44, step=1, value=44), gr.Slider(label="Guidance", minimum=1, maximum=11, value=5), gr.Slider(label="Width", minimum=512, maximum=2048, step=64, value=832), gr.Slider(label="Height", minimum=512, maximum=2048, step=64, value=1216), ], outputs=[ gr.Image(label="Generated Image") ], live=False ) # Launch the app iface.launch() - Assistant
To modify the Gradio app to use a gallery for displaying multiple images instead of a single image output, you can use the component. We will also need to modify the function to return a list of image URLs to be displayed in the gallery.