Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,129 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
| 9 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 10 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32)
|
| 11 |
-
pipe = pipe.to(device)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Set random seed if provided
|
|
|
|
| 18 |
if seed != -1:
|
| 19 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 20 |
-
else:
|
| 21 |
-
generator = None
|
| 22 |
|
| 23 |
-
# Generate image
|
| 24 |
-
with torch.autocast(device):
|
| 25 |
image = pipe(
|
| 26 |
prompt=prompt,
|
| 27 |
negative_prompt=negative_prompt,
|
| 28 |
num_inference_steps=int(steps),
|
| 29 |
guidance_scale=guidance_scale,
|
| 30 |
-
generator=generator
|
|
|
|
|
|
|
| 31 |
).images[0]
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# Gradio interface
|
| 36 |
-
with gr.Blocks() as demo:
|
| 37 |
-
gr.Markdown("
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
with gr.Row():
|
| 41 |
with gr.Column():
|
| 42 |
-
prompt = gr.Textbox(
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
with gr.Column():
|
| 50 |
-
output_image = gr.Image(label="Generated Image", type="pil")
|
| 51 |
-
|
| 52 |
-
generate_btn.click(
|
| 53 |
-
fn=generate_image,
|
| 54 |
-
inputs=[prompt, negative_prompt, steps, guidance_scale, seed],
|
| 55 |
-
outputs=output_image
|
| 56 |
-
)
|
| 57 |
|
|
|
|
| 58 |
gr.Examples(
|
| 59 |
examples=[
|
| 60 |
-
["A futuristic
|
| 61 |
-
["A cute
|
| 62 |
-
["
|
| 63 |
],
|
| 64 |
inputs=[prompt, negative_prompt, steps, guidance_scale, seed],
|
| 65 |
-
outputs=output_image,
|
| 66 |
fn=generate_image,
|
| 67 |
cache_examples=True
|
| 68 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
|
| 3 |
import torch
|
| 4 |
+
from datetime import datetime
|
| 5 |
|
| 6 |
+
# Optimization settings
|
| 7 |
+
torch.backends.cuda.matmul.allow_tf32 = True # Enable tf32 for faster matmuls on Ampere GPUs
|
| 8 |
+
torch.backends.cudnn.allow_tf32 = True # Enable tf32 for cuDNN
|
| 9 |
+
|
| 10 |
+
# Check for GPU and set up the model
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 13 |
|
| 14 |
+
# Use a smaller, faster model (try these alternatives if this doesn't work well)
|
| 15 |
+
# model_id = "prompthero/openjourney-v4" # Midjourney style
|
| 16 |
+
# model_id = "nitrosocke/redshift-diffusion" # 3D render style
|
| 17 |
model_id = "runwayml/stable-diffusion-v1-5"
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Load the pipeline with optimizations
|
| 20 |
+
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
|
| 21 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 22 |
+
model_id,
|
| 23 |
+
scheduler=scheduler,
|
| 24 |
+
torch_dtype=dtype,
|
| 25 |
+
safety_checker=None, # Disable safety checker for faster generation
|
| 26 |
+
requires_safety_checker=False
|
| 27 |
+
).to(device)
|
| 28 |
+
|
| 29 |
+
# Apply optimizations
|
| 30 |
+
pipe.enable_xformers_memory_efficient_attention() # Flash attention
|
| 31 |
+
pipe.enable_attention_slicing(1) # Minimal slicing for balance between speed and memory
|
| 32 |
|
| 33 |
+
# Warm up the model (important for consistent speed)
|
| 34 |
+
print("Warming up the model...")
|
| 35 |
+
with torch.inference_mode():
|
| 36 |
+
_ = pipe("warmup", num_inference_steps=1, guidance_scale=1)
|
| 37 |
+
|
| 38 |
+
def generate_image(
|
| 39 |
+
prompt: str,
|
| 40 |
+
negative_prompt: str = "",
|
| 41 |
+
steps: int = 15,
|
| 42 |
+
guidance_scale: float = 7.0,
|
| 43 |
+
seed: int = -1,
|
| 44 |
+
width: int = 512,
|
| 45 |
+
height: int = 512
|
| 46 |
+
):
|
| 47 |
+
# Start timer
|
| 48 |
+
start_time = datetime.now()
|
| 49 |
+
|
| 50 |
# Set random seed if provided
|
| 51 |
+
generator = None
|
| 52 |
if seed != -1:
|
| 53 |
generator = torch.Generator(device=device).manual_seed(seed)
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Generate image with optimizations
|
| 56 |
+
with torch.inference_mode(), torch.autocast(device):
|
| 57 |
image = pipe(
|
| 58 |
prompt=prompt,
|
| 59 |
negative_prompt=negative_prompt,
|
| 60 |
num_inference_steps=int(steps),
|
| 61 |
guidance_scale=guidance_scale,
|
| 62 |
+
generator=generator,
|
| 63 |
+
width=width,
|
| 64 |
+
height=height
|
| 65 |
).images[0]
|
| 66 |
|
| 67 |
+
# Calculate generation time
|
| 68 |
+
gen_time = (datetime.now() - start_time).total_seconds()
|
| 69 |
+
print(f"Generated image in {gen_time:.2f} seconds")
|
| 70 |
+
|
| 71 |
+
return image, f"Generated in {gen_time:.2f}s | Steps: {steps} | CFG: {guidance_scale} | Seed: {seed if seed != -1 else 'random'}"
|
| 72 |
|
| 73 |
+
# Gradio interface with optimizations
|
| 74 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 75 |
+
gr.Markdown("""
|
| 76 |
+
# ⚡ Ultra-Fast Diffusion Image Generator
|
| 77 |
+
*Optimized to generate images in under 5 seconds*
|
| 78 |
+
""")
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
with gr.Column():
|
| 82 |
+
prompt = gr.Textbox(
|
| 83 |
+
label="Prompt",
|
| 84 |
+
placeholder="A beautiful landscape with mountains and a lake",
|
| 85 |
+
max_lines=3
|
| 86 |
+
)
|
| 87 |
+
negative_prompt = gr.Textbox(
|
| 88 |
+
label="Negative Prompt",
|
| 89 |
+
placeholder="blurry, low quality, distorted",
|
| 90 |
+
max_lines=2
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 94 |
+
with gr.Row():
|
| 95 |
+
steps = gr.Slider(8, 30, value=15, step=1, label="Inference Steps")
|
| 96 |
+
guidance_scale = gr.Slider(1, 10, value=7.0, step=0.5, label="Guidance Scale")
|
| 97 |
+
with gr.Row():
|
| 98 |
+
seed = gr.Number(value=-1, label="Seed (-1 for random)")
|
| 99 |
+
width = gr.Slider(256, 768, value=512, step=64, label="Width")
|
| 100 |
+
height = gr.Slider(256, 768, value=512, step=64, label="Height")
|
| 101 |
+
|
| 102 |
+
generate_btn = gr.Button("Generate Image", variant="primary")
|
| 103 |
+
info_output = gr.Textbox(label="Generation Info", interactive=False)
|
| 104 |
|
| 105 |
with gr.Column():
|
| 106 |
+
output_image = gr.Image(label="Generated Image", type="pil", show_download_button=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
# Examples for quick testing
|
| 109 |
gr.Examples(
|
| 110 |
examples=[
|
| 111 |
+
["A futuristic cyberpunk city at night, neon lights, rain reflections", "blurry, low quality", 15, 7.0, 42],
|
| 112 |
+
["A cute robot cat, digital art, vibrant colors", "ugly, deformed", 12, 7.5, -1],
|
| 113 |
+
["A majestic dragon flying over mountains at sunset, fantasy art", "cartoon, sketch", 20, 8.0, 123]
|
| 114 |
],
|
| 115 |
inputs=[prompt, negative_prompt, steps, guidance_scale, seed],
|
| 116 |
+
outputs=[output_image, info_output],
|
| 117 |
fn=generate_image,
|
| 118 |
cache_examples=True
|
| 119 |
)
|
| 120 |
+
|
| 121 |
+
generate_btn.click(
|
| 122 |
+
fn=generate_image,
|
| 123 |
+
inputs=[prompt, negative_prompt, steps, guidance_scale, seed, width, height],
|
| 124 |
+
outputs=[output_image, info_output]
|
| 125 |
+
)
|
| 126 |
|
| 127 |
+
# For Hugging Face Spaces
|
| 128 |
+
demo.queue(max_size=4) # Limit concurrent requests to prevent OOM errors
|
| 129 |
+
demo.launch(debug=False)
|