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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
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
from fastapi import FastAPI, HTTPException, UploadFile, File
from fastapi.responses import JSONResponse, HTMLResponse, PlainTextResponse
from pydantic import BaseModel
from typing import List, Optional, Dict, Any
import json
from PIL import Image
import io
import time
import uvicorn
from transformers import AutoImageProcessor, AutoModelForObjectDetection, SwinModel, SwinConfig
from transformers.pipelines import pipeline
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as v2
from huggingface_hub import PyTorchModelHubMixin
import numpy as np
import warnings
import os
import json
from pathlib import Path

# Suppress specific warnings for cleaner output
warnings.filterwarnings("ignore", message=".*use_fast.*")
warnings.filterwarnings("ignore", message=".*copying from a non-meta parameter.*")
warnings.filterwarnings("ignore", message=".*slow image processor.*")
warnings.filterwarnings("ignore", message=".*slow processor.*")

app = FastAPI(title="HuggingFace Fashion Analyzer API", version="1.0.0")

# Fashion Image Encoder class for yainage90 model
class ImageEncoder(nn.Module, PyTorchModelHubMixin):
    def __init__(self, config=None):
        super(ImageEncoder, self).__init__()
        if config is None:
            # Create a default config if none provided
            config = SwinConfig()
        elif isinstance(config, dict):
            # Convert dict to SwinConfig if needed
            config = SwinConfig(**config)

        self.swin = SwinModel(config)
        self.embedding_layer = nn.Linear(config.hidden_size, 128)

    def forward(self, image_tensor):
        features = self.swin(image_tensor).pooler_output
        embeddings = self.embedding_layer(features)
        embeddings = F.normalize(embeddings, p=2, dim=1)
        return embeddings

class HuggingFaceFashionAnalyzer:
    def __init__(self):
        """Initialize specialized fashion models from yainage90"""
        print("Loading yainage90 fashion models...")

        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        print(f"Using device: {self.device}")

        # Set CPU optimization settings
        if self.device == "cpu":
            torch.set_num_threads(2)  # Limit CPU threads to reduce load
            print("CPU optimization: Limited threads to 2 for better performance")

        # Initialize yainage90 fashion object detection model
        try:
            self.detection_ckpt = 'yainage90/fashion-object-detection'
            # Use fast processor to avoid warnings
            self.detection_processor = AutoImageProcessor.from_pretrained(
                self.detection_ckpt,
                use_fast=True
            )
            # Load model with proper parameter assignment to avoid warnings
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                self.detection_model = AutoModelForObjectDetection.from_pretrained(
                    self.detection_ckpt,
                    torch_dtype=torch.float32 if self.device == "cpu" else torch.float16,
                    low_cpu_mem_usage=True if self.device == "cpu" else False
                ).to(self.device)
            print("Fashion object detection model loaded successfully")
        except Exception as e:
            print(f"Error loading fashion detection model: {e}")
            self.detection_model = None
            self.detection_processor = None

        # Initialize yainage90 fashion feature extractor
        try:
            self.encoder_ckpt = "yainage90/fashion-image-feature-extractor"
            self.encoder_config = SwinConfig.from_pretrained(self.encoder_ckpt)
            # Use fast processor to avoid warnings
            self.encoder_image_processor = AutoImageProcessor.from_pretrained(
                self.encoder_ckpt,
                use_fast=True
            )

            # Create the encoder with proper configuration - use from_pretrained directly
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                self.feature_encoder = ImageEncoder.from_pretrained(self.encoder_ckpt).to(self.device)
                # Set appropriate dtype after loading
                if self.device == "cpu":
                    self.feature_encoder = self.feature_encoder.float()
                else:
                    self.feature_encoder = self.feature_encoder.half()

            # Setup image transforms for feature extraction
            self.transform = v2.Compose([
                v2.Resize((self.encoder_config.image_size, self.encoder_config.image_size)),
                v2.ToTensor(),
                v2.Normalize(mean=self.encoder_image_processor.image_mean, std=self.encoder_image_processor.image_std),
            ])
            print("Fashion feature extractor model loaded successfully")
        except Exception as e:
            print(f"Error loading fashion feature extractor: {e}")
            self.feature_encoder = None
            self.transform = None

        # Initialize basic image captioning as fallback with CPU optimization
        try:
            # Configure model kwargs for CPU optimization
            model_kwargs = {}
            if self.device == "cpu":
                model_kwargs["low_cpu_mem_usage"] = True
                model_kwargs["torch_dtype"] = torch.float32
            else:
                model_kwargs["torch_dtype"] = torch.float16

            self.image_to_text = pipeline(
                "image-to-text",
                model="Salesforce/blip-image-captioning-base",
                device=0 if torch.cuda.is_available() else -1,
                model_kwargs=model_kwargs
            )
            print("Basic image captioning model loaded successfully")
        except Exception as e:
            print(f"Error loading image captioning model: {e}")
            self.image_to_text = None

        # Fashion categories mapping
        self.fashion_categories = {
            0: 'bag', 1: 'bottom', 2: 'dress', 3: 'hat', 4: 'shoes', 5: 'outer', 6: 'top'
        }

        # Set models to evaluation mode for inference optimization
        if self.detection_model:
            self.detection_model.eval()
        if self.feature_encoder:
            self.feature_encoder.eval()

    def process_image_from_bytes(self, image_bytes):
        """Process image bytes and return PIL Image"""
        image = Image.open(io.BytesIO(image_bytes))

        # Convert to RGB if necessary
        if image.mode != 'RGB':
            image = image.convert('RGB')

        return image

    def analyze_clothing_from_bytes(self, image_bytes):
        """Advanced fashion analysis using yainage90 specialized models"""

        try:
            # Process image
            image = self.process_image_from_bytes(image_bytes)

            # Get fashion object detection results
            detection_results = self.detect_fashion_objects(image)

            # Extract fashion features
            fashion_features = self.extract_fashion_features(image)

            # Get basic image description as fallback
            basic_description = self.get_basic_description(image)

            # Create comprehensive fashion analysis
            analysis = self.create_advanced_fashion_analysis(detection_results, fashion_features, basic_description, image)

            return analysis

        except Exception as e:
            return f"Error analyzing image: {str(e)}"

    def analyze_clothing_structured_format(self, image_bytes):
        """Analyze clothing using the refined structured format"""
        try:
            # Process image
            image = self.process_image_from_bytes(image_bytes)

            # Get fashion object detection results
            detection_results = self.detect_fashion_objects(image)

            # Get basic image description as fallback
            basic_description = self.get_basic_description(image)

            # Create structured analysis in the exact requested format
            analysis = self.create_refined_structured_analysis(detection_results, basic_description)

            return analysis

        except Exception as e:
            return f"Error analyzing image: {str(e)}"

    def analyze_clothing_enhanced_prompt_format(self, image_bytes):
        """Analyze clothing using the enhanced prompt format optimized for feature extraction"""
        try:
            # Process image
            image = self.process_image_from_bytes(image_bytes)

            # Get fashion object detection results
            detection_results = self.detect_fashion_objects(image)

            # Get basic image description as fallback
            basic_description = self.get_basic_description(image)

            # Create enhanced analysis using the refined prompt structure
            analysis = self.create_enhanced_prompt_analysis(detection_results, basic_description)

            return analysis

        except Exception as e:
            return f"Error analyzing image: {str(e)}"

    def create_refined_structured_analysis(self, detection_results, basic_description):
        """Create analysis in the exact refined format requested"""

        # Process detection results
        detected_items = detection_results.get('detected_items', [])

        # Categorize detected items
        upper_items = []
        lower_items = []
        footwear_items = []

        for item in detected_items:
            category = item['category'].lower()
            if category in ['top', 'shirt', 'blouse', 'outer', 'jacket', 'blazer', 'dress']:
                upper_items.append(item)
            elif category in ['bottom', 'pants', 'jeans', 'skirt']:
                lower_items.append(item)
            elif category in ['shoes']:
                footwear_items.append(item)

        analysis_parts = []

        # UPPER GARMENT
        analysis_parts.append("UPPER GARMENT:")
        if upper_items or 'dress' in basic_description.lower() or any(word in basic_description.lower() for word in ['shirt', 'blouse', 'top', 'jacket']):
            if upper_items:
                primary_upper = upper_items[0]
                garment_type = primary_upper['category'].title()
            else:
                garment_type = self.extract_garment_type(basic_description)

            analysis_parts.append(f"Type: {garment_type}")
            analysis_parts.append(f"Color: {self.extract_colors(basic_description)}")
            analysis_parts.append(f"Material: {self.extract_material(basic_description)}")
            analysis_parts.append(f"Features: {self.extract_comprehensive_features(basic_description, garment_type)}")
        else:
            analysis_parts.append("Type: Not clearly visible in image")
            analysis_parts.append("Color: Unable to determine")
            analysis_parts.append("Material: Unable to determine")
            analysis_parts.append("Features: Unable to determine")

        analysis_parts.append("")

        # LOWER GARMENT
        analysis_parts.append("LOWER GARMENT:")
        if 'dress' in basic_description.lower() and not lower_items:
            analysis_parts.append("[Not applicable - dress serves as complete outfit]")
        elif lower_items or any(word in basic_description.lower() for word in ['pants', 'jeans', 'skirt', 'shorts']):
            if lower_items:
                primary_lower = lower_items[0]
                garment_type = primary_lower['category'].title()
            else:
                # Infer from description
                if 'jeans' in basic_description.lower():
                    garment_type = "Jeans"
                elif 'pants' in basic_description.lower():
                    garment_type = "Pants"
                elif 'skirt' in basic_description.lower():
                    garment_type = "Skirt"
                else:
                    garment_type = "Lower garment"

            analysis_parts.append(f"Type: {garment_type}")
            analysis_parts.append(f"Color: {self.extract_colors(basic_description)}")
            analysis_parts.append(f"Material: {self.extract_material(basic_description)}")
            analysis_parts.append(f"Features: {self.extract_comprehensive_features(basic_description, garment_type)}")
        else:
            analysis_parts.append("Type: Not clearly visible in image")
            analysis_parts.append("Color: Unable to determine")
            analysis_parts.append("Material: Unable to determine")
            analysis_parts.append("Features: Unable to determine")

        analysis_parts.append("")

        # FOOTWEAR
        analysis_parts.append("FOOTWEAR:")
        if footwear_items or any(word in basic_description.lower() for word in ['shoes', 'boots', 'sneakers', 'heels']):
            if footwear_items:
                primary_footwear = footwear_items[0]
                footwear_type = primary_footwear['category'].title()
            else:
                # Infer from description
                if 'sneakers' in basic_description.lower():
                    footwear_type = "Sneakers"
                elif 'boots' in basic_description.lower():
                    footwear_type = "Boots"
                elif 'heels' in basic_description.lower():
                    footwear_type = "Heels"
                else:
                    footwear_type = "Shoes"

            analysis_parts.append(f"Type: {footwear_type}")
            analysis_parts.append(f"Color: {self.extract_colors(basic_description)}")
            analysis_parts.append(f"Material: {self.extract_material(basic_description)}")
            analysis_parts.append(f"Features: {self.extract_comprehensive_features(basic_description, footwear_type)}")
        else:
            analysis_parts.append("Type: Not clearly visible in image")
            analysis_parts.append("Color: Unable to determine")
            analysis_parts.append("Material: Unable to determine")
            analysis_parts.append("Features: Unable to determine")

        analysis_parts.append("")

        # OUTFIT SUMMARY
        analysis_parts.append("OUTFIT SUMMARY:")
        outfit_summary = self.create_comprehensive_outfit_summary(detected_items, basic_description)
        analysis_parts.append(outfit_summary)

        return "\n".join(analysis_parts)

    def create_enhanced_prompt_analysis(self, detection_results, basic_description):
        """Create analysis using the enhanced prompt format optimized for feature extraction"""

        # Process detection results
        detected_items = detection_results.get('detected_items', [])

        # Categorize detected items
        upper_items = []
        lower_items = []
        footwear_items = []

        for item in detected_items:
            category = item['category'].lower()
            if category in ['top', 'shirt', 'blouse', 'outer', 'jacket', 'blazer', 'dress']:
                upper_items.append(item)
            elif category in ['bottom', 'pants', 'jeans', 'skirt']:
                lower_items.append(item)
            elif category in ['shoes']:
                footwear_items.append(item)

        analysis_parts = []

        # UPPER GARMENT
        analysis_parts.append("**UPPER GARMENT**")
        analysis_parts.append("")
        if upper_items or 'dress' in basic_description.lower() or any(word in basic_description.lower() for word in ['shirt', 'blouse', 'top', 'jacket']):
            if upper_items:
                primary_upper = upper_items[0]
                garment_type = primary_upper['category'].title()
            else:
                garment_type = self.extract_garment_type(basic_description)

            analysis_parts.append(f"**Type**: {garment_type}")
            analysis_parts.append(f"**Color**: {self.extract_enhanced_colors(basic_description)}")
            analysis_parts.append(f"**Material**: {self.extract_enhanced_material(basic_description, garment_type)}")
            analysis_parts.append(f"**Features**: {self.extract_enhanced_features(basic_description, garment_type)}")
        else:
            analysis_parts.append("**Type**: Not clearly visible in image")
            analysis_parts.append("**Color**: Unable to determine")
            analysis_parts.append("**Material**: Unable to determine")
            analysis_parts.append("**Features**: Unable to determine")

        analysis_parts.append("")
        analysis_parts.append("---")
        analysis_parts.append("")

        # LOWER GARMENT
        analysis_parts.append("**LOWER GARMENT**")
        analysis_parts.append("")
        if 'dress' in basic_description.lower() and not lower_items:
            analysis_parts.append("**Type**: Not applicable - dress serves as complete outfit")
            analysis_parts.append("**Color**: N/A")
            analysis_parts.append("**Material**: N/A")
            analysis_parts.append("**Features**: N/A")
        elif lower_items or any(word in basic_description.lower() for word in ['pants', 'jeans', 'skirt', 'shorts']):
            if lower_items:
                primary_lower = lower_items[0]
                garment_type = primary_lower['category'].title()
            else:
                # Infer from description
                if 'jeans' in basic_description.lower():
                    garment_type = "Jeans"
                elif 'pants' in basic_description.lower():
                    garment_type = "Pants"
                elif 'skirt' in basic_description.lower():
                    garment_type = "Skirt"
                else:
                    garment_type = "Lower garment"

            analysis_parts.append(f"**Type**: {garment_type}")
            analysis_parts.append(f"**Color**: {self.extract_enhanced_colors(basic_description)}")
            analysis_parts.append(f"**Material**: {self.extract_enhanced_material(basic_description, garment_type)}")
            analysis_parts.append(f"**Features**: {self.extract_enhanced_features(basic_description, garment_type)}")
        else:
            analysis_parts.append("**Type**: Not clearly visible in image")
            analysis_parts.append("**Color**: Unable to determine")
            analysis_parts.append("**Material**: Unable to determine")
            analysis_parts.append("**Features**: Unable to determine")

        analysis_parts.append("")
        analysis_parts.append("---")
        analysis_parts.append("")

        # FOOTWEAR
        analysis_parts.append("**FOOTWEAR**")
        analysis_parts.append("")
        if footwear_items or any(word in basic_description.lower() for word in ['shoes', 'boots', 'sneakers', 'heels']):
            if footwear_items:
                primary_footwear = footwear_items[0]
                footwear_type = primary_footwear['category'].title()
            else:
                # Infer from description
                if 'sneakers' in basic_description.lower():
                    footwear_type = "Sneakers"
                elif 'boots' in basic_description.lower():
                    footwear_type = "Boots"
                elif 'heels' in basic_description.lower():
                    footwear_type = "Heels"
                else:
                    footwear_type = "Shoes"

            analysis_parts.append(f"**Type**: {footwear_type}")
            analysis_parts.append(f"**Color**: {self.extract_enhanced_colors(basic_description)}")
            analysis_parts.append(f"**Material**: {self.extract_enhanced_material(basic_description, footwear_type)}")
            analysis_parts.append(f"**Features**: {self.extract_enhanced_features(basic_description, footwear_type)}")
        else:
            analysis_parts.append("**Type**: Not clearly visible in image")
            analysis_parts.append("**Color**: Unable to determine")
            analysis_parts.append("**Material**: Unable to determine")
            analysis_parts.append("**Features**: Unable to determine")

        analysis_parts.append("")
        analysis_parts.append("---")
        analysis_parts.append("")

        # OUTFIT SUMMARY
        analysis_parts.append("**OUTFIT SUMMARY**")
        analysis_parts.append("")
        outfit_summary = self.create_enhanced_outfit_summary(detected_items, basic_description)
        analysis_parts.append(outfit_summary)

        return "\n".join(analysis_parts)

    def extract_comprehensive_features(self, description, garment_type):
        """Extract comprehensive features based on garment type"""
        desc_lower = description.lower()
        garment_lower = garment_type.lower()

        features = []

        # Pattern and design features
        pattern = self.extract_pattern(description)
        if pattern and pattern != "Solid or minimal pattern":
            features.append(pattern)

        # Garment-specific features
        if any(word in garment_lower for word in ['shirt', 'blouse', 'top']):
            # Neckline
            neckline = self.extract_neckline(description)
            if neckline != "Neckline present but style not specified":
                features.append(neckline)

            # Sleeves
            sleeves = self.extract_sleeves(description)
            if sleeves != "Sleeve style not specified in description":
                features.append(sleeves)

            # Collar
            if 'collar' in desc_lower:
                features.append("Collared design")

            # Buttons
            if 'button' in desc_lower:
                features.append("Button closure")

        elif 'dress' in garment_lower:
            # Length
            if 'midi' in desc_lower:
                features.append("Midi length")
            elif 'maxi' in desc_lower:
                features.append("Maxi length")
            elif 'mini' in desc_lower:
                features.append("Mini length")

            # Sleeves
            sleeves = self.extract_sleeves(description)
            if sleeves != "Sleeve style not specified in description":
                features.append(sleeves)

            # Neckline
            neckline = self.extract_neckline(description)
            if neckline != "Neckline present but style not specified":
                features.append(neckline)

            # Fit
            fit = self.extract_fit(description)
            if fit != "Fit style not specified in description":
                features.append(fit)

        elif any(word in garment_lower for word in ['pants', 'jeans', 'skirt']):
            # Fit
            fit = self.extract_fit(description)
            if fit != "Fit style not specified in description":
                features.append(fit)

            # Pockets
            if 'pocket' in desc_lower:
                features.append("Pockets")

            # Belt
            if 'belt' in desc_lower:
                features.append("Belt")

            # Length for skirts
            if 'skirt' in garment_lower:
                if 'mini' in desc_lower:
                    features.append("Mini length")
                elif 'midi' in desc_lower:
                    features.append("Midi length")
                elif 'maxi' in desc_lower:
                    features.append("Maxi length")

        elif any(word in garment_lower for word in ['shoes', 'sneakers', 'boots', 'heels']):
            # Closure
            if 'lace' in desc_lower:
                features.append("Lace-up closure")
            elif 'buckle' in desc_lower:
                features.append("Buckle closure")
            elif 'slip' in desc_lower:
                features.append("Slip-on style")

            # Heel
            if 'heel' in desc_lower:
                features.append("Heeled")
            elif 'flat' in desc_lower:
                features.append("Flat sole")

            # Style details
            if 'stud' in desc_lower:
                features.append("Studded details")

        # General features
        if 'zip' in desc_lower:
            features.append("Zipper details")

        return ', '.join(features) if features else "Standard design features"

    def create_comprehensive_outfit_summary(self, detected_items, basic_description):
        """Create a comprehensive outfit summary paragraph"""

        # Extract key elements
        garment_types = [item['category'] for item in detected_items if item['confidence'] > 0.5]
        colors = self.extract_colors(basic_description)
        style = self.extract_style(basic_description)
        pattern = self.extract_pattern(basic_description)

        summary_sentences = []

        # Opening statement about overall aesthetic
        if garment_types:
            if 'dress' in garment_types:
                summary_sentences.append("This outfit centers around an elegant dress that serves as a complete styling solution, demonstrating the power of a well-chosen single piece.")
            elif len(garment_types) > 1:
                summary_sentences.append("This outfit showcases thoughtful coordination between multiple garment pieces, creating a cohesive and intentional look.")
            else:
                summary_sentences.append("This outfit highlights a key fashion piece with strong individual character and styling potential.")
        else:
            summary_sentences.append("This outfit demonstrates contemporary fashion sensibility with carefully considered design elements.")

        # Color and pattern analysis
        if 'neutral' in colors.lower():
            summary_sentences.append("The neutral color palette provides exceptional versatility and sophisticated appeal, making it suitable for various occasions and easy to accessorize.")
        elif 'warm' in colors.lower():
            summary_sentences.append("The warm color tones create an approachable and energetic aesthetic that conveys confidence and positivity.")
        elif 'cool' in colors.lower():
            summary_sentences.append("The cool color palette conveys professionalism and calming elegance, perfect for both casual and semi-formal settings.")

        if 'floral' in pattern.lower():
            summary_sentences.append("The floral elements add feminine charm and seasonal freshness, bringing natural beauty and romantic appeal to the overall ensemble.")
        elif 'solid' in pattern.lower():
            summary_sentences.append("The solid construction maximizes styling flexibility and serves as an excellent foundation for accessory experimentation.")
        elif 'striped' in pattern.lower():
            summary_sentences.append("The striped pattern adds visual interest and classic appeal while maintaining timeless sophistication.")

        # Style and occasion assessment
        if 'casual' in style.lower():
            summary_sentences.append("The casual styling makes this outfit perfect for everyday wear, weekend activities, and relaxed social occasions while maintaining a polished appearance.")
        elif 'formal' in style.lower():
            summary_sentences.append("The formal elements ensure this outfit meets professional standards and elegant occasion requirements with confidence and grace.")
        else:
            summary_sentences.append("The versatile design allows this outfit to transition seamlessly between different settings, from casual daytime events to more polished evening occasions.")

        # Concluding statement about fashion-forward thinking
        summary_sentences.append("The pieces work harmoniously together, reflecting contemporary fashion awareness and demonstrating how thoughtful styling choices can create an effortlessly chic and memorable appearance.")

        return " ".join(summary_sentences)

    def parse_structured_analysis(self, detection_results, basic_description):
        """Parse detection results and description into structured JSON format"""

        # Process detection results
        detected_items = detection_results.get('detected_items', [])

        # Categorize detected items
        upper_items = []
        lower_items = []
        footwear_items = []

        for item in detected_items:
            category = item['category'].lower()
            if category in ['top', 'shirt', 'blouse', 'outer', 'jacket', 'blazer', 'dress']:
                upper_items.append(item)
            elif category in ['bottom', 'pants', 'jeans', 'skirt']:
                lower_items.append(item)
            elif category in ['shoes']:
                footwear_items.append(item)

        # Extract upper garment details
        if upper_items or 'dress' in basic_description.lower():
            if upper_items:
                garment_type = upper_items[0]['category'].title()
            else:
                garment_type = self.extract_garment_type(basic_description)

            upper_garment = {
                "type": garment_type,
                "color": self.extract_colors(basic_description),
                "material": self.extract_material(basic_description),
                "features": self.extract_comprehensive_features(basic_description, garment_type)
            }
        else:
            upper_garment = {
                "type": "Not clearly visible",
                "color": "Unable to determine",
                "material": "Unable to determine",
                "features": "Unable to determine"
            }

        # Extract lower garment details
        if 'dress' in basic_description.lower() and not lower_items:
            lower_garment = {
                "type": "Not applicable - dress serves as complete outfit",
                "color": "N/A",
                "material": "N/A",
                "features": "N/A"
            }
        elif lower_items:
            garment_type = lower_items[0]['category'].title()
            lower_garment = {
                "type": garment_type,
                "color": self.extract_colors(basic_description),
                "material": self.extract_material(basic_description),
                "features": self.extract_comprehensive_features(basic_description, garment_type)
            }
        else:
            lower_garment = {
                "type": "Not clearly visible",
                "color": "Unable to determine",
                "material": "Unable to determine",
                "features": "Unable to determine"
            }

        # Extract footwear details
        if footwear_items:
            footwear_type = footwear_items[0]['category'].title()
            footwear = {
                "type": footwear_type,
                "color": self.extract_colors(basic_description),
                "material": self.extract_material(basic_description),
                "features": self.extract_comprehensive_features(basic_description, footwear_type)
            }
        else:
            footwear = {
                "type": "Not clearly visible",
                "color": "Unable to determine",
                "material": "Unable to determine",
                "features": "Unable to determine"
            }

        # Create outfit summary
        outfit_summary_text = self.create_comprehensive_outfit_summary(detected_items, basic_description)

        # Parse outfit summary into components
        outfit_summary = {
            "aesthetic": self.extract_style(basic_description),
            "style_notes": self.extract_pattern(basic_description),
            "occasion_suitability": "Versatile for multiple occasions",
            "color_harmony": self.extract_colors(basic_description),
            "overall_assessment": outfit_summary_text
        }

        # Calculate confidence score
        confidence_scores = [item['confidence'] for item in detected_items if item['confidence'] > 0.3]
        confidence_score = sum(confidence_scores) / len(confidence_scores) if confidence_scores else 0.5

        # Create structured response
        return StructuredAnalysisResponse(
            upper_garment=GarmentDetails(**upper_garment),
            lower_garment=GarmentDetails(**lower_garment),
            footwear=GarmentDetails(**footwear),
            outfit_summary=OutfitSummary(**outfit_summary),
            confidence_score=round(confidence_score, 3),
            detected_items=detected_items
        )

    def detect_fashion_objects(self, image):
        """Detect fashion objects using yainage90 fashion detection model"""
        if self.detection_model is None or self.detection_processor is None:
            return {"error": "Fashion detection model not available"}

        try:
            # Use inference mode for better performance
            with torch.inference_mode():
                inputs = self.detection_processor(images=[image], return_tensors="pt")
                # Move inputs to device efficiently
                inputs = {k: v.to(self.device) for k, v in inputs.items()}
                outputs = self.detection_model(**inputs)
                target_sizes = torch.tensor([[image.size[1], image.size[0]]])
                results = self.detection_processor.post_process_object_detection(
                    outputs, threshold=0.4, target_sizes=target_sizes
                )[0]

                detected_items = []
                for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
                    score = score.item()
                    label = label.item()
                    box = [i.item() for i in box]
                    category = self.detection_model.config.id2label[label]
                    detected_items.append({
                        'category': category,
                        'confidence': round(score, 3),
                        'bbox': box
                    })

                return {"detected_items": detected_items}
        except Exception as e:
            return {"error": f"Detection failed: {str(e)}"}

    def extract_fashion_features(self, image):
        """Extract fashion features using yainage90 feature extractor"""
        if self.feature_encoder is None or self.transform is None:
            return {"error": "Feature extractor not available"}

        try:
            # Transform image for feature extraction
            image_tensor = self.transform(image)

            # Use inference mode for better performance
            with torch.inference_mode():
                embedding = self.feature_encoder(image_tensor.unsqueeze(0).to(self.device))

            return {
                "feature_vector": embedding.cpu().numpy().tolist(),
                "feature_dimension": embedding.shape[1]
            }
        except Exception as e:
            return {"error": f"Feature extraction failed: {str(e)}"}

    def get_basic_description(self, image):
        """Get basic image description as fallback"""
        if self.image_to_text is None:
            return "Image analysis not available"

        try:
            result = self.image_to_text(image)
            # Convert result to list if it's a generator/iterator
            if result is not None and hasattr(result, '__iter__') and not isinstance(result, (str, bytes)):
                result_list = list(result)
                if result_list and len(result_list) > 0:
                    first_result = result_list[0]
                    if isinstance(first_result, dict):
                        return first_result.get('generated_text', 'Unable to describe image')
            return "Unable to describe image"
        except Exception as e:
            return f"Description failed: {str(e)}"

    def create_advanced_fashion_analysis(self, detection_results, fashion_features, basic_description, image):
        """Create comprehensive fashion analysis using yainage90 model results"""

        # Process detection results
        detected_items = detection_results.get('detected_items', [])
        detection_summary = self.summarize_detections(detected_items)

        # Create structured clothing analysis
        structured_analysis = self.create_structured_clothing_analysis(detected_items, basic_description)

        # Create comprehensive analysis
        analysis_template = f"""🎽 ADVANCED FASHION ANALYSIS REPORT

πŸ” AI-POWERED OBJECT DETECTION:
{detection_summary}

πŸ“‹ STRUCTURED CLOTHING ANALYSIS:
{structured_analysis}

πŸ“‹ DETAILED GARMENT ANALYSIS:
{self.create_detailed_garment_analysis(detected_items, basic_description)}

🎨 STYLE & DESIGN ASSESSMENT:
{self.create_style_assessment(detected_items, basic_description)}

πŸ’‘ PROFESSIONAL STYLING RECOMMENDATIONS:
{self.generate_styling_recommendations(detected_items)}

πŸ”„ WARDROBE INTEGRATION ADVICE:
{self.generate_wardrobe_advice(detected_items)}

πŸ“Š FASHION SUMMARY:
{self.create_fashion_summary(detected_items, basic_description)}

πŸ€– TECHNICAL DETAILS:
β€’ Feature Vector Dimension: {fashion_features.get('feature_dimension', 'N/A')}
β€’ Detection Confidence: {self.get_average_confidence(detected_items)}
β€’ Analysis Method: yainage90 Fashion AI Models"""

        return analysis_template

    def create_structured_clothing_analysis(self, detected_items, basic_description):
        """Create structured clothing analysis in the requested format"""

        # Categorize detected items
        upper_items = []
        lower_items = []
        footwear_items = []

        for item in detected_items:
            category = item['category'].lower()
            if category in ['top', 'shirt', 'blouse', 'outer', 'jacket', 'blazer', 'dress']:
                upper_items.append(item)
            elif category in ['bottom', 'pants', 'jeans', 'skirt']:
                lower_items.append(item)
            elif category in ['shoes']:
                footwear_items.append(item)

        analysis_parts = []

        # Upper Garment Analysis
        if upper_items or 'dress' in basic_description.lower() or any(word in basic_description.lower() for word in ['shirt', 'blouse', 'top', 'jacket']):
            analysis_parts.append("UPPER GARMENT:")
            if upper_items:
                primary_upper = upper_items[0]
                analysis_parts.append(f"Type: {primary_upper['category'].title()}")
            else:
                analysis_parts.append(f"Type: {self.extract_garment_type(basic_description)}")

            analysis_parts.append(f"Color: {self.extract_colors(basic_description)}")
            analysis_parts.append(f"Material: {self.extract_material(basic_description)}")
            analysis_parts.append(f"Features: {self.extract_features(basic_description)}")
            analysis_parts.append("")

        # Lower Garment Analysis
        if lower_items or any(word in basic_description.lower() for word in ['pants', 'jeans', 'skirt', 'shorts']):
            analysis_parts.append("LOWER GARMENT:")
            if lower_items:
                primary_lower = lower_items[0]
                analysis_parts.append(f"Type: {primary_lower['category'].title()}")
            else:
                # Infer from description
                if 'jeans' in basic_description.lower():
                    analysis_parts.append("Type: Jeans")
                elif 'pants' in basic_description.lower():
                    analysis_parts.append("Type: Pants")
                elif 'skirt' in basic_description.lower():
                    analysis_parts.append("Type: Skirt")
                else:
                    analysis_parts.append("Type: Lower garment")

            analysis_parts.append(f"Color: {self.extract_colors(basic_description)}")
            analysis_parts.append(f"Material: {self.extract_material(basic_description)}")
            analysis_parts.append(f"Features: {self.extract_features(basic_description)}")
            analysis_parts.append("")

        # Footwear Analysis
        if footwear_items or any(word in basic_description.lower() for word in ['shoes', 'boots', 'sneakers', 'heels']):
            analysis_parts.append("FOOTWEAR:")
            if footwear_items:
                primary_footwear = footwear_items[0]
                analysis_parts.append(f"Type: {primary_footwear['category'].title()}")
            else:
                # Infer from description
                if 'sneakers' in basic_description.lower():
                    analysis_parts.append("Type: Sneakers")
                elif 'boots' in basic_description.lower():
                    analysis_parts.append("Type: Boots")
                elif 'heels' in basic_description.lower():
                    analysis_parts.append("Type: Heels")
                else:
                    analysis_parts.append("Type: Shoes")

            analysis_parts.append(f"Color: {self.extract_colors(basic_description)}")
            analysis_parts.append(f"Material: {self.extract_material(basic_description)}")
            analysis_parts.append(f"Features: {self.extract_features(basic_description)}")
            analysis_parts.append("")

        # Outfit Summary
        analysis_parts.append("OUTFIT SUMMARY:")
        outfit_summary = self.create_outfit_summary(detected_items, basic_description)
        analysis_parts.append(outfit_summary)

        return "\n".join(analysis_parts)

    def create_outfit_summary(self, detected_items, basic_description):
        """Create a comprehensive outfit summary paragraph"""

        # Extract key elements
        garment_types = [item['category'] for item in detected_items if item['confidence'] > 0.5]
        colors = self.extract_colors(basic_description)
        style = self.extract_style(basic_description)
        pattern = self.extract_pattern(basic_description)

        summary_parts = []

        # Start with overall style assessment
        if garment_types:
            if 'dress' in garment_types:
                summary_parts.append("This outfit centers around an elegant dress that serves as a complete styling solution.")
            elif 'top' in garment_types and 'bottom' in garment_types:
                summary_parts.append("This outfit features a well-coordinated combination of separates that create a cohesive look.")
            elif len(garment_types) > 1:
                summary_parts.append("This outfit demonstrates thoughtful layering and coordination between multiple garment pieces.")
            else:
                summary_parts.append("This outfit showcases a key fashion piece with strong individual character.")
        else:
            summary_parts.append("This outfit demonstrates contemporary fashion sensibility with versatile appeal.")

        # Add color and pattern insights
        if 'neutral' in colors.lower():
            summary_parts.append("The neutral color palette provides excellent versatility and sophisticated appeal.")
        elif 'warm' in colors.lower():
            summary_parts.append("The warm color tones create an approachable and energetic aesthetic.")
        elif 'cool' in colors.lower():
            summary_parts.append("The cool color palette conveys professionalism and calming elegance.")

        if 'floral' in pattern.lower():
            summary_parts.append("The floral elements add feminine charm and seasonal freshness to the overall look.")
        elif 'solid' in pattern.lower():
            summary_parts.append("The solid construction maximizes styling flexibility and accessory compatibility.")

        # Add style context
        if 'casual' in style.lower():
            summary_parts.append("The casual styling makes this outfit perfect for everyday wear and relaxed social occasions.")
        elif 'formal' in style.lower():
            summary_parts.append("The formal elements ensure this outfit is suitable for professional and elegant occasions.")
        elif 'versatile' in style.lower():
            summary_parts.append("The versatile design allows this outfit to transition seamlessly between different settings and occasions.")

        # Conclude with overall assessment
        summary_parts.append("The pieces complement each other harmoniously, creating a polished and intentional appearance that reflects contemporary fashion awareness.")

        return " ".join(summary_parts)

    def summarize_detections(self, detected_items):
        """Summarize detected fashion items"""
        if not detected_items:
            return "No specific fashion items detected. Using general image analysis."

        summary_lines = []
        for item in detected_items:
            category = item['category'].upper()
            confidence = item['confidence']
            summary_lines.append(f"β€’ {category}: {confidence*100:.1f}% confidence")

        return "\n".join(summary_lines)

    def create_detailed_garment_analysis(self, detected_items, basic_description):
        """Create detailed analysis of detected garments"""
        if not detected_items:
            return f"Based on image analysis: {basic_description}\n" + self.extract_comprehensive_details(basic_description)

        analysis_parts = []
        for item in detected_items:
            category = item['category']
            confidence = item['confidence']

            if confidence > 0.5:  # High confidence detections
                analysis_parts.append(f"**{category.upper()}** (Confidence: {confidence*100:.1f}%)")
                analysis_parts.append(self.get_category_specific_analysis(category, basic_description))
                analysis_parts.append("")  # Add spacing

        return "\n".join(analysis_parts)

    def get_category_specific_analysis(self, category, description):
        """Get specific analysis based on detected category"""
        category_analyses = {
            'top': self.analyze_top_garment(description),
            'bottom': self.analyze_bottom_garment(description),
            'dress': self.analyze_dress_garment(description),
            'outer': self.analyze_outer_garment(description),
            'shoes': self.analyze_shoes(description),
            'bag': self.analyze_bag(description),
            'hat': self.analyze_hat(description)
        }

        return category_analyses.get(category, f"General {category} analysis based on visual features.")

    def analyze_top_garment(self, description):
        """Analyze top garments (shirts, blouses, t-shirts)"""
        analysis = []
        desc_lower = description.lower()

        # Determine specific type
        if 't-shirt' in desc_lower or 'tee' in desc_lower:
            analysis.append("β€’ Type: T-shirt - casual wardrobe staple")
            analysis.append("β€’ Styling: Perfect for layering, casual wear, and relaxed occasions")
        elif 'shirt' in desc_lower or 'blouse' in desc_lower:
            analysis.append("β€’ Type: Shirt/Blouse - versatile professional piece")
            analysis.append("β€’ Styling: Suitable for business casual, can be dressed up or down")
        else:
            analysis.append("β€’ Type: Top garment - versatile upper body wear")

        # Color analysis
        colors = self.extract_colors(description)
        analysis.append(f"β€’ Color Profile: {colors}")

        # Fit and style
        fit = self.extract_fit(description)
        analysis.append(f"β€’ Fit & Silhouette: {fit}")

        # Styling suggestions
        analysis.append("β€’ Styling Tips: Tuck into high-waisted bottoms, layer under jackets, or wear loose for casual looks")

        return "\n".join(analysis)

    def analyze_bottom_garment(self, description):
        """Analyze bottom garments (pants, jeans, skirts)"""
        analysis = []
        desc_lower = description.lower()

        if 'jeans' in desc_lower:
            analysis.append("β€’ Type: Jeans - denim casual essential")
            analysis.append("β€’ Material: Denim fabric, durable and versatile")
        elif 'pants' in desc_lower or 'trousers' in desc_lower:
            analysis.append("β€’ Type: Pants/Trousers - structured bottom wear")
            analysis.append("β€’ Occasion: Professional, semi-formal, or smart casual")
        elif 'skirt' in desc_lower:
            analysis.append("β€’ Type: Skirt - feminine silhouette piece")
            analysis.append("β€’ Style: Adds elegance and movement to outfits")
        else:
            analysis.append("β€’ Type: Bottom garment - lower body wear")

        fit = self.extract_fit(description)
        analysis.append(f"β€’ Fit & Cut: {fit}")

        analysis.append("β€’ Styling: Pair with fitted tops for balanced proportions")

        return "\n".join(analysis)

    def analyze_dress_garment(self, description):
        """Analyze dress garments"""
        analysis = []

        analysis.append("β€’ Type: Dress - complete outfit piece")
        analysis.append("β€’ Advantage: Single garment solution for put-together looks")

        pattern = self.extract_pattern(description)
        analysis.append(f"β€’ Pattern & Design: {pattern}")

        style = self.extract_style(description)
        analysis.append(f"β€’ Style Category: {style}")

        occasion = self.extract_occasion(description)
        analysis.append(f"β€’ Best For: {occasion}")

        analysis.append("β€’ Styling: Add layers, accessories, or shoes to change the look's formality")

        return "\n".join(analysis)

    def analyze_outer_garment(self, description):
        """Analyze outer garments (jackets, coats, blazers)"""
        analysis = []
        desc_lower = description.lower()

        if 'blazer' in desc_lower:
            analysis.append("β€’ Type: Blazer - structured professional outerwear")
            analysis.append("β€’ Function: Adds polish and authority to outfits")
        elif 'jacket' in desc_lower:
            analysis.append("β€’ Type: Jacket - versatile layering piece")
            analysis.append("β€’ Function: Provides warmth and style enhancement")
        elif 'coat' in desc_lower:
            analysis.append("β€’ Type: Coat - substantial outerwear")
            analysis.append("β€’ Function: Weather protection with style")
        else:
            analysis.append("β€’ Type: Outer garment - layering piece")

        analysis.append("β€’ Styling: Layer over various outfits to change formality level")
        analysis.append("β€’ Versatility: Essential for transitional weather and professional looks")

        return "\n".join(analysis)

    def analyze_shoes(self, description):
        """Analyze footwear"""
        analysis = []
        analysis.append("β€’ Type: Footwear - outfit foundation piece")
        analysis.append("β€’ Impact: Significantly influences overall look formality and style")
        analysis.append("β€’ Styling: Choose based on occasion, comfort needs, and outfit balance")
        analysis.append("β€’ Tip: Quality footwear elevates any outfit")

        return "\n".join(analysis)

    def analyze_bag(self, description):
        """Analyze bags and accessories"""
        analysis = []
        analysis.append("β€’ Type: Bag/Accessory - functional style element")
        analysis.append("β€’ Purpose: Combines practicality with fashion statement")
        analysis.append("β€’ Styling: Should complement outfit colors and formality level")
        analysis.append("β€’ Tip: Quality bags are investment pieces that enhance multiple outfits")

        return "\n".join(analysis)

    def analyze_hat(self, description):
        """Analyze hats and headwear"""
        analysis = []
        analysis.append("β€’ Type: Hat/Headwear - statement accessory")
        analysis.append("β€’ Function: Adds personality and can change outfit's entire vibe")
        analysis.append("β€’ Styling: Consider face shape, hair style, and occasion")
        analysis.append("β€’ Tip: Hats can make simple outfits more interesting and unique")

        return "\n".join(analysis)

    def create_style_assessment(self, detected_items, basic_description):
        """Create style assessment based on detected items"""
        if not detected_items:
            return self.extract_style(basic_description)

        style_elements = []
        categories = [item['category'] for item in detected_items if item['confidence'] > 0.5]

        if 'dress' in categories:
            style_elements.append("β€’ Feminine and elegant aesthetic")
            style_elements.append("β€’ Single-piece sophistication")

        if 'blazer' in categories or 'outer' in categories:
            style_elements.append("β€’ Professional and structured elements")
            style_elements.append("β€’ Layered sophistication")

        if 'jeans' in basic_description.lower() or 'bottom' in categories:
            style_elements.append("β€’ Casual and approachable foundation")
            style_elements.append("β€’ Versatile everyday appeal")

        if not style_elements:
            style_elements.append("β€’ Contemporary fashion sensibility")
            style_elements.append("β€’ Versatile styling potential")

        return "\n".join(style_elements)

    def generate_styling_recommendations(self, detected_items):
        """Generate styling recommendations based on detected items"""
        if not detected_items:
            return "β€’ Focus on fit, color coordination, and occasion appropriateness\nβ€’ Layer pieces to create visual interest\nβ€’ Accessorize to personalize the look"

        recommendations = []
        categories = [item['category'] for item in detected_items if item['confidence'] > 0.5]

        if 'top' in categories:
            recommendations.append("β€’ Tuck into high-waisted bottoms for a polished silhouette")
            recommendations.append("β€’ Layer under jackets or cardigans for depth")

        if 'bottom' in categories:
            recommendations.append("β€’ Pair with fitted tops to balance proportions")
            recommendations.append("β€’ Choose shoes that complement the formality level")

        if 'dress' in categories:
            recommendations.append("β€’ Add a belt to define the waist")
            recommendations.append("β€’ Layer with jackets or cardigans for versatility")
            recommendations.append("β€’ Change accessories to shift from day to night")

        if 'outer' in categories:
            recommendations.append("β€’ Use as a statement piece over simple outfits")
            recommendations.append("β€’ Ensure proper fit in shoulders and sleeves")

        if not recommendations:
            recommendations.append("β€’ Focus on color harmony and proportion balance")
            recommendations.append("β€’ Consider the occasion and dress code")

        return "\n".join(recommendations)

    def generate_wardrobe_advice(self, detected_items):
        """Generate wardrobe integration advice"""
        if not detected_items:
            return "β€’ Invest in versatile, quality basics\nβ€’ Build around neutral colors\nβ€’ Choose pieces that work for multiple occasions"

        advice = []
        categories = [item['category'] for item in detected_items if item['confidence'] > 0.5]

        if 'top' in categories:
            advice.append("β€’ Essential wardrobe building block")
            advice.append("β€’ Pairs well with multiple bottom styles")

        if 'bottom' in categories:
            advice.append("β€’ Foundation piece for outfit construction")
            advice.append("β€’ Invest in quality fit and classic styles")

        if 'dress' in categories:
            advice.append("β€’ Versatile one-piece solution")
            advice.append("β€’ Can be styled for multiple occasions")

        if 'outer' in categories:
            advice.append("β€’ Transforms and elevates basic outfits")
            advice.append("β€’ Essential for professional wardrobes")

        advice.append("β€’ Consider cost-per-wear when building wardrobe")
        advice.append("β€’ Focus on pieces that reflect your lifestyle needs")

        return "\n".join(advice)

    def create_fashion_summary(self, detected_items, basic_description):
        """Create comprehensive fashion summary"""
        if not detected_items:
            return f"This garment demonstrates contemporary design with versatile styling potential. {basic_description}"

        primary_items = [item for item in detected_items if item['confidence'] > 0.6]

        if primary_items:
            main_category = primary_items[0]['category']
            confidence = primary_items[0]['confidence']

            summary = f"This {main_category} demonstrates {confidence*100:.0f}% detection confidence with professional fashion AI analysis. "

            if main_category in ['dress']:
                summary += "Offers complete outfit solution with feminine appeal and versatile styling options."
            elif main_category in ['top', 'shirt', 'blouse']:
                summary += "Serves as essential wardrobe foundation with excellent layering and styling flexibility."
            elif main_category in ['bottom', 'pants', 'jeans']:
                summary += "Provides structural foundation for balanced outfit proportions and professional styling."
            elif main_category in ['outer', 'jacket', 'blazer']:
                summary += "Functions as transformative layering piece that elevates casual outfits to professional standards."
            else:
                summary += "Represents quality fashion piece with contemporary appeal and styling versatility."
        else:
            summary = "Fashion analysis indicates contemporary styling with good versatility potential."

        return summary

    def get_average_confidence(self, detected_items):
        """Calculate average detection confidence"""
        if not detected_items:
            return "N/A"

        confidences = [item['confidence'] for item in detected_items]
        avg_confidence = sum(confidences) / len(confidences)
        return f"{avg_confidence*100:.1f}%"

    def extract_comprehensive_details(self, description):
        """Extract comprehensive details from description"""
        details = []
        details.append(f"β€’ Garment Type: {self.extract_garment_type(description)}")
        details.append(f"β€’ Color Analysis: {self.extract_colors(description)}")
        details.append(f"β€’ Pattern & Design: {self.extract_pattern(description)}")
        details.append(f"β€’ Style Category: {self.extract_style(description)}")
        details.append(f"β€’ Occasion Suitability: {self.extract_occasion(description)}")

        return "\n".join(details)

    def clean_generated_text(self, text):
        """Clean and format generated text for better readability"""
        if not text:
            return ""

        # Remove common artifacts and improve formatting
        text = text.strip()

        # Remove repetitive phrases
        lines = text.split('\n')
        cleaned_lines = []
        seen_lines = set()

        for line in lines:
            line = line.strip()
            if line and line not in seen_lines and len(line) > 10:
                cleaned_lines.append(line)
                seen_lines.add(line)

        return '\n'.join(cleaned_lines[:5])  # Limit to 5 unique lines

    def generate_advanced_insights(self, description):
        """Generate sophisticated fashion insights when AI generation fails"""
        garment_type = self.extract_garment_type(description).lower()
        colors = self.extract_colors(description).lower()
        pattern = self.extract_pattern(description).lower()
        style = self.extract_style(description).lower()

        insights = []

        # Advanced garment-specific insights
        if 'dress' in garment_type:
            if 'floral' in pattern:
                insights.extend([
                    "This floral dress exemplifies the timeless appeal of botanical motifs in women's fashion.",
                    "The floral pattern creates visual movement and adds romantic femininity to the silhouette.",
                    "Ideal for transitional seasons and outdoor social events where natural beauty is celebrated."
                ])
            elif 'black' in colors:
                insights.extend([
                    "The classic black dress represents the epitome of versatile elegance in fashion.",
                    "This piece serves as a foundational wardrobe element with endless styling possibilities.",
                    "Perfect for day-to-night transitions with simple accessory changes."
                ])
            else:
                insights.extend([
                    "This dress demonstrates contemporary design principles with classic appeal.",
                    "The silhouette offers both comfort and sophisticated style for modern lifestyles."
                ])
        elif any(item in garment_type for item in ['shirt', 'blouse', 'top']):
            insights.extend([
                "This top represents a versatile foundation piece essential for capsule wardrobes.",
                "The design allows for multiple styling interpretations from casual to professional.",
                "Perfect for layering strategies and seasonal wardrobe transitions."
            ])
        elif any(item in garment_type for item in ['pants', 'jeans', 'trousers']):
            insights.extend([
                "These bottoms provide structural foundation for balanced outfit proportions.",
                "The cut and fit demonstrate attention to contemporary silhouette preferences.",
                "Suitable for building cohesive looks across casual and semi-formal contexts."
            ])

        # Add sophisticated pattern and color insights
        if 'floral' in pattern:
            insights.append("The botanical motif connects the wearer to nature-inspired fashion trends and seasonal styling.")
        elif 'solid' in pattern:
            insights.append("The solid construction maximizes styling versatility and accessory compatibility.")
        elif 'striped' in pattern:
            insights.append("The linear pattern creates visual interest while maintaining classic appeal.")

        # Add color psychology insights
        if 'black' in colors:
            insights.append("Black conveys sophistication, authority, and timeless elegance in fashion psychology.")
        elif 'white' in colors:
            insights.append("White represents purity, freshness, and minimalist aesthetic principles.")
        elif any(color in colors for color in ['blue', 'navy']):
            insights.append("Blue tones suggest reliability, professionalism, and calming visual impact.")
        elif any(color in colors for color in ['red', 'burgundy']):
            insights.append("Red spectrum colors project confidence, energy, and bold fashion statements.")

        return ' '.join(insights) if insights else "This garment demonstrates thoughtful design principles with contemporary market appeal and versatile styling potential."

    def generate_fallback_insights(self, description):
        """Generate fallback insights when AI text generation fails"""
        garment_type = self.extract_garment_type(description).lower()
        pattern = self.extract_pattern(description).lower()

        insights = []

        if 'dress' in garment_type:
            if 'floral' in pattern:
                insights.extend([
                    "This floral dress embodies feminine elegance and seasonal charm.",
                    "The floral pattern adds visual interest and romantic appeal.",
                    "Perfect for spring/summer occasions and outdoor events."
                ])
            else:
                insights.extend([
                    "This dress offers versatile styling options for various occasions.",
                    "The silhouette provides both comfort and style."
                ])
        elif any(item in garment_type for item in ['shirt', 'blouse']):
            insights.extend([
                "This top serves as a versatile wardrobe foundation piece.",
                "Can be styled up or down depending on the occasion."
            ])
        elif any(item in garment_type for item in ['pants', 'jeans']):
            insights.extend([
                "These bottoms provide a solid foundation for outfit building.",
                "Suitable for both casual and semi-formal styling."
            ])

        # Add pattern-specific insights
        if 'floral' in pattern:
            insights.append("The floral motif brings natural beauty and femininity to the design.")
        elif 'solid' in pattern:
            insights.append("The solid color provides versatility for accessorizing and layering.")

        return ' '.join(insights) if insights else "This garment demonstrates classic design principles with contemporary appeal."

    def extract_garment_type(self, description):
        """Extract garment type from description with enhanced detection"""
        garment_keywords = {
            'dress': 'Dress',
            'gown': 'Evening Gown',
            'shirt': 'Shirt/Blouse',
            'blouse': 'Blouse',
            'top': 'Top',
            't-shirt': 'T-Shirt',
            'tank': 'Tank Top',
            'camisole': 'Camisole',
            'pants': 'Pants/Trousers',
            'trousers': 'Trousers',
            'jeans': 'Jeans',
            'leggings': 'Leggings',
            'jacket': 'Jacket',
            'blazer': 'Blazer',
            'coat': 'Coat',
            'cardigan': 'Cardigan',
            'sweater': 'Sweater',
            'pullover': 'Pullover',
            'hoodie': 'Hoodie',
            'sweatshirt': 'Sweatshirt',
            'skirt': 'Skirt',
            'shorts': 'Shorts',
            'jumpsuit': 'Jumpsuit',
            'romper': 'Romper',
            'vest': 'Vest',
            'tunic': 'Tunic'
        }

        description_lower = description.lower()

        # Check for specific garment types first
        for keyword, garment_type in garment_keywords.items():
            if keyword in description_lower:
                return garment_type

        # Fallback analysis based on context clues
        if any(word in description_lower for word in ['wearing', 'outfit', 'clothing']):
            return "Fashion Garment"

        return "Clothing Item"

    def extract_colors(self, description):
        """Extract colors from description with enhanced detection and color theory analysis"""
        color_keywords = {
            'black': {'name': 'Black', 'category': 'neutral', 'season': 'all', 'formality': 'high'},
            'white': {'name': 'White', 'category': 'neutral', 'season': 'all', 'formality': 'high'},
            'blue': {'name': 'Blue', 'category': 'cool', 'season': 'all', 'formality': 'medium'},
            'navy': {'name': 'Navy Blue', 'category': 'neutral', 'season': 'all', 'formality': 'high'},
            'red': {'name': 'Red', 'category': 'warm', 'season': 'winter', 'formality': 'medium'},
            'green': {'name': 'Green', 'category': 'cool', 'season': 'spring', 'formality': 'medium'},
            'yellow': {'name': 'Yellow', 'category': 'warm', 'season': 'summer', 'formality': 'low'},
            'pink': {'name': 'Pink', 'category': 'warm', 'season': 'spring', 'formality': 'low'},
            'purple': {'name': 'Purple', 'category': 'cool', 'season': 'fall', 'formality': 'medium'},
            'brown': {'name': 'Brown', 'category': 'neutral', 'season': 'fall', 'formality': 'medium'},
            'gray': {'name': 'Gray', 'category': 'neutral', 'season': 'all', 'formality': 'high'},
            'grey': {'name': 'Gray', 'category': 'neutral', 'season': 'all', 'formality': 'high'},
            'orange': {'name': 'Orange', 'category': 'warm', 'season': 'fall', 'formality': 'low'},
            'beige': {'name': 'Beige', 'category': 'neutral', 'season': 'all', 'formality': 'medium'},
            'cream': {'name': 'Cream', 'category': 'neutral', 'season': 'spring', 'formality': 'medium'},
            'tan': {'name': 'Tan', 'category': 'neutral', 'season': 'summer', 'formality': 'medium'},
            'gold': {'name': 'Gold', 'category': 'warm', 'season': 'fall', 'formality': 'high'},
            'silver': {'name': 'Silver', 'category': 'cool', 'season': 'winter', 'formality': 'high'},
            'maroon': {'name': 'Maroon', 'category': 'warm', 'season': 'fall', 'formality': 'high'},
            'burgundy': {'name': 'Burgundy', 'category': 'warm', 'season': 'fall', 'formality': 'high'},
            'teal': {'name': 'Teal', 'category': 'cool', 'season': 'winter', 'formality': 'medium'},
            'turquoise': {'name': 'Turquoise', 'category': 'cool', 'season': 'summer', 'formality': 'low'},
            'coral': {'name': 'Coral', 'category': 'warm', 'season': 'summer', 'formality': 'low'},
            'mint': {'name': 'Mint', 'category': 'cool', 'season': 'spring', 'formality': 'low'},
            'lavender': {'name': 'Lavender', 'category': 'cool', 'season': 'spring', 'formality': 'low'}
        }

        description_lower = description.lower()
        found_colors = []

        for keyword, color_info in color_keywords.items():
            if keyword in description_lower:
                if color_info not in found_colors:  # Avoid duplicates
                    found_colors.append(color_info)

        if found_colors:
            primary_color = found_colors[0]
            color_analysis = []

            # Primary color analysis
            color_analysis.append(f"Primary: {primary_color['name']}")

            # Color theory insights
            if primary_color['category'] == 'neutral':
                color_analysis.append("(Neutral - highly versatile)")
            elif primary_color['category'] == 'warm':
                color_analysis.append("(Warm tone - energetic and approachable)")
            else:
                color_analysis.append("(Cool tone - calming and professional)")

            # Seasonal and styling context
            if primary_color['season'] != 'all':
                color_analysis.append(f"Best for {primary_color['season']} styling")

            # Formality level
            if primary_color['formality'] == 'high':
                color_analysis.append("Suitable for formal occasions")
            elif primary_color['formality'] == 'medium':
                color_analysis.append("Versatile for casual to semi-formal")
            else:
                color_analysis.append("Perfect for casual, relaxed styling")

            # Additional colors
            if len(found_colors) > 1:
                other_colors = [c['name'] for c in found_colors[1:]]
                color_analysis.append(f"Secondary: {', '.join(other_colors)}")

            return ' β€’ '.join(color_analysis)
        else:
            # Try to infer from context
            if any(word in description_lower for word in ['dark', 'deep']):
                return "Dark tones detected β€’ Creates sophisticated, slimming effect β€’ Suitable for formal occasions"
            elif any(word in description_lower for word in ['light', 'pale', 'bright']):
                return "Light/bright tones detected β€’ Fresh, youthful appearance β€’ Perfect for casual, daytime wear"
            else:
                return "Mixed color palette β€’ Offers styling versatility β€’ Consider color coordination principles"

    def extract_neckline(self, description):
        """Extract neckline information with intelligent inference"""
        description_lower = description.lower()

        # Direct detection
        neckline_keywords = {
            'v-neck': 'V-neckline',
            'scoop': 'Scoop neckline',
            'crew': 'Crew neckline',
            'round': 'Round neckline',
            'collar': 'Collared',
            'turtleneck': 'Turtleneck',
            'off-shoulder': 'Off-shoulder',
            'strapless': 'Strapless',
            'halter': 'Halter neckline'
        }

        for keyword, neckline_type in neckline_keywords.items():
            if keyword in description_lower:
                return neckline_type

        # Intelligent inference based on garment type
        if 'dress' in description_lower:
            if 'formal' in description_lower or 'evening' in description_lower:
                return "Likely elegant neckline (common for dresses)"
            else:
                return "Standard dress neckline (round or scoop typical)"
        elif any(word in description_lower for word in ['shirt', 'blouse']):
            return "Standard shirt collar or neckline"
        elif 't-shirt' in description_lower:
            return "Crew or round neckline (typical for t-shirts)"

        return "Neckline present but style not specified"

    def extract_sleeves(self, description):
        """Extract sleeve information with intelligent inference"""
        description_lower = description.lower()

        # Direct detection
        sleeve_keywords = {
            'long sleeve': 'Long sleeves',
            'short sleeve': 'Short sleeves',
            'sleeveless': 'Sleeveless',
            'cap sleeve': 'Cap sleeves',
            'three-quarter': '3/4 sleeves',
            'bell sleeve': 'Bell sleeves',
            'puff sleeve': 'Puff sleeves'
        }

        for keyword, sleeve_type in sleeve_keywords.items():
            if keyword in description_lower:
                return sleeve_type

        # Intelligent inference based on garment type
        if 'dress' in description_lower:
            if 'summer' in description_lower or 'floral' in description_lower:
                return "Likely short sleeves or sleeveless (common for summer/floral dresses)"
            elif 'winter' in description_lower or 'formal' in description_lower:
                return "Likely long sleeves (common for formal/winter dresses)"
            else:
                return "Sleeve style varies (short sleeves, sleeveless, or straps typical for dresses)"
        elif 't-shirt' in description_lower:
            return "Short sleeves (standard for t-shirts)"
        elif any(word in description_lower for word in ['jacket', 'blazer', 'coat']):
            return "Long sleeves (standard for outerwear)"
        elif any(word in description_lower for word in ['tank', 'camisole']):
            return "Sleeveless or thin straps"

        return "Sleeve style not specified in description"

    def extract_pattern(self, description):
        """Extract pattern information with enhanced detection"""
        pattern_keywords = {
            'floral': 'Floral print',
            'flower': 'Floral design',
            'striped': 'Striped pattern',
            'stripes': 'Striped pattern',
            'plaid': 'Plaid pattern',
            'checkered': 'Checkered pattern',
            'polka dot': 'Polka dot pattern',
            'dots': 'Dotted pattern',
            'geometric': 'Geometric pattern',
            'abstract': 'Abstract pattern',
            'paisley': 'Paisley pattern',
            'animal print': 'Animal print',
            'leopard': 'Leopard print',
            'zebra': 'Zebra print',
            'solid': 'Solid color',
            'plain': 'Plain/solid',
            'printed': 'Printed design',
            'embroidered': 'Embroidered details',
            'lace': 'Lace pattern',
            'textured': 'Textured fabric'
        }

        description_lower = description.lower()
        found_patterns = []

        for keyword, pattern_name in pattern_keywords.items():
            if keyword in description_lower:
                if pattern_name not in found_patterns:
                    found_patterns.append(pattern_name)

        if found_patterns:
            return ', '.join(found_patterns)
        else:
            # Try to infer from context
            if any(word in description_lower for word in ['print', 'design', 'pattern']):
                return "Decorative pattern present"
            else:
                return "Solid or minimal pattern"

    def extract_fit(self, description):
        """Extract fit information with intelligent inference"""
        description_lower = description.lower()

        # Direct detection
        fit_keywords = {
            'loose': 'Loose fit',
            'tight': 'Tight/fitted',
            'fitted': 'Fitted',
            'oversized': 'Oversized',
            'slim': 'Slim fit',
            'relaxed': 'Relaxed fit',
            'bodycon': 'Body-conscious fit',
            'a-line': 'A-line silhouette',
            'straight': 'Straight fit'
        }

        for keyword, fit_type in fit_keywords.items():
            if keyword in description_lower:
                return fit_type

        # Intelligent inference based on garment type and style
        if 'dress' in description_lower:
            if 'floral' in description_lower:
                return "Likely flowing or A-line fit (common for floral dresses)"
            elif 'formal' in description_lower:
                return "Likely fitted or tailored silhouette"
            else:
                return "Dress silhouette (fit varies by style)"
        elif any(word in description_lower for word in ['jeans', 'pants']):
            return "Standard trouser fit (straight, slim, or relaxed)"
        elif any(word in description_lower for word in ['blazer', 'jacket']):
            return "Tailored fit (structured silhouette)"
        elif 't-shirt' in description_lower:
            return "Regular fit (standard t-shirt silhouette)"

        return "Fit style not specified in description"

    def extract_material(self, description):
        """Extract material information with intelligent inference"""
        description_lower = description.lower()

        # Direct detection
        material_keywords = {
            'cotton': 'Cotton',
            'denim': 'Denim',
            'silk': 'Silk',
            'wool': 'Wool',
            'polyester': 'Polyester',
            'leather': 'Leather',
            'linen': 'Linen',
            'chiffon': 'Chiffon',
            'satin': 'Satin',
            'velvet': 'Velvet',
            'lace': 'Lace',
            'knit': 'Knit fabric',
            'jersey': 'Jersey',
            'tweed': 'Tweed'
        }

        found_materials = []
        for keyword, material_name in material_keywords.items():
            if keyword in description_lower:
                found_materials.append(material_name)

        if found_materials:
            return ', '.join(found_materials)

        # Intelligent inference based on garment type
        if 'dress' in description_lower:
            if 'floral' in description_lower:
                return "Likely lightweight fabric (cotton, chiffon, or jersey common for floral dresses)"
            elif 'formal' in description_lower:
                return "Likely elegant fabric (silk, satin, or crepe)"
            else:
                return "Dress fabric (varies by style and season)"
        elif 'jeans' in description_lower:
            return "Denim (standard for jeans)"
        elif 't-shirt' in description_lower:
            return "Cotton or cotton blend (typical for t-shirts)"
        elif any(word in description_lower for word in ['blazer', 'suit']):
            return "Structured fabric (wool, polyester blend, or cotton)"
        elif 'sweater' in description_lower:
            return "Knit fabric (wool, cotton, or synthetic blend)"

        return "Fabric type not specified in description"

    def extract_features(self, description):
        """Extract features information"""
        feature_keywords = ['button', 'zip', 'pocket', 'belt', 'hood']
        description_lower = description.lower()
        found_features = [feature for feature in feature_keywords if feature in description_lower]
        return ', '.join(found_features).title() if found_features else "Specific features not clearly visible"

    def extract_enhanced_colors(self, description):
        """Enhanced color extraction with tone analysis and fashion context"""
        description_lower = description.lower()

        # Enhanced color detection with tone analysis
        color_analysis = {
            'black': {'name': 'Black', 'tone': 'neutral', 'context': 'timeless and versatile'},
            'white': {'name': 'White', 'tone': 'neutral', 'context': 'clean and minimalist'},
            'blue': {'name': 'Blue', 'tone': 'cool', 'context': 'calming and professional'},
            'navy': {'name': 'Navy blue', 'tone': 'neutral', 'context': 'sophisticated and classic'},
            'red': {'name': 'Red', 'tone': 'warm', 'context': 'bold and energetic'},
            'green': {'name': 'Green', 'tone': 'cool', 'context': 'natural and refreshing'},
            'yellow': {'name': 'Yellow', 'tone': 'warm', 'context': 'cheerful and optimistic'},
            'pink': {'name': 'Pink', 'tone': 'warm', 'context': 'feminine and soft'},
            'purple': {'name': 'Purple', 'tone': 'cool', 'context': 'creative and luxurious'},
            'orange': {'name': 'Orange', 'tone': 'warm', 'context': 'vibrant and playful'},
            'brown': {'name': 'Brown', 'tone': 'warm', 'context': 'earthy and grounded'},
            'gray': {'name': 'Gray', 'tone': 'neutral', 'context': 'sophisticated and balanced'},
            'grey': {'name': 'Grey', 'tone': 'neutral', 'context': 'sophisticated and balanced'},
            'beige': {'name': 'Beige', 'tone': 'neutral', 'context': 'warm neutral and versatile'},
            'cream': {'name': 'Cream', 'tone': 'warm', 'context': 'soft and elegant'},
            'tan': {'name': 'Tan', 'tone': 'warm', 'context': 'natural and casual'},
            'khaki': {'name': 'Khaki', 'tone': 'neutral', 'context': 'utilitarian and relaxed'}
        }

        detected_colors = []
        tone_categories = []

        for color_key, color_info in color_analysis.items():
            if color_key in description_lower:
                detected_colors.append(color_info['name'])
                tone_categories.append(color_info['tone'])

        if detected_colors:
            primary_color = detected_colors[0]
            primary_tone = tone_categories[0]

            if len(detected_colors) > 1:
                color_description = f"{primary_color} with {', '.join(detected_colors[1:])} accents ({primary_tone} tone palette)"
            else:
                color_description = f"{primary_color} ({primary_tone} tone)"

            # Add seasonal and styling context
            if primary_tone == 'neutral':
                color_description += " - versatile for all seasons and easy to style"
            elif primary_tone == 'warm':
                color_description += " - energetic warm tone, ideal for autumn/winter"
            elif primary_tone == 'cool':
                color_description += " - calming cool tone, perfect for spring/summer"

            return color_description

        # Fallback color inference
        if any(word in description_lower for word in ['dark', 'deep']):
            return "Dark tones (sophisticated and formal)"
        elif any(word in description_lower for word in ['light', 'pale', 'soft']):
            return "Light tones (fresh and approachable)"
        elif any(word in description_lower for word in ['bright', 'vibrant']):
            return "Bright tones (energetic and eye-catching)"

        return "Color not clearly specified (likely neutral or muted tones)"

    def extract_enhanced_material(self, description, garment_type):
        """Enhanced material extraction with texture and quality insights"""
        description_lower = description.lower()
        garment_lower = garment_type.lower()

        # Enhanced material detection with context
        material_analysis = {
            'cotton': {'name': 'Cotton', 'texture': 'soft and breathable', 'care': 'easy-care'},
            'denim': {'name': 'Denim', 'texture': 'structured and durable', 'care': 'low-maintenance'},
            'silk': {'name': 'Silk', 'texture': 'smooth and luxurious', 'care': 'delicate'},
            'wool': {'name': 'Wool', 'texture': 'warm and textured', 'care': 'special care'},
            'polyester': {'name': 'Polyester', 'texture': 'smooth and wrinkle-resistant', 'care': 'easy-care'},
            'leather': {'name': 'Leather', 'texture': 'smooth and structured', 'care': 'special care'},
            'linen': {'name': 'Linen', 'texture': 'crisp and breathable', 'care': 'wrinkle-prone'},
            'chiffon': {'name': 'Chiffon', 'texture': 'flowing and delicate', 'care': 'delicate'},
            'satin': {'name': 'Satin', 'texture': 'smooth and lustrous', 'care': 'delicate'},
            'velvet': {'name': 'Velvet', 'texture': 'plush and luxurious', 'care': 'special care'},
            'knit': {'name': 'Knit fabric', 'texture': 'stretchy and comfortable', 'care': 'easy-care'},
            'jersey': {'name': 'Jersey', 'texture': 'soft and stretchy', 'care': 'easy-care'},
            'tweed': {'name': 'Tweed', 'texture': 'textured and structured', 'care': 'dry clean'},
            'chambray': {'name': 'Chambray', 'texture': 'lightweight and soft', 'care': 'easy-care'},
            'flannel': {'name': 'Flannel', 'texture': 'soft and warm', 'care': 'easy-care'}
        }

        detected_materials = []
        for material_key, material_info in material_analysis.items():
            if material_key in description_lower:
                detected_materials.append(f"{material_info['name']} ({material_info['texture']})")

        if detected_materials:
            return ', '.join(detected_materials)

        # Intelligent material inference based on garment type
        if any(word in garment_lower for word in ['jeans', 'denim']):
            return "Denim (structured cotton weave, durable and versatile)"
        elif any(word in garment_lower for word in ['t-shirt', 'tee']):
            return "Cotton jersey (soft, breathable, and comfortable)"
        elif any(word in garment_lower for word in ['dress', 'blouse']):
            if 'formal' in description_lower or 'elegant' in description_lower:
                return "Silk or silk-blend (smooth drape, elegant finish)"
            else:
                return "Cotton or cotton blend (comfortable and versatile)"
        elif any(word in garment_lower for word in ['blazer', 'jacket']):
            return "Wool blend or structured fabric (tailored appearance, professional)"
        elif any(word in garment_lower for word in ['sweater', 'pullover']):
            return "Knit wool or cotton blend (warm, textured, cozy)"
        elif any(word in garment_lower for word in ['shoes', 'boots']):
            return "Leather or synthetic leather (durable, structured)"
        elif any(word in garment_lower for word in ['sneakers']):
            return "Canvas, mesh, or synthetic materials (lightweight, breathable)"

        return "Material not clearly visible (likely standard garment-appropriate fabric)"

    def extract_enhanced_features(self, description, garment_type):
        """Enhanced feature extraction with detailed fashion analysis"""
        desc_lower = description.lower()
        garment_lower = garment_type.lower()

        features = []

        # Detailed feature detection with fashion context
        feature_mapping = {
            'button': 'Button closure (classic and adjustable)',
            'zip': 'Zipper details (modern and functional)',
            'pocket': 'Functional pockets (practical and stylish)',
            'belt': 'Belt or waist definition (silhouette-enhancing)',
            'hood': 'Hood detail (casual and functional)',
            'collar': 'Collar design (structured and polished)',
            'sleeve': 'Distinctive sleeve styling',
            'pattern': 'Patterned design (visual interest)',
            'print': 'Printed motifs (decorative elements)',
            'embroidered': 'Embroidered details (artisanal touch)',
            'lace': 'Lace accents (feminine and delicate)',
            'ruffle': 'Ruffle details (romantic and textured)',
            'pleat': 'Pleated construction (structured movement)',
            'gather': 'Gathered fabric (soft volume)',
            'tuck': 'Tucked details (tailored precision)'
        }

        for keyword, feature_desc in feature_mapping.items():
            if keyword in desc_lower:
                features.append(feature_desc)

        # Garment-specific feature analysis
        if any(word in garment_lower for word in ['shirt', 'blouse']):
            # Neckline analysis
            if 'v-neck' in desc_lower:
                features.append("V-neckline (elongating and flattering)")
            elif 'crew' in desc_lower or 'round' in desc_lower:
                features.append("Round neckline (classic and versatile)")
            elif 'scoop' in desc_lower:
                features.append("Scoop neckline (feminine and soft)")

            # Sleeve analysis
            if 'long sleeve' in desc_lower:
                features.append("Long sleeves (coverage and sophistication)")
            elif 'short sleeve' in desc_lower:
                features.append("Short sleeves (casual and comfortable)")
            elif 'sleeveless' in desc_lower:
                features.append("Sleeveless design (modern and airy)")

            # Fit analysis
            if 'fitted' in desc_lower or 'slim' in desc_lower:
                features.append("Fitted silhouette (body-conscious and modern)")
            elif 'loose' in desc_lower or 'relaxed' in desc_lower:
                features.append("Relaxed fit (comfortable and casual)")
            elif 'oversized' in desc_lower:
                features.append("Oversized fit (contemporary and comfortable)")

        elif any(word in garment_lower for word in ['pants', 'jeans', 'trousers']):
            # Fit analysis
            if 'skinny' in desc_lower:
                features.append("Skinny fit (body-hugging and modern)")
            elif 'slim' in desc_lower:
                features.append("Slim fit (tailored and flattering)")
            elif 'straight' in desc_lower:
                features.append("Straight leg (classic and versatile)")
            elif 'wide' in desc_lower or 'flare' in desc_lower:
                features.append("Wide leg (flowing and dramatic)")

            # Rise analysis
            if 'high-waisted' in desc_lower or 'high waist' in desc_lower:
                features.append("High-waisted (vintage-inspired and flattering)")
            elif 'low-rise' in desc_lower:
                features.append("Low-rise (casual and relaxed)")

            # Length analysis
            if 'cropped' in desc_lower or 'ankle' in desc_lower:
                features.append("Cropped length (modern and versatile)")

        elif any(word in garment_lower for word in ['dress']):
            # Length analysis
            if 'mini' in desc_lower:
                features.append("Mini length (youthful and bold)")
            elif 'midi' in desc_lower:
                features.append("Midi length (elegant and versatile)")
            elif 'maxi' in desc_lower:
                features.append("Maxi length (dramatic and flowing)")

            # Silhouette analysis
            if 'a-line' in desc_lower:
                features.append("A-line silhouette (universally flattering)")
            elif 'bodycon' in desc_lower or 'fitted' in desc_lower:
                features.append("Fitted silhouette (body-conscious and sleek)")
            elif 'shift' in desc_lower:
                features.append("Shift silhouette (modern and comfortable)")

        elif any(word in garment_lower for word in ['shoes', 'sneakers', 'boots']):
            # Closure analysis
            if 'lace' in desc_lower:
                features.append("Lace-up closure (adjustable and secure)")
            elif 'slip-on' in desc_lower:
                features.append("Slip-on style (convenient and streamlined)")
            elif 'buckle' in desc_lower:
                features.append("Buckle closure (decorative and functional)")

            # Heel analysis
            if 'heel' in desc_lower:
                if 'high' in desc_lower:
                    features.append("High heel (elegant and formal)")
                elif 'low' in desc_lower:
                    features.append("Low heel (comfortable and versatile)")
                else:
                    features.append("Heeled design (elevated and polished)")
            elif 'flat' in desc_lower:
                features.append("Flat sole (comfortable and casual)")

            # Style details
            if 'platform' in desc_lower:
                features.append("Platform sole (height and comfort)")
            if 'pointed' in desc_lower:
                features.append("Pointed toe (sleek and sophisticated)")
            elif 'round' in desc_lower:
                features.append("Round toe (classic and comfortable)")

        # General styling features
        if 'layered' in desc_lower:
            features.append("Layered styling (dimensional and interesting)")
        if 'textured' in desc_lower:
            features.append("Textured fabric (tactile and visual interest)")
        if 'structured' in desc_lower:
            features.append("Structured construction (tailored and polished)")
        if 'flowing' in desc_lower:
            features.append("Flowing silhouette (graceful and feminine)")

        return ', '.join(features) if features else "Clean, minimalist design with standard construction details"

    def create_enhanced_outfit_summary(self, detected_items, basic_description):
        """Create enhanced outfit summary with sophisticated fashion analysis"""

        # Extract key elements for analysis
        garment_types = [item['category'] for item in detected_items if item['confidence'] > 0.5]
        colors = self.extract_enhanced_colors(basic_description)
        style_elements = self.extract_style(basic_description)
        pattern_info = self.extract_pattern(basic_description)

        summary_components = []

        # Opening statement about overall aesthetic and fashion intent
        if 'dress' in garment_types:
            summary_components.append("This outfit showcases the power of a well-chosen dress as a complete styling solution, demonstrating how a single piece can create a sophisticated and intentional look.")
        elif len(garment_types) >= 2:
            summary_components.append("This outfit exemplifies thoughtful coordination between multiple garment pieces, creating a harmonious ensemble that reflects contemporary fashion sensibility.")
        else:
            summary_components.append("This outfit highlights the impact of a key fashion piece, demonstrating how individual garments can make a strong style statement.")

        # Color, texture, and silhouette interaction analysis
        if 'neutral' in colors.lower():
            summary_components.append("The neutral color palette creates exceptional versatility and timeless appeal, allowing for easy accessorizing and seamless integration into various settings.")
        elif 'warm' in colors.lower():
            summary_components.append("The warm color tones generate an approachable and energetic aesthetic that conveys confidence and optimism, perfect for making a positive impression.")
        elif 'cool' in colors.lower():
            summary_components.append("The cool color palette establishes a calming and professional presence, ideal for both casual sophistication and semi-formal occasions.")
        else:
            summary_components.append("The color choices demonstrate thoughtful consideration for both visual impact and wearability across different contexts.")

        # Pattern and design element analysis
        if 'floral' in pattern_info.lower():
            summary_components.append("The floral elements introduce natural beauty and romantic charm, adding feminine appeal while maintaining contemporary relevance.")
        elif 'striped' in pattern_info.lower():
            summary_components.append("The striped pattern provides classic visual interest and timeless sophistication, creating structure and movement within the design.")
        elif 'solid' in pattern_info.lower() or 'minimal' in pattern_info.lower():
            summary_components.append("The solid construction maximizes styling flexibility and serves as an excellent foundation for creative accessorizing and layering.")

        # Cohesion and balance assessment
        if len(garment_types) > 1:
            summary_components.append("The pieces work together harmoniously, demonstrating an understanding of proportion, balance, and color coordination that creates visual cohesion.")

        # Occasion suitability and versatility analysis
        if 'casual' in style_elements.lower():
            summary_components.append("The casual styling approach makes this outfit perfect for everyday wear, weekend activities, and relaxed social gatherings while maintaining a polished appearance.")
        elif 'formal' in style_elements.lower() or 'business' in style_elements.lower():
            summary_components.append("The formal elements ensure this outfit meets professional standards and elegant occasion requirements with confidence and sophistication.")
        elif 'versatile' in style_elements.lower():
            summary_components.append("The versatile design allows this outfit to transition seamlessly between different settings, from casual daytime events to more polished evening occasions.")
        else:
            summary_components.append("The styling choices reflect adaptability and modern lifestyle needs, suitable for multiple occasions and settings.")

        # Fashion-forward observations and concluding statement
        if any(word in basic_description.lower() for word in ['layered', 'mixed', 'contrast']):
            summary_components.append("The layering techniques and contrast elements showcase contemporary fashion awareness, demonstrating how modern styling can create depth and visual interest while maintaining wearability.")
        elif any(word in basic_description.lower() for word in ['minimalist', 'clean', 'simple']):
            summary_components.append("The minimalist approach reflects current fashion trends toward refined simplicity, proving that thoughtful restraint can create maximum impact and enduring style.")
        else:
            summary_components.append("The overall composition reflects contemporary fashion intelligence, combining classic principles with modern sensibilities to create an effortlessly chic and memorable appearance.")

        # Combine components into flowing paragraph
        return " ".join(summary_components)

    def extract_style(self, description):
        """Extract style information with intelligent inference"""
        description_lower = description.lower()

        # Direct detection
        style_keywords = {
            'casual': 'Casual',
            'formal': 'Formal',
            'business': 'Business/Professional',
            'sporty': 'Sporty/Athletic',
            'elegant': 'Elegant',
            'vintage': 'Vintage',
            'bohemian': 'Bohemian',
            'minimalist': 'Minimalist',
            'romantic': 'Romantic',
            'edgy': 'Edgy',
            'classic': 'Classic',
            'trendy': 'Trendy/Contemporary'
        }

        found_styles = []
        for keyword, style_name in style_keywords.items():
            if keyword in description_lower:
                found_styles.append(style_name)

        if found_styles:
            return ', '.join(found_styles)

        # Intelligent inference based on garment type and patterns
        if 'dress' in description_lower:
            if 'floral' in description_lower:
                return "Feminine/Romantic (floral dresses often have romantic appeal)"
            elif 'black' in description_lower:
                return "Classic/Versatile (black dresses are wardrobe staples)"
            else:
                return "Feminine/Versatile (dresses suit various style preferences)"
        elif any(word in description_lower for word in ['blazer', 'suit']):
            return "Professional/Business (structured formal wear)"
        elif 'jeans' in description_lower:
            return "Casual/Everyday (denim is casual staple)"
        elif 't-shirt' in description_lower:
            return "Casual/Relaxed (t-shirts are casual basics)"
        elif any(word in description_lower for word in ['jacket', 'coat']):
            return "Outerwear/Functional (practical and stylish)"

        return "Versatile style (suitable for multiple aesthetics)"

    def extract_occasion(self, description):
        """Extract occasion information"""
        if any(word in description.lower() for word in ['formal', 'business', 'suit']):
            return "Formal occasions, business settings"
        elif any(word in description.lower() for word in ['casual', 'everyday']):
            return "Casual wear, everyday use"
        else:
            return "Versatile for multiple occasions"

    def generate_styling_tips(self, description):
        """Generate styling tips based on the garment"""
        garment_type = self.extract_garment_type(description).lower()
        pattern = self.extract_pattern(description).lower()

        tips = []

        if 'dress' in garment_type:
            tips.extend([
                "β€’ Pair with a denim jacket for casual daywear",
                "β€’ Add heels and accessories for evening events",
                "β€’ Layer with a cardigan for office-appropriate styling"
            ])
        elif 'shirt' in garment_type or 'blouse' in garment_type:
            tips.extend([
                "β€’ Tuck into high-waisted pants for a polished look",
                "β€’ Wear open over a tank top for layered styling",
                "β€’ Knot at the waist for a casual, trendy appearance"
            ])
        elif 'pants' in garment_type or 'jeans' in garment_type:
            tips.extend([
                "β€’ Pair with a fitted top to balance proportions",
                "β€’ Add a blazer for business casual styling",
                "β€’ Combine with sneakers for comfortable everyday wear"
            ])

        if 'floral' in pattern:
            tips.append("β€’ Keep accessories minimal to let the pattern shine")
        elif 'solid' in pattern:
            tips.append("β€’ Perfect base for statement accessories and bold jewelry")

        return '\n'.join(tips) if tips else "β€’ Versatile piece suitable for various styling approaches"

    def get_versatility_assessment(self, description):
        """Assess the versatility of the garment"""
        garment_type = self.extract_garment_type(description).lower()
        colors = self.extract_colors(description).lower()
        pattern = self.extract_pattern(description).lower()

        versatility_score = 0
        assessment_parts = []

        # Assess based on garment type
        if any(item in garment_type for item in ['dress', 'shirt', 'blouse', 'pants', 'jeans']):
            versatility_score += 2
            assessment_parts.append("highly versatile garment type")

        # Assess based on colors
        if any(color in colors for color in ['black', 'white', 'navy', 'gray']):
            versatility_score += 2
            assessment_parts.append("neutral color palette")
        elif colors == "colors not clearly identified":
            versatility_score += 1
            assessment_parts.append("adaptable color scheme")

        # Assess based on pattern
        if 'solid' in pattern:
            versatility_score += 2
            assessment_parts.append("solid pattern for easy mixing")
        elif any(p in pattern for p in ['floral', 'striped']):
            versatility_score += 1
            assessment_parts.append("distinctive pattern adds character")

        if versatility_score >= 4:
            return f"This is a {', '.join(assessment_parts)} making it an excellent wardrobe staple."
        elif versatility_score >= 2:
            return f"Features {', '.join(assessment_parts)} providing good styling flexibility."
        else:
            return "Offers unique styling opportunities for specific occasions."

# Import DeepFashion2 utilities
try:
    from deepfashion2_utils import DeepFashion2Config, get_deepfashion2_statistics
    from deepfashion2_evaluation import run_full_evaluation
    DEEPFASHION2_AVAILABLE = True
except ImportError as e:
    print(f"DeepFashion2 utilities not available: {e}")
    DEEPFASHION2_AVAILABLE = False

# Initialize analyzer
analyzer = HuggingFaceFashionAnalyzer()

# Initialize DeepFashion2 configuration if available
deepfashion2_config = None
if DEEPFASHION2_AVAILABLE:
    try:
        deepfashion2_config = DeepFashion2Config()
        print(f"DeepFashion2 integration initialized. Dataset root: {deepfashion2_config.dataset_root}")
    except Exception as e:
        print(f"Failed to initialize DeepFashion2 config: {e}")
        DEEPFASHION2_AVAILABLE = False

# Request/Response models
class GarmentDetails(BaseModel):
    type: str
    color: str
    material: str
    features: str

class OutfitSummary(BaseModel):
    aesthetic: str
    style_notes: str
    occasion_suitability: str
    color_harmony: str
    overall_assessment: str

class StructuredAnalysisResponse(BaseModel):
    upper_garment: GarmentDetails
    lower_garment: GarmentDetails
    footwear: GarmentDetails
    outfit_summary: OutfitSummary
    confidence_score: float
    detected_items: List[Dict[str, Any]] = []

class DetailedAnalysisResponse(BaseModel):
    structured_analysis: StructuredAnalysisResponse
    raw_analysis: str
    processing_time: float
    model_info: Dict[str, str]

# API Endpoints
@app.get("/", response_class=HTMLResponse)
async def root():
    """Main page with file upload interface"""
    return """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Fashion Analyzer</title>
        <style>
            body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
            .upload-area { border: 2px dashed #ccc; padding: 50px; text-align: center; margin: 20px 0; }
            .result { background: #f5f5f5; padding: 20px; margin: 20px 0; border-radius: 5px; }
        </style>
    </head>
    <body>
        <h1>🎽 Fashion Analyzer - JSON API</h1>
        <p>Upload an image of clothing to get detailed fashion analysis in structured JSON format</p>
        
        <div class="upload-area">
            <input type="file" id="imageInput" accept="image/*" style="margin: 10px;">
            <br>
            <button onclick="analyzeImage()" style="padding: 10px 20px; margin: 10px; background-color: #28a745; color: white;">Analyze Fashion (Detailed JSON)</button>
            <button onclick="analyzeStructured()" style="padding: 10px 20px; margin: 10px; background-color: #007bff; color: white;">Analyze Fashion (Structured JSON)</button>
            <button onclick="detectObjects()" style="padding: 10px 20px; margin: 10px; background-color: #fd7e14; color: white;">Detect Objects Only</button>
            <button onclick="checkDeepFashion2Status()" style="padding: 10px 20px; margin: 10px; background-color: #6f42c1; color: white;">DeepFashion2 Status</button>
            <br>
            <a href="/refined-prompt" target="_blank" style="color: #007bff; text-decoration: none;">View Refined Prompt Format</a>
        </div>
        
        <div id="result" class="result" style="display: none;">
            <h3>Analysis Result:</h3>
            <pre id="analysisText"></pre>
        </div>

        <script>
        async function analyzeImage() {
            const input = document.getElementById('imageInput');
            const file = input.files[0];

            if (!file) {
                alert('Please select an image file');
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            document.getElementById('analysisText').textContent = 'Analyzing with detailed JSON format... Please wait...';
            document.getElementById('result').style.display = 'block';

            try {
                const response = await fetch('/analyze-image', {
                    method: 'POST',
                    body: formData
                });

                const result = await response.json();

                // Format JSON output nicely
                let jsonOutput = 'DETAILED FASHION ANALYSIS RESULT:\\n\\n';
                jsonOutput += 'STRUCTURED ANALYSIS:\\n';
                jsonOutput += JSON.stringify(result.structured_analysis, null, 2);
                jsonOutput += '\\n\\nRAW ANALYSIS:\\n';
                jsonOutput += result.raw_analysis;
                jsonOutput += '\\n\\nPROCESSING INFO:\\n';
                jsonOutput += `Processing Time: ${result.processing_time.toFixed(3)} seconds\\n`;
                jsonOutput += `Device: ${result.model_info.device}\\n`;
                jsonOutput += `Detection Model: ${result.model_info.detection_model}\\n`;
                jsonOutput += `Feature Model: ${result.model_info.feature_model}\\n`;

                document.getElementById('analysisText').textContent = jsonOutput;
            } catch (error) {
                document.getElementById('analysisText').textContent = 'Error: ' + error.message;
            }
        }

        async function analyzeStructured() {
            const input = document.getElementById('imageInput');
            const file = input.files[0];

            if (!file) {
                alert('Please select an image file');
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            document.getElementById('analysisText').textContent = 'Analyzing with structured JSON format... Please wait...';
            document.getElementById('result').style.display = 'block';

            try {
                const response = await fetch('/analyze-structured', {
                    method: 'POST',
                    body: formData
                });

                const result = await response.json();

                // Format JSON output nicely
                let jsonOutput = 'STRUCTURED FASHION ANALYSIS RESULT:\\n\\n';
                jsonOutput += JSON.stringify(result, null, 2);

                document.getElementById('analysisText').textContent = jsonOutput;
            } catch (error) {
                document.getElementById('analysisText').textContent = 'Error: ' + error.message;
            }
        }

        async function detectObjects() {
            const input = document.getElementById('imageInput');
            const file = input.files[0];

            if (!file) {
                alert('Please select an image file');
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            document.getElementById('analysisText').textContent = 'Detecting fashion objects... Please wait...';
            document.getElementById('result').style.display = 'block';

            try {
                const response = await fetch('/detect-objects', {
                    method: 'POST',
                    body: formData
                });

                const result = await response.json();

                // Format JSON output nicely
                let jsonOutput = 'FASHION OBJECT DETECTION RESULT:\\n\\n';
                jsonOutput += JSON.stringify(result, null, 2);

                document.getElementById('analysisText').textContent = jsonOutput;
            } catch (error) {
                document.getElementById('analysisText').textContent = 'Error: ' + error.message;
            }
        }

        async function checkDeepFashion2Status() {
            document.getElementById('analysisText').textContent = 'Checking DeepFashion2 status...';
            document.getElementById('result').style.display = 'block';

            try {
                const response = await fetch('/deepfashion2/status');
                const result = await response.json();

                let statusText = 'DeepFashion2 Integration Status:\\n\\n';
                statusText += `Available: ${result.available}\\n`;

                if (result.available) {
                    statusText += `Dataset Exists: ${result.dataset_exists}\\n`;
                    statusText += `Dataset Root: ${result.dataset_root}\\n`;
                    statusText += `Categories: ${result.categories.length} categories\\n`;
                    statusText += `Image Size: ${result.image_size[0]}x${result.image_size[1]}\\n\\n`;

                    if (!result.dataset_exists) {
                        statusText += 'Dataset not found. Click "Setup Instructions" for download guide.\\n';
                    } else {
                        statusText += 'Dataset ready! You can run evaluations.\\n';
                    }
                } else {
                    statusText += `Message: ${result.message}\\n`;
                }

                document.getElementById('analysisText').textContent = statusText;

                // Add setup instructions button if needed
                if (result.available && !result.dataset_exists) {
                    const setupBtn = document.createElement('button');
                    setupBtn.textContent = 'Get Setup Instructions';
                    setupBtn.onclick = getSetupInstructions;
                    setupBtn.style.cssText = 'padding: 10px 20px; margin: 10px; background-color: #17a2b8; color: white;';
                    document.getElementById('result').appendChild(setupBtn);
                }

            } catch (error) {
                document.getElementById('analysisText').textContent = 'Error checking status: ' + error.message;
            }
        }

        async function getSetupInstructions() {
            try {
                const response = await fetch('/deepfashion2/setup-instructions');
                const result = await response.json();

                let instructionsText = result.title + '\\n\\n';

                result.steps.forEach(step => {
                    instructionsText += `Step ${step.step}: ${step.description}\\n`;
                    if (step.url) instructionsText += `URL: ${step.url}\\n`;
                    if (step.command) instructionsText += `Command: ${step.command}\\n`;
                    if (step.structure) {
                        instructionsText += 'Structure:\\n';
                        step.structure.forEach(line => instructionsText += `  ${line}\\n`);
                    }
                    if (step.endpoint) instructionsText += `Endpoint: ${step.endpoint}\\n`;
                    instructionsText += '\\n';
                });

                instructionsText += 'Notes:\\n';
                result.notes.forEach(note => instructionsText += `β€’ ${note}\\n`);

                document.getElementById('analysisText').textContent = instructionsText;

            } catch (error) {
                document.getElementById('analysisText').textContent = 'Error getting instructions: ' + error.message;
            }
        }
        </script>
    </body>
    </html>
    """

@app.post("/analyze-image", response_model=DetailedAnalysisResponse)
async def analyze_image(file: UploadFile = File(...)):
    """Analyze uploaded image and return structured JSON response"""
    try:
        start_time = time.time()

        # Read image bytes
        image_bytes = await file.read()

        # Process image
        image = analyzer.process_image_from_bytes(image_bytes)

        # Get fashion object detection results
        detection_results = analyzer.detect_fashion_objects(image)

        # Get basic image description
        basic_description = analyzer.get_basic_description(image)

        # Extract structured information
        structured_analysis = analyzer.parse_structured_analysis(detection_results, basic_description)

        # Get raw analysis for comparison
        raw_analysis = analyzer.analyze_clothing_structured_format(image_bytes)

        processing_time = time.time() - start_time

        return DetailedAnalysisResponse(
            structured_analysis=structured_analysis,
            raw_analysis=raw_analysis,
            processing_time=processing_time,
            model_info={
                "detection_model": analyzer.detection_ckpt if analyzer.detection_model else "Not available",
                "feature_model": analyzer.encoder_ckpt if analyzer.feature_encoder else "Not available",
                "device": analyzer.device
            }
        )

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")

@app.post("/analyze-structured", response_model=StructuredAnalysisResponse)
async def analyze_structured(file: UploadFile = File(...)):
    """Analyze uploaded image with structured format and return JSON"""
    try:
        # Read image bytes
        image_bytes = await file.read()

        # Process image
        image = analyzer.process_image_from_bytes(image_bytes)

        # Get fashion object detection results
        detection_results = analyzer.detect_fashion_objects(image)

        # Get basic image description
        basic_description = analyzer.get_basic_description(image)

        # Extract structured information
        structured_analysis = analyzer.parse_structured_analysis(detection_results, basic_description)

        return structured_analysis

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")

@app.post("/analyze-enhanced", response_class=PlainTextResponse)
async def analyze_clothing_enhanced(file: UploadFile = File(...)):
    """Analyze clothing using enhanced prompt format optimized for feature extraction"""
    try:
        # Read image bytes
        image_bytes = await file.read()

        # Analyze clothing using enhanced prompt format
        analysis = analyzer.analyze_clothing_enhanced_prompt_format(image_bytes)

        return analysis

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")

@app.post("/detect-objects", response_model=Dict[str, Any])
async def detect_objects(file: UploadFile = File(...)):
    """Detect fashion objects and return JSON structure"""
    try:
        # Read image bytes
        image_bytes = await file.read()

        # Process image
        image = analyzer.process_image_from_bytes(image_bytes)

        # Get fashion object detection results
        detection_results = analyzer.detect_fashion_objects(image)

        return detection_results

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error detecting objects: {str(e)}")

@app.post("/extract-features", response_model=Dict[str, Any])
async def extract_features(file: UploadFile = File(...)):
    """Extract fashion features and return JSON structure"""
    try:
        # Read image bytes
        image_bytes = await file.read()

        # Process image
        image = analyzer.process_image_from_bytes(image_bytes)

        # Extract fashion features
        feature_results = analyzer.extract_fashion_features(image)

        return feature_results

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error extracting features: {str(e)}")

@app.get("/refined-prompt", response_class=PlainTextResponse)
async def get_refined_prompt():
    """Get the refined prompt for clothing analysis"""
    refined_prompt = """
πŸ” **CLOTHING ANALYSIS INSTRUCTION PROMPT β€” OPTIMIZED FOR OUTFIT RECOMMENDATION AI**

You are an expert fashion stylist and computer vision analyst. Your task is to **analyze a photo of a person** and produce a **detailed, structured breakdown** of their outfit. Focus on identifying visible garments and extracting features relevant to **style, fit, color theory, material, and occasion**.

Use the following **strict format** and ensure each field is filled with accurate, professional fashion insights. When unsure, make reasonable inferences based on visual clues and fashion knowledge.

---

### 🧩 **RESPONSE STRUCTURE**:

---

### **1. UPPER GARMENT**

* **Type**: *(e.g., t-shirt, shirt, blouse, jacket, coat, dress, blazer, hoodie)*
* **Color**: *(Primary color + notable accents; include tone category β€” warm, cool, neutral)*
* **Material**: *(Visually identifiable or inferred β€” cotton, denim, knit, silk, etc.)*
* **Features**: *(e.g., sleeve length, neckline, collar, fit, embellishments, print/pattern, texture)*

---

### **2. LOWER GARMENT**

* **Type**: *(e.g., jeans, trousers, skirt, shorts, joggers, culottes)*
* **Color**: *(Primary + secondary tones; match to upper color context)*
* **Material**: *(e.g., denim, polyester, linen; infer if not obvious)*
* **Features**: *(e.g., cut, length, waist rise, fit, pleats, pockets, fasteners, patterns)*

---

### **3. FOOTWEAR**

* **Type**: *(e.g., sneakers, boots, sandals, loafers, heels, formal shoes)*
* **Color**: *(Main color + accent details; match to outfit tones if relevant)*
* **Material**: *(e.g., leather, suede, canvas, mesh β€” inferred if unclear)*
* **Features**: *(e.g., laces, buckles, platform, heel height, style, sole type)*

---

### **4. OUTFIT SUMMARY**

Write a **3–5 sentence paragraph** that:

* Evaluates the **overall style and fashion intent** of the outfit
* Describes how **color, texture, and silhouette** interact
* Comments on **cohesion and balance** between garments
* Assesses **occasion suitability** (e.g., casual, business casual, streetwear, evening)
* Provides **fashion-forward observations** (e.g., layering techniques, contrast, minimalism, etc.)

---

### βœ… **GUIDELINES FOR THE MODEL**:

* Use **fashion-specific vocabulary** (e.g., oversized, tailored, cropped, monochromatic, muted tones)
* Apply **color theory**: mention harmony/contrast, seasonal suitability, or tone warmth
* Infer **formality level** (e.g., smart casual, athleisure, streetwear, semi-formal)
* Mention **fit and silhouette** if visible (e.g., slim-fit, relaxed, boxy, flowy)
* If a garment is partially occluded, describe what's visible and make plausible inferences
* Use consistent structure and complete all sections unless a category is **not applicable**

---

### πŸ“ **EXAMPLE OUTPUT FORMAT**:

---

#### **UPPER GARMENT**

**Type**: Button-down chambray shirt
**Color**: Light denim blue (cool tone) with faded areas on collar and seams
**Material**: Chambray (lightweight woven cotton)
**Features**: Long sleeves rolled to elbows, pointed collar, front patch pockets, relaxed fit, visible stitching

---

#### **LOWER GARMENT**

**Type**: Black slim-fit chinos
**Color**: Solid black (neutral tone)
**Material**: Stretch cotton twill
**Features**: Flat front, minimal pockets, cropped ankle length, clean silhouette

---

#### **FOOTWEAR**

**Type**: White minimalist sneakers
**Color**: White with subtle grey accents on heel tab
**Material**: Leather upper with rubber sole
**Features**: Lace-up closure, low-profile design, round toe

---

#### **OUTFIT SUMMARY**

This outfit channels effortless smart-casual style with its blend of soft textures and minimal structure. The chambray shirt introduces a laid-back, workwear-inspired vibe, complemented by the refined edge of slim black chinos. The white sneakers tie the look together with understated coolness, ensuring comfort without sacrificing polish. The color palette of light blue, black, and white reflects balance and neutrality, making the outfit adaptable for casual office settings, social outings, or relaxed dates. The overall silhouette is clean and streamlined, making it modern, versatile, and easy to accessorize.
"""
    return refined_prompt

@app.get("/health")
async def health_check():
    """Health check endpoint"""
    try:
        # Test HuggingFace models availability
        if hasattr(analyzer, 'image_to_text') and analyzer.image_to_text is not None:
            return {"status": "healthy", "models": "loaded", "device": analyzer.device}
        else:
            return {"status": "unhealthy", "models": "not_loaded"}
    except Exception as e:
        return {"status": "unhealthy", "error": str(e)}

# DeepFashion2 API endpoints
@app.get("/deepfashion2/status")
async def deepfashion2_status():
    """Get DeepFashion2 integration status"""
    if not DEEPFASHION2_AVAILABLE:
        return {"available": False, "message": "DeepFashion2 utilities not available"}

    if not deepfashion2_config:
        return {"available": False, "message": "DeepFashion2 configuration not initialized"}

    # Check if dataset exists
    dataset_path = Path(deepfashion2_config.dataset_root)
    dataset_exists = dataset_path.exists()

    return {
        "available": True,
        "dataset_exists": dataset_exists,
        "dataset_root": deepfashion2_config.dataset_root,
        "categories": deepfashion2_config.categories,
        "image_size": deepfashion2_config.image_size
    }

@app.get("/deepfashion2/statistics")
async def deepfashion2_statistics():
    """Get DeepFashion2 dataset statistics"""
    if not DEEPFASHION2_AVAILABLE or not deepfashion2_config:
        raise HTTPException(status_code=503, detail="DeepFashion2 not available")

    try:
        stats = get_deepfashion2_statistics(deepfashion2_config)
        return stats
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error getting statistics: {str(e)}")

@app.post("/deepfashion2/evaluate")
async def deepfashion2_evaluate(max_samples: int = 50):
    """Run evaluation using DeepFashion2 dataset"""
    if not DEEPFASHION2_AVAILABLE or not deepfashion2_config:
        raise HTTPException(status_code=503, detail="DeepFashion2 not available")

    try:
        # Run evaluation in background (for demo purposes, limit samples)
        report_path = run_full_evaluation(analyzer, deepfashion2_config, max_samples=max_samples)

        return {
            "status": "completed",
            "report_path": report_path,
            "max_samples": max_samples,
            "message": f"Evaluation completed with {max_samples} samples"
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Evaluation failed: {str(e)}")

@app.get("/deepfashion2/setup-instructions")
async def deepfashion2_setup_instructions():
    """Get setup instructions for DeepFashion2 dataset"""
    return {
        "title": "DeepFashion2 Dataset Setup Instructions",
        "steps": [
            {
                "step": 1,
                "description": "Visit the official DeepFashion2 repository",
                "url": "https://github.com/switchablenorms/DeepFashion2"
            },
            {
                "step": 2,
                "description": "Follow the dataset download instructions in the repository"
            },
            {
                "step": 3,
                "description": "Create the dataset directory",
                "command": f"mkdir -p {deepfashion2_config.dataset_root if deepfashion2_config else './data/deepfashion2'}"
            },
            {
                "step": 4,
                "description": "Extract the dataset with the following structure:",
                "structure": [
                    "deepfashion2/",
                    "β”œβ”€β”€ train/",
                    "β”‚   β”œβ”€β”€ image/",
                    "β”‚   └── annos/",
                    "β”œβ”€β”€ validation/",
                    "β”‚   β”œβ”€β”€ image/",
                    "β”‚   └── annos/",
                    "└── test/",
                    "    β”œβ”€β”€ image/",
                    "    └── annos/"
                ]
            },
            {
                "step": 5,
                "description": "Verify the setup by checking the status endpoint",
                "endpoint": "/deepfashion2/status"
            }
        ],
        "notes": [
            "The DeepFashion2 dataset is large (~15GB) and requires registration",
            "Make sure you have sufficient disk space",
            "The dataset contains 491K images across 13 clothing categories"
        ]
    }

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)