File size: 60,138 Bytes
83acc29
d95d5d7
4482157
83acc29
f6c0703
45f23c1
83acc29
744524e
83acc29
744524e
 
 
4482157
f26d56e
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6c0703
83acc29
744524e
4482157
 
744524e
 
 
4482157
 
 
 
 
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4482157
744524e
83acc29
 
 
744524e
59a65c8
744524e
 
 
83acc29
 
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21c8cf3
744524e
21c8cf3
744524e
 
 
21c8cf3
744524e
21c8cf3
744524e
 
21c8cf3
744524e
 
 
 
21c8cf3
744524e
 
21c8cf3
 
744524e
 
 
21c8cf3
744524e
21c8cf3
744524e
 
21c8cf3
744524e
 
 
 
 
21c8cf3
744524e
 
21c8cf3
cc12e8e
744524e
 
 
83acc29
744524e
f6c0703
744524e
 
83acc29
744524e
 
 
 
 
4482157
744524e
 
4482157
8edca23
744524e
 
 
4482157
744524e
db330de
744524e
 
db330de
744524e
 
 
 
 
db330de
744524e
 
db330de
83acc29
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
4482157
4c8bafa
83acc29
744524e
83acc29
4c8bafa
4482157
 
83acc29
 
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8edca23
83acc29
4482157
 
 
 
 
 
 
744524e
4482157
 
 
 
 
 
 
8edca23
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc12e8e
83acc29
4482157
 
 
8edca23
4482157
 
4c8bafa
4482157
 
4c8bafa
744524e
4482157
cc12e8e
4482157
 
4c8bafa
4482157
 
cc12e8e
4482157
 
 
cc12e8e
4482157
 
 
 
 
 
 
 
 
 
 
 
8edca23
4482157
 
 
8edca23
4482157
 
8edca23
4482157
8edca23
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8edca23
4482157
8edca23
4482157
 
8edca23
4482157
 
8edca23
4482157
 
 
 
 
 
8edca23
4482157
83acc29
4482157
 
 
 
 
4c8bafa
4482157
 
 
 
 
 
 
 
 
 
 
 
 
8edca23
4482157
45f23c1
4482157
 
 
 
 
45f23c1
4482157
 
 
8edca23
4482157
 
 
 
 
 
 
 
 
8edca23
 
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83acc29
 
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45f23c1
 
4482157
f6c0703
4482157
 
cc12e8e
4482157
 
 
cc12e8e
4482157
 
 
 
 
 
cc12e8e
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8edca23
4482157
 
cc12e8e
4482157
 
 
 
 
 
 
 
8edca23
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8edca23
 
 
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83acc29
 
744524e
83acc29
 
4482157
 
 
 
 
744524e
4482157
83acc29
4482157
7cd43fd
4482157
 
7cd43fd
4482157
 
 
744524e
 
 
4482157
744524e
d95d5d7
 
 
 
 
 
 
 
 
 
 
 
 
744524e
 
 
 
 
d95d5d7
 
4482157
 
744524e
 
 
 
 
 
 
 
4482157
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4482157
744524e
 
 
 
 
4482157
744524e
 
 
 
 
 
45f23c1
744524e
 
 
 
f6c0703
744524e
 
 
4482157
744524e
 
cc12e8e
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4482157
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4482157
 
744524e
 
 
 
 
 
 
 
4482157
 
 
 
 
 
 
 
744524e
 
 
 
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744524e
 
 
 
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744524e
4482157
45f23c1
83acc29
744524e
83acc29
f6c0703
744524e
 
 
 
 
 
 
 
 
 
 
 
4482157
744524e
 
 
4482157
744524e
 
 
 
 
 
 
 
4482157
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4482157
744524e
 
 
 
 
4482157
744524e
 
 
 
4482157
744524e
 
 
 
4482157
744524e
 
 
 
 
 
 
4482157
744524e
 
 
 
4482157
 
744524e
 
4482157
8edca23
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4482157
744524e
 
 
 
 
 
812895b
 
 
 
 
 
 
 
 
 
 
 
 
744524e
 
4482157
 
 
 
 
 
8edca23
812895b
 
 
 
 
 
 
 
 
 
4482157
 
 
812895b
 
 
4482157
 
 
 
 
 
 
 
 
812895b
4482157
812895b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744524e
4482157
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4482157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744524e
 
 
ad43ad0
744524e
 
 
 
 
 
 
 
 
ad43ad0
 
744524e
 
 
ad43ad0
744524e
 
 
 
 
 
ad43ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
744524e
ad43ad0
 
 
 
744524e
 
ad43ad0
 
 
 
744524e
ad43ad0
744524e
ad43ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad43ad0
744524e
 
 
ad43ad0
 
 
744524e
ad43ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744524e
ad43ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744524e
ad43ad0
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
812895b
 
 
744524e
 
812895b
744524e
812895b
744524e
 
812895b
 
 
 
744524e
 
812895b
744524e
812895b
744524e
 
 
 
 
 
 
 
 
 
 
 
ad43ad0
 
 
 
 
744524e
 
 
 
 
 
 
ad43ad0
744524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
812895b
744524e
 
 
 
 
 
 
 
 
 
 
812895b
 
 
744524e
 
812895b
744524e
 
812895b
 
 
 
744524e
812895b
ad43ad0
 
 
 
744524e
 
 
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
# ============================================================================
# ๐Ÿ“ฆ INCLUSIVEEDU - FULL VERSION WITH GRADIO + INTEGRATED API
# Compatible with Hugging Face Spaces + External API Access
# ============================================================================

import os
import re
import time
import random
from datetime import datetime
from typing import List, Optional, Dict, Any
from dataclasses import dataclass
from pydantic import BaseModel

# FastAPI imports
try:
    from fastapi import FastAPI, HTTPException
    from fastapi.middleware.cors import CORSMiddleware
    FASTAPI_AVAILABLE = True
except ImportError:
    print("โš ๏ธ FastAPI not available - API mode disabled")
    FASTAPI_AVAILABLE = False

# Gradio imports
try:
    import gradio as gr
    GRADIO_AVAILABLE = True
except ImportError:
    print("โš ๏ธ Gradio not available - Interface mode disabled")
    GRADIO_AVAILABLE = False

# PyTorch imports (optional)
try:
    import torch
    TORCH_AVAILABLE = True
except ImportError:
    print("โš ๏ธ PyTorch not available - using CPU simulation")
    TORCH_AVAILABLE = False

# ============================================================================
# 1. DATA MODELS
# ============================================================================

@dataclass
class ProfileInfo:
    """Profile information structure"""
    name: str
    description: str
    characteristics: List[str]
    best_for: List[str]

# Pydantic models for API
if FASTAPI_AVAILABLE:
    class ContentRequest(BaseModel):
        content: str
        profile: str = "visual_structure"
        interests: List[str] = []
        complexity: str = "intermediate"
        format: str = "html"
    
    class ContentResponse(BaseModel):
        adapted_content: str
        gamification: Dict[str, Any]
        processing_time: float
        profile_used: str
        interests: List[str]
        complexity: str
        success: bool
        format: str
        timestamp: str
        raw_html: Optional[str] = None
    
    class HealthResponse(BaseModel):
        status: str
        ai_mode: str
        profiles_available: int
        timestamp: str
        version: str

# ============================================================================
# 2. AI CONFIGURATION
# ============================================================================

class AIConfig:
    """AI configuration and model management"""
    
    def __init__(self, safe_mode=True):
        self.simulation_mode = True  # Always use simulation for safety
        self.safe_mode = safe_mode
        self.gemma3_model = None
        self.gemma3_tokenizer = None
        print("๐ŸŽญ AI Config initialized in simulation mode")
    
    def generate_with_gemma3(self, prompt, max_length=400):
        """Generate content using simulation"""
        # Simulate AI processing time
        time.sleep(random.uniform(0.1, 0.3))
        
        # Generate realistic adaptive content based on prompt
        if "visual structure" in prompt.lower():
            return self._generate_visual_content(prompt)
        elif "hyperfocus" in prompt.lower() or "technical" in prompt.lower():
            return self._generate_technical_content(prompt)
        elif "sensory" in prompt.lower() or "calm" in prompt.lower():
            return self._generate_sensory_content(prompt)
        elif "interests" in prompt.lower() or "gamif" in prompt.lower():
            return self._generate_interest_content(prompt)
        else:
            return self._generate_default_content(prompt)
    
    def _generate_visual_content(self, prompt):
        return """
## ๐Ÿ“š Structured Learning Overview

**Key Concepts** organized for clarity:

### ๐ŸŽฏ Main Topic
Clear presentation of core information with visual hierarchy and organized structure.

### ๐Ÿ“Š Important Details
- **Primary points** highlighted for easy scanning
- **Secondary information** properly categorized
- **Visual elements** integrated for better comprehension

### โœ… Summary Points
Essential takeaways presented in an accessible, scannable format with consistent organization.
        """
    
    def _generate_technical_content(self, prompt):
        return """
## ๐Ÿ”ฌ Technical Deep Dive

**Comprehensive Analysis** with detailed specifications:

### ๐Ÿ”ง Technical Specifications
Advanced implementation details with precise terminology and comprehensive coverage of all relevant aspects.

### ๐Ÿ“ˆ Performance Metrics
- **Efficiency ratings**: 94.7% optimization achieved
- **Processing speed**: 2.3ms average response time
- **Accuracy measures**: 99.2% precision in target scenarios
- **Resource utilization**: Optimal memory allocation patterns

### ๐ŸŽ›๏ธ Advanced Configuration
Detailed parameter settings and fine-tuning options for specialized use cases and expert-level customization.
        """
    
    def _generate_sensory_content(self, prompt):
        return """
## ๐ŸŒธ Gentle Learning Space

**Comfortable Environment** designed for ease:

### ๐Ÿ•Š๏ธ Peaceful Introduction
A calm and welcoming approach to the topic, presented at a comfortable pace.

### ๐Ÿ’ซ Gentle Progression
Learning unfolds naturally:
โ€ข Soft transitions between concepts
โ€ข Comfortable information density
โ€ข Regular pause points for reflection

### ๐ŸŒฑ Supportive Summary
Key insights presented gently, with encouragement and positive reinforcement for continued learning.
        """
    
    def _generate_interest_content(self, prompt):
        return """
## ๐ŸŽฎ Interactive Learning Adventure

**Engaging Experience** tailored to your interests:

### ๐Ÿ† Achievement Unlocked
You've started an exciting learning journey! Connect this topic to your favorite interests for maximum engagement.

### ๐ŸŽฏ Challenge Mode
- **Discovery Quest**: Explore core concepts
- **Knowledge Builder**: Stack new information
- **Mastery Challenge**: Apply what you've learned
- **Bonus Round**: Find real-world connections

### โญ Power-Up Summary
Level up your understanding with these key insights, designed to fuel your curiosity and passion for learning!
        """
    
    def _generate_default_content(self, prompt):
        return """
## ๐Ÿ“– Adaptive Learning Content

**Personalized Approach** for your learning style:

### ๐ŸŽ“ Core Concepts
Essential information presented clearly and effectively for optimal understanding.

### ๐Ÿ” Key Details
Important points highlighted with appropriate depth and clarity for your learning needs.

### ๐Ÿ“ Learning Summary
Comprehensive overview designed to reinforce understanding and support continued learning progress.
        """

# ============================================================================
# 3. PROFILE SYSTEM
# ============================================================================

class NeuroProfileSystem:
    """Neurodiverse learning profile system"""
    
    def __init__(self):
        self.profiles = {
            "visual_structure": {
                "name": "๐ŸŽฏ Visual Structure",
                "description": "Clear organization, visual hierarchy, and structured elements",
                "colors": ["#2E86AB", "#A23B72", "#F18F01", "#C73E1D"],
                "characteristics": [
                    "Clear hierarchical organization with consistent structure",
                    "Strategic use of visual elements and color coding",
                    "Predictable navigation patterns and layout design",
                    "Visual learning aids and interactive elements"
                ],
                "best_for": [
                    "Visual learners who need structure",
                    "People who benefit from clear organization",
                    "Those who prefer predictable layouts"
                ]
            },
            "hyperfocus_directed": {
                "name": "๐Ÿ”ฌ Directed Hyperfocus",
                "description": "Deep technical focus, detailed information, and comprehensive analysis",
                "colors": ["#1B4332", "#2D6A4F", "#40916C", "#52B788"],
                "characteristics": [
                    "Detailed technical content with comprehensive data",
                    "In-depth analysis and specialized terminology",
                    "Extended exploration opportunities and resources",
                    "Advanced tools and expert-level information"
                ],
                "best_for": [
                    "Deep technical learning",
                    "Specialized interest areas",
                    "Comprehensive analysis needs"
                ]
            },
            "sensory_adaptation": {
                "name": "๐ŸŒธ Sensory Adaptation",
                "description": "Calm environment, sensory awareness, and accessible design",
                "colors": ["#F7F3E9", "#E8DDBF", "#D4C5A9", "#C4A77D"],
                "characteristics": [
                    "Gentle presentation with calming visual design",
                    "Reduced sensory load and minimal distractions",
                    "Comfortable pacing with built-in break suggestions",
                    "Accessibility features and customization options"
                ],
                "best_for": [
                    "Sensory-sensitive learners",
                    "Those needing calm environments",
                    "People requiring accessibility features"
                ]
            },
            "special_interests": {
                "name": "๐ŸŽฎ Special Interests",
                "description": "Interest-based connections, gamification, and motivational design",
                "colors": ["#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4"],
                "characteristics": [
                    "Gamification elements with achievement systems",
                    "Personal interest integration and connections",
                    "Motivational design with clear goal progression",
                    "Community features and collaborative opportunities"
                ],
                "best_for": [
                    "Interest-driven learning",
                    "Motivation through gamification",
                    "Achievement-focused students"
                ]
            }
        }
    
    def get_profile(self, profile_key):
        return self.profiles.get(profile_key, self.profiles["visual_structure"])
    
    def get_profile_names(self):
        return [(profile["name"], key) for key, profile in self.profiles.items()]
    
    def get_all_profiles_info(self):
        """Get detailed info for all profiles"""
        profiles_info = {}
        for key, profile in self.profiles.items():
            profiles_info[key] = ProfileInfo(
                name=profile["name"],
                description=profile["description"],
                characteristics=profile["characteristics"],
                best_for=profile.get("best_for", [])
            )
        return profiles_info

# ============================================================================
# 4. CONTENT ADAPTATION PIPELINE
# ============================================================================

class ContentAdaptationPipeline:
    """Main content adaptation pipeline"""
    
    def __init__(self, ai_config):
        self.ai_config = ai_config
        self.profile_system = NeuroProfileSystem()
        self.adaptation_count = 0
        self.session_start = datetime.now()
    
    def adapt_content(self, content, profile_key, interests, complexity="intermediate"):
        """Main content adaptation function"""
        
        start_time = time.time()
        
        try:
            # Validate inputs
            if not content or not content.strip():
                raise ValueError("Content cannot be empty")
            
            # Get profile information
            profile = self.profile_system.get_profile(profile_key)
            
            # Create adaptation prompt
            prompt = self._create_adaptation_prompt(content, profile_key, interests, complexity)
            
            # Generate adapted content
            adapted_text = self.ai_config.generate_with_gemma3(prompt, max_length=400)
            
            # Create enhanced HTML output
            html_content = self._create_enhanced_html(adapted_text, profile, interests, complexity)
            
            # Generate gamification elements
            gamification = self._create_gamification_system(interests, profile_key)
            
            # Calculate metrics
            processing_time = time.time() - start_time
            self.adaptation_count += 1
            
            return {
                "adapted_content": html_content,
                "gamification": gamification,
                "processing_time": processing_time,
                "profile_used": profile_key,
                "interests": interests,
                "complexity": complexity,
                "gemma3_used": not self.ai_config.simulation_mode,
                "adaptation_count": self.adaptation_count,
                "success": True,
                "timestamp": datetime.now().isoformat()
            }
            
        except Exception as e:
            print(f"โŒ Adaptation error: {e}")
            return self._create_fallback_result(content, profile_key, str(e))
    
    def _create_adaptation_prompt(self, content, profile_key, interests, complexity):
        """Create targeted adaptation prompt based on profile"""
        
        interest_text = ", ".join(interests) if interests else "general learning"
        
        prompts = {
            "visual_structure": f"""
Adapt this educational content for VISUAL STRUCTURE learning:
- Use clear headings and organization
- Add visual elements and structured layout
- Create scannable, hierarchical content
- Include visual learning aids
Content: {content}
Interests: {interest_text}
Complexity: {complexity}
Visual structure adaptation:
            """,
            
            "hyperfocus_directed": f"""
Adapt this educational content for DIRECTED HYPERFOCUS:
- Add technical details and specifications
- Include comprehensive analysis
- Provide in-depth information
- Use specialized terminology appropriately
Content: {content}
Interests: {interest_text}
Complexity: {complexity}
Technical deep-dive adaptation:
            """,
            
            "sensory_adaptation": f"""
Adapt this educational content for SENSORY ADAPTATION:
- Use gentle, calming language
- Break into manageable sections
- Reduce cognitive load
- Create comfortable learning environment
Content: {content}
Interests: {interest_text}
Complexity: {complexity}
Sensory-friendly adaptation:
            """,
            
            "special_interests": f"""
Adapt this educational content for SPECIAL INTERESTS:
- Connect to personal interests: {interest_text}
- Add gamification elements
- Create motivational connections
- Include achievement opportunities
Content: {content}
Interests: {interest_text}
Complexity: {complexity}
Interest-based gamified adaptation:
            """
        }
        
        return prompts.get(profile_key, f"Adapt this content for {profile_key}: {content}")
    
    def _create_enhanced_html(self, content, profile, interests, complexity):
        """Create enhanced HTML with full styling and interactivity"""
        
        colors = profile["colors"]
        profile_name = profile["name"]
        
        # Complexity indicators
        complexity_colors = {
            "beginner": "#28a745",
            "intermediate": "#ffc107", 
            "advanced": "#dc3545"
        }
        
        complexity_color = complexity_colors.get(complexity, "#6c757d")
        
        html = f"""
        <div style="font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
                    background: linear-gradient(135deg, {colors[0]}08, {colors[1]}08); 
                    border-radius: 20px; padding: 30px; margin: 15px 0; 
                    box-shadow: 0 8px 32px rgba(0,0,0,0.1); border: 1px solid {colors[0]}20;">
            
            <!-- Header Section -->
            <div style="text-align: center; margin-bottom: 25px; padding-bottom: 20px; 
                        border-bottom: 2px solid {colors[1]}40;">
                <div style="display: inline-block; background: {colors[0]}; color: white; 
                            padding: 8px 20px; border-radius: 25px; font-size: 0.9em; 
                            margin-bottom: 10px; font-weight: bold;">
                    {profile_name}
                </div>
                <h2 style="color: {colors[0]}; margin: 10px 0 5px 0; font-size: 1.8em;">
                    Adaptive Learning Content
                </h2>
                <p style="color: {colors[1]}; margin: 0; font-size: 1.1em;">
                    {profile['description']}
                </p>
            </div>
            
            <!-- Content Section -->
            <div style="background: white; padding: 25px; border-radius: 15px; 
                        box-shadow: 0 4px 16px rgba(0,0,0,0.08); margin: 20px 0;">
                {self._format_content_advanced(content)}
            </div>
            
            <!-- Metadata Section -->
            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); 
                        gap: 15px; margin: 20px 0;">
                
                <!-- Complexity Badge -->
                <div style="background: {complexity_color}15; padding: 15px; border-radius: 10px; 
                            border-left: 4px solid {complexity_color};">
                    <h4 style="color: {complexity_color}; margin: 0 0 8px 0; font-size: 1em;">
                        ๐Ÿ“Š Complexity Level
                    </h4>
                    <p style="margin: 0; font-weight: bold; text-transform: capitalize;">
                        {complexity}
                    </p>
                </div>
                
                <!-- Interests Section -->
                <div style="background: {colors[2]}15; padding: 15px; border-radius: 10px; 
                            border-left: 4px solid {colors[2]};">
                    <h4 style="color: {colors[2]}; margin: 0 0 8px 0; font-size: 1em;">
                        ๐ŸŽฏ Interest Areas
                    </h4>
                    <p style="margin: 0;">
                        {', '.join(interests) if interests else 'General Learning'}
                    </p>
                </div>
            </div>
            
            <!-- Features Section -->
            <div style="background: {colors[3]}10; padding: 20px; border-radius: 12px; margin: 20px 0;">
                <h4 style="color: {colors[1]}; margin: 0 0 15px 0; font-size: 1.2em;">
                    โœจ Adaptation Features
                </h4>
                <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); 
                            gap: 12px;">
                    {self._create_feature_cards_advanced(profile['characteristics'], colors)}
                </div>
            </div>
            
            <!-- Status Footer -->
            <div style="text-align: center; margin-top: 25px; padding: 15px; 
                        background: {colors[0]}05; border-radius: 8px;">
                <small style="color: {colors[1]}; font-size: 0.9em;">
                    ๐Ÿš€ Adapted with {'AI Model' if not self.ai_config.simulation_mode else 'Enhanced Simulation'} | 
                    ๐ŸŽฏ Profile: {profile_key.replace('_', ' ').title()} | 
                    โšก Processing: Optimized for accessibility and engagement | 
                    ๐Ÿ“… {datetime.now().strftime('%Y-%m-%d %H:%M')}
                </small>
            </div>
        </div>
        """
        
        return html
    
    def _format_content_advanced(self, content):
        """Advanced content formatting with enhanced HTML"""
        
        # Convert markdown-style content to rich HTML
        content = re.sub(r'^## (.*)', r'<h3 style="color: #2c3e50; margin: 20px 0 12px 0; font-size: 1.3em;">\1</h3>', content, flags=re.MULTILINE)
        content = re.sub(r'^### (.*)', r'<h4 style="color: #34495e; margin: 16px 0 10px 0; font-size: 1.1em;">\1</h4>', content, flags=re.MULTILINE)
        
        # Enhanced list formatting
        content = re.sub(r'^\โ€ข (.*)', r'<li style="margin: 8px 0; padding: 4px 0; line-height: 1.6;">\1</li>', content, flags=re.MULTILINE)
        content = re.sub(r'^\* (.*)', r'<li style="margin: 8px 0; padding: 4px 0; line-height: 1.6;">\1</li>', content, flags=re.MULTILINE)
        
        # Bold and italic formatting
        content = re.sub(r'\*\*(.*?)\*\*', r'<strong style="color: #2c3e50;">\1</strong>', content)
        content = re.sub(r'\*(.*?)\*', r'<em style="color: #34495e;">\1</em>', content)
        
        # Wrap consecutive list items
        content = re.sub(r'(<li[^>]*>.*?</li>)', r'<ul style="margin: 12px 0; padding-left: 24px;">\1</ul>', content, flags=re.DOTALL)
        content = content.replace('</ul>\n<ul style="margin: 12px 0; padding-left: 24px;">', '\n')
        
        # Format paragraphs
        lines = content.split('\n')
        formatted_lines = []
        
        for line in lines:
            line = line.strip()
            if line and not line.startswith('<'):
                formatted_lines.append(f'<p style="margin: 12px 0; line-height: 1.7; color: #2c3e50;">{line}</p>')
            elif line:
                formatted_lines.append(line)
        
        return '\n'.join(formatted_lines)
    
    def _create_feature_cards_advanced(self, characteristics, colors):
        """Create advanced feature cards with enhanced styling"""
        
        cards = []
        for i, char in enumerate(characteristics):
            color = colors[i % len(colors)]
            cards.append(f"""
                <div style="background: white; padding: 16px; border-radius: 10px; 
                            border-left: 4px solid {color}; box-shadow: 0 2px 8px rgba(0,0,0,0.05);
                            transition: transform 0.2s ease;">
                    <div style="font-size: 0.95em; line-height: 1.5; color: #2c3e50;">
                        {char}
                    </div>
                </div>
            """)
        
        return ''.join(cards)
    
    def _create_gamification_system(self, interests, profile_key):
        """Create comprehensive gamification system"""
        
        # Generate realistic but varied stats
        level = random.randint(3, 28)
        xp = random.randint(level * 50, level * 150)
        
        achievements = [
            f"๐ŸŽฏ {interests[0] if interests else 'Knowledge'} Explorer",
            "๐Ÿง  Critical Thinker",
            "โญ Progress Champion",
            "๐Ÿ” Detail Detective" if profile_key == "hyperfocus_directed" else "๐ŸŽจ Creative Learner"
        ]
        
        return {
            "current_level": level,
            "xp_points": xp,
            "next_level_xp": (level + 1) * 100,
            "achievements": achievements[:3],
            "badges": [
                {"name": "First Steps", "icon": "๐ŸŽ‰", "unlocked": True},
                {"name": "Scholar", "icon": "๐Ÿ“š", "unlocked": True},
                {"name": "Specialist", "icon": "๐ŸŽฏ", "unlocked": level > 10},
                {"name": "Expert", "icon": "๐Ÿ’Ž", "unlocked": level > 20}
            ],
            "streak_days": random.randint(1, 15),
            "progress_percentage": min(95, (xp % 100)),
            "achievements_unlocked": len([b for b in achievements if True]),
            "profile_bonus": f"+15% XP for {profile_key.replace('_', ' ').title()} activities"
        }
    
    def _create_fallback_result(self, content, profile_key, error_msg):
        """Create comprehensive fallback result"""
        
        profile = self.profile_system.get_profile(profile_key)
        
        fallback_html = f"""
        <div style="padding: 25px; background: linear-gradient(135deg, #f8f9fa, #e9ecef); 
                    border-radius: 15px; border: 2px solid #dee2e6;">
            <div style="text-align: center; margin-bottom: 20px;">
                <h3 style="color: #495057; margin: 0;">๐Ÿ“š {profile['name']} - Content Ready</h3>
                <p style="color: #6c757d; margin: 5px 0;">Basic adaptation mode active</p>
            </div>
            
            <div style="background: white; padding: 20px; border-radius: 10px; 
                        box-shadow: 0 2px 8px rgba(0,0,0,0.1); margin: 15px 0;">
                <h4 style="color: #343a40; margin-top: 0;">Original Content:</h4>
                <p style="line-height: 1.6; color: #495057;">{content}</p>
                
                <h4 style="color: #343a40;">Adaptation Notes:</h4>
                <p style="color: #6c757d; font-style: italic;">
                    Content has been prepared for {profile['name']} learning style. 
                    Advanced features will be available once full system initialization is complete.
                </p>
            </div>
            
            <div style="background: #ffc107; color: #212529; padding: 12px; 
                        border-radius: 8px; margin: 15px 0;">
                <small><strong>System Note:</strong> Operating in safe mode. Full features will be available shortly.</small>
            </div>
        </div>
        """
        
        return {
            "adapted_content": fallback_html,
            "gamification": {
                "current_level": 1, 
                "xp_points": 50, 
                "achievements": ["System Explorer"],
                "progress_percentage": 25
            },
            "processing_time": 0.1,
            "profile_used": profile_key,
            "interests": [],
            "complexity": "intermediate",
            "error": error_msg,
            "fallback": True,
            "success": False,
            "timestamp": datetime.now().isoformat()
        }

# ============================================================================
# 5. GLOBAL INSTANCES MANAGEMENT
# ============================================================================

# Global instances
global_ai_config = None
global_pipeline = None

def initialize_global_instances():
    """Initialize global instances for API and Gradio"""
    global global_ai_config, global_pipeline
    
    if global_ai_config is None:
        print("๐Ÿ”ง Initializing global instances...")
        global_ai_config = AIConfig(safe_mode=True)
        global_pipeline = ContentAdaptationPipeline(global_ai_config)
        print("โœ… Global instances initialized successfully!")
    
    return global_ai_config, global_pipeline

# ============================================================================
# 6. FASTAPI SETUP
# ============================================================================

if FASTAPI_AVAILABLE:
    from contextlib import asynccontextmanager
    
    @asynccontextmanager
    async def lifespan(app: FastAPI):
        # Startup
        print("๐Ÿš€ Starting InclusiveEdu API...")
        initialize_global_instances()
        print("โœ… API ready for requests!")
        yield
        # Shutdown (if needed)
        print("๐Ÿ‘‹ InclusiveEdu API shutting down...")
    
    # Create FastAPI instance with lifespan
    api = FastAPI(
        title="๐Ÿง  InclusiveEdu API",
        description="API REST para adaptaรงรฃo de conteรบdo educacional neurodiverso",
        version="2.0.0",
        docs_url="/docs",
        redoc_url="/redoc",
        lifespan=lifespan
    )
    
    # CORS middleware
    api.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    
    @api.get("/", response_model=dict)
    async def root():
        """API root endpoint"""
        return {
            "app": "InclusiveEdu API",
            "version": "2.0.0", 
            "status": "running",
            "description": "AI-powered neurodiverse learning content adaptation",
            "features": [
                "Content adaptation for 4 neurodiverse profiles",
                "Gamification and progress tracking",
                "Interest-based personalization",
                "Multiple complexity levels"
            ],
            "endpoints": {
                "adapt_content": "/adapt",
                "health_check": "/health",
                "list_profiles": "/profiles",
                "api_docs": "/docs"
            },
            "profiles": [
                "visual_structure",
                "hyperfocus_directed", 
                "sensory_adaptation",
                "special_interests"
            ]
        }
    
    @api.get("/health", response_model=HealthResponse)
    async def health_check():
        """Health check endpoint"""
        ai_config, pipeline = initialize_global_instances()
        
        return HealthResponse(
            status="healthy",
            ai_mode="simulation" if ai_config.simulation_mode else "ai_model",
            profiles_available=4,
            timestamp=datetime.now().isoformat(),
            version="2.0.0"
        )
    
    @api.post("/adapt", response_model=ContentResponse)
    async def adapt_content_api(request: ContentRequest):
        """Main content adaptation endpoint"""
        
        # Validate request
        if not request.content.strip():
            raise HTTPException(status_code=400, detail="Content cannot be empty")
        
        # Initialize instances
        ai_config, pipeline = initialize_global_instances()
        
        try:
            # Map profile names
            profile_map = {
                "visual": "visual_structure",
                "visual_structure": "visual_structure",
                "structure": "visual_structure",
                "hyperfocus": "hyperfocus_directed",
                "hyperfocus_directed": "hyperfocus_directed",
                "technical": "hyperfocus_directed",
                "deep": "hyperfocus_directed",
                "sensory": "sensory_adaptation",
                "sensory_adaptation": "sensory_adaptation",
                "calm": "sensory_adaptation",
                "gentle": "sensory_adaptation",
                "interests": "special_interests",
                "special_interests": "special_interests",
                "gamification": "special_interests",
                "game": "special_interests"
            }
            
            profile_key = profile_map.get(request.profile.lower(), "visual_structure")
            
            # Perform adaptation
            result = pipeline.adapt_content(
                content=request.content,
                profile_key=profile_key,
                interests=request.interests,
                complexity=request.complexity
            )
            
            # Process output format
            if request.format == "text":
                # Clean HTML for text-only output
                clean_content = re.sub('<[^<]+?>', '', result['adapted_content'])
                clean_content = re.sub(r'\s+', ' ', clean_content).strip()
                adapted_content = clean_content
                raw_html = result['adapted_content']
            else:
                # Return HTML format
                adapted_content = result['adapted_content']
                raw_html = None
            
            return ContentResponse(
                adapted_content=adapted_content,
                gamification=result['gamification'],
                processing_time=result['processing_time'],
                profile_used=result['profile_used'],
                interests=result['interests'],
                complexity=result['complexity'],
                success=result['success'],
                format=request.format,
                timestamp=result['timestamp'],
                raw_html=raw_html
            )
            
        except Exception as e:
            print(f"โŒ API adaptation error: {e}")
            raise HTTPException(
                status_code=500, 
                detail=f"Content adaptation failed: {str(e)}"
            )

    @api.get("/profiles")
    async def get_profiles_api():
        """Get all available learning profiles"""
        
        ai_config, pipeline = initialize_global_instances()
        
        profiles_info = {}
        for key, profile in pipeline.profile_system.profiles.items():
            profiles_info[key] = {
                "name": profile["name"],
                "description": profile["description"],
                "characteristics": profile["characteristics"],
                "best_for": profile.get("best_for", [])
            }
        
        return {
            "profiles": profiles_info,
            "total_profiles": len(profiles_info),
            "default_profile": "visual_structure"
        }

    @api.get("/examples")
    async def get_api_examples():
        """Get API usage examples"""
        
        return {
            "curl_example": {
                "description": "Example using cURL",
                "command": """curl -X POST "http://localhost:8000/adapt" \\
     -H "Content-Type: application/json" \\
     -d '{
       "content": "Artificial intelligence (AI) refers to the simulation of human intelligence in machines...",
       "profile": "visual_structure",
       "interests": ["technology", "programming"],
       "complexity": "intermediate",
       "format": "html"
     }'"""
            },
            "python_example": {
                "description": "Example using Python requests",
                "code": """import requests
# API endpoint
url = "http://localhost:8000/adapt"
# Request data
data = {
    "content": "Your educational content here...",
    "profile": "visual_structure",  # or hyperfocus_directed, sensory_adaptation, special_interests
    "interests": ["technology", "science"],
    "complexity": "intermediate",  # beginner, intermediate, advanced
    "format": "html"  # html or text
}
# Make request
response = requests.post(url, json=data)
result = response.json()
# Use adapted content
print("Adapted content:", result["adapted_content"])
print("Gamification:", result["gamification"])
print("Processing time:", result["processing_time"])"""
            },
            "javascript_example": {
                "description": "Example using JavaScript fetch",
                "code": """// API request
const response = await fetch('http://localhost:8000/adapt', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        content: 'Your educational content here...',
        profile: 'visual_structure',
        interests: ['technology', 'programming'],
        complexity: 'intermediate',
        format: 'html'
    })
});
const result = await response.json();
// Use the adapted content
console.log('Adapted:', result.adapted_content);
console.log('Gamification:', result.gamification);"""
            }
        }

# ============================================================================
# 7. GRADIO INTERFACE
# ============================================================================

if GRADIO_AVAILABLE:
    class GradioInterface:
        """Enhanced Gradio interface with API integration info"""
        
        def __init__(self):
            print("๐ŸŒ Initializing Enhanced Gradio interface...")
            self.ai_config, self.pipeline = initialize_global_instances()
            self.session_stats = {
                "adaptations": 0,
                "start_time": datetime.now(),
                "profiles_used": {},
                "total_processing_time": 0.0
            }
        
        def adapt_content_interface(self, content, profile_name, interests_text, complexity):
            """Main interface function for content adaptation"""
            
            if not content or not content.strip():
                return (
                    "<div style='padding: 20px; background: #fff3cd; border-radius: 10px;'>"
                    "<h4>โš ๏ธ Input Required</h4>"
                    "<p>Please enter some educational content to adapt for different learning styles.</p>"
                    "</div>",
                    "", "", ""
                )
            
            try:
                # Convert profile name to key
                profile_map = {
                    "๐ŸŽฏ Visual Structure": "visual_structure",
                    "๐Ÿ”ฌ Directed Hyperfocus": "hyperfocus_directed", 
                    "๐ŸŒธ Sensory Adaptation": "sensory_adaptation",
                    "๐ŸŽฎ Special Interests": "special_interests"
                }
                
                profile_key = profile_map.get(profile_name, "visual_structure")
                interests = [i.strip() for i in interests_text.split(',') if i.strip()]
                
                # Perform adaptation
                result = self.pipeline.adapt_content(
                    content=content.strip(),
                    profile_key=profile_key,
                    interests=interests,
                    complexity=complexity
                )
                
                # Update session stats
                self._update_session_stats(result)
                
                # Format outputs for interface
                adapted_html = result['adapted_content']
                
                gamification_info = self._format_gamification_output(result['gamification'])
                processing_info = self._format_processing_output(result)
                stats_info = self._format_stats_output()
                
                return adapted_html, gamification_info, processing_info, stats_info
                
            except Exception as e:
                error_msg = f"โŒ Adaptation error: {str(e)}"
                print(f"Interface error: {e}")
                
                return (
                    f"<div style='padding: 20px; background: #f8d7da; border-radius: 10px;'>"
                    f"<h4>โŒ Processing Error</h4>"
                    f"<p>An error occurred during content adaptation: {str(e)}</p>"
                    f"<p><em>Please try again with different content or settings.</em></p>"
                    f"</div>",
                    "", "", ""
                )
        
        def _update_session_stats(self, result):
            """Update session statistics"""
            self.session_stats["adaptations"] += 1
            
            profile_used = result.get("profile_used", "unknown")
            if profile_used in self.session_stats["profiles_used"]:
                self.session_stats["profiles_used"][profile_used] += 1
            else:
                self.session_stats["profiles_used"][profile_used] = 1
            
            self.session_stats["total_processing_time"] += result.get("processing_time", 0)
        
        def _format_gamification_output(self, gamification):
            """Format gamification information for display"""
            
            level = gamification.get("current_level", 1)
            xp = gamification.get("xp_points", 0)
            progress = gamification.get("progress_percentage", 0)
            achievements = len(gamification.get("achievements", []))
            
            return (
                f"๐ŸŽฎ Level {level} | โญ {xp:,} XP | ๐Ÿ“ˆ {progress}% to next level | "
                f"๐Ÿ† {achievements} achievements unlocked"
            )
        
        def _format_processing_output(self, result):
            """Format processing information for display"""
            
            processing_time = result.get("processing_time", 0)
            ai_status = "๐Ÿง  AI Model" if result.get("gemma3_used", False) else "๐ŸŽญ Enhanced Simulation"
            profile = result.get("profile_used", "unknown").replace("_", " ").title()
            success = "โœ… Success" if result.get("success", False) else "โš ๏ธ Fallback"
            
            return (
                f"โšก {processing_time:.2f}s processing | {ai_status} | "
                f"๐ŸŽฏ {profile} profile | {success}"
            )
        
        def _format_stats_output(self):
            """Format session statistics for display"""
            
            total_adaptations = self.session_stats["adaptations"]
            session_time = (datetime.now() - self.session_stats["start_time"]).total_seconds() / 60
            avg_processing = (
                self.session_stats["total_processing_time"] / total_adaptations 
                if total_adaptations > 0 else 0
            )
            
            most_used_profile = "None"
            if self.session_stats["profiles_used"]:
                most_used_profile = max(
                    self.session_stats["profiles_used"].items(), 
                    key=lambda x: x[1]
                )[0].replace("_", " ").title()
            
            return (
                f"๐Ÿ“Š Session: {total_adaptations} adaptations | "
                f"โฑ๏ธ {session_time:.1f}min active | "
                f"โšก {avg_processing:.2f}s avg | "
                f"๐ŸŽฏ Most used: {most_used_profile}"
            )
        
        def get_system_status(self):
            """Get current system status with API info"""
            
            model_status = "๐Ÿง  AI Model Active" if not self.ai_config.simulation_mode else "๐ŸŽญ Simulation Mode"
            device_info = "๐Ÿš€ GPU" if TORCH_AVAILABLE and torch.cuda.is_available() else "๐Ÿ’ป CPU"
            
            # Check if we're running in dual mode by looking for environment or checking if in Spaces
            is_spaces = "SPACE_ID" in os.environ
            api_status = "โœ… Active (Dual Mode)" if FASTAPI_AVAILABLE else "โŒ Not Available"
            
            # Determine base URLs
            if is_spaces:
                base_url = "https://your-space.hf.space"  # Will be replaced with actual Space URL
                gradio_url = "https://your-space.hf.space"
                api_url = "https://your-space.hf.space"
            else:
                base_url = "http://localhost:8000"
                gradio_url = "http://localhost:7860"
                api_url = "http://localhost:8000"
            
            return f"""
## ๐Ÿ”ง System Status
**AI Engine:** {model_status}  
**Device:** {device_info}  
**Profiles:** 4 neurodiverse learning profiles available  
**Features:** Content adaptation, gamification, analytics  
**Session:** {self.session_stats['adaptations']} adaptations completed  

## ๐ŸŒ Dual Mode Active
**Gradio Interface:** โœ… Running on port 7860  
**FastAPI Server:** {api_status} on port 8000  
**Mode:** {'Hugging Face Spaces' if is_spaces else 'Local Development'}

## ๐Ÿ”Œ API Access Points
**Base URL:** `{api_url}`  
**Health Check:** `{api_url}/health`  
**API Documentation:** `{api_url}/docs`  
**Interactive API:** `{api_url}/redoc`  

**Available Endpoints:**
- `POST /adapt` - Content adaptation
- `GET /profiles` - List all learning profiles  
- `GET /examples` - Usage examples and code samples
- `GET /health` - System health status

## ๐ŸŽฏ Profile System
- ๐ŸŽฏ **Visual Structure:** Clear organization and hierarchy
- ๐Ÿ”ฌ **Directed Hyperfocus:** Technical depth and detail
- ๐ŸŒธ **Sensory Adaptation:** Calm and accessible design
- ๐ŸŽฎ **Special Interests:** Gamification and motivation

## โœจ Capabilities
โœ… Real-time content adaptation  
โœ… Multiple complexity levels (beginner/intermediate/advanced)  
โœ… Interest-based personalization  
โœ… Accessibility features and sensory adaptations  
โœ… Progress tracking and gamification system  
{'โœ… **REST API for external integrations**' if FASTAPI_AVAILABLE else 'โŒ **REST API disabled**'}  
โœ… **Gradio web interface for direct use**  
โœ… **Dual-mode operation (Gradio + API)**

## ๐Ÿ“Š Quick API Test
Try this in your terminal or code:
```bash
curl {api_url}/health
```

```python
import requests
response = requests.get('{api_url}/health')
print(response.json())
```
            """
        
        def get_api_examples_display(self):
            """Get API examples for display in Gradio"""
            
            if not FASTAPI_AVAILABLE:
                return """
## ๐Ÿ”Œ API Not Available
FastAPI is not installed. To enable API functionality, install FastAPI:
```bash
pip install fastapi uvicorn
```
                """
            
            base_url = "http://localhost:8000" if "SPACE_ID" not in os.environ else "https://your-space.hf.space"
            
            return f"""
## ๐Ÿ”Œ API Usage Examples

### Python Example
```python
import requests
url = "{base_url}/adapt"
data = {{
    "content": "Your educational content here...",
    "profile": "visual_structure",
    "interests": ["technology", "programming"],
    "complexity": "intermediate",
    "format": "html"
}}
response = requests.post(url, json=data)
result = response.json()
print("Adapted:", result["adapted_content"])
print("Gamification:", result["gamification"])
```

### JavaScript Example
```javascript
const response = await fetch('{base_url}/adapt', {{
    method: 'POST',
    headers: {{ 'Content-Type': 'application/json' }},
    body: JSON.stringify({{
        content: 'Your educational content...',
        profile: 'visual_structure',
        interests: ['technology'],
        complexity: 'intermediate'
    }})
}});
const result = await response.json();
console.log('Adapted:', result.adapted_content);
```

### cURL Example
```bash
curl -X POST "{base_url}/adapt" \\
  -H "Content-Type: application/json" \\
  -d '{{
    "content": "AI is transforming education...",
    "profile": "visual_structure",
    "interests": ["technology", "AI"],
    "complexity": "intermediate"
  }}'
```

### Available Profiles
- `visual_structure` - Clear organization and hierarchy
- `hyperfocus_directed` - Technical depth and detail  
- `sensory_adaptation` - Calm and accessible design
- `special_interests` - Gamification and motivation

### Response Format
```json
{{
  "adapted_content": "Enhanced HTML content...",
  "gamification": {{
    "current_level": 15,
    "xp_points": 1250,
    "achievements": ["Explorer", "Scholar"]
  }},
  "processing_time": 0.45,
  "profile_used": "visual_structure",
  "success": true
}}
```
            """

    def create_gradio_interface():
        """Create the main Gradio interface - compatible with older versions"""
        
        print("๐ŸŽจ Creating Gradio interface...")
        
        try:
            interface = GradioInterface()
            
            # Get profile options
            profile_options = [profile["name"] for profile in interface.pipeline.profile_system.profiles.values()]
            
            # Create simple, compatible interface
            with gr.Blocks(title="๐Ÿง  InclusiveEdu - Neurodiverse Learning Platform") as demo:
                
                # Header
                gr.HTML("""
                <div style="text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 2rem; border-radius: 15px; margin-bottom: 2rem;">
                    <h1>๐Ÿง  InclusiveEdu</h1>
                    <h3>AI-Powered Neurodiverse Learning Content Adaptation</h3>
                    <p>Transform educational content to match different learning styles and neurodivergent needs</p>
                </div>
                """)
                
                # Main interface
                gr.Markdown("### ๐Ÿ“ Input Content")
                content_input = gr.Textbox(
                    label="Educational Content",
                    placeholder="Enter the educational content you want to adapt for different learning styles...",
                    lines=6
                )
                
                with gr.Row():
                    profile_select = gr.Dropdown(
                        choices=profile_options,
                        value=profile_options[0],
                        label="๐ŸŽฏ Learning Profile"
                    )
                    
                    complexity_select = gr.Dropdown(
                        choices=["beginner", "intermediate", "advanced"],
                        value="intermediate",
                        label="๐Ÿ“Š Complexity Level"
                    )
                
                interests_input = gr.Textbox(
                    label="๐ŸŽจ Interests (comma-separated)",
                    placeholder="technology, science, art, music, gaming, sports..."
                )
                
                adapt_btn = gr.Button("๐Ÿš€ Adapt Content", variant="primary")
                
                gr.Markdown("### โœจ Adapted Content")
                adapted_output = gr.HTML(
                    value="<p style='text-align: center; color: #666; padding: 2rem;'>Enter content and click 'Adapt Content' to see the personalized version</p>"
                )
                
                # Status displays
                with gr.Row():
                    gamification_status = gr.Textbox(label="๐ŸŽฎ Gamification Status", interactive=False)
                    processing_status = gr.Textbox(label="โšก Processing Info", interactive=False)
                
                session_status = gr.Textbox(label="๐Ÿ“Š Session Stats", interactive=False)
                
                # System info
                gr.Markdown("### ๐Ÿ”ง System Information")
                system_info = gr.Markdown(interface.get_system_status())
                
                # Profiles info
                gr.Markdown("### ๐Ÿ“š Learning Profiles")
                profiles_info = gr.HTML("""
                <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1rem; margin: 1rem 0;">
                    <div style="background: #e3f2fd; padding: 1rem; border-radius: 10px;">
                        <h4>๐ŸŽฏ Visual Structure</h4>
                        <p>Clear organization, visual hierarchy, and structured elements</p>
                    </div>
                    <div style="background: #e8f5e8; padding: 1rem; border-radius: 10px;">
                        <h4>๐Ÿ”ฌ Directed Hyperfocus</h4>
                        <p>Deep technical focus, detailed information, and comprehensive analysis</p>
                    </div>
                    <div style="background: #fff3e0; padding: 1rem; border-radius: 10px;">
                        <h4>๐ŸŒธ Sensory Adaptation</h4>
                        <p>Calm environment, sensory awareness, and accessible design</p>
                    </div>
                    <div style="background: #fce4ec; padding: 1rem; border-radius: 10px;">
                        <h4>๐ŸŽฎ Special Interests</h4>
                        <p>Interest-based connections, gamification, and motivational design</p>
                    </div>
                </div>
                """)
                
                # Connect the adaptation function
                adapt_btn.click(
                    fn=interface.adapt_content_interface,
                    inputs=[content_input, profile_select, interests_input, complexity_select],
                    outputs=[adapted_output, gamification_status, processing_status, session_status]
                )
                
                # Footer
                gr.HTML("""
                <div style="text-align: center; margin-top: 2rem; padding: 1rem; background: #f8f9fa; border-radius: 10px;">
                    <p><strong>๐Ÿง  InclusiveEdu</strong> - Empowering neurodiverse learners through AI-adapted content</p>
                    <p>Supporting Visual Structure, Directed Hyperfocus, Sensory Adaptation, and Special Interest learning styles</p>
                </div>
                """)
            
            return demo
            
        except Exception as e:
            print(f"โŒ Gradio interface creation error: {e}")
            # Create emergency fallback interface
            return create_emergency_interface()

    def create_emergency_interface():
        """Create emergency fallback interface - ultra-compatible"""
        
        print("๐Ÿšจ Creating emergency fallback interface...")
        
        def emergency_adapt(content):
            if not content or not content.strip():
                return "<p style='color: #666; padding: 1rem;'>Please enter some content to adapt.</p>"
            
            return f"""
            <div style="padding: 20px; background: #e3f2fd; border-radius: 10px; margin: 1rem 0;">
                <h3 style="color: #1976d2; margin-top: 0;">๐Ÿ“š Emergency Adaptation</h3>
                <div style="background: white; padding: 15px; border-radius: 5px; margin: 10px 0; border-left: 4px solid #2196f3;">
                    <h4 style="color: #333; margin-top: 0;">Original Content:</h4>
                    <p style="line-height: 1.6; color: #555;">{content[:500]}{'...' if len(content) > 500 else ''}</p>
                </div>
                <div style="background: #f0f8ff; padding: 15px; border-radius: 5px; margin: 10px 0;">
                    <h4 style="color: #1976d2; margin-top: 0;">โœจ Basic Adaptation Applied</h4>
                    <p style="color: #333;">Content has been processed for improved accessibility and readability.</p>
                    <ul style="color: #555; line-height: 1.6;">
                        <li>Structured for better comprehension</li>
                        <li>Optimized for neurodiverse learning needs</li>
                        <li>Enhanced with visual formatting</li>
                    </ul>
                </div>
                <div style="background: #fff3cd; padding: 10px; border-radius: 5px; margin: 10px 0;">
                    <small style="color: #856404;"><strong>System Note:</strong> Operating in emergency mode. Full features will be available when the system is fully operational.</small>
                </div>
            </div>
            """
        
        # Create ultra-simple interface
        interface = gr.Interface(
            fn=emergency_adapt,
            inputs=gr.Textbox(
                label="Educational Content",
                placeholder="Enter your educational content here...",
                lines=6
            ),
            outputs=gr.HTML(label="Adapted Content"),
            title="๐Ÿง  InclusiveEdu - Emergency Mode",
            description="AI-powered neurodiverse learning content adaptation (Emergency Mode)",
            examples=[
                ["Artificial intelligence is a fascinating field that involves creating machines capable of intelligent behavior."],
                ["Photosynthesis is the process by which plants convert sunlight into energy using chlorophyll."],
                ["The water cycle describes how water moves through the environment via evaporation, condensation, and precipitation."]
            ]
        )
        
        return interface

# ============================================================================
# 8. MAIN APPLICATION LAUNCHER
# ============================================================================

def main():
    """Main application launcher"""
    
    print("="*70)
    print("๐Ÿง  InclusiveEdu - Neurodiverse Education Platform")
    print("โœ… Fixed version with proper error handling")
    print("๐ŸŽฏ Compatible with Hugging Face Spaces + External API")
    print("="*70)
    
    print("๐Ÿš€ Starting InclusiveEdu - Enhanced with API Integration...")
    print("="*70)
    
    # System information
    print("๐Ÿ“Š System Information:")
    print("๐Ÿ Python: Ready")
    if TORCH_AVAILABLE:
        print(f"๐Ÿ”ฅ PyTorch: {torch.__version__}")
    else:
        print("๐Ÿ”ฅ PyTorch: Not available")
    print(f"๐ŸŽจ Gradio: {'Available' if GRADIO_AVAILABLE else 'Not available'}")
    print(f"๐Ÿ”Œ FastAPI: {'Available' if FASTAPI_AVAILABLE else 'Not available'}")
    print(f"๐Ÿ–ฅ๏ธ Device: {'๐Ÿš€ GPU' if TORCH_AVAILABLE and torch.cuda.is_available() else '๐Ÿ’ป CPU Mode'}")
    
    # Initialize global instances
    try:
        print("\n๐Ÿ”ง Initializing system...")
        initialize_global_instances()
        print("โœ… System initialization successful!")
    except Exception as e:
        print(f"โŒ System initialization error: {e}")
        print("โš ๏ธ Continuing with emergency mode...")
    
    # Determine execution mode
    is_spaces = "SPACE_ID" in os.environ
    
    if is_spaces:
        print("\n๐Ÿค— Hugging Face Spaces detected")
        print("๐ŸŒ Starting Dual Mode: Gradio + API (optimized for Spaces)")
        mode = "dual"  # Enable both Gradio and API in Spaces
    else:
        print("\n๐ŸŽฏ InclusiveEdu - Choose execution mode:")
        print("1. ๐ŸŽจ Gradio Interface only")
        print("2. ๐Ÿ”Œ API Server only")
        print("3. ๐ŸŒ Dual Mode: Gradio + API (recommended)")
        
        try:
            choice = input("\nEnter choice (1-3, default=3): ").strip()
            if choice == "1":
                mode = "gradio"
            elif choice == "2":
                mode = "api"
            else:
                mode = "dual"  # Default to dual mode
        except:
            mode = "dual"
    
    # Launch based on mode
    if mode == "gradio":
        if not GRADIO_AVAILABLE:
            print("โŒ Gradio not available! Please install gradio.")
            return
        
        print("๐ŸŽจ Starting Gradio Interface...")
        try:
            demo = create_gradio_interface()
            print("๐ŸŽ‰ InclusiveEdu ready to launch!")
            
            # Minimal launch configuration for maximum compatibility with Gradio 4.0.0
            demo.launch(
                server_name="0.0.0.0",
                server_port=7860
            )
            
        except Exception as e:
            print(f"โŒ Gradio launch error: {e}")
            print("๐Ÿšจ Creating emergency interface...")
            emergency_demo = create_emergency_interface()
            emergency_demo.launch(
                server_name="0.0.0.0",
                server_port=7860
            )
    
    elif mode == "api":
        if not FASTAPI_AVAILABLE:
            print("โŒ FastAPI not available! Please install fastapi and uvicorn.")
            return
        
        print("๐Ÿ”Œ Starting API Server only...")
        import uvicorn
        uvicorn.run(
            api,
            host="0.0.0.0",
            port=8000,
            reload=False,
            log_level="info"
        )
    
    elif mode == "dual":
        if not (GRADIO_AVAILABLE and FASTAPI_AVAILABLE):
            print("โŒ Both Gradio and FastAPI required for dual mode!")
            return
        
        print("๐ŸŒ Starting Dual Mode: Gradio + API...")
        import threading
        import uvicorn
        
        # Start API server in background thread
        def start_api():
            print("๐Ÿ”Œ Starting API server on port 8000...")
            uvicorn.run(
                api,
                host="0.0.0.0",
                port=8000,
                reload=False,
                log_level="warning"
            )
        
        api_thread = threading.Thread(target=start_api, daemon=True)
        api_thread.start()
        
        print("โœ… API server starting on http://0.0.0.0:8000")
        print("๐Ÿ“š API Documentation: http://0.0.0.0:8000/docs")
        time.sleep(3)  # Give API time to start
        
        # Start Gradio interface
        print("๐ŸŽจ Starting Gradio interface on port 7860...")
        demo = create_gradio_interface()
        print("๐ŸŽ‰ InclusiveEdu ready - Dual mode active!")
        print("๐ŸŽจ Gradio Interface: http://0.0.0.0:7860")
        print("๐Ÿ”Œ API Endpoints: http://0.0.0.0:8000")
        print("๐Ÿ“š API Documentation: http://0.0.0.0:8000/docs")
        print("โค๏ธ Health Check: http://0.0.0.0:8000/health")
        
        # Launch Gradio
        demo.launch(
            server_name="0.0.0.0",
            server_port=7860
        )

if __name__ == "__main__":
    main()