Yankkee commited on
Commit
239e52e
·
verified ·
1 Parent(s): fa87ede

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +156 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Model loading (cached – runs once on Space startup)
10
+ # ---------------------------------------------------------------------------
11
+
12
+ def load_pipeline():
13
+ controlnet = ControlNetModel.from_pretrained(
14
+ "lllyasviel/sd-controlnet-canny",
15
+ torch_dtype=torch.float16,
16
+ )
17
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
18
+ "runwayml/stable-diffusion-v1-5",
19
+ controlnet=controlnet,
20
+ torch_dtype=torch.float16,
21
+ safety_checker=None,
22
+ )
23
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
24
+ pipe.enable_model_cpu_offload() # keeps VRAM usage low
25
+ return pipe
26
+
27
+ pipe = load_pipeline()
28
+
29
+ # ---------------------------------------------------------------------------
30
+ # Helper: extract Canny edges
31
+ # ---------------------------------------------------------------------------
32
+
33
+ def extract_canny(image: Image.Image, low: int, high: int) -> Image.Image:
34
+ img_array = np.array(image.convert("RGB"))
35
+ edges = cv2.Canny(img_array, low, high)
36
+ edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
37
+ return Image.fromarray(edges_rgb)
38
+
39
+ # ---------------------------------------------------------------------------
40
+ # Main generation function
41
+ # ---------------------------------------------------------------------------
42
+
43
+ def generate(
44
+ input_image,
45
+ prompt,
46
+ negative_prompt,
47
+ canny_low,
48
+ canny_high,
49
+ guidance_scale,
50
+ steps,
51
+ seed,
52
+ ):
53
+ if input_image is None:
54
+ raise gr.Error("Bitte lade ein Bild hoch.")
55
+ if not prompt.strip():
56
+ raise gr.Error("Bitte gib einen Prompt ein.")
57
+
58
+ pil_image = Image.fromarray(input_image).resize((512, 512))
59
+ control_image = extract_canny(pil_image, int(canny_low), int(canny_high))
60
+
61
+ generator = torch.manual_seed(int(seed)) if seed >= 0 else None
62
+
63
+ result = pipe(
64
+ prompt=prompt,
65
+ negative_prompt=negative_prompt or None,
66
+ image=control_image,
67
+ num_inference_steps=int(steps),
68
+ guidance_scale=float(guidance_scale),
69
+ generator=generator,
70
+ ).images[0]
71
+
72
+ return control_image, result
73
+
74
+ # ---------------------------------------------------------------------------
75
+ # Gradio UI
76
+ # ---------------------------------------------------------------------------
77
+
78
+ css = """
79
+ body { font-family: 'Inter', sans-serif; background: #0f0f11; color: #e8e8f0; }
80
+ .gradio-container { max-width: 1100px; margin: 0 auto; }
81
+ #title { text-align: center; padding: 2rem 0 0.5rem; }
82
+ #title h1 { font-size: 2rem; font-weight: 700; letter-spacing: -0.5px;
83
+ background: linear-gradient(90deg, #a78bfa, #60a5fa);
84
+ -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
85
+ #title p { color: #9090a8; font-size: 0.95rem; margin-top: 0.25rem; }
86
+ .panel { background: #1a1a22; border: 1px solid #2a2a38; border-radius: 12px; padding: 1.25rem; }
87
+ .generate-btn { background: linear-gradient(135deg, #7c3aed, #2563eb) !important;
88
+ color: white !important; border: none !important;
89
+ font-weight: 600 !important; font-size: 1rem !important;
90
+ border-radius: 8px !important; height: 48px !important; }
91
+ .generate-btn:hover { opacity: 0.9 !important; }
92
+ """
93
+
94
+ with gr.Blocks(css=css, title="ControlNet Canny") as demo:
95
+
96
+ gr.HTML("""
97
+ <div id="title">
98
+ <h1>⚡ ControlNet · Canny Edge</h1>
99
+ <p>Lade ein Bild hoch, schreib einen Prompt – und erzeuge ein neues Bild, das die Struktur deines Originals übernimmt.</p>
100
+ </div>
101
+ """)
102
+
103
+ with gr.Row():
104
+ # ---- Left column: inputs ----
105
+ with gr.Column(scale=1, elem_classes="panel"):
106
+ gr.Markdown("### 📥 Eingabe")
107
+ input_image = gr.Image(label="Referenzbild", type="numpy", height=300)
108
+
109
+ prompt = gr.Textbox(
110
+ label="Prompt",
111
+ placeholder="a futuristic city at night, neon lights, photorealistic, 8k",
112
+ lines=3,
113
+ )
114
+ negative_prompt = gr.Textbox(
115
+ label="Negative Prompt (optional)",
116
+ placeholder="blurry, low quality, watermark, deformed",
117
+ lines=2,
118
+ )
119
+
120
+ with gr.Accordion("⚙️ Erweiterte Einstellungen", open=False):
121
+ with gr.Row():
122
+ canny_low = gr.Slider(0, 255, value=100, step=1, label="Canny Low Threshold")
123
+ canny_high = gr.Slider(0, 255, value=200, step=1, label="Canny High Threshold")
124
+ with gr.Row():
125
+ guidance_scale = gr.Slider(1, 20, value=7.5, step=0.5, label="Guidance Scale")
126
+ steps = gr.Slider(10, 50, value=20, step=1, label="Inference Steps")
127
+ seed = gr.Number(value=42, label="Seed (-1 = zufällig)", precision=0)
128
+
129
+ run_btn = gr.Button("🎨 Generieren", elem_classes="generate-btn")
130
+
131
+ # ---- Right column: outputs ----
132
+ with gr.Column(scale=1, elem_classes="panel"):
133
+ gr.Markdown("### 📤 Ergebnis")
134
+ canny_out = gr.Image(label="Canny-Kantenbild", height=250)
135
+ result_out = gr.Image(label="Generiertes Bild", height=350)
136
+
137
+ # ---- Example images ----
138
+ gr.Markdown("---")
139
+ gr.Markdown("### 💡 Beispiel-Prompts")
140
+ gr.Examples(
141
+ examples=[
142
+ [None, "a pencil sketch of a medieval castle, detailed, black and white", "blurry, color", 100, 200, 7.5, 20, 42],
143
+ [None, "a cyberpunk street scene, neon lights, rain, cinematic", "low quality", 80, 180, 8.0, 25, 0],
144
+ [None, "oil painting of a mountain landscape, impressionist style", "photo, realistic", 100, 200, 7.0, 20, 7],
145
+ ],
146
+ inputs=[input_image, prompt, negative_prompt, canny_low, canny_high, guidance_scale, steps, seed],
147
+ label="Zum Laden anklicken",
148
+ )
149
+
150
+ run_btn.click(
151
+ fn=generate,
152
+ inputs=[input_image, prompt, negative_prompt, canny_low, canny_high, guidance_scale, steps, seed],
153
+ outputs=[canny_out, result_out],
154
+ )
155
+
156
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ torch==2.1.0
2
+ torchvision==0.16.0
3
+ diffusers==0.25.0
4
+ transformers==4.37.2
5
+ accelerate==0.26.0
6
+ opencv-python-headless==4.9.0.80
7
+ Pillow==10.2.0
8
+ numpy==1.26.3
9
+ gradio==4.19.2