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

# 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 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 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 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_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."

# Initialize analyzer
analyzer = HuggingFaceFashionAnalyzer()

# Request/Response models
class AnalysisResponse(BaseModel):
    analysis: 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</h1>
        <p>Upload an image of clothing to get detailed fashion analysis</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;">Analyze Fashion (Detailed)</button>
            <button onclick="analyzeStructured()" style="padding: 10px 20px; margin: 10px;">Analyze Fashion (Structured)</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... Please wait...';
            document.getElementById('result').style.display = 'block';

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

                const result = await response.json();
                document.getElementById('analysisText').textContent = result.analysis;
            } 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 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();
                document.getElementById('analysisText').textContent = result.analysis;
            } catch (error) {
                document.getElementById('analysisText').textContent = 'Error: ' + error.message;
            }
        }
        </script>
    </body>
    </html>
    """

@app.post("/analyze-image", response_model=AnalysisResponse)
async def analyze_image(file: UploadFile = File(...)):
    """Analyze uploaded image"""
    try:
        # Read image bytes
        image_bytes = await file.read()
        
        # Analyze the clothing
        analysis = analyzer.analyze_clothing_from_bytes(image_bytes)
        
        return AnalysisResponse(analysis=analysis)
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")

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

        # Get structured analysis
        analysis = analyzer.analyze_clothing_structured_format(image_bytes)

        return AnalysisResponse(analysis=analysis)

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

@app.get("/refined-prompt", response_class=PlainTextResponse)
async def get_refined_prompt():
    """Get the refined prompt for clothing analysis"""
    refined_prompt = """
REFINED CLOTHING ANALYSIS PROMPT

This task involves analyzing an image of a person and providing a detailed description of their clothing, focusing on the upper garment, lower garment, and footwear. Your response should follow this structured format:

ANALYSIS STRUCTURE:

1. UPPER GARMENT:
   Type: [e.g., shirt, blouse, sweater, t-shirt, dress, jacket, blazer]
   Color: [Primary color and any secondary colors with color theory insights]
   Material: [Fabric type if visible, or intelligent inference based on garment type]
   Features: [Distinguishing features like patterns, buttons, collars, sleeves, neckline]

2. LOWER GARMENT:
   Type: [e.g., jeans, pants, skirt, shorts, trousers]
   Color: [Primary color and any secondary colors]
   Material: [Fabric type if visible, or intelligent inference]
   Features: [Distinguishing features like pockets, belt, pleats, fit, length]

3. FOOTWEAR:
   Type: [e.g., sneakers, heels, boots, flats, sandals]
   Color: [Primary color and any secondary colors]
   Material: [Material type if visible, or intelligent inference]
   Features: [Distinguishing features like laces, buckles, studs, heel height, style]

4. OUTFIT SUMMARY:
   Provide a comprehensive paragraph (3-5 sentences) that:
   - Describes the overall aesthetic and style of the outfit
   - Explains how the different pieces complement each other
   - Assesses the occasion appropriateness and versatility
   - Includes color harmony and pattern coordination analysis
   - Offers styling insights and fashion-forward observations

ANALYSIS GUIDELINES:

• For each field, provide detailed descriptions that capture both visible elements and professional fashion insights
• Use fashion terminology appropriately (silhouette, drape, texture, etc.)
• Include color theory analysis (warm/cool tones, neutrals, seasonal appropriateness)
• Consider fit, proportion, and styling principles
• Assess formality level and occasion suitability
• Provide intelligent inferences when specific details aren't clearly visible
• The Outfit Summary should be creative, informative, and demonstrate fashion expertise

OUTPUT FORMAT:
Structure your response exactly as shown above, with clear section headers and consistent formatting. Each section should provide both factual observations and professional fashion analysis.

EXAMPLE OUTPUT FORMAT:

UPPER GARMENT:
Type: Floral midi dress
Color: Navy blue base with white and pink floral print (cool-toned palette, versatile for multiple seasons)
Material: Lightweight cotton or cotton blend (suitable for comfort and drape)
Features: Short sleeves, round neckline, fitted bodice with A-line skirt, all-over floral pattern

LOWER GARMENT:
[Not applicable - dress serves as complete outfit]

FOOTWEAR:
Type: White leather sneakers
Color: Clean white with minimal accent details
Material: Leather upper with rubber sole
Features: Lace-up closure, low-profile design, classic athletic styling

OUTFIT SUMMARY:
This outfit demonstrates a perfect balance between feminine charm and casual comfort through the floral midi dress paired with clean white sneakers. The navy blue base with delicate floral motifs creates visual interest while maintaining sophistication, making it suitable for daytime social events, casual dates, or weekend outings. The contrast between the dress's romantic aesthetic and the sneakers' sporty edge exemplifies modern mixed-styling approaches. The cool-toned color palette ensures versatility across seasons, while the comfortable silhouette and practical footwear choice reflect contemporary lifestyle needs. This combination successfully bridges the gap between dressed-up and dressed-down, creating an effortlessly chic appearance that's both approachable and stylish.
"""
    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)}

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