Spaces:
Sleeping
Sleeping
File size: 1,919 Bytes
987344a 4f85ac9 fbba7f0 987344a fbba7f0 98eb761 1766620 98eb761 987344a 4f85ac9 987344a 98eb761 987344a 4f85ac9 987344a 4f85ac9 0f6c29c 987344a 0f6c29c 4f85ac9 987344a 4f85ac9 987344a 4f85ac9 987344a e5ef5c7 |
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 53 54 55 56 57 58 59 60 61 62 |
import gradio as gr
from diffusers import StableDiffusionXLPipeline
import torch
# Cache model between generations
pipe = None
def load_model():
global pipe
if pipe is None:
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipe = StableDiffusionXLPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True
)
if torch.cuda.is_available():
pipe.to("cuda")
# Enable memory optimizations
pipe.enable_attention_slicing()
pipe.enable_model_cpu_offload()
return pipe
def generate_image(prompt, negative_prompt, steps=30, guidance_scale=7.5):
pipe = load_model()
# Generate image with reduced resolution for stability
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=int(steps),
guidance_scale=guidance_scale,
width=1024,
height=1024,
).images[0]
return image
# Optimized Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# 🖼️ SDXL Image Generator (Stable)")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(label="Prompt", placeholder="Describe your image...", max_lines=2)
negative_prompt = gr.Textbox(label="Negative Prompt", value="low quality, blurry", max_lines=2)
with gr.Row():
steps = gr.Slider(10, 50, value=30, label="Steps")
guidance = gr.Slider(1.0, 15.0, value=7.5, label="Guidance Scale")
submit = gr.Button("Generate", variant="primary")
with gr.Column():
output = gr.Image(label="Result", height=512)
submit.click(
generate_image,
inputs=[prompt, negative_prompt, steps, guidance],
outputs=output
)
if __name__ == "__main__":
demo.launch()
|