Yankkee commited on
Commit
b63d9f9
·
verified ·
1 Parent(s): ac47d57

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +18 -0
  2. app.py +132 -0
  3. controlnet_space.zip +0 -0
  4. requirements.txt +10 -0
README.md ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: ControlNet Canny
3
+ emoji: ⚡
4
+ colorFrom: purple
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 5.28.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ # ControlNet · Canny Edge
13
+
14
+ Lade ein Bild hoch, schreib einen Prompt – und erzeuge ein neues Bild,
15
+ das die Kanten/Struktur deines Originals übernimmt.
16
+
17
+ Läuft auf CPU (langsam, ~2–5 Min pro Bild). Für schnellere Ergebnisse
18
+ in den Space-Settings auf GPU upgraden – der Code funktioniert ohne Änderung.
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+ from PIL import Image
5
+ import cv2
6
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
7
+
8
+ # ---------------------------------------------------------------------------
9
+ # Device setup (works on free CPU Spaces)
10
+ # ---------------------------------------------------------------------------
11
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
12
+ DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
13
+
14
+ # ---------------------------------------------------------------------------
15
+ # Model loading (cached – runs once on Space startup)
16
+ # ---------------------------------------------------------------------------
17
+ def load_pipeline():
18
+ controlnet = ControlNetModel.from_pretrained(
19
+ "lllyasviel/sd-controlnet-canny",
20
+ torch_dtype=DTYPE,
21
+ )
22
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
23
+ "runwayml/stable-diffusion-v1-5",
24
+ controlnet=controlnet,
25
+ torch_dtype=DTYPE,
26
+ safety_checker=None,
27
+ )
28
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
29
+ pipe = pipe.to(DEVICE)
30
+ if DEVICE == "cuda":
31
+ pipe.enable_model_cpu_offload()
32
+ return pipe
33
+
34
+ pipe = load_pipeline()
35
+
36
+ # ---------------------------------------------------------------------------
37
+ # Helper: extract Canny edges
38
+ # ---------------------------------------------------------------------------
39
+ def extract_canny(image: Image.Image, low: int, high: int) -> Image.Image:
40
+ img_array = np.array(image.convert("RGB"))
41
+ edges = cv2.Canny(img_array, low, high)
42
+ edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
43
+ return Image.fromarray(edges_rgb)
44
+
45
+ # ---------------------------------------------------------------------------
46
+ # Main generation function
47
+ # ---------------------------------------------------------------------------
48
+ def generate(input_image, prompt, negative_prompt, canny_low, canny_high,
49
+ guidance_scale, steps, seed):
50
+ if input_image is None:
51
+ raise gr.Error("Bitte lade ein Bild hoch.")
52
+ if not prompt.strip():
53
+ raise gr.Error("Bitte gib einen Prompt ein.")
54
+
55
+ pil_image = Image.fromarray(input_image).resize((512, 512))
56
+ control_image = extract_canny(pil_image, int(canny_low), int(canny_high))
57
+
58
+ generator = torch.manual_seed(int(seed)) if seed >= 0 else None
59
+
60
+ result = pipe(
61
+ prompt=prompt,
62
+ negative_prompt=negative_prompt or None,
63
+ image=control_image,
64
+ num_inference_steps=int(steps),
65
+ guidance_scale=float(guidance_scale),
66
+ generator=generator,
67
+ ).images[0]
68
+
69
+ return control_image, result
70
+
71
+ # ---------------------------------------------------------------------------
72
+ # Gradio UI
73
+ # ---------------------------------------------------------------------------
74
+ css = """
75
+ body { font-family: 'Inter', sans-serif; background: #0f0f11; color: #e8e8f0; }
76
+ .gradio-container { max-width: 1100px; margin: 0 auto; }
77
+ #title { text-align: center; padding: 2rem 0 0.5rem; }
78
+ #title h1 { font-size: 2rem; font-weight: 700; letter-spacing: -0.5px;
79
+ background: linear-gradient(90deg, #a78bfa, #60a5fa);
80
+ -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
81
+ #title p { color: #9090a8; font-size: 0.95rem; margin-top: 0.25rem; }
82
+ .panel { background: #1a1a22; border: 1px solid #2a2a38; border-radius: 12px; padding: 1.25rem; }
83
+ .generate-btn { background: linear-gradient(135deg, #7c3aed, #2563eb) !important;
84
+ color: white !important; border: none !important;
85
+ font-weight: 600 !important; font-size: 1rem !important;
86
+ border-radius: 8px !important; height: 48px !important; }
87
+ .generate-btn:hover { opacity: 0.9 !important; }
88
+ """
89
+
90
+ with gr.Blocks(css=css, title="ControlNet Canny") as demo:
91
+ gr.HTML("""
92
+ <div id="title">
93
+ <h1>⚡ ControlNet · Canny Edge</h1>
94
+ <p>Lade ein Bild hoch, schreib einen Prompt – und erzeuge ein neues Bild, das die Struktur deines Originals übernimmt.</p>
95
+ </div>
96
+ """)
97
+
98
+ gr.Markdown(f"> 🖥️ Läuft auf: **{DEVICE.upper()}** — auf CPU dauert eine Generierung ca. 2–5 Minuten. Bitte Geduld.")
99
+
100
+ with gr.Row():
101
+ with gr.Column(scale=1, elem_classes="panel"):
102
+ gr.Markdown("### 📥 Eingabe")
103
+ input_image = gr.Image(label="Referenzbild", type="numpy", height=300)
104
+ prompt = gr.Textbox(label="Prompt",
105
+ placeholder="a futuristic city at night, neon lights, photorealistic, 8k", lines=3)
106
+ negative_prompt = gr.Textbox(label="Negative Prompt (optional)",
107
+ placeholder="blurry, low quality, watermark, deformed", lines=2)
108
+
109
+ with gr.Accordion("⚙️ Erweiterte Einstellungen", open=False):
110
+ with gr.Row():
111
+ canny_low = gr.Slider(0, 255, value=100, step=1, label="Canny Low Threshold")
112
+ canny_high = gr.Slider(0, 255, value=200, step=1, label="Canny High Threshold")
113
+ with gr.Row():
114
+ guidance_scale = gr.Slider(1, 20, value=7.5, step=0.5, label="Guidance Scale")
115
+ steps = gr.Slider(10, 30, value=15, step=1, label="Inference Steps")
116
+ seed = gr.Number(value=42, label="Seed (-1 = zufällig)", precision=0)
117
+
118
+ run_btn = gr.Button("🎨 Generieren", elem_classes="generate-btn")
119
+
120
+ with gr.Column(scale=1, elem_classes="panel"):
121
+ gr.Markdown("### 📤 Ergebnis")
122
+ canny_out = gr.Image(label="Canny-Kantenbild", height=250)
123
+ result_out = gr.Image(label="Generiertes Bild", height=350)
124
+
125
+ run_btn.click(
126
+ fn=generate,
127
+ inputs=[input_image, prompt, negative_prompt, canny_low, canny_high,
128
+ guidance_scale, steps, seed],
129
+ outputs=[canny_out, result_out],
130
+ )
131
+
132
+ demo.queue().launch()
controlnet_space.zip ADDED
Binary file (3.38 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cpu
2
+ torch
3
+ torchvision
4
+ diffusers
5
+ transformers
6
+ accelerate
7
+ opencv-python-headless
8
+ Pillow
9
+ numpy
10
+ gradio