File size: 64,586 Bytes
c94a830
 
 
 
3cd786b
 
 
 
 
 
 
 
 
 
 
 
 
c94a830
 
 
 
 
 
 
 
 
 
 
4888b0f
 
 
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
 
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
 
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
d31b995
4888b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c94a830
 
4888b0f
 
 
c94a830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cd786b
 
 
 
 
 
c94a830
 
 
3cd786b
 
c94a830
 
 
3cd786b
 
c94a830
 
 
3cd786b
c94a830
 
 
 
3cd786b
 
 
c94a830
 
 
 
 
 
3cd786b
 
 
 
 
 
 
 
 
 
c94a830
 
 
 
 
 
 
 
 
 
 
 
0ab3b9a
 
c94a830
 
0ab3b9a
c94a830
 
0ab3b9a
 
7d0d69e
20ea728
7d0d69e
 
 
 
20ea728
7d0d69e
20ea728
 
7d0d69e
20ea728
 
 
 
 
7d0d69e
20ea728
 
7d0d69e
eec08d5
 
 
7d0d69e
20ea728
7d0d69e
20ea728
 
 
 
 
 
 
 
 
eec08d5
20ea728
 
 
 
7d0d69e
20ea728
 
 
7d0d69e
eec08d5
 
791a40b
 
 
 
 
 
 
20ea728
7d0d69e
20ea728
 
7d0d69e
aa022ce
 
eec08d5
aa022ce
eec08d5
 
 
 
 
 
 
 
 
7d0d69e
9bb1ba2
 
120d561
 
 
 
 
 
9bb1ba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2767665
9bb1ba2
 
 
2767665
9bb1ba2
 
 
 
2767665
9bb1ba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2767665
9bb1ba2
 
 
 
2767665
9bb1ba2
 
 
 
 
2767665
9bb1ba2
 
2767665
9bb1ba2
 
2767665
9bb1ba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2767665
9bb1ba2
 
9ac8ebb
 
111952e
 
9ac8ebb
111952e
 
 
9ac8ebb
111952e
9ac8ebb
111952e
2767665
9ac8ebb
111952e
9ac8ebb
111952e
9ac8ebb
 
 
111952e
9ac8ebb
111952e
9ac8ebb
111952e
9ac8ebb
 
 
111952e
9ac8ebb
 
111952e
9ac8ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111952e
 
9ac8ebb
 
111952e
9ac8ebb
 
 
 
 
111952e
 
120d561
9ac8ebb
 
042bccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ac8ebb
762d21b
 
 
 
042bccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495b6e6
 
 
a178a59
 
495b6e6
 
 
 
 
 
 
 
 
 
 
042bccd
762d21b
495b6e6
 
 
120d561
762d21b
042bccd
 
 
 
 
 
 
762d21b
 
 
042bccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ab3b9a
 
 
c94a830
0ab3b9a
 
c94a830
0ab3b9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c94a830
3cd786b
7d0d69e
c94a830
 
 
 
 
 
 
 
 
3cd786b
 
 
 
 
 
 
 
c94a830
 
579b7d0
4888b0f
 
579b7d0
4888b0f
c94a830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d0d69e
042bccd
 
 
 
 
 
 
 
 
120d561
 
495b6e6
 
 
 
 
 
 
 
 
 
 
c00b86e
495b6e6
 
 
 
 
042bccd
495b6e6
7d0d69e
 
120d561
eec08d5
c94a830
a178a59
 
042bccd
 
a178a59
 
eec08d5
 
 
 
 
 
 
aa022ce
c94a830
 
042bccd
c94a830
aa022ce
c94a830
 
 
 
042bccd
 
 
c94a830
 
 
 
 
0ab3b9a
 
094af74
c94a830
 
 
 
3cd786b
 
 
 
c94a830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4888b0f
c94a830
 
 
 
 
4888b0f
c94a830
 
 
 
7d0d69e
 
 
042bccd
 
7d0d69e
042bccd
7d0d69e
 
 
094af74
c94a830
 
d31b995
 
c94a830
 
042bccd
 
c94a830
 
042bccd
 
c94a830
 
 
762d21b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c94a830
 
 
 
 
 
 
 
 
ee4b0ff
 
c94a830
 
762d21b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c94a830
 
7d0d69e
c94a830
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
import gradio as gr
import torch
import numpy as np
from PIL import Image
# Handle spaces import for both local and Hugging Face deployment
try:
    import spaces  # Required for Hugging Face Spaces GPU
    SPACES_AVAILABLE = True
except ImportError:
    # Local development - create dummy decorator
    class DummySpaces:
        @staticmethod
        def GPU(func):
            return func
    
    spaces = DummySpaces()
    SPACES_AVAILABLE = False

# Model imports with error handling
try:
    from diffusers import ControlNetModel, StableDiffusionControlNetInpaintPipeline, UniPCMultistepScheduler
    from transformers import AutoImageProcessor, SegformerForSemanticSegmentation
    from controlnet_aux import MLSDdetector
    MODELS_AVAILABLE = True
except ImportError as e:
    print(f"Failed to load model libraries: {e}")
    MODELS_AVAILABLE = False

# Detailed room templates by style combinations
DETAILED_PROMPTS = {
    # Living Room combinations
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    # Bedroom combinations
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    # Kitchen combinations
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    # Dining Room combinations
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    # Home Office combinations
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    # Bathroom combinations
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    # Kids Room combinations
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    # Master Bedroom combinations
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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.",
    
    ("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."
}

# Room types and styles for dropdowns
ROOM_TYPES = list(set([combo[0] for combo in DETAILED_PROMPTS.keys()]))
STYLE_TYPES = list(set([combo[1] for combo in DETAILED_PROMPTS.keys()]))

# Global variables for models
pipe = None
seg_processor = None
seg_model = None
mlsd_processor = None

def load_models():
    """Load models (called once)"""
    global pipe, seg_processor, seg_model, mlsd_processor
    
    if not MODELS_AVAILABLE:
        return "❌ Model libraries not available"
    
    try:
        print("πŸ”„ Loading AI models...")
        
        # Optimized ControlNet setup for GPU memory efficiency
        device = "cuda" if torch.cuda.is_available() else "cpu"
        dtype = torch.float16 if torch.cuda.is_available() else torch.float32
        
        print(f"Using device: {device}, dtype: {dtype}")
        
        controlnet = [
            ControlNetModel.from_pretrained(
                "BertChristiaens/controlnet-seg-room", 
                torch_dtype=dtype,
                low_cpu_mem_usage=True
            ),
            ControlNetModel.from_pretrained(
                "lllyasviel/sd-controlnet-mlsd", 
                torch_dtype=dtype,
                low_cpu_mem_usage=True
            ),
        ]
        
        # Main pipeline with memory optimization
        pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
            "SG161222/Realistic_Vision_V3.0_VAE",
            controlnet=controlnet,
            safety_checker=None,
            torch_dtype=dtype,
            low_cpu_mem_usage=True,
            variant="fp16" if torch.cuda.is_available() else None
        )
        
        pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
        
        if torch.cuda.is_available():
            pipe = pipe.to("cuda")
            # Enable memory efficient attention for GPU
            try:
                pipe.enable_xformers_memory_efficient_attention()
                print("βœ… XFormers memory efficient attention enabled")
            except:
                print("⚠️ XFormers not available, using default attention")
                
            # Enable model offloading to save GPU memory
            pipe.enable_model_cpu_offload()
            print("βœ… Model CPU offloading enabled")
        
        # Segmentation models
        seg_processor = AutoImageProcessor.from_pretrained("nvidia/segformer-b5-finetuned-ade-640-640")
        seg_model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b5-finetuned-ade-640-640")
        mlsd_processor = MLSDdetector.from_pretrained("lllyasviel/Annotators")
        
        print("βœ… Models loaded successfully!")
        return "βœ… Models loaded successfully!"
        
    except Exception as e:
        return f"❌ Failed to load models: {e}"

def create_full_mask(image):
    """Create full image mask to eliminate all boundaries"""
    w, h = image.size
    
    # Full white mask - allows modification of entire image
    mask = np.ones((h, w), dtype=np.uint8) * 255
    
    return Image.fromarray(mask).convert("RGB")

def create_smart_mask(image):
    """Create conservative smart mask that primarily targets floor center area"""
    import cv2
    
    # Convert PIL to numpy
    img_array = np.array(image)
    h, w = img_array.shape[:2]
    
    # Create a very conservative mask - only target center floor area
    mask = np.zeros((h, w), dtype=np.uint8)
    
    # Define safe zone - center 60% of image, bottom 70% (floor area)
    safe_x_start = int(w * 0.2)  # 20% from left
    safe_x_end = int(w * 0.8)    # 20% from right  
    safe_y_start = int(h * 0.3)  # 30% from top (avoid ceiling)
    safe_y_end = int(h * 0.9)    # 10% from bottom
    
    # Create elliptical mask in center floor area
    center_x, center_y = w // 2, int(h * 0.65)  # Slightly lower center
    
    # Create ellipse parameters - larger size for better furniture generation
    ellipse_w = int(w * 0.40)  # 40% of width (larger area)
    ellipse_h = int(h * 0.28)  # 28% of height (larger area)
    
    # Draw ellipse mask
    Y, X = np.ogrid[:h, :w]
    ellipse_mask = ((X - center_x) / ellipse_w) ** 2 + ((Y - center_y) / ellipse_h) ** 2 <= 1
    
    # Apply gradient within ellipse - strongest at center, fade to edges
    for y in range(h):
        for x in range(w):
            if ellipse_mask[y, x]:
                # Distance from ellipse center
                dist = np.sqrt(((x - center_x) / ellipse_w) ** 2 + ((y - center_y) / ellipse_h) ** 2)
                # Gradient: stronger at center (dist=0), weaker at edges (dist=1)
                strength = max(0, 1 - dist) * 255  # Max 255 - full strength
                mask[y, x] = int(strength)
    
    # Additional safety: exclude areas that are too bright (likely windows) or too dark (likely corners)
    gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
    
    # Mask out very bright areas (windows) and very dark areas (corners/shadows)
    bright_mask = gray > 220  # Very bright
    dark_mask = gray < 40     # Very dark
    
    # Reduced corner protection - exclude outer 5% of image only
    corner_margin = 0.05
    corner_mask = np.zeros((h, w), dtype=bool)
    corner_mask[:int(h*corner_margin), :] = True  # Top
    corner_mask[-int(h*corner_margin):, :] = True  # Bottom
    corner_mask[:, :int(w*corner_margin)] = True  # Left
    corner_mask[:, -int(w*corner_margin):] = True  # Right
    
    exclude_mask = bright_mask | dark_mask | corner_mask
    mask[exclude_mask] = 0
    
    # Apply blur for smooth transitions
    mask = cv2.GaussianBlur(mask, (21, 21), 0)
    
    # Debug: print mask statistics
    print(f"Mask stats - Min: {mask.min()}, Max: {mask.max()}, Non-zero pixels: {np.count_nonzero(mask)}")
    print(f"Mask shape: {mask.shape}, Image shape: {img_array.shape}")
    
    # Save mask for debugging (temporary)
    mask_debug = Image.fromarray(mask).convert("RGB")
    try:
        mask_debug.save("/tmp/debug_mask.png")
        print("Debug mask saved to /tmp/debug_mask.png")
    except:
        pass
    
    return mask_debug

def analyze_room_structure(image):
    """NEVER TOUCH STRUCTURE: Analyze walls, windows, ceiling - READ ONLY"""
    import cv2
    
    img_array = np.array(image)
    h, w = img_array.shape[:2]
    gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
    
    # Detect structural elements that must NEVER be modified
    structure_mask = np.zeros((h, w), dtype=np.uint8)
    
    # 1. Wall detection (strong vertical/horizontal lines)
    edges = cv2.Canny(gray, 50, 150)
    lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=50, minLineLength=50, maxLineGap=10)
    
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line[0]
            cv2.line(structure_mask, (x1, y1), (x2, y2), 255, 5)
    
    # 2. Window detection (very bright rectangular areas)
    bright_mask = gray > 200
    contours, _ = cv2.findContours(bright_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for contour in contours:
        area = cv2.contourArea(contour)
        if area > 1000:  # Large bright areas = windows
            cv2.fillPoly(structure_mask, [contour], 255)
    
    # 3. Ceiling detection (top 30% of image)
    structure_mask[:int(h*0.3), :] = 255
    
    # 4. Wall edges (outer 15% of image)
    border = int(min(w, h) * 0.15)
    structure_mask[:border, :] = 255      # Top
    structure_mask[-border:, :] = 255     # Bottom
    structure_mask[:, :border] = 255      # Left
    structure_mask[:, -border:] = 255     # Right
    
    # Expand structure protection
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
    structure_mask = cv2.dilate(structure_mask, kernel, iterations=2)
    
    return structure_mask

def detect_floor_area(image, structure_mask):
    """Physics-based floor detection with hard constraints"""
    import cv2
    
    img_array = np.array(image)
    h, w = img_array.shape[:2]
    gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
    
    # Floor must be in bottom 70% of image (physics constraint)
    floor_region = np.zeros((h, w), dtype=np.uint8)
    floor_start_y = int(h * 0.3)  # Floor cannot be in top 30%
    
    # Detect horizontal surfaces (floor characteristics)
    gray_floor = gray[floor_start_y:, :]
    
    # Horizontal gradient analysis - floors have low vertical variation
    grad_y = cv2.Sobel(gray_floor, cv2.CV_64F, 0, 1, ksize=3)
    horizontal_surfaces = np.abs(grad_y) < 15
    
    # Consistent texture/color (floor property)
    blurred = cv2.GaussianBlur(gray_floor, (21, 21), 0)
    floor_brightness = np.median(blurred)
    consistent_areas = np.abs(blurred - floor_brightness) < 25
    
    # Combine constraints: horizontal + consistent = floor
    floor_candidates = horizontal_surfaces & consistent_areas
    floor_region[floor_start_y:, :] = floor_candidates.astype(np.uint8) * 255
    
    # HARD CONSTRAINT: Remove any overlap with structure
    floor_region[structure_mask > 0] = 0
    
    # Physics validation: floor must be connected and substantial
    contours, _ = cv2.findContours(floor_region, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    if contours:
        # Keep only largest floor area (physics: one continuous floor)
        largest = max(contours, key=cv2.contourArea)
        floor_region = np.zeros_like(floor_region)
        cv2.fillPoly(floor_region, [largest], 255)
    
    return floor_region

def create_furniture_placement_zones(floor_mask, image):
    """Define valid furniture placement with physics constraints"""
    import cv2
    
    img_array = np.array(image)
    h, w = img_array.shape[:2]
    
    # Physics constraints for furniture placement
    placement_zones = np.zeros((h, w), dtype=np.uint8)
    
    if np.any(floor_mask > 0):
        # Find floor center of mass (realistic furniture placement)
        moments = cv2.moments(floor_mask)
        if moments["m00"] != 0:
            cx = int(moments["m10"] / moments["m00"])
            cy = int(moments["m01"] / moments["m00"])
        else:
            cx, cy = w//2, int(h*0.7)
        
        # Create placement zones around floor center
        # Bedroom: bed against wall, nightstands beside
        if True:  # For now, general furniture placement
            # Main furniture zone (bed, sofa, etc.)
            main_radius_x = int(w * 0.25)
            main_radius_y = int(h * 0.15)
            
            Y, X = np.ogrid[:h, :w]
            main_zone = ((X - cx) / main_radius_x) ** 2 + ((Y - cy) / main_radius_y) ** 2 <= 1
            placement_zones[main_zone & (floor_mask > 0)] = 255
        
        # Only place furniture on detected floor
        placement_zones[floor_mask == 0] = 0
    
    return placement_zones

def create_layered_furniture_mask(image):
    """LAYERED APPROACH: Allow furniture on walls but prevent structure replacement"""
    import cv2
    
    print("🎨 Creating layered furniture mask...")
    
    img_array = np.array(image)
    h, w = img_array.shape[:2]
    gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
    
    # Create large mask covering most of the room
    mask = np.zeros((h, w), dtype=np.uint8)
    
    # Main furniture area - much larger to allow wall furniture
    center_x = w // 2
    center_y = int(h * 0.6)  # 60% down from top
    
    # Large coverage area for layered furniture
    radius_x = int(w * 0.45)  # 45% of width - covers most room
    radius_y = int(h * 0.35)  # 35% of height - includes wall areas
    
    # Create large elliptical coverage
    Y, X = np.ogrid[:h, :w]
    main_ellipse = ((X - center_x) / radius_x) ** 2 + ((Y - center_y) / radius_y) ** 2 <= 1
    
    # Add additional area for wall furniture (bookcases, wall art, etc.)
    wall_coverage = np.zeros((h, w), dtype=bool)
    wall_coverage[int(h*0.2):int(h*0.9), int(w*0.1):int(w*0.9)] = True
    
    # Combine main area with wall coverage
    furniture_area = main_ellipse | wall_coverage
    
    # Create gradient mask - stronger in center, lighter near edges
    for y in range(h):
        for x in range(w):
            if furniture_area[y, x]:
                # Calculate distance from center
                dist_from_center = np.sqrt((x - center_x)**2 + (y - center_y)**2)
                max_dist = np.sqrt(radius_x**2 + radius_y**2)
                
                # Gradient: strong in center, medium at edges
                if dist_from_center <= max_dist * 0.6:
                    mask[y, x] = 255  # Full strength in center
                elif dist_from_center <= max_dist:
                    # Fade from 255 to 180 towards edges
                    strength = 255 - int((dist_from_center - max_dist * 0.6) / (max_dist * 0.4) * 75)
                    mask[y, x] = max(180, strength)
                else:
                    # Light coverage for wall areas
                    mask[y, x] = 180
    
    # Exclude only window areas (very bright)
    windows = gray > 220
    mask[windows] = 0
    
    # Exclude extreme corners (likely ceiling/wall joints)
    corner_size = int(min(w, h) * 0.05)
    mask[:corner_size, :corner_size] = 0      # Top-left
    mask[:corner_size, -corner_size:] = 0     # Top-right
    
    # Smooth transitions
    mask = cv2.GaussianBlur(mask, (5, 5), 0)
    
    # Statistics
    furniture_pixels = np.count_nonzero(mask)
    coverage = (furniture_pixels / (h * w)) * 100
    strong_pixels = np.count_nonzero(mask > 240)
    medium_pixels = np.count_nonzero((mask > 160) & (mask <= 240))
    
    print(f"🎯 Layered mask coverage:")
    print(f"  - Total coverage: {furniture_pixels} pixels ({coverage:.1f}%)")
    print(f"  - Strong areas (floor): {strong_pixels} pixels")
    print(f"  - Medium areas (walls): {medium_pixels} pixels")
    print(f"  - Center: ({center_x}, {center_y}), Size: {radius_x}x{radius_y}")
    
    return Image.fromarray(mask).convert("RGB")

# Keep the old function for compatibility
def create_precise_furniture_mask(image):
    """Create ultra-conservative mask that ONLY targets floor center - preserves ALL walls and windows"""
    import cv2
    
    print("🎯 Creating ULTRA-CONSERVATIVE furniture mask...")
    
    img_array = np.array(image)
    h, w = img_array.shape[:2]
    gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
    
    # Create conservative mask - ONLY center floor area
    mask = np.zeros((h, w), dtype=np.uint8)
    
    # ULTRA CONSERVATIVE: Only center 30% of image, bottom 40% (floor only)
    safe_x_start = int(w * 0.35)  # 35% from left
    safe_x_end = int(w * 0.65)    # 35% from right  
    safe_y_start = int(h * 0.6)   # 60% from top (avoid walls/windows)
    safe_y_end = int(h * 0.85)    # 15% from bottom
    
    # Create small circular mask in safe center floor area
    center_x, center_y = w // 2, int(h * 0.72)  # Lower center for floor
    
    # SMALL ellipse parameters - very conservative
    ellipse_w = int(w * 0.18)  # Only 18% of width (very small area)
    ellipse_h = int(h * 0.15)  # Only 15% of height (very small area)
    
    # Draw small ellipse mask
    Y, X = np.ogrid[:h, :w]
    ellipse_mask = ((X - center_x) / ellipse_w) ** 2 + ((Y - center_y) / ellipse_h) ** 2 <= 1
    
    # Apply soft gradient within ellipse - strong at center, fade at edges
    for y in range(h):
        for x in range(w):
            if ellipse_mask[y, x]:
                # Distance from center
                dist = np.sqrt(((x - center_x) / ellipse_w) ** 2 + ((y - center_y) / ellipse_h) ** 2)
                # Very conservative gradient
                strength = max(0, (1 - dist) ** 2) * 200  # Softer, max 200 not 255
                mask[y, x] = int(strength)
    
    # HARD EXCLUSIONS - absolutely no modification near walls/windows
    
    # Exclude all bright areas (windows) completely
    windows = gray > 180  # Lower threshold to catch more window areas
    mask[windows] = 0
    
    # Exclude all dark areas (corners/shadows)
    dark_areas = gray < 60
    mask[dark_areas] = 0
    
    # Exclude outer 25% of image (wall areas)
    wall_margin = 0.25
    mask[:int(h*wall_margin), :] = 0  # Top 25%
    mask[-int(h*wall_margin):, :] = 0  # Bottom 25%
    mask[:, :int(w*wall_margin)] = 0  # Left 25%
    mask[:, -int(w*wall_margin):] = 0  # Right 25%
    
    # Exclude top 50% completely (ceiling/wall area)
    mask[:int(h*0.5), :] = 0
    
    # Apply strong blur for very smooth transitions
    mask = cv2.GaussianBlur(mask, (31, 31), 0)
    
    # Final safety check - make sure mask is very conservative
    mask = np.clip(mask, 0, 150)  # Lower maximum intensity
    
    # Statistics
    furniture_pixels = np.count_nonzero(mask)
    coverage = (furniture_pixels / (h * w)) * 100
    
    print(f"πŸ›‘οΈ ULTRA-CONSERVATIVE mask stats:")
    print(f"  - Total coverage: {furniture_pixels} pixels ({coverage:.1f}%)")
    print(f"  - Max intensity: {mask.max()}")
    print(f"  - Center: ({center_x}, {center_y}), Size: {ellipse_w}x{ellipse_h}")
    print(f"  - Coverage should be < 15% for wall preservation")
    
    return Image.fromarray(mask).convert("RGB")

def get_prompt_preview(room_type, design_style, inpainting_mode):
    """Generate preview of prompt and negative prompt that will be used"""
    
    # Create positive prompt based on mode
    if inpainting_mode == "layered":
        # Layered furniture generation
        if room_type == "Living Room":
            furniture_items = "modern sofa, coffee table, side table, floor lamp"
        elif room_type == "Bedroom" or room_type == "Master Bedroom":
            furniture_items = "bed with headboard, two nightstands, dresser"
        elif room_type == "Kitchen":
            furniture_items = "kitchen island, bar stools"
        elif room_type == "Dining Room":
            furniture_items = "dining table, dining chairs"
        elif room_type == "Home Office":
            furniture_items = "desk, office chair, bookshelf"
        elif room_type == "Bathroom":
            furniture_items = "vanity, mirror, storage cabinet"
        else:
            furniture_items = "appropriate furniture"
            
        positive_prompt = f"LAYERED APPROACH: same room layout, preserve perspective, layout preserving realistic interior design - Generate realistic {furniture_items}, {design_style.lower()} style, photorealistic furniture placement, maintain room proportions, professional furniture photography, clean lighting, realistic materials and shadows"
        
    elif inpainting_mode == "smart":
        # Simple, direct furniture prompt for smart mode
        if room_type == "Living Room":
            furniture_items = "sofa, coffee table, side tables, floor lamp, dining table with chairs"
        elif room_type == "Bedroom" or room_type == "Master Bedroom":
            furniture_items = "MUST INCLUDE: large bed with headboard, two nightstands with lamps, dresser or wardrobe, accent chair, area rug under bed"
        elif room_type == "Kitchen":
            furniture_items = "kitchen island, bar stools, dining table with chairs"
        elif room_type == "Dining Room":
            furniture_items = "dining table, dining chairs, sideboard"
        elif room_type == "Home Office":
            furniture_items = "desk, office chair, bookshelf, filing cabinet"
        elif room_type == "Bathroom":
            furniture_items = "vanity, mirror, storage cabinet"
        else:
            furniture_items = "appropriate furniture"
        
        positive_prompt = f"FURNITURE ONLY: add {furniture_items} on floor center, {design_style.lower()} style, photorealistic furniture objects, PRESERVE: keep all walls unchanged, keep ceiling unchanged, keep floor color unchanged, keep window unchanged, no structural changes, no wall modifications, only place furniture objects in room center, professional furniture placement, realistic shadows"
    else:
        # Get detailed template-based prompt for full mode
        detailed_prompt = DETAILED_PROMPTS.get((room_type, design_style), 
                                             DETAILED_PROMPTS[("Living Room", "Modern")])
        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"
    
    # Updated negative prompt
    if inpainting_mode == "layered":
        negative_prompt = "distortion, warped structure, perspective distortion, room layout changes, architectural changes, structural modifications, empty room, no furniture, floating furniture, unrealistic placement, bad proportions, distorted furniture, warped perspective, gray background, neutral background, plain background, lowres, watermark, blurry, deformed"
    elif inpainting_mode == "smart":
        negative_prompt = "FORBIDDEN CHANGES: changing walls, different wall color, wall texture changes, new wall paint, different walls, wall modifications, changing windows, different window, new windows, window alterations, changing ceiling, different ceiling, ceiling changes, changing floor, different floor material, floor changes, structural modifications, architectural changes, room alterations, wall decorations, wall art, curtains, blinds, wall shelves, wall mounted items, lowres, watermark, blurry, deformed, floating furniture, unrealistic placement"
    else:
        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"
    
    return positive_prompt, negative_prompt

def generate_furniture_layer(original_image, room_type, design_style, num_steps, guidance_scale, strength):
    """Generate furniture objects using neutral background, then extract for layering"""
    global pipe, seg_processor, seg_model, mlsd_processor
    
    print("πŸͺ‘ Generating furniture layer...")
    
    # Create neutral gray background same size as original for furniture generation
    w, h = original_image.size
    neutral_bg = Image.new('RGB', (w, h), (128, 128, 128))  # Neutral gray
    
    # Create furniture mask (center area only)
    furniture_mask = create_precise_furniture_mask(neutral_bg)
    
    # Create furniture prompt
    if room_type == "Living Room":
        furniture_items = "modern sofa, coffee table, side table, floor lamp"
    elif room_type == "Bedroom":
        furniture_items = "bed with headboard, two nightstands, dresser"
    elif room_type == "Kitchen":
        furniture_items = "kitchen island, bar stools"
    elif room_type == "Dining Room":
        furniture_items = "dining table, dining chairs"
    elif room_type == "Home Office":
        furniture_items = "desk, office chair, bookshelf"
    else:
        furniture_items = "appropriate furniture"
    
    furniture_prompt = f"same room layout, preserve perspective, layout preserving realistic interior design: {furniture_items}, {design_style.lower()} style, photorealistic furniture placement, natural floor positioning, professional furniture photography, clean professional lighting, realistic materials and textures, high quality furniture catalog, maintain room proportions, realistic shadows and reflections"
    
    # Generate furniture on neutral background
    try:
        if pipe is None:
            print("❌ Pipeline not loaded")
            return Image.new('RGBA', (w, h), (0, 0, 0, 0))
        
        # Resize for processing
        max_size = 768
        if max(w, h) > max_size:
            if w > h:
                new_w, new_h = max_size, int(max_size * h / w)
            else:
                new_w, new_h = int(max_size * w / h), max_size
        else:
            new_w, new_h = w, h
            
        resized_neutral = neutral_bg.resize((new_w, new_h))
        resized_mask = furniture_mask.resize((new_w, new_h))
        
        # Generate furniture with inpainting
        seg_control = resized_neutral.copy()
        mlsd_image = resized_neutral.copy()
        
        # Optimized parameters based on user feedback
        optimized_guidance = max(7.0, min(10.0, float(guidance_scale)))  # Clamp 7-10
        optimized_strength = max(0.4, min(0.6, float(strength)))  # Clamp 0.4-0.6
        
        result = pipe(
            prompt=furniture_prompt,
            negative_prompt="distortion, warped structure, perspective distortion, room layout changes, architectural changes, structural modifications, empty room, no furniture, floating furniture, unrealistic placement, bad proportions, distorted furniture, warped perspective",
            num_inference_steps=int(num_steps),
            strength=optimized_strength,  # Optimized strength range
            guidance_scale=optimized_guidance,  # Optimized guidance range
            image=resized_neutral,
            mask_image=resized_mask,
            control_image=[seg_control, mlsd_image],
            controlnet_conditioning_scale=[0.5, 0.3],  # Stronger control for layout preservation
            control_guidance_start=[0, 0],
            control_guidance_end=[0.7, 0.5],  # Extended guidance for better control
        ).images[0]
        
        # Restore original size
        furniture_generated = result.resize((w, h), Image.Resampling.LANCZOS)
        
        print("βœ… Furniture generated on neutral background")
        return furniture_generated
        
    except Exception as e:
        print(f"❌ Furniture layer generation failed: {e}")
        return Image.new('RGBA', (w, h), (0, 0, 0, 0))

def extract_furniture_from_generated(furniture_generated, original_neutral_bg):
    """Extract furniture from generated image by removing neutral background"""
    import cv2
    
    print("βœ‚οΈ Extracting furniture from generated image...")
    
    # Convert to numpy arrays
    furniture_array = np.array(furniture_generated)
    neutral_array = np.array(original_neutral_bg)
    
    h, w = furniture_array.shape[:2]
    
    # Create mask for furniture areas (areas that changed from neutral gray)
    # Neutral gray is (128, 128, 128)
    gray_tolerance = 30
    
    # Calculate difference from neutral gray
    diff = np.abs(furniture_array.astype(np.float32) - 128.0)
    diff_magnitude = np.sqrt(np.sum(diff**2, axis=2))
    
    # Create furniture mask - areas significantly different from gray
    furniture_mask = diff_magnitude > gray_tolerance
    
    # Refine mask - remove small noise
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
    furniture_mask = cv2.morphologyEx(furniture_mask.astype(np.uint8), cv2.MORPH_OPEN, kernel)
    furniture_mask = cv2.morphologyEx(furniture_mask, cv2.MORPH_CLOSE, kernel)
    
    # Create RGBA image with transparency
    furniture_rgba = np.zeros((h, w, 4), dtype=np.uint8)
    furniture_rgba[:, :, :3] = furniture_array  # Copy RGB
    furniture_rgba[:, :, 3] = furniture_mask * 255  # Alpha channel
    
    # Apply gaussian blur to alpha for smoother edges
    furniture_rgba[:, :, 3] = cv2.GaussianBlur(furniture_rgba[:, :, 3], (3, 3), 0)
    
    furniture_layer = Image.fromarray(furniture_rgba, 'RGBA')
    
    print(f"βœ… Extracted furniture layer with {np.count_nonzero(furniture_mask)} furniture pixels")
    
    return furniture_layer

def composite_layers(background, furniture_generated, furniture_mask=None):
    """Composite furniture layer onto background with realistic placement"""
    print("🎨 Compositing layers...")
    
    # Step 1: Extract furniture from generated image
    neutral_bg = Image.new('RGB', background.size, (128, 128, 128))
    furniture_layer = extract_furniture_from_generated(furniture_generated, neutral_bg)
    
    # Step 2: Composite furniture onto original background
    if background.mode != 'RGBA':
        background = background.convert('RGBA')
    
    # Alpha composite
    result = Image.alpha_composite(background, furniture_layer)
    
    # Convert back to RGB
    final = Image.new('RGB', result.size, (255, 255, 255))
    final.paste(result, mask=result.split()[-1])
    
    return final

def create_layered_design(input_image, room_type, design_style, num_steps, guidance_scale, strength):
    """LAYERED APPROACH: Generate furniture separately and composite onto preserved background"""
    print("πŸ—οΈ  Starting layered design generation...")
    print(f"πŸ“Š Optimized parameters - Guidance: {guidance_scale} β†’ {max(7.0, min(10.0, float(guidance_scale)))}, Strength: {strength} β†’ {max(0.4, min(0.6, float(strength)))}")
    
    # Step 1: Preserve original background completely
    background_layer = input_image.copy()
    print("βœ… Background layer preserved")
    
    # Step 2: Generate furniture objects separately  
    furniture_layer = generate_furniture_layer(input_image, room_type, design_style, num_steps, guidance_scale, strength)
    print("βœ… Furniture layer generated")
    
    # Step 3: Composite layers
    final_image = composite_layers(background_layer, furniture_layer)
    print("βœ… Layers composited")
    
    return final_image

def post_process_blend(original, generated):
    """Post-process to reduce seam artifacts between original and generated areas"""
    from PIL import ImageFilter
    
    # Apply slight blur to reduce harsh transitions
    blended = generated.filter(ImageFilter.GaussianBlur(radius=0.8))
    
    # Blend with original in border areas for smoother transitions
    orig_array = np.array(original, dtype=np.float32)
    gen_array = np.array(blended, dtype=np.float32)
    
    h, w = orig_array.shape[:2]
    
    # Create blend mask - stronger blending near edges
    blend_mask = np.ones((h, w), dtype=np.float32)
    
    # Reduce blend strength near borders
    border_size = min(h, w) // 20
    for i in range(border_size):
        alpha = i / border_size
        blend_mask[i, :] = alpha
        blend_mask[-(i+1), :] = alpha
        blend_mask[:, i] = np.minimum(blend_mask[:, i], alpha)
        blend_mask[:, -(i+1)] = np.minimum(blend_mask[:, -(i+1)], alpha)
    
    # Apply blending
    if len(orig_array.shape) == 3:
        blend_mask = blend_mask[:, :, np.newaxis]
    
    final_array = orig_array * (1 - blend_mask) + gen_array * blend_mask
    final_array = np.clip(final_array, 0, 255).astype(np.uint8)
    
    return Image.fromarray(final_array)

@spaces.GPU  # Required for Hugging Face Spaces GPU
def design_space(input_image, room_type, design_style, inpainting_mode, num_steps, guidance_scale, strength):
    """Generate space design using ZeroGPU"""
    
    global pipe, seg_processor, seg_model, mlsd_processor
    
    if input_image is None:
        return None, "❌ Please upload an image!"
    
    if pipe is None:
        # Load models if not already loaded
        try:
            status = load_models()
            if "❌" in status:
                return None, f"Model loading failed: {status}"
        except Exception as e:
            error_msg = f"❌ Failed to initialize models: {str(e)}"
            print(error_msg)
            return None, error_msg
    
    try:
        # Use detailed template-based prompt with photorealistic emphasis
        detailed_prompt = DETAILED_PROMPTS.get((room_type, design_style), 
                                             DETAILED_PROMPTS[("Living Room", "Modern")])
        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"
        prompt_type = f"{room_type} in {design_style} style"
        
        # Resize image
        orig_w, orig_h = input_image.size
        max_size = 768
        if max(orig_w, orig_h) > max_size:
            if orig_w > orig_h:
                new_w, new_h = max_size, int(max_size * orig_h / orig_w)
            else:
                new_w, new_h = int(max_size * orig_w / orig_h), max_size
        else:
            new_w, new_h = orig_w, orig_h
            
        resized_image = input_image.resize((new_w, new_h))
        
        # Simple segmentation (create basic control image)
        seg_control = resized_image.copy()
        
        # MLSD processing
        if mlsd_processor:
            mlsd_image = mlsd_processor(resized_image)
            mlsd_image = mlsd_image.resize((new_w, new_h))
        else:
            mlsd_image = resized_image.copy()
        
        # Create mask based on selected mode
        if inpainting_mode == "layered":
            # Use new layered approach
            final_image = create_layered_design(resized_image, room_type, design_style, num_steps, guidance_scale, strength)
            # Restore original size
            final_image = final_image.resize((orig_w, orig_h), Image.Resampling.LANCZOS)
            success_msg = f"βœ… Layered {room_type} in {design_style} style completed!"
            return final_image, success_msg
            
        elif inpainting_mode == "smart":
            # Use precise smart mask for furniture-only placement
            mask_image = create_precise_furniture_mask(resized_image)
            
            # Simple, direct furniture prompt for smart mode
            if room_type == "Living Room":
                furniture_items = "sofa, coffee table, side tables, floor lamp, dining table with chairs"
            elif room_type == "Bedroom":
                furniture_items = "bed, nightstands, dresser, chair"
            elif room_type == "Kitchen":
                furniture_items = "kitchen island, bar stools, dining table with chairs"
            elif room_type == "Dining Room":
                furniture_items = "dining table, dining chairs, sideboard"
            elif room_type == "Home Office":
                furniture_items = "desk, office chair, bookshelf, filing cabinet"  
            elif room_type == "Bathroom":
                furniture_items = "vanity, mirror, storage cabinet"
            else:
                furniture_items = "appropriate furniture"
            
            prompt = f"FURNITURE ONLY: add {furniture_items} on floor center, {design_style.lower()} style, photorealistic furniture objects, PRESERVE: keep all walls unchanged, keep ceiling unchanged, keep floor color unchanged, keep window unchanged, no structural changes, no wall modifications, only place furniture objects in room center, professional furniture placement, realistic shadows"
            print(f"Smart mode prompt: {prompt}")
        else:
            mask_image = create_full_mask(resized_image)
            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"
            print("Using full mask mode")
        
        # Generate image with optimized strength for furniture generation
        if inpainting_mode == "smart":
            # Lower strength for better structure preservation in smart mode
            actual_strength = min(0.65, float(strength) * 0.8)  # Reduce by 20%, cap at 0.65
        else:
            actual_strength = float(strength)
        
        print(f"Generation parameters:")
        print(f"  - Mode: {inpainting_mode}")
        print(f"  - Original strength: {strength}")
        print(f"  - Actual strength: {actual_strength}")
        print(f"  - Steps: {int(num_steps)}")
        print(f"  - Guidance scale: {float(guidance_scale)}")
        
        result = pipe(
            prompt=prompt,
            negative_prompt="FORBIDDEN CHANGES: changing walls, different wall color, wall texture changes, new wall paint, different walls, wall modifications, changing windows, different window, new windows, window alterations, changing ceiling, different ceiling, ceiling changes, changing floor, different floor material, floor changes, structural modifications, architectural changes, room alterations, wall decorations, wall art, curtains, blinds, wall shelves, wall mounted items, lowres, watermark, blurry, deformed, floating furniture, unrealistic placement",
            num_inference_steps=int(num_steps),
            strength=actual_strength,
            guidance_scale=float(guidance_scale),
            image=resized_image,
            mask_image=mask_image,
            control_image=[seg_control, mlsd_image],
            controlnet_conditioning_scale=[0.8, 0.6] if inpainting_mode == "smart" else [0.4, 0.2],
            control_guidance_start=[0, 0] if inpainting_mode == "smart" else [0, 0.1],
            control_guidance_end=[0.9, 0.8] if inpainting_mode == "smart" else [0.5, 0.25],
        ).images[0]
        
        # Restore original size
        final_image = result.resize((orig_w, orig_h), Image.Resampling.LANCZOS)
        
        # No post-processing needed with full mask
        
        success_msg = f"βœ… {prompt_type} completed! Generated in {int(num_steps)} steps."
        
        return final_image, success_msg
        
    except Exception as e:
        import traceback
        error_details = traceback.format_exc()
        error_msg = f"❌ Error: {str(e)}\n\nDetails:\n{error_details}"
        print(f"Full error trace: {error_details}")
        return None, error_msg

# Gradio interface
def create_interface():
    """Create Gradio interface"""
    
    with gr.Blocks(title="Spacely AI Interior Designer", theme=gr.themes.Soft()) as demo:
        gr.HTML("<h1>🏠 Spacely AI Interior Designer</h1>")
        gr.Markdown("Upload an empty room photo and AI will design it with furniture")
        
        with gr.Row():
            with gr.Column(scale=1):
                # Input controls
                input_image = gr.Image(
                    label="Upload Empty Room Image",
                    type="pil",
                    height=300
                )
                
                room_type = gr.Dropdown(
                    choices=ROOM_TYPES,
                    value="Living Room",
                    label="Select Room Type"
                )
                
                design_style = gr.Dropdown(
                    choices=STYLE_TYPES,
                    value="Modern",
                    label="Select Design Style"
                )
                
                inpainting_mode = gr.Radio(
                    choices=[
                        ("Complete Room Redesign", "full"),
                        ("Add Furniture Only (Preserve Walls)", "smart"),
                        ("πŸ†• Layered Furniture (Background + Furniture Overlay)", "layered")
                    ],
                    value="layered",
                    label="🎨 Design Mode",
                    info="Choose how to modify your image"
                )
                
                with gr.Accordion("βš™οΈ Advanced Settings", open=False):
                    num_steps = gr.Slider(
                        minimum=1, maximum=500, value=50, step=1,
                        label="Number of denoising steps"
                    )
                    guidance_scale = gr.Slider(
                        minimum=1, maximum=50, value=8, step=0.5,
                        label="Scale for classifier-free guidance (7-10 optimal for Layered mode)"
                    )
                    strength = gr.Slider(
                        minimum=0, maximum=1, value=0.5, step=0.05,
                        label="Prompt strength for inpainting (0.4-0.6 optimal for Layered mode)"
                    )
                
                generate_btn = gr.Button("🎨 Generate Design", variant="primary", size="lg")
                
                # Prompt Preview Section
                with gr.Accordion("πŸ“‹ Prompt Preview", open=False):
                    positive_prompt_preview = gr.Textbox(
                        label="βœ… Positive Prompt",
                        lines=4,
                        interactive=False,
                        value="Select room type and style to see prompt preview"
                    )
                    negative_prompt_preview = gr.Textbox(
                        label="❌ Negative Prompt", 
                        lines=3,
                        interactive=False,
                        value="Select room type and style to see negative prompt preview"
                    )
            
            with gr.Column(scale=1):
                # Output
                output_image = gr.Image(
                    label="AI Design Result",
                    height=400
                )
                result_message = gr.Textbox(
                    label="Status",
                    interactive=False,
                    value="Ready to generate design"
                )
        
        # Update prompt preview when inputs change
        def update_prompt_preview(room_type, design_style, inpainting_mode):
            pos_prompt, neg_prompt = get_prompt_preview(room_type, design_style, inpainting_mode)
            return pos_prompt, neg_prompt
        
        # Event handlers for prompt preview updates
        for input_component in [room_type, design_style, inpainting_mode]:
            input_component.change(
                fn=update_prompt_preview,
                inputs=[room_type, design_style, inpainting_mode],
                outputs=[positive_prompt_preview, negative_prompt_preview]
            )
        
        # Initial prompt preview update
        demo.load(
            fn=update_prompt_preview,
            inputs=[room_type, design_style, inpainting_mode],
            outputs=[positive_prompt_preview, negative_prompt_preview]
        )
        
        # Main generation event handler
        generate_btn.click(
            fn=design_space,
            inputs=[input_image, room_type, design_style, inpainting_mode, num_steps, guidance_scale, strength],
            outputs=[output_image, result_message]
        )
        
    
    return demo

if __name__ == "__main__":
    # Load models on startup
    print("Starting Spacely AI Interior Designer...")
    
    # Create and launch interface
    demo = create_interface()
    demo.launch(share=True)