SpacelyJohn commited on
Commit
9ac8ebb
Β·
verified Β·
1 Parent(s): a178a59

Upload 14 files

Browse files
Files changed (3) hide show
  1. app_gradio.py +64 -25
  2. app_gradio.py.bak +795 -0
  3. layered_prompt_test.py +18 -0
app_gradio.py CHANGED
@@ -411,49 +411,88 @@ def create_furniture_placement_zones(floor_mask, image):
411
 
412
  return placement_zones
413
 
414
- def create_precise_furniture_mask(image):
415
- """SIMPLIFIED: Create strong furniture mask in center floor area"""
416
  import cv2
417
 
418
- print("πŸ—οΈ Creating furniture placement mask...")
419
 
420
  img_array = np.array(image)
421
  h, w = img_array.shape[:2]
 
422
 
423
- # Create strong mask in center-bottom area
424
  mask = np.zeros((h, w), dtype=np.uint8)
425
 
426
- # Furniture placement zone - center of room, lower area
427
  center_x = w // 2
428
- center_y = int(h * 0.7) # 70% down from top
429
 
430
- # Large elliptical area for furniture - INCREASED SIZE
431
- radius_x = int(w * 0.4) # 40% of width (increased from 30%)
432
- radius_y = int(h * 0.25) # 25% of height (increased from 20%)
433
 
434
- # Create ellipse with full strength
435
  Y, X = np.ogrid[:h, :w]
436
- ellipse = ((X - center_x) / radius_x) ** 2 + ((Y - center_y) / radius_y) ** 2 <= 1
437
- mask[ellipse] = 255
438
 
439
- # Exclude only extreme edges
440
- edge = int(min(w, h) * 0.03) # 3% from edges
441
- mask[:edge, :] = 0
442
- mask[-edge:, :] = 0
443
- mask[:, :edge] = 0
444
- mask[:, -edge:] = 0
445
 
446
- # Light smoothing
447
- mask = cv2.GaussianBlur(mask, (3, 3), 0)
448
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
  furniture_pixels = np.count_nonzero(mask)
450
  coverage = (furniture_pixels / (h * w)) * 100
 
 
451
 
452
- print(f"πŸͺ‘ Furniture mask: {furniture_pixels} pixels ({coverage:.1f}% coverage)")
453
- print(f"πŸ“ Center: ({center_x}, {center_y}), Size: {radius_x}x{radius_y}")
 
 
 
454
 
455
  return Image.fromarray(mask).convert("RGB")
456
 
 
 
 
 
 
457
  def get_prompt_preview(room_type, design_style, inpainting_mode):
458
  """Generate preview of prompt and negative prompt that will be used"""
459
 
@@ -475,15 +514,15 @@ def get_prompt_preview(room_type, design_style, inpainting_mode):
475
  else:
476
  furniture_items = "appropriate furniture"
477
 
478
- positive_prompt = f"LAYER-BASED GENERATION: add {furniture_items} ONLY on floor surface, {design_style.lower()} style furniture, photorealistic, NEVER TOUCH STRUCTURE: walls ceiling windows floors are READ-only, HARD CONSTRAINT: furniture objects placed on existing floor only, PHYSICS VALIDATION: realistic furniture placement, professional interior design, ultra-realistic, perfect lighting integration"
479
  else:
480
  # Get detailed template-based prompt for full mode
481
  detailed_prompt = DETAILED_PROMPTS.get((room_type, design_style),
482
  DETAILED_PROMPTS[("Living Room", "Modern")])
483
  positive_prompt = f"photorealistic interior design, {detailed_prompt}, keep existing windows unchanged, preserve original window placement, professionally photographed, architectural photography, natural lighting, ultra-realistic, high resolution, sharp focus, interior design magazine quality, realistic textures, realistic materials"
484
 
485
- # HARD CONSTRAINTS negative prompt
486
- negative_prompt = "STRUCTURE MODIFICATION FORBIDDEN: changing walls, modifying walls, different walls, new walls, changing ceiling, modifying ceiling, different ceiling, changing floor, modifying floor, different floor, new windows, extra windows, additional windows, changing windows, modifying windows, different windows, window alterations, removing windows, PHYSICS VIOLATIONS: floating furniture, unrealistic placement, furniture in walls, furniture in ceiling, structural changes, architectural changes, room alterations, upholstered walls, fabric walls, plush walls, lowres, watermark, banner, logo, contactinfo, text, deformed, blurry, blur, out of focus, surreal, ugly"
487
 
488
  return positive_prompt, negative_prompt
489
 
 
411
 
412
  return placement_zones
413
 
414
+ def create_layered_furniture_mask(image):
415
+ """LAYERED APPROACH: Allow furniture on walls but prevent structure replacement"""
416
  import cv2
417
 
418
+ print("🎨 Creating layered furniture mask...")
419
 
420
  img_array = np.array(image)
421
  h, w = img_array.shape[:2]
422
+ gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
423
 
424
+ # Create large mask covering most of the room
425
  mask = np.zeros((h, w), dtype=np.uint8)
426
 
427
+ # Main furniture area - much larger to allow wall furniture
428
  center_x = w // 2
429
+ center_y = int(h * 0.6) # 60% down from top
430
 
431
+ # Large coverage area for layered furniture
432
+ radius_x = int(w * 0.45) # 45% of width - covers most room
433
+ radius_y = int(h * 0.35) # 35% of height - includes wall areas
434
 
435
+ # Create large elliptical coverage
436
  Y, X = np.ogrid[:h, :w]
437
+ main_ellipse = ((X - center_x) / radius_x) ** 2 + ((Y - center_y) / radius_y) ** 2 <= 1
 
438
 
439
+ # Add additional area for wall furniture (bookcases, wall art, etc.)
440
+ wall_coverage = np.zeros((h, w), dtype=bool)
441
+ wall_coverage[int(h*0.2):int(h*0.9), int(w*0.1):int(w*0.9)] = True
 
 
 
442
 
443
+ # Combine main area with wall coverage
444
+ furniture_area = main_ellipse | wall_coverage
445
 
446
+ # Create gradient mask - stronger in center, lighter near edges
447
+ for y in range(h):
448
+ for x in range(w):
449
+ if furniture_area[y, x]:
450
+ # Calculate distance from center
451
+ dist_from_center = np.sqrt((x - center_x)**2 + (y - center_y)**2)
452
+ max_dist = np.sqrt(radius_x**2 + radius_y**2)
453
+
454
+ # Gradient: strong in center, medium at edges
455
+ if dist_from_center <= max_dist * 0.6:
456
+ mask[y, x] = 255 # Full strength in center
457
+ elif dist_from_center <= max_dist:
458
+ # Fade from 255 to 180 towards edges
459
+ strength = 255 - int((dist_from_center - max_dist * 0.6) / (max_dist * 0.4) * 75)
460
+ mask[y, x] = max(180, strength)
461
+ else:
462
+ # Light coverage for wall areas
463
+ mask[y, x] = 180
464
+
465
+ # Exclude only window areas (very bright)
466
+ windows = gray > 220
467
+ mask[windows] = 0
468
+
469
+ # Exclude extreme corners (likely ceiling/wall joints)
470
+ corner_size = int(min(w, h) * 0.05)
471
+ mask[:corner_size, :corner_size] = 0 # Top-left
472
+ mask[:corner_size, -corner_size:] = 0 # Top-right
473
+
474
+ # Smooth transitions
475
+ mask = cv2.GaussianBlur(mask, (5, 5), 0)
476
+
477
+ # Statistics
478
  furniture_pixels = np.count_nonzero(mask)
479
  coverage = (furniture_pixels / (h * w)) * 100
480
+ strong_pixels = np.count_nonzero(mask > 240)
481
+ medium_pixels = np.count_nonzero((mask > 160) & (mask <= 240))
482
 
483
+ print(f"🎯 Layered mask coverage:")
484
+ print(f" - Total coverage: {furniture_pixels} pixels ({coverage:.1f}%)")
485
+ print(f" - Strong areas (floor): {strong_pixels} pixels")
486
+ print(f" - Medium areas (walls): {medium_pixels} pixels")
487
+ print(f" - Center: ({center_x}, {center_y}), Size: {radius_x}x{radius_y}")
488
 
489
  return Image.fromarray(mask).convert("RGB")
490
 
491
+ # Keep the old function for compatibility
492
+ def create_precise_furniture_mask(image):
493
+ """Legacy function - redirects to layered approach"""
494
+ return create_layered_furniture_mask(image)
495
+
496
  def get_prompt_preview(room_type, design_style, inpainting_mode):
497
  """Generate preview of prompt and negative prompt that will be used"""
498
 
 
514
  else:
515
  furniture_items = "appropriate furniture"
516
 
517
+ positive_prompt = f"LAYERED FURNITURE DESIGN: add {furniture_items}, {design_style.lower()} style, photorealistic, place furniture naturally on floor and against walls, add wall decorations like artwork and shelves, preserve existing room structure, no structural changes to walls windows or ceiling, layer furniture over existing surfaces, professional interior design, ultra-realistic lighting and shadows"
518
  else:
519
  # Get detailed template-based prompt for full mode
520
  detailed_prompt = DETAILED_PROMPTS.get((room_type, design_style),
521
  DETAILED_PROMPTS[("Living Room", "Modern")])
522
  positive_prompt = f"photorealistic interior design, {detailed_prompt}, keep existing windows unchanged, preserve original window placement, professionally photographed, architectural photography, natural lighting, ultra-realistic, high resolution, sharp focus, interior design magazine quality, realistic textures, realistic materials"
523
 
524
+ # LAYERED DESIGN negative prompt
525
+ negative_prompt = "STRUCTURAL REPLACEMENT FORBIDDEN: changing wall color, different wall texture, new walls, removing walls, changing ceiling, different ceiling, new windows, different windows, removing windows, changing floor material, different floor, PRESERVE STRUCTURE: keep original room architecture, floating furniture, unrealistic placement, furniture in ceiling, lowres, watermark, banner, logo, contactinfo, text, deformed, blurry, blur, out of focus, surreal, ugly"
526
 
527
  return positive_prompt, negative_prompt
528
 
app_gradio.py.bak ADDED
@@ -0,0 +1,795 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+ from PIL import Image
5
+ # import spaces # Comment out for local development
6
+
7
+ # Model imports with error handling
8
+ try:
9
+ from diffusers import ControlNetModel, StableDiffusionControlNetInpaintPipeline, UniPCMultistepScheduler
10
+ from transformers import AutoImageProcessor, SegformerForSemanticSegmentation
11
+ from controlnet_aux import MLSDdetector
12
+ MODELS_AVAILABLE = True
13
+ except ImportError as e:
14
+ print(f"Failed to load model libraries: {e}")
15
+ MODELS_AVAILABLE = False
16
+
17
+ # Detailed room templates by style combinations
18
+ DETAILED_PROMPTS = {
19
+ # Living Room combinations
20
+ ("Living Room", "Modern"): "A modern living room centered around a sleek sectional sofa and glass coffee table. A contemporary dining table with minimalist chairs provides an eating area while floor lamps and LED strips create ambient lighting throughout the clean space.",
21
+
22
+ ("Living Room", "Scandinavian"): "A Scandinavian living room with a cream linen sofa and light oak coffee table complemented by a wooden dining table with simple chairs. Floating shelves display ceramics and plants while a jute rug anchors the seating area.",
23
+
24
+ ("Living Room", "Industrial"): "An industrial living room anchored by a vintage leather sofa and reclaimed wood coffee table. A rustic dining table with metal chairs provides seating while pipe shelving and Edison bulb fixtures complete the urban loft aesthetic.",
25
+
26
+ ("Living Room", "Bohemian"): "A bohemian living room layered with colorful Persian rugs and floor cushions around a low wooden coffee table. A vintage dining table with mismatched chairs creates a dining space while macrame wall hangings and plants in woven baskets bring life to the room.",
27
+
28
+ ("Living Room", "Traditional"): "A traditional living room featuring a mahogany coffee table surrounded by wingback chairs upholstered in damask fabric. A formal dining table with upholstered chairs provides elegant seating while crystal chandeliers and antique side tables complete the classic design.",
29
+
30
+ ("Living Room", "Mid-Century"): "A mid-century living room showcasing an Eames lounge chair and walnut credenza. A walnut dining table with molded plastic chairs creates a dining area while tapered leg furniture and atomic-era lighting fixtures complete the retro aesthetic.",
31
+
32
+ ("Living Room", "Farmhouse"): "A farmhouse living room built around a weathered wood coffee table and slipcovered sofa. A rustic dining table with wooden chairs provides family seating while shiplap walls and mason jar lighting create authentic countryside charm.",
33
+
34
+ ("Living Room", "Luxury"): "A luxury living room featuring Italian leather seating around a marble-topped coffee table. An elegant marble dining table with upholstered chairs creates sophisticated dining while crystal chandeliers and gold leaf details complete the opulent design.",
35
+
36
+ # Bedroom combinations
37
+ ("Bedroom", "Modern"): "A modern bedroom centered around a platform bed with integrated nightstands and clean geometric lines. A floating vanity and built-in wardrobes maximize space while neutral colors and accent lighting create a serene sanctuary.",
38
+
39
+ ("Bedroom", "Scandinavian"): "A Scandinavian bedroom featuring a light wood bed frame with white linens and chunky knit throws. Floating nightstands hold minimalist lamps while a reading nook with sheepskin-draped chair creates the perfect hygge retreat.",
40
+
41
+ ("Bedroom", "Industrial"): "An industrial bedroom with exposed brick accent wall behind a wrought iron bed frame. Metal pipe clothing racks and vintage leather trunks serve as storage while Edison bulb fixtures provide warm lighting.",
42
+
43
+ ("Bedroom", "Bohemian"): "A bohemian bedroom centered around a relaxed canopy bed complemented by a large macrame wall hanging. An eclectic dresser serves as a unique storage solution while an array of potted plants brings life and color to the room.",
44
+
45
+ ("Bedroom", "Traditional"): "A traditional bedroom featuring an ornate four-poster bed with mahogany finish and luxurious bedding. Matching nightstands with crystal lamps flank the bed while an antique armoire provides elegant storage.",
46
+
47
+ ("Bedroom", "Mid-Century"): "A mid-century bedroom showcasing a walnut platform bed with geometric headboard and tapered leg nightstands. Atomic-era lighting fixtures complement bold graphic textiles while a vintage dresser displays period accessories.",
48
+
49
+ ("Bedroom", "Farmhouse"): "A farmhouse bedroom built around a weathered wood bed frame with vintage quilt and linen pillows. A distressed dresser topped with mason jar flowers adds rustic charm while barn door closets complete the countryside aesthetic.",
50
+
51
+ ("Bedroom", "Luxury"): "A luxury bedroom featuring a custom upholstered bed with tufted silk velvet headboard. Crystal chandeliers illuminate marble nightstands and cashmere bedding while a velvet chaise lounge creates an elegant sitting area.",
52
+
53
+ # Kitchen combinations
54
+ ("Kitchen", "Modern"): "A modern kitchen featuring handleless cabinets in matte charcoal with quartz waterfall countertops. Stainless steel appliances integrate seamlessly while pendant lights illuminate a large island with bar seating.",
55
+
56
+ ("Kitchen", "Scandinavian"): "A Scandinavian kitchen with light oak cabinets and white marble countertops creating a clean, airy feel. Open shelving displays ceramics while natural wood bar stools surround a central island with pendant lighting.",
57
+
58
+ ("Kitchen", "Industrial"): "An industrial kitchen featuring concrete countertops and exposed brick walls with black metal cabinets. Stainless steel appliances complement pipe shelving while a butcher block island and Edison bulb fixtures complete the urban aesthetic.",
59
+
60
+ ("Kitchen", "Bohemian"): "A bohemian kitchen mixing vintage cabinets in sage green with colorful mosaic tile backsplash. Open shelving displays pottery and plants while copper pots hang from wrought iron racks creating artistic charm.",
61
+
62
+ ("Kitchen", "Traditional"): "An elegant traditional kitchen with raised panel cabinets in warm cherry finish and granite countertops. Crown molding and decorative corbels add architectural interest, while a large island with turned legs provides additional workspace. Crystal pendant lights and oil rubbed bronze fixtures complete the classic design.",
63
+
64
+ ("Kitchen", "Mid-Century"): "A retro mid-century kitchen featuring flat-front cabinets in warm walnut with stainless steel countertops. Geometric tile backsplash in turquoise and white creates visual interest, while pendant lights with atomic-era design illuminate the breakfast bar. Vintage appliances and bar stools complete the period aesthetic.",
65
+
66
+ ("Kitchen", "Farmhouse"): "A rustic farmhouse kitchen with shaker cabinets in cream finish and butcher block countertops. Subway tile backsplash extends to the ceiling, while a large farmhouse sink sits below window herb gardens. Vintage-style appliances, barn door pantry, and mason jar lighting create authentic country charm.",
67
+
68
+ ("Kitchen", "Luxury"): "An opulent luxury kitchen featuring custom cabinets with gold hardware and Calacatta marble countertops. Professional-grade appliances hide behind matching panels, while crystal chandeliers illuminate a large marble island. Coffered ceiling, wine storage, and fresh flowers create an atmosphere of culinary elegance.",
69
+
70
+ # Dining Room combinations
71
+ ("Dining Room", "Modern"): "A sophisticated modern dining room centered around a glass-top table with sculptural metal base, surrounded by sleek upholstered chairs. Linear chandelier provides dramatic lighting, while a built-in sideboard displays contemporary art. Floor-to-ceiling windows and neutral palette create an elegant entertaining space.",
72
+
73
+ ("Dining Room", "Scandinavian"): "A warm Scandinavian dining room featuring a light oak table surrounded by wishbone chairs and illuminated by a simple pendant light. White walls showcase floating shelves with ceramics, while natural textures and plants create a cozy, family-friendly atmosphere perfect for long meals and conversation.",
74
+
75
+ ("Dining Room", "Industrial"): "An edgy industrial dining room with exposed brick walls and a reclaimed wood table paired with metal chairs. Vintage factory pendant lights hang from exposed beams, while a metal pipe shelving unit displays dishes and plants. Concrete floors and vintage leather seating create authentic urban character.",
76
+
77
+ ("Dining Room", "Bohemian"): "A vibrant bohemian dining room mixing vintage furniture pieces around an ornate wooden table draped with colorful textiles. Eclectic chandelier illuminates mismatched chairs, while gallery walls display an array of art and mirrors. Persian rugs and plants in macrame hangers create worldly dining charm.",
78
+
79
+ ("Dining Room", "Traditional"): "A formal traditional dining room featuring a mahogany pedestal table surrounded by upholstered chairs with carved details. Crystal chandelier provides elegant lighting above fine china displayed in built-in hutch. Persian rug, oil paintings, and silk drapes create sophisticated entertaining space.",
80
+
81
+ ("Dining Room", "Mid-Century"): "A stylish mid-century dining room showcasing a walnut table with geometric base surrounded by iconic molded chairs. Atomic-era chandelier illuminates vintage bar cart and credenza displaying period accessories. Bold geometric art and warm wood tones define the retro modern aesthetic.",
82
+
83
+ ("Dining Room", "Farmhouse"): "A cozy farmhouse dining room built around a weathered wood trestle table with bench seating and Windsor chairs. Mason jar chandelier provides rustic lighting, while a vintage hutch displays ironstone dishes. Shiplap walls, barn wood accents, and fresh flowers create countryside dining charm.",
84
+
85
+ ("Dining Room", "Luxury"): "An opulent luxury dining room featuring a custom table with marble top and gold leaf base, surrounded by velvet chairs with crystal buttons. Massive chandelier illuminates fine art and antique serving pieces displayed on marble-topped sideboard. Silk wallpaper and fresh orchids create elegant entertaining atmosphere.",
86
+
87
+ # Home Office combinations
88
+ ("Home Office", "Modern"): "A sleek modern home office featuring a minimalist white desk with integrated storage and ergonomic chair. Wall-mounted shelving displays books and decor, while hidden cable management keeps technology invisible. Large windows provide natural light, and strategic task lighting ensures productive work environment.",
89
+
90
+ ("Home Office", "Scandinavian"): "A bright Scandinavian home office with light wood desk and comfortable chair positioned near window for natural light. Floating shelves display plants and books, while pegboard organizes supplies and inspiration. White walls, natural textures, and cozy lighting create perfect work-from-home hygge.",
91
+
92
+ ("Home Office", "Industrial"): "An inspiring industrial home office with exposed brick walls and concrete floors, featuring a reclaimed wood desk paired with vintage leather chair. Metal pipe shelving displays books and files, while Edison bulb fixtures provide warm task lighting. Vintage typewriter and factory-style accessories complete the creative workspace.",
93
+
94
+ ("Home Office", "Bohemian"): "A creative bohemian home office mixing vintage furniture pieces including ornate desk and colorful textiles. Macrame wall hangings and gallery walls provide inspiration, while plants cascade from shelves and hanging planters. Layered rugs, brass accessories, and natural light create artistic work environment.",
95
+
96
+ ("Home Office", "Traditional"): "A sophisticated traditional home office featuring rich mahogany desk with leather inlay and matching bookshelf filled with leather-bound volumes. Elegant table lamp provides task lighting, while Persian rug and oil paintings create refined scholarly atmosphere. Wing-back chair offers comfortable reading spot.",
97
+
98
+ ("Home Office", "Mid-Century"): "A stylish mid-century home office showcasing iconic walnut desk with hairpin legs and molded chair. Atomic-era task lamp illuminates work surface, while geometric shelving displays books and period accessories. Bold artwork and warm wood tones create inspiring retro modern workspace.",
99
+
100
+ ("Home Office", "Farmhouse"): "A cozy farmhouse home office built around weathered wood desk with vintage chair and mason jar storage. Shiplap walls display inspiration boards and vintage signs, while galvanized accessories organize supplies. Natural light from window and rustic chandelier create productive countryside workspace.",
101
+
102
+ ("Home Office", "Luxury"): "An opulent luxury home office featuring custom built-ins in rich mahogany with gold hardware and marble accents. Executive leather chair faces elegant desk with crystal accessories, while chandelier illuminates fine art and fresh flowers. Silk drapes and Persian rug create sophisticated work environment.",
103
+
104
+ # Bathroom combinations
105
+ ("Bathroom", "Modern"): "A spa-like modern bathroom featuring floating vanity with vessel sinks and waterfall faucets. Floor-to-ceiling tiles create seamless surfaces, while frameless glass shower and freestanding tub maximize the minimalist aesthetic. Hidden LED lighting and natural stone accents create serene sanctuary.",
106
+
107
+ ("Bathroom", "Scandinavian"): "A bright Scandinavian bathroom with light wood vanity and white marble countertops creating clean, airy feel. Natural textures include woven baskets for storage and wood bath caddy, while plants thrive in the humid environment. Simple fixtures and abundant natural light complete the Nordic spa aesthetic.",
108
+
109
+ ("Bathroom", "Industrial"): "An edgy industrial bathroom with exposed pipes and concrete vanity topped with vessel sinks. Subway tiles and metal fixtures complement vintage mirror and Edison bulb lighting, while cast iron tub anchors the space. Raw materials and utilitarian design create authentic urban loft character.",
110
+
111
+ ("Bathroom", "Bohemian"): "A luxurious bohemian bathroom mixing vintage furniture pieces as vanity with ornate mirror and colorful mosaic tiles. Macrame plant hangers and layered textiles create spa-like atmosphere, while clawfoot tub surrounded by candles offers relaxing retreat. Eclectic accessories and natural elements complete the worldly aesthetic.",
112
+
113
+ ("Bathroom", "Traditional"): "An elegant traditional bathroom featuring marble countertops with undermount sinks and polished brass fixtures. Wainscoting and crown molding add architectural detail, while crystal chandelier provides luxury lighting. Clawfoot tub, Persian rug, and fresh flowers complete the sophisticated spa experience.",
114
+
115
+ ("Bathroom", "Mid-Century"): "A stylish mid-century bathroom showcasing geometric tiles and walnut vanity with brass fixtures. Atomic-era mirror and lighting fixtures complement sleek lines, while sunken tub and bold accent colors define the retro modern aesthetic. Period accessories and warm wood tones complete the vintage look.",
116
+
117
+ ("Bathroom", "Farmhouse"): "A cozy farmhouse bathroom with shiplap walls and weathered wood vanity topped with vessel sinks. Galvanized fixtures and mason jar lighting create rustic charm, while clawfoot tub surrounded by vintage accessories offers relaxing retreat. Natural textures and countryside elements complete the authentic aesthetic.",
118
+
119
+ ("Bathroom", "Luxury"): "An opulent luxury bathroom featuring marble surfaces throughout with gold fixtures and crystal chandelier. Freestanding soaking tub faces fireplace, while double vanity offers ample storage. Heated floors, fresh orchids, and plush towels create five-star spa experience at home.",
120
+
121
+ # Kids Room combinations
122
+ ("Kids Room", "Modern"): "A sleek modern kids room featuring built-in bunk beds with integrated storage and study areas. Bright accent colors pop against white walls, while interactive technology and educational displays encourage learning. Smart storage solutions and safety features create functional space for growing children.",
123
+
124
+ ("Kids Room", "Scandinavian"): "A cozy Scandinavian kids room with light wood furniture and neutral colors creating calm environment. Natural toys and books display on floating shelves, while cozy reading nook with sheepskin throw encourages quiet time. Simple design and quality materials create timeless childhood space.",
125
+
126
+ ("Kids Room", "Industrial"): "A creative industrial kids room with exposed elements softened by colorful textiles and playful accessories. Metal pipe clothing rack and vintage trunks provide storage, while chalkboard walls encourage artistic expression. Edison bulb fixtures and reclaimed wood furniture create unique urban nursery.",
127
+
128
+ ("Kids Room", "Bohemian"): "A whimsical bohemian kids room layered with colorful textiles and global-inspired decor. Teepee reading corner and floor cushions create cozy play areas, while macrame details and plants add natural elements. Eclectic furniture and artistic displays encourage creativity and imagination.",
129
+
130
+ ("Kids Room", "Traditional"): "A classic traditional kids room featuring quality wood furniture including four-poster bed and matching dresser. Timeless patterns and colors create sophisticated yet age-appropriate space, while built-in bookcases encourage reading. Heirloom quality pieces grow with child through years.",
131
+
132
+ ("Kids Room", "Mid-Century"): "A playful mid-century kids room showcasing period furniture in miniature scale with bold geometric patterns. Atomic-era accessories and retro color palette create fun vintage atmosphere, while built-in storage and study areas support growing needs. Quality design principles create lasting childhood memories.",
133
+
134
+ ("Kids Room", "Farmhouse"): "A charming farmhouse kids room with weathered wood furniture and vintage accessories creating countryside appeal. Shiplap accent wall displays family photos and artwork, while galvanized storage bins organize toys. Natural materials and rustic charm create wholesome childhood environment.",
135
+
136
+ ("Kids Room", "Luxury"): "An elegant luxury kids room featuring custom built-ins and high-end materials adapted for young users. Crystal chandelier and silk curtains create sophisticated atmosphere, while quality furniture and accessories ensure lasting beauty. Premium materials and thoughtful design create special childhood sanctuary.",
137
+
138
+ # Master Bedroom combinations
139
+ ("Master Bedroom", "Modern"): "A sophisticated modern master bedroom featuring king platform bed with integrated nightstands and dramatic headboard wall. Floor-to-ceiling windows with automated controls provide natural light and privacy, while walk-in closet and ensuite bathroom create luxurious retreat. Neutral palette and clean lines ensure restful environment.",
140
+
141
+ ("Master Bedroom", "Scandinavian"): "A serene Scandinavian master bedroom with light wood bed frame and crisp white linens creating peaceful sanctuary. Cozy sitting area by window includes reading chair and side table, while natural textures and muted colors promote relaxation. Quality materials and simple design create timeless bedroom retreat.",
142
+
143
+ ("Master Bedroom", "Industrial"): "A dramatic industrial master bedroom with exposed brick accent wall and steel beam ceiling details. Vintage leather furniture and metal accents complement platform bed, while factory-style lighting provides ambient illumination. Raw materials and urban aesthetics create unique romantic retreat.",
144
+
145
+ ("Master Bedroom", "Bohemian"): "A luxurious bohemian master bedroom with canopy bed draped in flowing fabrics and surrounded by eclectic vintage furniture. Layered textiles in rich colors create cozy atmosphere, while plants and global accessories add worldly charm. Artistic details and natural elements create romantic sanctuary.",
146
+
147
+ ("Master Bedroom", "Traditional"): "An elegant traditional master bedroom featuring ornate four-poster bed with luxury bedding and matching furniture suite. Crystal chandelier and silk drapes create formal atmosphere, while sitting area and fireplace add comfort. Classic design elements and quality materials create timeless romantic retreat.",
148
+
149
+ ("Master Bedroom", "Mid-Century"): "A stylish mid-century master bedroom showcasing iconic furniture pieces including walnut platform bed and vintage accessories. Bold geometric patterns and warm wood tones create retro modern atmosphere, while period lighting and textiles complete the vintage aesthetic. Quality design creates lasting bedroom sanctuary.",
150
+
151
+ ("Master Bedroom", "Farmhouse"): "A cozy farmhouse master bedroom built around weathered wood bed frame with vintage quilt and natural linens. Shiplap walls and rustic accessories create countryside charm, while sitting area by window offers peaceful retreat. Natural materials and authentic details create romantic rural sanctuary.",
152
+
153
+ ("Master Bedroom", "Luxury"): "An opulent luxury master bedroom featuring custom upholstered bed with silk velvet headboard and crystal chandelier illumination. Marble fireplace anchors sitting area with velvet chairs, while walk-in closet and spa bathroom create five-star hotel experience. Premium materials and elegant details create ultimate romantic retreat."
154
+ }
155
+
156
+ # Room types and styles for dropdowns
157
+ ROOM_TYPES = list(set([combo[0] for combo in DETAILED_PROMPTS.keys()]))
158
+ STYLE_TYPES = list(set([combo[1] for combo in DETAILED_PROMPTS.keys()]))
159
+
160
+ # Global variables for models
161
+ pipe = None
162
+ seg_processor = None
163
+ seg_model = None
164
+ mlsd_processor = None
165
+
166
+ def load_models():
167
+ """Load models (called once)"""
168
+ global pipe, seg_processor, seg_model, mlsd_processor
169
+
170
+ if not MODELS_AVAILABLE:
171
+ return "❌ Model libraries not available"
172
+
173
+ try:
174
+ print("πŸ”„ Loading AI models...")
175
+
176
+ # Dual ControlNet setup
177
+ controlnet = [
178
+ ControlNetModel.from_pretrained(
179
+ "BertChristiaens/controlnet-seg-room",
180
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
181
+ ),
182
+ ControlNetModel.from_pretrained(
183
+ "lllyasviel/sd-controlnet-mlsd",
184
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
185
+ ),
186
+ ]
187
+
188
+ # Main pipeline
189
+ pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
190
+ "SG161222/Realistic_Vision_V3.0_VAE",
191
+ controlnet=controlnet,
192
+ safety_checker=None,
193
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
194
+ )
195
+
196
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
197
+
198
+ if torch.cuda.is_available():
199
+ pipe = pipe.to("cuda")
200
+
201
+ # Segmentation models
202
+ seg_processor = AutoImageProcessor.from_pretrained("nvidia/segformer-b5-finetuned-ade-640-640")
203
+ seg_model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b5-finetuned-ade-640-640")
204
+ mlsd_processor = MLSDdetector.from_pretrained("lllyasviel/Annotators")
205
+
206
+ print("βœ… Models loaded successfully!")
207
+ return "βœ… Models loaded successfully!"
208
+
209
+ except Exception as e:
210
+ return f"❌ Failed to load models: {e}"
211
+
212
+ def create_full_mask(image):
213
+ """Create full image mask to eliminate all boundaries"""
214
+ w, h = image.size
215
+
216
+ # Full white mask - allows modification of entire image
217
+ mask = np.ones((h, w), dtype=np.uint8) * 255
218
+
219
+ return Image.fromarray(mask).convert("RGB")
220
+
221
+ def create_smart_mask(image):
222
+ """Create conservative smart mask that primarily targets floor center area"""
223
+ import cv2
224
+
225
+ # Convert PIL to numpy
226
+ img_array = np.array(image)
227
+ h, w = img_array.shape[:2]
228
+
229
+ # Create a very conservative mask - only target center floor area
230
+ mask = np.zeros((h, w), dtype=np.uint8)
231
+
232
+ # Define safe zone - center 60% of image, bottom 70% (floor area)
233
+ safe_x_start = int(w * 0.2) # 20% from left
234
+ safe_x_end = int(w * 0.8) # 20% from right
235
+ safe_y_start = int(h * 0.3) # 30% from top (avoid ceiling)
236
+ safe_y_end = int(h * 0.9) # 10% from bottom
237
+
238
+ # Create elliptical mask in center floor area
239
+ center_x, center_y = w // 2, int(h * 0.65) # Slightly lower center
240
+
241
+ # Create ellipse parameters - larger size for better furniture generation
242
+ ellipse_w = int(w * 0.40) # 40% of width (larger area)
243
+ ellipse_h = int(h * 0.28) # 28% of height (larger area)
244
+
245
+ # Draw ellipse mask
246
+ Y, X = np.ogrid[:h, :w]
247
+ ellipse_mask = ((X - center_x) / ellipse_w) ** 2 + ((Y - center_y) / ellipse_h) ** 2 <= 1
248
+
249
+ # Apply gradient within ellipse - strongest at center, fade to edges
250
+ for y in range(h):
251
+ for x in range(w):
252
+ if ellipse_mask[y, x]:
253
+ # Distance from ellipse center
254
+ dist = np.sqrt(((x - center_x) / ellipse_w) ** 2 + ((y - center_y) / ellipse_h) ** 2)
255
+ # Gradient: stronger at center (dist=0), weaker at edges (dist=1)
256
+ strength = max(0, 1 - dist) * 255 # Max 255 - full strength
257
+ mask[y, x] = int(strength)
258
+
259
+ # Additional safety: exclude areas that are too bright (likely windows) or too dark (likely corners)
260
+ gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
261
+
262
+ # Mask out very bright areas (windows) and very dark areas (corners/shadows)
263
+ bright_mask = gray > 220 # Very bright
264
+ dark_mask = gray < 40 # Very dark
265
+
266
+ # Reduced corner protection - exclude outer 5% of image only
267
+ corner_margin = 0.05
268
+ corner_mask = np.zeros((h, w), dtype=bool)
269
+ corner_mask[:int(h*corner_margin), :] = True # Top
270
+ corner_mask[-int(h*corner_margin):, :] = True # Bottom
271
+ corner_mask[:, :int(w*corner_margin)] = True # Left
272
+ corner_mask[:, -int(w*corner_margin):] = True # Right
273
+
274
+ exclude_mask = bright_mask | dark_mask | corner_mask
275
+ mask[exclude_mask] = 0
276
+
277
+ # Apply blur for smooth transitions
278
+ mask = cv2.GaussianBlur(mask, (21, 21), 0)
279
+
280
+ # Debug: print mask statistics
281
+ print(f"Mask stats - Min: {mask.min()}, Max: {mask.max()}, Non-zero pixels: {np.count_nonzero(mask)}")
282
+ print(f"Mask shape: {mask.shape}, Image shape: {img_array.shape}")
283
+
284
+ # Save mask for debugging (temporary)
285
+ mask_debug = Image.fromarray(mask).convert("RGB")
286
+ try:
287
+ mask_debug.save("/tmp/debug_mask.png")
288
+ print("Debug mask saved to /tmp/debug_mask.png")
289
+ except:
290
+ pass
291
+
292
+ return mask_debug
293
+
294
+ def analyze_room_structure(image):
295
+ """NEVER TOUCH STRUCTURE: Analyze walls, windows, ceiling - READ ONLY"""
296
+ import cv2
297
+
298
+ img_array = np.array(image)
299
+ h, w = img_array.shape[:2]
300
+ gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
301
+
302
+ # Detect structural elements that must NEVER be modified
303
+ structure_mask = np.zeros((h, w), dtype=np.uint8)
304
+
305
+ # 1. Wall detection (strong vertical/horizontal lines)
306
+ edges = cv2.Canny(gray, 50, 150)
307
+ lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=50, minLineLength=50, maxLineGap=10)
308
+
309
+ if lines is not None:
310
+ for line in lines:
311
+ x1, y1, x2, y2 = line[0]
312
+ cv2.line(structure_mask, (x1, y1), (x2, y2), 255, 5)
313
+
314
+ # 2. Window detection (very bright rectangular areas)
315
+ bright_mask = gray > 200
316
+ contours, _ = cv2.findContours(bright_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
317
+ for contour in contours:
318
+ area = cv2.contourArea(contour)
319
+ if area > 1000: # Large bright areas = windows
320
+ cv2.fillPoly(structure_mask, [contour], 255)
321
+
322
+ # 3. Ceiling detection (top 30% of image)
323
+ structure_mask[:int(h*0.3), :] = 255
324
+
325
+ # 4. Wall edges (outer 15% of image)
326
+ border = int(min(w, h) * 0.15)
327
+ structure_mask[:border, :] = 255 # Top
328
+ structure_mask[-border:, :] = 255 # Bottom
329
+ structure_mask[:, :border] = 255 # Left
330
+ structure_mask[:, -border:] = 255 # Right
331
+
332
+ # Expand structure protection
333
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
334
+ structure_mask = cv2.dilate(structure_mask, kernel, iterations=2)
335
+
336
+ return structure_mask
337
+
338
+ def detect_floor_area(image, structure_mask):
339
+ """Physics-based floor detection with hard constraints"""
340
+ import cv2
341
+
342
+ img_array = np.array(image)
343
+ h, w = img_array.shape[:2]
344
+ gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
345
+
346
+ # Floor must be in bottom 70% of image (physics constraint)
347
+ floor_region = np.zeros((h, w), dtype=np.uint8)
348
+ floor_start_y = int(h * 0.3) # Floor cannot be in top 30%
349
+
350
+ # Detect horizontal surfaces (floor characteristics)
351
+ gray_floor = gray[floor_start_y:, :]
352
+
353
+ # Horizontal gradient analysis - floors have low vertical variation
354
+ grad_y = cv2.Sobel(gray_floor, cv2.CV_64F, 0, 1, ksize=3)
355
+ horizontal_surfaces = np.abs(grad_y) < 15
356
+
357
+ # Consistent texture/color (floor property)
358
+ blurred = cv2.GaussianBlur(gray_floor, (21, 21), 0)
359
+ floor_brightness = np.median(blurred)
360
+ consistent_areas = np.abs(blurred - floor_brightness) < 25
361
+
362
+ # Combine constraints: horizontal + consistent = floor
363
+ floor_candidates = horizontal_surfaces & consistent_areas
364
+ floor_region[floor_start_y:, :] = floor_candidates.astype(np.uint8) * 255
365
+
366
+ # HARD CONSTRAINT: Remove any overlap with structure
367
+ floor_region[structure_mask > 0] = 0
368
+
369
+ # Physics validation: floor must be connected and substantial
370
+ contours, _ = cv2.findContours(floor_region, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
371
+ if contours:
372
+ # Keep only largest floor area (physics: one continuous floor)
373
+ largest = max(contours, key=cv2.contourArea)
374
+ floor_region = np.zeros_like(floor_region)
375
+ cv2.fillPoly(floor_region, [largest], 255)
376
+
377
+ return floor_region
378
+
379
+ def create_furniture_placement_zones(floor_mask, image):
380
+ """Define valid furniture placement with physics constraints"""
381
+ import cv2
382
+
383
+ img_array = np.array(image)
384
+ h, w = img_array.shape[:2]
385
+
386
+ # Physics constraints for furniture placement
387
+ placement_zones = np.zeros((h, w), dtype=np.uint8)
388
+
389
+ if np.any(floor_mask > 0):
390
+ # Find floor center of mass (realistic furniture placement)
391
+ moments = cv2.moments(floor_mask)
392
+ if moments["m00"] != 0:
393
+ cx = int(moments["m10"] / moments["m00"])
394
+ cy = int(moments["m01"] / moments["m00"])
395
+ else:
396
+ cx, cy = w//2, int(h*0.7)
397
+
398
+ # Create placement zones around floor center
399
+ # Bedroom: bed against wall, nightstands beside
400
+ if True: # For now, general furniture placement
401
+ # Main furniture zone (bed, sofa, etc.)
402
+ main_radius_x = int(w * 0.25)
403
+ main_radius_y = int(h * 0.15)
404
+
405
+ Y, X = np.ogrid[:h, :w]
406
+ main_zone = ((X - cx) / main_radius_x) ** 2 + ((Y - cy) / main_radius_y) ** 2 <= 1
407
+ placement_zones[main_zone & (floor_mask > 0)] = 255
408
+
409
+ # Only place furniture on detected floor
410
+ placement_zones[floor_mask == 0] = 0
411
+
412
+ return placement_zones
413
+
414
+ def create_layered_furniture_mask(image):
415
+ """LAYERED APPROACH: Allow furniture on walls but prevent structure replacement"""
416
+ import cv2
417
+
418
+ print("🎨 Creating layered furniture mask...")
419
+
420
+ img_array = np.array(image)
421
+ h, w = img_array.shape[:2]
422
+ gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
423
+
424
+ # Create large mask covering most of the room
425
+ mask = np.zeros((h, w), dtype=np.uint8)
426
+
427
+ # Main furniture area - much larger to allow wall furniture
428
+ center_x = w // 2
429
+ center_y = int(h * 0.6) # 60% down from top
430
+
431
+ # Large coverage area for layered furniture
432
+ radius_x = int(w * 0.45) # 45% of width - covers most room
433
+ radius_y = int(h * 0.35) # 35% of height - includes wall areas
434
+
435
+ # Create large elliptical coverage
436
+ Y, X = np.ogrid[:h, :w]
437
+ main_ellipse = ((X - center_x) / radius_x) ** 2 + ((Y - center_y) / radius_y) ** 2 <= 1
438
+
439
+ # Add additional area for wall furniture (bookcases, wall art, etc.)
440
+ wall_coverage = np.zeros((h, w), dtype=bool)
441
+ wall_coverage[int(h*0.2):int(h*0.9), int(w*0.1):int(w*0.9)] = True
442
+
443
+ # Combine main area with wall coverage
444
+ furniture_area = main_ellipse | wall_coverage
445
+
446
+ # Create gradient mask - stronger in center, lighter near edges
447
+ for y in range(h):
448
+ for x in range(w):
449
+ if furniture_area[y, x]:
450
+ # Calculate distance from center
451
+ dist_from_center = np.sqrt((x - center_x)**2 + (y - center_y)**2)
452
+ max_dist = np.sqrt(radius_x**2 + radius_y**2)
453
+
454
+ # Gradient: strong in center, medium at edges
455
+ if dist_from_center <= max_dist * 0.6:
456
+ mask[y, x] = 255 # Full strength in center
457
+ elif dist_from_center <= max_dist:
458
+ # Fade from 255 to 180 towards edges
459
+ strength = 255 - int((dist_from_center - max_dist * 0.6) / (max_dist * 0.4) * 75)
460
+ mask[y, x] = max(180, strength)
461
+ else:
462
+ # Light coverage for wall areas
463
+ mask[y, x] = 180
464
+
465
+ # Exclude only window areas (very bright)
466
+ windows = gray > 220
467
+ mask[windows] = 0
468
+
469
+ # Exclude extreme corners (likely ceiling/wall joints)
470
+ corner_size = int(min(w, h) * 0.05)
471
+ mask[:corner_size, :corner_size] = 0 # Top-left
472
+ mask[:corner_size, -corner_size:] = 0 # Top-right
473
+
474
+ # Smooth transitions
475
+ mask = cv2.GaussianBlur(mask, (5, 5), 0)
476
+
477
+ # Statistics
478
+ furniture_pixels = np.count_nonzero(mask)
479
+ coverage = (furniture_pixels / (h * w)) * 100
480
+ strong_pixels = np.count_nonzero(mask > 240)
481
+ medium_pixels = np.count_nonzero((mask > 160) & (mask <= 240))
482
+
483
+ print(f"🎯 Layered mask coverage:")
484
+ print(f" - Total coverage: {furniture_pixels} pixels ({coverage:.1f}%)")
485
+ print(f" - Strong areas (floor): {strong_pixels} pixels")
486
+ print(f" - Medium areas (walls): {medium_pixels} pixels")
487
+ print(f" - Center: ({center_x}, {center_y}), Size: {radius_x}x{radius_y}")
488
+
489
+ return Image.fromarray(mask).convert("RGB")
490
+
491
+ # Keep the old function for compatibility
492
+ def create_precise_furniture_mask(image):
493
+ """Legacy function - redirects to layered approach"""
494
+ return create_layered_furniture_mask(image)
495
+
496
+ def get_prompt_preview(room_type, design_style, inpainting_mode):
497
+ """Generate preview of prompt and negative prompt that will be used"""
498
+
499
+ # Create positive prompt based on mode
500
+ if inpainting_mode == "smart":
501
+ # Simple, direct furniture prompt for smart mode
502
+ if room_type == "Living Room":
503
+ furniture_items = "sofa, coffee table, side tables, floor lamp, dining table with chairs"
504
+ elif room_type == "Bedroom" or room_type == "Master Bedroom":
505
+ furniture_items = "MUST INCLUDE: large bed with headboard, two nightstands with lamps, dresser or wardrobe, accent chair, area rug under bed"
506
+ elif room_type == "Kitchen":
507
+ furniture_items = "kitchen island, bar stools, dining table with chairs"
508
+ elif room_type == "Dining Room":
509
+ furniture_items = "dining table, dining chairs, sideboard"
510
+ elif room_type == "Home Office":
511
+ furniture_items = "desk, office chair, bookshelf, filing cabinet"
512
+ elif room_type == "Bathroom":
513
+ furniture_items = "vanity, mirror, storage cabinet"
514
+ else:
515
+ furniture_items = "appropriate furniture"
516
+
517
+ positive_prompt = f"LAYER-BASED GENERATION: add {furniture_items} ONLY on floor surface, {design_style.lower()} style furniture, photorealistic, NEVER TOUCH STRUCTURE: walls ceiling windows floors are READ-only, HARD CONSTRAINT: furniture objects placed on existing floor only, PHYSICS VALIDATION: realistic furniture placement, professional interior design, ultra-realistic, perfect lighting integration"
518
+ else:
519
+ # Get detailed template-based prompt for full mode
520
+ detailed_prompt = DETAILED_PROMPTS.get((room_type, design_style),
521
+ DETAILED_PROMPTS[("Living Room", "Modern")])
522
+ positive_prompt = f"photorealistic interior design, {detailed_prompt}, keep existing windows unchanged, preserve original window placement, professionally photographed, architectural photography, natural lighting, ultra-realistic, high resolution, sharp focus, interior design magazine quality, realistic textures, realistic materials"
523
+
524
+ # HARD CONSTRAINTS negative prompt
525
+ negative_prompt = "STRUCTURE MODIFICATION FORBIDDEN: changing walls, modifying walls, different walls, new walls, changing ceiling, modifying ceiling, different ceiling, changing floor, modifying floor, different floor, new windows, extra windows, additional windows, changing windows, modifying windows, different windows, window alterations, removing windows, PHYSICS VIOLATIONS: floating furniture, unrealistic placement, furniture in walls, furniture in ceiling, structural changes, architectural changes, room alterations, upholstered walls, fabric walls, plush walls, lowres, watermark, banner, logo, contactinfo, text, deformed, blurry, blur, out of focus, surreal, ugly"
526
+
527
+ return positive_prompt, negative_prompt
528
+
529
+ def post_process_blend(original, generated):
530
+ """Post-process to reduce seam artifacts between original and generated areas"""
531
+ from PIL import ImageFilter
532
+
533
+ # Apply slight blur to reduce harsh transitions
534
+ blended = generated.filter(ImageFilter.GaussianBlur(radius=0.8))
535
+
536
+ # Blend with original in border areas for smoother transitions
537
+ orig_array = np.array(original, dtype=np.float32)
538
+ gen_array = np.array(blended, dtype=np.float32)
539
+
540
+ h, w = orig_array.shape[:2]
541
+
542
+ # Create blend mask - stronger blending near edges
543
+ blend_mask = np.ones((h, w), dtype=np.float32)
544
+
545
+ # Reduce blend strength near borders
546
+ border_size = min(h, w) // 20
547
+ for i in range(border_size):
548
+ alpha = i / border_size
549
+ blend_mask[i, :] = alpha
550
+ blend_mask[-(i+1), :] = alpha
551
+ blend_mask[:, i] = np.minimum(blend_mask[:, i], alpha)
552
+ blend_mask[:, -(i+1)] = np.minimum(blend_mask[:, -(i+1)], alpha)
553
+
554
+ # Apply blending
555
+ if len(orig_array.shape) == 3:
556
+ blend_mask = blend_mask[:, :, np.newaxis]
557
+
558
+ final_array = orig_array * (1 - blend_mask) + gen_array * blend_mask
559
+ final_array = np.clip(final_array, 0, 255).astype(np.uint8)
560
+
561
+ return Image.fromarray(final_array)
562
+
563
+ # @spaces.GPU # Comment out for local development
564
+ def design_space(input_image, room_type, design_style, inpainting_mode, num_steps, guidance_scale, strength):
565
+ """Generate space design using ZeroGPU"""
566
+
567
+ global pipe, seg_processor, seg_model, mlsd_processor
568
+
569
+ if input_image is None:
570
+ return None, "❌ Please upload an image!"
571
+
572
+ if pipe is None:
573
+ # Load models if not already loaded
574
+ status = load_models()
575
+ if "❌" in status:
576
+ return None, status
577
+
578
+ try:
579
+ # Use detailed template-based prompt with photorealistic emphasis
580
+ detailed_prompt = DETAILED_PROMPTS.get((room_type, design_style),
581
+ DETAILED_PROMPTS[("Living Room", "Modern")])
582
+ prompt = f"photorealistic interior design, {detailed_prompt}, professionally photographed, architectural photography, natural lighting, ultra-realistic, high resolution, sharp focus, interior design magazine quality, realistic textures, realistic materials"
583
+ prompt_type = f"{room_type} in {design_style} style"
584
+
585
+ # Resize image
586
+ orig_w, orig_h = input_image.size
587
+ max_size = 768
588
+ if max(orig_w, orig_h) > max_size:
589
+ if orig_w > orig_h:
590
+ new_w, new_h = max_size, int(max_size * orig_h / orig_w)
591
+ else:
592
+ new_w, new_h = int(max_size * orig_w / orig_h), max_size
593
+ else:
594
+ new_w, new_h = orig_w, orig_h
595
+
596
+ resized_image = input_image.resize((new_w, new_h))
597
+
598
+ # Simple segmentation (create basic control image)
599
+ seg_control = resized_image.copy()
600
+
601
+ # MLSD processing
602
+ if mlsd_processor:
603
+ mlsd_image = mlsd_processor(resized_image)
604
+ mlsd_image = mlsd_image.resize((new_w, new_h))
605
+ else:
606
+ mlsd_image = resized_image.copy()
607
+
608
+ # Create mask based on selected mode
609
+ if inpainting_mode == "smart":
610
+ # Use precise smart mask for furniture-only placement
611
+ mask_image = create_precise_furniture_mask(resized_image)
612
+
613
+ # Simple, direct furniture prompt for smart mode
614
+ if room_type == "Living Room":
615
+ furniture_items = "sofa, coffee table, side tables, floor lamp, dining table with chairs"
616
+ elif room_type == "Bedroom":
617
+ furniture_items = "bed, nightstands, dresser, chair"
618
+ elif room_type == "Kitchen":
619
+ furniture_items = "kitchen island, bar stools, dining table with chairs"
620
+ elif room_type == "Dining Room":
621
+ furniture_items = "dining table, dining chairs, sideboard"
622
+ elif room_type == "Home Office":
623
+ furniture_items = "desk, office chair, bookshelf, filing cabinet"
624
+ elif room_type == "Bathroom":
625
+ furniture_items = "vanity, mirror, storage cabinet"
626
+ else:
627
+ furniture_items = "appropriate furniture"
628
+
629
+ prompt = f"add {furniture_items} to this room, {design_style.lower()} style furniture, photorealistic, keep all walls ceiling floor windows unchanged, preserve all existing room structure, only add furniture objects on the floor, do not modify room architecture, professional interior design, ultra-realistic, high quality"
630
+ print(f"Smart mode prompt: {prompt}")
631
+ else:
632
+ mask_image = create_full_mask(resized_image)
633
+ prompt = f"photorealistic interior design, {detailed_prompt}, keep existing windows unchanged, preserve original window placement, professionally photographed, architectural photography, natural lighting, ultra-realistic, high resolution, sharp focus, interior design magazine quality, realistic textures, realistic materials"
634
+ print("Using full mask mode")
635
+
636
+ # Generate image with optimized strength for furniture generation
637
+ if inpainting_mode == "smart":
638
+ # Higher strength for better furniture generation in smart mode
639
+ actual_strength = min(0.85, float(strength) * 1.1) # Boost by 10%, cap at 0.85
640
+ else:
641
+ actual_strength = float(strength)
642
+
643
+ print(f"Generation parameters:")
644
+ print(f" - Mode: {inpainting_mode}")
645
+ print(f" - Original strength: {strength}")
646
+ print(f" - Actual strength: {actual_strength}")
647
+ print(f" - Steps: {int(num_steps)}")
648
+ print(f" - Guidance scale: {float(guidance_scale)}")
649
+
650
+ result = pipe(
651
+ prompt=prompt,
652
+ negative_prompt="lowres, watermark, banner, logo, watermark, contactinfo, text, deformed, blurry, blur, out of focus, out of frame, surreal, extra, ugly, upholstered walls, fabric walls, plush walls, mirror, mirrored, functional, realistic, new windows, extra windows, additional windows, changing windows, modifying windows, different windows, window alterations, removing windows, changing walls, modifying walls, different walls, new walls, changing ceiling, modifying ceiling, different ceiling, changing floor, modifying floor, different floor, structural changes, architectural changes, room alterations",
653
+ num_inference_steps=int(num_steps),
654
+ strength=actual_strength,
655
+ guidance_scale=float(guidance_scale),
656
+ image=resized_image,
657
+ mask_image=mask_image,
658
+ control_image=[seg_control, mlsd_image],
659
+ controlnet_conditioning_scale=[0.4, 0.2],
660
+ control_guidance_start=[0, 0.1],
661
+ control_guidance_end=[0.5, 0.25],
662
+ ).images[0]
663
+
664
+ # Restore original size
665
+ final_image = result.resize((orig_w, orig_h), Image.Resampling.LANCZOS)
666
+
667
+ # No post-processing needed with full mask
668
+
669
+ success_msg = f"βœ… {prompt_type} completed! Generated in {int(num_steps)} steps."
670
+
671
+ return final_image, success_msg
672
+
673
+ except Exception as e:
674
+ error_msg = f"❌ Error: {str(e)}"
675
+ return None, error_msg
676
+
677
+ # Gradio interface
678
+ def create_interface():
679
+ """Create Gradio interface"""
680
+
681
+ with gr.Blocks(title="Spacely AI Interior Designer", theme=gr.themes.Soft()) as demo:
682
+ gr.HTML("<h1>🏠 Spacely AI Interior Designer</h1>")
683
+ gr.Markdown("Upload an empty room photo and AI will design it with furniture")
684
+
685
+ with gr.Row():
686
+ with gr.Column(scale=1):
687
+ # Input controls
688
+ input_image = gr.Image(
689
+ label="Upload Empty Room Image",
690
+ type="pil",
691
+ height=300
692
+ )
693
+
694
+ room_type = gr.Dropdown(
695
+ choices=ROOM_TYPES,
696
+ value="Living Room",
697
+ label="Select Room Type"
698
+ )
699
+
700
+ design_style = gr.Dropdown(
701
+ choices=STYLE_TYPES,
702
+ value="Modern",
703
+ label="Select Design Style"
704
+ )
705
+
706
+ inpainting_mode = gr.Radio(
707
+ choices=[
708
+ ("Complete Room Redesign", "full"),
709
+ ("Add Furniture Only (Preserve Walls)", "smart")
710
+ ],
711
+ value="smart",
712
+ label="🎨 Design Mode",
713
+ info="Choose how to modify your image"
714
+ )
715
+
716
+ with gr.Accordion("βš™οΈ Advanced Settings", open=False):
717
+ num_steps = gr.Slider(
718
+ minimum=1, maximum=500, value=50, step=1,
719
+ label="Number of denoising steps"
720
+ )
721
+ guidance_scale = gr.Slider(
722
+ minimum=1, maximum=50, value=18, step=0.5,
723
+ label="Scale for classifier-free guidance"
724
+ )
725
+ strength = gr.Slider(
726
+ minimum=0, maximum=1, value=0.8, step=0.05,
727
+ label="Prompt strength for inpainting"
728
+ )
729
+
730
+ generate_btn = gr.Button("🎨 Generate Design", variant="primary", size="lg")
731
+
732
+ # Prompt Preview Section
733
+ with gr.Accordion("πŸ“‹ Prompt Preview", open=False):
734
+ positive_prompt_preview = gr.Textbox(
735
+ label="βœ… Positive Prompt",
736
+ lines=4,
737
+ interactive=False,
738
+ value="Select room type and style to see prompt preview"
739
+ )
740
+ negative_prompt_preview = gr.Textbox(
741
+ label="❌ Negative Prompt",
742
+ lines=3,
743
+ interactive=False,
744
+ value="Select room type and style to see negative prompt preview"
745
+ )
746
+
747
+ with gr.Column(scale=1):
748
+ # Output
749
+ output_image = gr.Image(
750
+ label="AI Design Result",
751
+ height=400
752
+ )
753
+ result_message = gr.Textbox(
754
+ label="Status",
755
+ interactive=False,
756
+ value="Ready to generate design"
757
+ )
758
+
759
+ # Update prompt preview when inputs change
760
+ def update_prompt_preview(room_type, design_style, inpainting_mode):
761
+ pos_prompt, neg_prompt = get_prompt_preview(room_type, design_style, inpainting_mode)
762
+ return pos_prompt, neg_prompt
763
+
764
+ # Event handlers for prompt preview updates
765
+ for input_component in [room_type, design_style, inpainting_mode]:
766
+ input_component.change(
767
+ fn=update_prompt_preview,
768
+ inputs=[room_type, design_style, inpainting_mode],
769
+ outputs=[positive_prompt_preview, negative_prompt_preview]
770
+ )
771
+
772
+ # Initial prompt preview update
773
+ demo.load(
774
+ fn=update_prompt_preview,
775
+ inputs=[room_type, design_style, inpainting_mode],
776
+ outputs=[positive_prompt_preview, negative_prompt_preview]
777
+ )
778
+
779
+ # Main generation event handler
780
+ generate_btn.click(
781
+ fn=design_space,
782
+ inputs=[input_image, room_type, design_style, inpainting_mode, num_steps, guidance_scale, strength],
783
+ outputs=[output_image, result_message]
784
+ )
785
+
786
+
787
+ return demo
788
+
789
+ if __name__ == "__main__":
790
+ # Load models on startup
791
+ print("Starting Spacely AI Interior Designer...")
792
+
793
+ # Create and launch interface
794
+ demo = create_interface()
795
+ demo.launch(share=True)
layered_prompt_test.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Test the new layered furniture prompt
2
+
3
+ def get_layered_prompt(furniture_items, design_style):
4
+ """Generate layered furniture prompt that allows wall decorations"""
5
+
6
+ positive_prompt = f"LAYERED FURNITURE DESIGN: add {furniture_items}, {design_style.lower()} style, photorealistic, place furniture naturally on floor and against walls, add wall decorations like artwork and shelves, preserve existing room structure, no structural changes to walls windows or ceiling, layer furniture over existing surfaces, professional interior design, ultra-realistic lighting and shadows"
7
+
8
+ negative_prompt = "STRUCTURAL REPLACEMENT FORBIDDEN: changing wall color, different wall texture, new walls, removing walls, changing ceiling, different ceiling, new windows, different windows, removing windows, changing floor material, different floor, PRESERVE STRUCTURE: keep original room architecture, lowres, watermark, banner, logo, contactinfo, text, deformed, blurry, blur, out of focus, surreal, ugly"
9
+
10
+ return positive_prompt, negative_prompt
11
+
12
+ # Test
13
+ furniture_items = "MUST INCLUDE: large bed with headboard, two nightstands with lamps, dresser or wardrobe, accent chair, area rug under bed"
14
+ design_style = "Bohemian"
15
+
16
+ pos, neg = get_layered_prompt(furniture_items, design_style)
17
+ print("POSITIVE:", pos)
18
+ print("\nNEGATIVE:", neg)