lucashudsn commited on
Commit
29fa334
·
verified ·
1 Parent(s): 662a73d

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +389 -0
app.py ADDED
@@ -0,0 +1,389 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ArchiCAD AI Renderer — Gradio app for Hugging Face ZeroGPU Spaces.
3
+
4
+ Takes an ArchiCAD viewport export (shaded 3D view, white/clay model, elevation,
5
+ or line drawing) and turns it into a photorealistic render while preserving the
6
+ geometry. Under the hood:
7
+
8
+ * Depth-Anything-V2 (depth map) ─┐
9
+ * Canny edges (linework) ├─> multi-ControlNet SDXL (RealVisXL V5)
10
+ * Prompt builder (scene/time/etc) ─┘ │
11
+ └─> optional ControlNet img2img
12
+ "detail refine" + 1.5-2x upscale
13
+ """
14
+
15
+ import gc
16
+ import random
17
+
18
+ import cv2
19
+ import gradio as gr
20
+ import numpy as np
21
+ import spaces
22
+ import torch
23
+ from diffusers import (
24
+ AutoencoderKL,
25
+ ControlNetModel,
26
+ DPMSolverMultistepScheduler,
27
+ StableDiffusionXLControlNetImg2ImgPipeline,
28
+ StableDiffusionXLControlNetPipeline,
29
+ )
30
+ from PIL import Image, ImageOps
31
+ from transformers import pipeline as hf_pipeline
32
+
33
+ # ----------------------------------------------------------------------------
34
+ # Model loading (done once at import; ZeroGPU moves to GPU on demand)
35
+ # ----------------------------------------------------------------------------
36
+ DTYPE = torch.float16
37
+ BASE_MODEL = "SG161222/RealVisXL_V5.0"
38
+ VAE_MODEL = "madebyollin/sdxl-vae-fp16-fix"
39
+ CN_DEPTH = "diffusers/controlnet-depth-sdxl-1.0-small"
40
+ CN_CANNY = "diffusers/controlnet-canny-sdxl-1.0-small"
41
+ DEPTH_MODEL = "depth-anything/Depth-Anything-V2-Small-hf"
42
+
43
+ print("Loading models…")
44
+ vae = AutoencoderKL.from_pretrained(VAE_MODEL, torch_dtype=DTYPE)
45
+ controlnets = [
46
+ ControlNetModel.from_pretrained(CN_DEPTH, torch_dtype=DTYPE, use_safetensors=True),
47
+ ControlNetModel.from_pretrained(CN_CANNY, torch_dtype=DTYPE, use_safetensors=True),
48
+ ]
49
+ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
50
+ BASE_MODEL,
51
+ controlnet=controlnets,
52
+ vae=vae,
53
+ torch_dtype=DTYPE,
54
+ variant="fp16",
55
+ use_safetensors=True,
56
+ )
57
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(
58
+ pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="dpmsolver++"
59
+ )
60
+ pipe.vae.enable_tiling()
61
+ pipe.vae.enable_slicing()
62
+ pipe.to("cuda")
63
+
64
+ # Refine pipeline shares all weights with the main one (no extra VRAM)
65
+ refine_pipe = StableDiffusionXLControlNetImg2ImgPipeline(**pipe.components)
66
+ refine_pipe.to("cuda")
67
+
68
+ depth_estimator = hf_pipeline("depth-estimation", model=DEPTH_MODEL, device="cuda")
69
+ print("Models ready.")
70
+
71
+ # ----------------------------------------------------------------------------
72
+ # Prompt building
73
+ # ----------------------------------------------------------------------------
74
+ QUALITY = (
75
+ "architectural photography, photorealistic, ultra detailed, sharp focus, "
76
+ "natural lighting, realistic materials, professional DSLR, 8k"
77
+ )
78
+ NEGATIVE = (
79
+ "cartoon, illustration, painting, sketch, lowres, blurry, deformed geometry, "
80
+ "warped walls, distorted perspective, extra floors, extra windows, text, "
81
+ "watermark, logo, oversaturated, cgi, plastic, noise, jpeg artifacts"
82
+ )
83
+
84
+ SCENES = {
85
+ "Exterior – residential": "modern residential house exterior, landscaped garden, driveway",
86
+ "Exterior – commercial / office": "contemporary commercial building exterior, plaza, street trees",
87
+ "Exterior – urban / mixed-use": "urban mixed-use building, city street, pedestrians, cars",
88
+ "Interior – living space": "interior design photography, living room, furniture, decor",
89
+ "Interior – kitchen / dining": "interior design photography, kitchen and dining area, styled",
90
+ "Interior – bedroom": "interior design photography, bedroom, bedding, soft textiles",
91
+ "Interior – office / commercial": "modern office interior, workstations, plants, glazing",
92
+ "Detail / facade close-up": "architectural facade detail, material junctions, shadows",
93
+ }
94
+
95
+ TIMES = {
96
+ "Midday – clear sky": "bright midday sun, clear blue sky, crisp shadows",
97
+ "Golden hour": "golden hour, warm low sun, long soft shadows, glowing sky",
98
+ "Overcast / soft light": "overcast sky, soft diffuse light, muted tones",
99
+ "Blue hour / dusk": "blue hour, dusk, glowing interior lights, deep blue sky",
100
+ "Night": "night, illuminated interior lighting, warm lights, dark sky",
101
+ "Misty morning": "early morning mist, soft sunrise light, atmospheric haze",
102
+ }
103
+
104
+ SEASONS = {
105
+ "Summer": "lush green summer vegetation",
106
+ "Spring": "spring blossoms, fresh green foliage",
107
+ "Autumn": "autumn foliage, orange and red leaves",
108
+ "Winter": "winter, light snow, bare trees",
109
+ "Not specified": "",
110
+ }
111
+
112
+ STYLES = {
113
+ "Photorealistic": "photorealistic archviz render, V-Ray look",
114
+ "Editorial magazine": "architectural digest editorial photo, moody, high-end",
115
+ "Minimal / Scandinavian": "minimal Scandinavian aesthetic, light wood, white walls",
116
+ "Brutalist concrete": "brutalist architecture, raw board-formed concrete",
117
+ "Warm / Mediterranean": "Mediterranean style, warm plaster, terracotta, olive trees",
118
+ }
119
+
120
+
121
+ def build_prompt(description, scene, time_of_day, season, style, materials):
122
+ parts = [
123
+ description.strip(),
124
+ materials.strip(),
125
+ SCENES.get(scene, ""),
126
+ STYLES.get(style, ""),
127
+ TIMES.get(time_of_day, ""),
128
+ SEASONS.get(season, "") if scene.startswith("Exterior") else "",
129
+ QUALITY,
130
+ ]
131
+ return ", ".join(p for p in parts if p)
132
+
133
+
134
+ # ----------------------------------------------------------------------------
135
+ # Control-image preparation
136
+ # ----------------------------------------------------------------------------
137
+ def fit_size(w, h, long_side):
138
+ scale = long_side / max(w, h)
139
+ nw, nh = int(round(w * scale / 8) * 8), int(round(h * scale / 8) * 8)
140
+ return max(nw, 256), max(nh, 256)
141
+
142
+
143
+ def make_canny(img: Image.Image, low: int, high: int, thicken: bool) -> Image.Image:
144
+ gray = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY)
145
+ gray = cv2.GaussianBlur(gray, (3, 3), 0)
146
+ edges = cv2.Canny(gray, low, high)
147
+ if thicken:
148
+ edges = cv2.dilate(edges, np.ones((2, 2), np.uint8), iterations=1)
149
+ return Image.fromarray(np.stack([edges] * 3, axis=-1))
150
+
151
+
152
+ def make_depth(img: Image.Image) -> Image.Image:
153
+ d = depth_estimator(img)["depth"]
154
+ d = np.array(d).astype(np.float32)
155
+ d = (d - d.min()) / max(d.max() - d.min(), 1e-6)
156
+ d = (d * 255).astype(np.uint8)
157
+ return Image.fromarray(np.stack([d] * 3, axis=-1)).resize(img.size, Image.LANCZOS)
158
+
159
+
160
+ def prepare(image, long_side, mode, low, high):
161
+ img = ImageOps.exif_transpose(image).convert("RGB")
162
+ w, h = fit_size(*img.size, long_side)
163
+ img = img.resize((w, h), Image.LANCZOS)
164
+ depth = make_depth(img)
165
+ canny = make_canny(img, low, high, thicken=(mode == "Line drawing / elevation"))
166
+ return img, depth, canny
167
+
168
+
169
+ MODE_DEFAULTS = {
170
+ # (depth_scale, canny_scale)
171
+ "3D view (shaded / clay model)": (0.65, 0.55),
172
+ "Line drawing / elevation": (0.30, 0.90),
173
+ }
174
+
175
+
176
+ def on_mode_change(mode):
177
+ d, c = MODE_DEFAULTS[mode]
178
+ return d, c
179
+
180
+
181
+ # ----------------------------------------------------------------------------
182
+ # GPU functions
183
+ # ----------------------------------------------------------------------------
184
+ @spaces.GPU(duration=30)
185
+ def preview_controls(image, long_side, mode, canny_low, canny_high):
186
+ if image is None:
187
+ raise gr.Error("Upload an ArchiCAD export first.")
188
+ _, depth, canny = prepare(image, long_side, mode, canny_low, canny_high)
189
+ return [(depth, "Depth"), (canny, "Edges")]
190
+
191
+
192
+ @spaces.GPU(duration=150)
193
+ def render(
194
+ image,
195
+ description,
196
+ scene,
197
+ time_of_day,
198
+ season,
199
+ style,
200
+ materials,
201
+ negative_extra,
202
+ mode,
203
+ long_side,
204
+ depth_scale,
205
+ canny_scale,
206
+ canny_low,
207
+ canny_high,
208
+ steps,
209
+ guidance,
210
+ num_variations,
211
+ seed,
212
+ do_refine,
213
+ upscale,
214
+ refine_strength,
215
+ progress=gr.Progress(track_tqdm=True),
216
+ ):
217
+ if image is None:
218
+ raise gr.Error("Upload an ArchiCAD export first.")
219
+
220
+ seed = random.randint(0, 2**31 - 1) if seed is None or int(seed) < 0 else int(seed)
221
+ prompt = build_prompt(description or "", scene, time_of_day, season, style, materials or "")
222
+ negative = NEGATIVE + (", " + negative_extra.strip() if negative_extra else "")
223
+
224
+ progress(0.05, desc="Extracting depth + edges")
225
+ img, depth, canny = prepare(image, long_side, mode, canny_low, canny_high)
226
+ w, h = img.size
227
+ scales = [float(depth_scale), float(canny_scale)]
228
+
229
+ progress(0.15, desc="Rendering")
230
+ gen = torch.Generator("cuda").manual_seed(seed)
231
+ out = pipe(
232
+ prompt=prompt,
233
+ negative_prompt=negative,
234
+ image=[depth, canny],
235
+ width=w,
236
+ height=h,
237
+ num_inference_steps=int(steps),
238
+ guidance_scale=float(guidance),
239
+ controlnet_conditioning_scale=scales,
240
+ control_guidance_start=[0.0, 0.0],
241
+ control_guidance_end=[0.8, 0.9],
242
+ num_images_per_prompt=int(num_variations),
243
+ generator=gen,
244
+ ).images
245
+
246
+ if do_refine:
247
+ progress(0.7, desc="Refining details + upscaling")
248
+ nw = int(round(w * upscale / 8) * 8)
249
+ nh = int(round(h * upscale / 8) * 8)
250
+ d_up = depth.resize((nw, nh), Image.LANCZOS)
251
+ c_up = canny.resize((nw, nh), Image.LANCZOS)
252
+ refined = []
253
+ for i, im in enumerate(out):
254
+ up = im.resize((nw, nh), Image.LANCZOS)
255
+ r = refine_pipe(
256
+ prompt=prompt,
257
+ negative_prompt=negative,
258
+ image=up,
259
+ control_image=[d_up, c_up],
260
+ strength=float(refine_strength),
261
+ num_inference_steps=max(int(steps), 24),
262
+ guidance_scale=float(guidance),
263
+ controlnet_conditioning_scale=scales,
264
+ generator=torch.Generator("cuda").manual_seed(seed + i),
265
+ ).images[0]
266
+ refined.append(r)
267
+ out = refined
268
+
269
+ gc.collect()
270
+ torch.cuda.empty_cache()
271
+ captions = [(im, f"seed {seed}" + (f"+{i}" if i else "")) for i, im in enumerate(out)]
272
+ return captions, seed, prompt
273
+
274
+
275
+ # ----------------------------------------------------------------------------
276
+ # UI
277
+ # ----------------------------------------------------------------------------
278
+ TIPS = """
279
+ ### Exporting from ArchiCAD for best results
280
+
281
+ 1. **3D window → Perspective** (or Parallel). Set the *3D Window Settings → Model Display* to **Shading** or **Shading with Contours**.
282
+ 2. Use **Sun/Shadow** on so volumes read clearly — depth estimation loves shadows.
283
+ 3. **File → Save As → Image** (`.png`, ≥1500 px on the long side), or *Publisher → Image* for batches.
284
+ 4. For **Elevations/Sections**, publish as PNG with a white background, black lines, and set *Line drawing* mode.
285
+ 5. Hide furniture/labels/dimensions/zones you don't want interpreted.
286
+ 6. For a "clay" look: set all surfaces to a single light grey material via *Model View Options → Override*.
287
+
288
+ ### Tuning
289
+
290
+ | Goal | What to change |
291
+ |---|---|
292
+ | Geometry drifting | Raise **Depth** and **Edge** strength (0.8+) |
293
+ | Looks too CGI / stiff | Lower both strengths (0.4–0.5), raise guidance slightly |
294
+ | Too many weird details | Lower guidance (5–6), shorter prompt |
295
+ | Need a larger/sharper image | Enable **Refine + upscale** (1.5×) |
296
+ | Windows/doors missing | Lower Canny low threshold (e.g. 40/120) |
297
+
298
+ Material ideas for the prompt: `white lime render, charred timber cladding, black steel window frames, board-formed concrete, large glazing, standing seam zinc roof`.
299
+ """
300
+
301
+ CSS = """
302
+ .gradio-container {max-width: 1280px !important}
303
+ #title h1 {font-weight: 700; letter-spacing: -0.02em}
304
+ """
305
+
306
+ with gr.Blocks(title="ArchiCAD AI Renderer", theme=gr.themes.Soft(), css=CSS) as demo:
307
+ gr.Markdown(
308
+ "# 🏛️ ArchiCAD AI Renderer\n"
309
+ "Upload a viewport export from ArchiCAD and get a photorealistic render that "
310
+ "**keeps your geometry**. SDXL + depth/edge ControlNet on ZeroGPU.",
311
+ elem_id="title",
312
+ )
313
+
314
+ with gr.Row():
315
+ with gr.Column(scale=5):
316
+ image = gr.Image(label="ArchiCAD export (3D view, clay model, elevation…)", type="pil", height=380)
317
+ mode = gr.Radio(
318
+ list(MODE_DEFAULTS.keys()), value="3D view (shaded / clay model)", label="Input type"
319
+ )
320
+ description = gr.Textbox(
321
+ label="Describe the project",
322
+ placeholder="two-storey family home with flat roof, large corner glazing, timber screen",
323
+ lines=2,
324
+ )
325
+ materials = gr.Textbox(
326
+ label="Materials",
327
+ placeholder="white lime render, charred larch cladding, black aluminium windows, concrete paving",
328
+ )
329
+ with gr.Row():
330
+ scene = gr.Dropdown(list(SCENES), value="Exterior – residential", label="Scene")
331
+ style = gr.Dropdown(list(STYLES), value="Photorealistic", label="Style")
332
+ with gr.Row():
333
+ time_of_day = gr.Dropdown(list(TIMES), value="Golden hour", label="Lighting / time")
334
+ season = gr.Dropdown(list(SEASONS), value="Summer", label="Season (exteriors)")
335
+
336
+ with gr.Accordion("Control & generation settings", open=False):
337
+ negative_extra = gr.Textbox(label="Extra negative prompt", placeholder="people, cars")
338
+ long_side = gr.Slider(768, 1280, value=1024, step=64, label="Base resolution (long side)")
339
+ with gr.Row():
340
+ depth_scale = gr.Slider(0, 1.2, value=0.65, step=0.05, label="Depth strength")
341
+ canny_scale = gr.Slider(0, 1.2, value=0.55, step=0.05, label="Edge strength")
342
+ with gr.Row():
343
+ canny_low = gr.Slider(10, 200, value=70, step=5, label="Canny low")
344
+ canny_high = gr.Slider(50, 300, value=160, step=5, label="Canny high")
345
+ with gr.Row():
346
+ steps = gr.Slider(15, 50, value=30, step=1, label="Steps")
347
+ guidance = gr.Slider(3, 12, value=6.5, step=0.5, label="Guidance (CFG)")
348
+ with gr.Row():
349
+ num_variations = gr.Slider(1, 4, value=1, step=1, label="Variations")
350
+ seed = gr.Number(value=-1, precision=0, label="Seed (-1 = random)")
351
+
352
+ with gr.Accordion("Refine + upscale", open=True):
353
+ do_refine = gr.Checkbox(value=True, label="Detail refine pass (img2img + ControlNet)")
354
+ with gr.Row():
355
+ upscale = gr.Slider(1.0, 2.0, value=1.5, step=0.25, label="Upscale factor")
356
+ refine_strength = gr.Slider(0.15, 0.55, value=0.35, step=0.05, label="Refine strength")
357
+
358
+ with gr.Row():
359
+ preview_btn = gr.Button("👁️ Preview control maps", variant="secondary")
360
+ run_btn = gr.Button("🎨 Render", variant="primary")
361
+
362
+ with gr.Column(scale=6):
363
+ gallery = gr.Gallery(label="Renders", columns=2, height=560, object_fit="contain", preview=True)
364
+ with gr.Row():
365
+ used_seed = gr.Number(label="Seed used", interactive=False)
366
+ used_prompt = gr.Textbox(label="Final prompt", interactive=False, lines=3)
367
+ with gr.Accordion("Control maps", open=False):
368
+ control_gallery = gr.Gallery(columns=2, height=260, object_fit="contain", show_label=False)
369
+
370
+ with gr.Accordion("📘 ArchiCAD export guide & tuning tips", open=False):
371
+ gr.Markdown(TIPS)
372
+
373
+ mode.change(on_mode_change, mode, [depth_scale, canny_scale])
374
+ preview_btn.click(
375
+ preview_controls,
376
+ [image, long_side, mode, canny_low, canny_high],
377
+ control_gallery,
378
+ )
379
+ run_btn.click(
380
+ render,
381
+ [
382
+ image, description, scene, time_of_day, season, style, materials, negative_extra,
383
+ mode, long_side, depth_scale, canny_scale, canny_low, canny_high,
384
+ steps, guidance, num_variations, seed, do_refine, upscale, refine_strength,
385
+ ],
386
+ [gallery, used_seed, used_prompt],
387
+ )
388
+
389
+ demo.queue(max_size=20).launch()