Controllnet / app.py
Yankkee's picture
Upload 4 files
b63d9f9 verified
Raw
History Blame Contribute Delete
5.93 kB
import gradio as gr
import torch
import numpy as np
from PIL import Image
import cv2
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
# ---------------------------------------------------------------------------
# Device setup (works on free CPU Spaces)
# ---------------------------------------------------------------------------
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
# ---------------------------------------------------------------------------
# Model loading (cached – runs once on Space startup)
# ---------------------------------------------------------------------------
def load_pipeline():
controlnet = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-canny",
torch_dtype=DTYPE,
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
torch_dtype=DTYPE,
safety_checker=None,
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to(DEVICE)
if DEVICE == "cuda":
pipe.enable_model_cpu_offload()
return pipe
pipe = load_pipeline()
# ---------------------------------------------------------------------------
# Helper: extract Canny edges
# ---------------------------------------------------------------------------
def extract_canny(image: Image.Image, low: int, high: int) -> Image.Image:
img_array = np.array(image.convert("RGB"))
edges = cv2.Canny(img_array, low, high)
edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
return Image.fromarray(edges_rgb)
# ---------------------------------------------------------------------------
# Main generation function
# ---------------------------------------------------------------------------
def generate(input_image, prompt, negative_prompt, canny_low, canny_high,
guidance_scale, steps, seed):
if input_image is None:
raise gr.Error("Bitte lade ein Bild hoch.")
if not prompt.strip():
raise gr.Error("Bitte gib einen Prompt ein.")
pil_image = Image.fromarray(input_image).resize((512, 512))
control_image = extract_canny(pil_image, int(canny_low), int(canny_high))
generator = torch.manual_seed(int(seed)) if seed >= 0 else None
result = pipe(
prompt=prompt,
negative_prompt=negative_prompt or None,
image=control_image,
num_inference_steps=int(steps),
guidance_scale=float(guidance_scale),
generator=generator,
).images[0]
return control_image, result
# ---------------------------------------------------------------------------
# Gradio UI
# ---------------------------------------------------------------------------
css = """
body { font-family: 'Inter', sans-serif; background: #0f0f11; color: #e8e8f0; }
.gradio-container { max-width: 1100px; margin: 0 auto; }
#title { text-align: center; padding: 2rem 0 0.5rem; }
#title h1 { font-size: 2rem; font-weight: 700; letter-spacing: -0.5px;
background: linear-gradient(90deg, #a78bfa, #60a5fa);
-webkit-background-clip: text; -webkit-text-fill-color: transparent; }
#title p { color: #9090a8; font-size: 0.95rem; margin-top: 0.25rem; }
.panel { background: #1a1a22; border: 1px solid #2a2a38; border-radius: 12px; padding: 1.25rem; }
.generate-btn { background: linear-gradient(135deg, #7c3aed, #2563eb) !important;
color: white !important; border: none !important;
font-weight: 600 !important; font-size: 1rem !important;
border-radius: 8px !important; height: 48px !important; }
.generate-btn:hover { opacity: 0.9 !important; }
"""
with gr.Blocks(css=css, title="ControlNet Canny") as demo:
gr.HTML("""
<div id="title">
<h1>⚡ ControlNet · Canny Edge</h1>
<p>Lade ein Bild hoch, schreib einen Prompt – und erzeuge ein neues Bild, das die Struktur deines Originals übernimmt.</p>
</div>
""")
gr.Markdown(f"> 🖥️ Läuft auf: **{DEVICE.upper()}** — auf CPU dauert eine Generierung ca. 2–5 Minuten. Bitte Geduld.")
with gr.Row():
with gr.Column(scale=1, elem_classes="panel"):
gr.Markdown("### 📥 Eingabe")
input_image = gr.Image(label="Referenzbild", type="numpy", height=300)
prompt = gr.Textbox(label="Prompt",
placeholder="a futuristic city at night, neon lights, photorealistic, 8k", lines=3)
negative_prompt = gr.Textbox(label="Negative Prompt (optional)",
placeholder="blurry, low quality, watermark, deformed", lines=2)
with gr.Accordion("⚙️ Erweiterte Einstellungen", open=False):
with gr.Row():
canny_low = gr.Slider(0, 255, value=100, step=1, label="Canny Low Threshold")
canny_high = gr.Slider(0, 255, value=200, step=1, label="Canny High Threshold")
with gr.Row():
guidance_scale = gr.Slider(1, 20, value=7.5, step=0.5, label="Guidance Scale")
steps = gr.Slider(10, 30, value=15, step=1, label="Inference Steps")
seed = gr.Number(value=42, label="Seed (-1 = zufällig)", precision=0)
run_btn = gr.Button("🎨 Generieren", elem_classes="generate-btn")
with gr.Column(scale=1, elem_classes="panel"):
gr.Markdown("### 📤 Ergebnis")
canny_out = gr.Image(label="Canny-Kantenbild", height=250)
result_out = gr.Image(label="Generiertes Bild", height=350)
run_btn.click(
fn=generate,
inputs=[input_image, prompt, negative_prompt, canny_low, canny_high,
guidance_scale, steps, seed],
outputs=[canny_out, result_out],
)
demo.queue().launch()