Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,37 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load the Stable Diffusion model
|
| 6 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 7 |
+
pipeline = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 8 |
+
pipeline = pipeline.to("cuda")
|
| 9 |
+
|
| 10 |
+
def generate_image(prompt, num_inference_steps=50, guidance_scale=7.5):
|
| 11 |
+
"""Generate an image from a text prompt using Stable Diffusion."""
|
| 12 |
+
image = pipeline(prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale).images[0]
|
| 13 |
+
return image
|
| 14 |
+
|
| 15 |
+
# Define the Gradio app layout
|
| 16 |
+
with gr.Blocks() as app:
|
| 17 |
+
gr.Markdown("# Stable Diffusion v1.5 Gradio App\nGenerate stunning images from text prompts!")
|
| 18 |
+
|
| 19 |
+
with gr.Row():
|
| 20 |
+
with gr.Column():
|
| 21 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here", lines=2)
|
| 22 |
+
num_inference_steps = gr.Slider(label="Inference Steps", minimum=10, maximum=100, value=50, step=5)
|
| 23 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, value=7.5, step=0.5)
|
| 24 |
+
generate_btn = gr.Button("Generate Image")
|
| 25 |
+
|
| 26 |
+
with gr.Column():
|
| 27 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
| 28 |
+
|
| 29 |
+
generate_btn.click(
|
| 30 |
+
generate_image,
|
| 31 |
+
inputs=[prompt, num_inference_steps, guidance_scale],
|
| 32 |
+
outputs=output_image
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Run the app
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
app.launch()
|