Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionXLPipeline, DPMSolverSinglestepScheduler
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load the Stable Diffusion XL model
|
| 7 |
+
def load_model():
|
| 8 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 9 |
+
"sd-community/sdxl-flash",
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
low_cpu_mem_usage=True
|
| 12 |
+
)# .to("cuda")
|
| 13 |
+
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(
|
| 14 |
+
pipe.scheduler.config,
|
| 15 |
+
timestep_spacing="trailing"
|
| 16 |
+
)
|
| 17 |
+
return pipe
|
| 18 |
+
|
| 19 |
+
# Function to generate images
|
| 20 |
+
def generate_image(prompt, num_inference_steps=3, guidance_scale=1):
|
| 21 |
+
pipe = load_model()
|
| 22 |
+
image = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale).images[0]
|
| 23 |
+
return image
|
| 24 |
+
|
| 25 |
+
# Create the Gradio interface
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=generate_image,
|
| 28 |
+
inputs=[
|
| 29 |
+
gr.Textbox(label="Enter a prompt for the image"),
|
| 30 |
+
# gr.Slider(minimum=1, maximum=50, label="Number of Inference Steps"),
|
| 31 |
+
# gr.Slider(minimum=1, maximum=20, label="Guidance Scale")
|
| 32 |
+
],
|
| 33 |
+
outputs="image",
|
| 34 |
+
title="Stable Diffusion XL Text-to-Image",
|
| 35 |
+
description="Generate images from text prompts using Stable Diffusion XL."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Launch the Gradio app
|
| 39 |
+
iface.launch()
|