Spaces:
Runtime error
Runtime error
import gradio as gr | |
from PIL import Image | |
from diffusers import DiffusionPipeline | |
# Load model and scheduler | |
ldm = DiffusionPipeline.from_pretrained("CompVis/ldm-text2im-large-256") | |
def generate_image(prompt, negative_prompt="Low quality", width=512, height=512, num_steps=50, eta=0.3, guidance_scale=6): | |
# Run pipeline in inference (sample random noise and denoise) | |
images = ldm([prompt], num_inference_steps=num_steps, eta=eta, guidance_scale=guidance_scale, negative_prompts=[negative_prompt]).images | |
# Resize image to desired width and height | |
resized_images = [image.resize((width, height)) for image in images] | |
# Save images | |
for idx, image in enumerate(resized_images): | |
image.save(f"squirrel-{idx}.png") | |
return "Images generated successfully!" | |
# Define the interface | |
iface = gr.Interface( | |
fn=generate_image, | |
inputs=[ | |
gr.inputs.Textbox(label="Prompt", lines=2), | |
gr.inputs.Textbox(label="Negative Prompt (optional)", lines=2), | |
gr.inputs.Number(label="Width (optional)", default=512), | |
gr.inputs.Number(label="Height (optional)", default=512), | |
gr.inputs.Number(label="Number of Inference Steps (optional)", default=50), | |
gr.inputs.Number(label="Eta (optional)", default=0.3), | |
gr.inputs.Number(label="Guidance Scale (optional)", default=6), | |
], | |
outputs="text", | |
layout="vertical", | |
title="Image Generation", | |
description="Generate images based on prompts. Adjust the optional parameters for customization. The generated images will be resized to the specified width and height.", | |
article="Check out the documentation for more information: [link](https://docs.gradio.app/)", | |
examples=[ | |
["A painting of a squirrel eating a burger", "Low quality", 512, 512, 50, 0.3, 6], | |
["A breathtaking landscape with mountains", "Blurry", 800, 600, 30, 0.5, 8], | |
["An abstract artwork with vibrant colors", "Dull", 1024, 768, 70, 0.2, 10], | |
], | |
) | |
# Configure styling options | |
iface.configure( | |
label_font="Arial", | |
label_font_size=18, | |
border_width=2, | |
border_color="blue", | |
button_bg_color="lightblue", | |
button_text_color="black", | |
) | |
# Launch the interface | |
iface.launch() |