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("""
Lade ein Bild hoch, schreib einen Prompt – und erzeuge ein neues Bild, das die Struktur deines Originals übernimmt.