File size: 45,560 Bytes
9afb5c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# --------------------------------------------------------------
# IGCSE Science Platform โ€“ Chemistry & Biology with Deep Understanding Focus
# Models: Gemini 2.5 (Primary) โ†’ Cohere โ†’ Z.ai โ†’ MiniMax (Fallbacks)
# --------------------------------------------------------------

import os
import json
from datetime import datetime
import gradio as gr
import PyPDF2
import time
import re
from PIL import Image
import io

# ---------- 1. Configure ALL AI Systems ----------
# Gemini (Primary)
try:
    import google.generativeai as genai
    genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
    gemini_model = genai.GenerativeModel('gemini-2.5-pro')
    print("โœ… Gemini AI initialized successfully (PRIMARY)")
except Exception as e:
    print(f"โŒ Error initializing Gemini: {e}")
    gemini_model = None

# Cohere (Secondary)
try:
    import cohere
    cohere_client = cohere.Client(os.getenv("COHERE_API_KEY"))
    print("โœ… Cohere initialized successfully (SECONDARY)")
except Exception as e:
    print(f"โŒ Error initializing Cohere: {e}")
    cohere_client = None

# Z.ai (Tertiary)
try:
    from huggingface_hub import InferenceClient
    zai_client = InferenceClient(
        provider="novita",
        api_key=os.environ.get("HF_TOKEN"),
    )
    print("โœ… Z.ai GLM-4.6 initialized successfully (TERTIARY)")
except Exception as e:
    print(f"โŒ Error initializing Z.ai: {e}")
    zai_client = None

# MiniMax (Final Fallback)
try:
    minimax_client = InferenceClient(
        provider="novita",
        api_key=os.environ.get("HF_TOKEN"),
    )
    print("โœ… MiniMax AI initialized successfully (FINAL FALLBACK)")
except Exception as e:
    print(f"โŒ Error initializing MiniMax: {e}")
    minimax_client = None

# ---------- 2. Unified AI Function with Smart Fallback ----------
def ask_ai(prompt, temperature=0.7, max_retries=2):
    """
    Try models in order: Gemini โ†’ Cohere โ†’ Z.ai โ†’ MiniMax
    Returns: (response_text, source_name)
    """
    last_error = None
    
    # Try Gemini first (Primary)
    if gemini_model:
        for attempt in range(max_retries):
            try:
                response = gemini_model.generate_content(
                    prompt,
                    generation_config=genai.types.GenerationConfig(
                        temperature=temperature,
                    )
                )
                return response.text, "gemini"
            except Exception as e:
                last_error = e
                print(f"โš  Gemini attempt {attempt+1} failed: {str(e)}")
                if attempt < max_retries - 1:
                    time.sleep(1)
    
    # Try Cohere (Secondary)
    if cohere_client:
        for attempt in range(max_retries):
            try:
                response = cohere_client.chat(
                    model="command-r-plus-08-2024",
                    message=prompt,
                    temperature=temperature
                )
                return response.text, "cohere"
            except Exception as e:
                last_error = e
                print(f"โš  Cohere attempt {attempt+1} failed: {str(e)}")
                if attempt < max_retries - 1:
                    time.sleep(1)
    
    # Try Z.ai (Tertiary)
    if zai_client:
        for attempt in range(max_retries):
            try:
                completion = zai_client.chat.completions.create(
                    model="zai-org/GLM-4.6",
                    messages=[{"role": "user", "content": prompt}],
                    temperature=temperature
                )
                return completion.choices[0].message.content, "zai"
            except Exception as e:
                last_error = e
                print(f"โš  Z.ai attempt {attempt+1} failed: {str(e)}")
                if attempt < max_retries - 1:
                    time.sleep(1)
    
    # Try MiniMax (Final Fallback)
    if minimax_client:
        try:
            completion = minimax_client.chat.completions.create(
                model="MiniMaxAI/MiniMax-M2",
                messages=[{"role": "user", "content": prompt}],
                temperature=temperature
            )
            return completion.choices[0].message.content, "minimax"
        except Exception as e:
            last_error = e
            print(f"โš  MiniMax fallback failed: {str(e)}")
    
    # All failed
    error_msg = f"โŒ Error: All AI services failed. Last error: {str(last_error)}"
    return error_msg, "error"

# ---------- 3. Enhanced Global Storage ----------
papers_storage = []
pdf_content_storage = {}
insert_storage = {}
questions_index = []
ADMIN_PASSWORD = "@mikaelJ46"

# ---------- 4. Comprehensive Topic Lists ----------
chemistry_topics = [
    # Principles of Chemistry
    "States of Matter", "Atoms, Elements & Compounds", "Mixtures & Separation Techniques",
    "Atomic Structure", "Electronic Configuration", "Periodic Table",
    "Chemical Bonding: Ionic", "Chemical Bonding: Covalent", "Chemical Bonding: Metallic",
    "Structure & Properties of Materials", "Nanoparticles",
    
    # Inorganic Chemistry
    "Group 1: Alkali Metals", "Group 7: Halogens", "Group 0: Noble Gases",
    "Transition Metals", "Reactivity Series", "Extraction of Metals",
    "Corrosion & Rusting", "Alloys",
    
    # Physical Chemistry
    "Chemical Reactions", "Exothermic & Endothermic Reactions", "Energy Changes",
    "Rates of Reaction", "Catalysts", "Reversible Reactions", "Equilibrium",
    "Redox Reactions", "Electrolysis", "Electrochemistry",
    
    # Acids, Bases & Salts
    "Acids & Alkalis", "pH Scale", "Neutralization", "Making Salts",
    "Titrations", "Strong & Weak Acids",
    
    # Organic Chemistry
    "Hydrocarbons: Alkanes", "Hydrocarbons: Alkenes", "Crude Oil & Fractional Distillation",
    "Polymers", "Alcohols", "Carboxylic Acids", "Organic Synthesis",
    
    # Chemistry of the Environment
    "Air Composition", "Air Pollution", "Greenhouse Effect & Climate Change",
    "Water Treatment", "Sustainable Chemistry",
    
    # Quantitative Chemistry
    "Relative Formula Mass", "Moles & Molar Mass", "Empirical & Molecular Formulae",
    "Reacting Masses", "Limiting Reactants", "Percentage Yield",
    "Gas Volumes", "Concentration Calculations",
    
    # Practical Skills
    "Laboratory Safety", "Experimental Techniques", "Analysis & Evaluation"
]

biology_topics = [
    # Cell Biology
    "Cell Structure & Function", "Specialised Cells", "Microscopy",
    "Cell Division: Mitosis", "Cell Division: Meiosis", "Stem Cells",
    "Diffusion", "Osmosis", "Active Transport",
    
    # Organisation
    "Organisation of Organisms", "Enzymes", "Digestive System",
    "Circulatory System: Heart", "Circulatory System: Blood Vessels", "Blood Components",
    "Respiratory System", "Gas Exchange", "Breathing Mechanism",
    
    # Infection & Response
    "Communicable Diseases", "Pathogens: Bacteria & Viruses", "Disease Prevention",
    "Immune System", "Vaccination", "Antibiotics & Painkillers",
    "Developing New Medicines", "Monoclonal Antibodies",
    
    # Bioenergetics
    "Photosynthesis", "Factors Affecting Photosynthesis", "Uses of Glucose",
    "Respiration: Aerobic", "Respiration: Anaerobic", "Metabolism",
    
    # Homeostasis & Response
    "Homeostasis Principles", "Nervous System", "Reflex Actions", "Brain Structure",
    "Eye Structure & Function", "Body Temperature Control",
    "Endocrine System", "Hormones", "Blood Glucose Regulation",
    "Diabetes", "Water & Nitrogen Balance", "Kidneys & Dialysis",
    
    # Inheritance, Variation & Evolution
    "DNA Structure", "Protein Synthesis", "Genetic Inheritance",
    "Inherited Disorders", "Sex Determination", "Genetic Diagrams",
    "Variation", "Evolution", "Natural Selection", "Selective Breeding",
    "Genetic Engineering", "Cloning", "Classification",
    
    # Ecology
    "Ecosystems", "Food Chains & Webs", "Energy Transfer",
    "Nutrient Cycles: Carbon", "Nutrient Cycles: Water", "Nutrient Cycles: Nitrogen",
    "Biodiversity", "Habitat Loss", "Conservation",
    "Population Dynamics", "Competition", "Adaptations",
    "Waste Management", "Pollution", "Global Warming Impact",
    "Deforestation", "Sustainable Development",
    
    # Practical Skills
    "Scientific Method", "Variables & Controls", "Data Analysis",
    "Biological Techniques", "Field Studies"
]

# ---------- 5. Enhanced PDF Processing ----------
def extract_text_from_pdf(pdf_file):
    """Extract text from uploaded PDF file"""
    if pdf_file is None:
        return ""
    try:
        pdf_reader = PyPDF2.PdfReader(pdf_file)
        text = ""
        for page in pdf_reader.pages:
            text += page.extract_text() + "\n"
        return text
    except Exception as e:
        return f"Error extracting PDF: {e}"

def identify_paper_details(text, filename):
    """Use AI to identify paper year, series, variant, and subject from content"""
    sample_text = text[:2000] if len(text) > 2000 else text
    
    prompt = f"""Analyze this IGCSE science past paper and identify its details.

Filename: {filename}
Paper Text Sample:
{sample_text}

Identify and return ONLY a JSON object with:
- subject: "Chemistry" or "Biology"
- year: The year (e.g., "2023", "2022")
- series: The exam series (e.g., "June", "November", "May/June", "October/November")
- variant: The paper variant (e.g., "1", "2", "3" or "11", "12", "21", "22")
- paper_number: The paper number (e.g., "1", "2", "3", "4", "6")
- syllabus_code: If visible (e.g., "0620" for Chemistry, "0610" for Biology)

Look for clues like "Cambridge IGCSE", subject codes, dates, paper numbers.

Return ONLY valid JSON (no markdown):
{{"subject": "...", "year": "...", "series": "...", "variant": "...", "paper_number": "...", "syllabus_code": "..."}}"""
    
    try:
        response, _ = ask_ai(prompt, temperature=0.1)
        clean_txt = response.replace("```json", "").replace("```", "").strip()
        details = json.loads(clean_txt)
        return details
    except Exception as e:
        print(f"Error identifying paper details: {e}")
        return parse_filename_for_details(filename)

def parse_filename_for_details(filename):
    """Fallback: Parse filename for paper details"""
    details = {
        "subject": "Unknown",
        "year": "Unknown",
        "series": "Unknown",
        "variant": "Unknown",
        "paper_number": "Unknown",
        "syllabus_code": "Unknown"
    }
    
    # Extract year
    year_match = re.search(r'(20\d{2})|(\d{2}(?=_[wsmj]|[WS]))', filename)
    if year_match:
        year = year_match.group(1) or ("20" + year_match.group(2))
        details["year"] = year
    
    # Extract series
    if re.search(r'[Jj]une?|[Mm]ay[_/-]?[Jj]une?|mj|MJ', filename):
        details["series"] = "May/June"
    elif re.search(r'[Nn]ov(ember)?|[Oo]ct(ober)?|ON', filename):
        details["series"] = "October/November"
    elif re.search(r'[Mm]ar(ch)?|[Ff]eb(ruary)?|FM', filename):
        details["series"] = "February/March"
    
    # Extract variant
    variant_match = re.search(r'[Vv]ariant[_\s]?(\d)|[Pp]aper[_\s]?(\d{1,2})|_qp_(\d{1,2})', filename)
    if variant_match:
        details["variant"] = variant_match.group(1) or variant_match.group(2) or variant_match.group(3)
    
    # Extract syllabus code and subject
    code_match = re.search(r'\b(0\d{3})\b', filename)
    if code_match:
        details["syllabus_code"] = code_match.group(1)
        code_subject_map = {
            '0620': 'Chemistry', '0610': 'Biology'
        }
        details["subject"] = code_subject_map.get(code_match.group(1), "Unknown")
    
    return details

def extract_questions_from_text(text, paper_id, paper_title, subject, paper_details):
    """Use AI to intelligently extract questions from past paper text"""
    if not text or len(text) < 100:
        return []
    
    prompt = f"""Analyze this IGCSE {subject} past paper and extract ALL questions.

Paper Details:
- Subject: {subject}
- Year: {paper_details.get('year', 'Unknown')}
- Series: {paper_details.get('series', 'Unknown')}
- Paper: {paper_details.get('paper_number', 'Unknown')}
- Variant: {paper_details.get('variant', 'Unknown')}

Paper Text:
{text[:8000]}

Extract each question and return as JSON array. For each question include:
- question_number (e.g., "1(a)", "2(b)(i)")
- question_text (the complete question)
- marks (number of marks)
- topic (specific IGCSE {subject} topic)
- requires_insert (true/false - references diagrams, figures, data?)
- question_type (e.g., "multiple choice", "structured", "practical", "calculation", "explanation")

Return ONLY valid JSON array (no markdown):
[{{"question_number": "1(a)", "question_text": "...", "marks": 2, "topic": "...", "requires_insert": false, "question_type": "..."}}]"""
    
    try:
        response, _ = ask_ai(prompt, temperature=0.2)
        clean_txt = response.replace("```json", "").replace("```", "").strip()
        questions = json.loads(clean_txt)
        
        for q in questions:
            q['paper_id'] = paper_id
            q['paper_title'] = paper_title
            q['subject'] = subject
            q['year'] = paper_details.get('year', 'Unknown')
            q['series'] = paper_details.get('series', 'Unknown')
            q['variant'] = paper_details.get('variant', 'Unknown')
            q['paper_number'] = paper_details.get('paper_number', 'Unknown')
            q['syllabus_code'] = paper_details.get('syllabus_code', 'Unknown')
        
        return questions
    except Exception as e:
        print(f"Error extracting questions: {e}")
        return extract_questions_fallback(text, paper_id, paper_title, subject, paper_details)

def extract_questions_fallback(text, paper_id, paper_title, subject, paper_details):
    """Fallback method using regex patterns"""
    questions = []
    pattern = r'(\d+(?:\([a-z]\))?(?:\([ivx]+\))?)\s+(.{20,500}?)\[(\d+)\]'
    matches = re.finditer(pattern, text, re.IGNORECASE)
    
    for match in matches:
        q_num = match.group(1)
        q_text = match.group(2).strip()
        marks = int(match.group(3))
        
        questions.append({
            'question_number': q_num,
            'question_text': q_text,
            'marks': marks,
            'topic': 'General',
            'requires_insert': bool(re.search(r'Fig\.|diagram|table|graph|data|shown', q_text, re.IGNORECASE)),
            'question_type': 'structured',
            'paper_id': paper_id,
            'paper_title': paper_title,
            'subject': subject,
            'year': paper_details.get('year', 'Unknown'),
            'series': paper_details.get('series', 'Unknown'),
            'variant': paper_details.get('variant', 'Unknown'),
            'paper_number': paper_details.get('paper_number', 'Unknown'),
            'syllabus_code': paper_details.get('syllabus_code', 'Unknown')
        })
    
    return questions

def process_insert_file(insert_file):
    """Process insert file (PDF or image)"""
    if insert_file is None:
        return None, None
    
    try:
        file_name = insert_file.name
        file_ext = file_name.lower().split('.')[-1]
        
        if file_ext == 'pdf':
            text = extract_text_from_pdf(insert_file)
            return text, "pdf"
        elif file_ext in ['jpg', 'jpeg', 'png', 'gif']:
            image = Image.open(insert_file)
            return image, "image"
        else:
            return None, None
    except Exception as e:
        print(f"Error processing insert: {e}")
        return None, None

# ---------- 6. Deep Understanding AI Tutor ----------
def ai_tutor_chat(message, history, subject, topic):
    """AI tutor focused on deep understanding and conceptual clarity"""
    if not message.strip():
        return history

    subject_context = {
        "Chemistry": """You are an expert IGCSE Chemistry tutor who prioritizes DEEP UNDERSTANDING over memorization.

Your teaching approach:
- Always explain the WHY behind chemical phenomena (not just the what)
- Connect microscopic (atomic/molecular) behavior to macroscopic observations
- Use real-world examples and applications to make concepts tangible
- Break down complex reactions into step-by-step mechanisms
- Emphasize patterns and relationships (e.g., periodic trends, reaction types)
- Address common misconceptions directly
- Use analogies and visual descriptions to clarify abstract concepts
- Encourage students to predict outcomes based on understanding, not memorization
- Link different topics together (e.g., bonding โ†’ properties โ†’ reactivity)

Key teaching principles:
- Particle theory underlies everything (structure determines properties)
- Energy changes drive chemical processes
- Conservation laws (mass, charge, energy) are fundamental
- Equilibrium and rates are about competing processes""",

        "Biology": """You are an expert IGCSE Biology tutor who emphasizes DEEP UNDERSTANDING and interconnected thinking.

Your teaching approach:
- Always explain biological processes in terms of structure-function relationships
- Connect molecular/cellular processes to organism-level phenomena
- Use real-world health, ecology, and evolution examples
- Explain mechanisms step-by-step (don't just list facts)
- Emphasize the REASONS for biological adaptations and processes
- Address common misconceptions about evolution, genetics, and body systems
- Use analogies to make complex processes accessible (but explain their limits)
- Show how different biological systems interact and depend on each other
- Encourage students to apply knowledge to novel situations
- Link topics together (e.g., respiration โ†’ transport โ†’ gas exchange)

Key teaching principles:
- Evolution by natural selection explains adaptations
- Enzymes control the rate of life processes
- Homeostasis maintains stable internal conditions
- Energy flow and nutrient cycling connect ecology
- DNA โ†’ RNA โ†’ protein โ†’ trait (central dogma)"""
    }

    system = f"""{subject_context[subject]}

Current focus: {topic or 'any topic'}

When answering:
1. Check for understanding gaps before giving the full answer
2. Use the Socratic method - guide thinking with questions
3. Provide detailed step-by-step explanations with reasoning
4. Include diagrams descriptions when helpful
5. Give practice examples for students to try
6. Connect to exam skills (command words, mark schemes)
7. Celebrate curiosity and deeper questions

Remember: Understanding beats memorization. Help students THINK like scientists."""
    
    # Build conversation context
    conversation = ""
    for user_msg, bot_msg in history[-6:]:
        if user_msg:
            conversation += f"Student: {user_msg}\n"
        if bot_msg:
            clean_msg = bot_msg.replace("๐Ÿ”ต ", "").replace("๐ŸŸข ", "").replace("๐ŸŸฃ ", "")
            conversation += f"Tutor: {clean_msg}\n"
    
    conversation += f"Student: {message}\nTutor:"
    full_prompt = f"{system}\n\nConversation:\n{conversation}"
    
    bot_response, source = ask_ai(full_prompt, temperature=0.7)
    
    # Add source indicator
    if source == "cohere":
        bot_response = f"๐Ÿ”ต {bot_response}"
    elif source == "zai":
        bot_response = f"๐ŸŸข {bot_response}"
    elif source == "minimax":
        bot_response = f"๐ŸŸฃ {bot_response}"
    
    history.append((message, bot_response))
    return history

def clear_chat():
    return []

# ---------- 7. Concept Explainer with Depth ----------
def explain_concept(subject, concept):
    """Deep dive explanation of scientific concepts"""
    if not concept:
        return "Enter a concept to explain!"
    
    prompt = f"""Provide a COMPREHENSIVE explanation of this IGCSE {subject} concept: "{concept}"

Structure your explanation as follows:

**1. CORE IDEA** (In simple terms - what IS it?)

**2. DEEPER UNDERSTANDING** (Why does it work this way? What's the mechanism?)

**3. KEY DETAILS & FACTS** (Important specifics students need to know)

**4. COMMON MISCONCEPTIONS** (What do students often get wrong?)

**5. REAL-WORLD CONNECTIONS** (Where do we see this? Why does it matter?)

**6. EXAM TIPS** (What questions test this? How to approach them?)

**7. PRACTICE THINKING** (A question to test understanding)

Use clear language, step-by-step reasoning, and helpful analogies.
Make connections to other topics. Focus on UNDERSTANDING, not just facts."""
    
    response, source = ask_ai(prompt, temperature=0.5)
    
    if source in ["cohere", "zai", "minimax"]:
        response = f"{response}\n\n_[Explained by {source.title()}]_"
    
    return response

# ---------- 8. Calculation Helper ----------
def solve_calculation(subject, problem, show_steps):
    """Step-by-step calculation solver with conceptual explanation"""
    if not problem.strip():
        return "Enter a calculation problem!"
    
    steps_instruction = "Show EVERY step with full working" if show_steps else "Show key steps"
    
    prompt = f"""Solve this IGCSE {subject} calculation problem with DEEP EXPLANATION:

Problem: {problem}

Provide:
1. **What we're finding**: Identify what the question asks for
2. **What we know**: List given information and its meaning
3. **Formula/Concept**: Which formula/principle applies and WHY
4. **Step-by-step solution**: {steps_instruction} with units
5. **Checking**: Does the answer make sense? Why?
6. **Concept explanation**: What does this result mean scientifically?
7. **Common mistakes**: What errors do students typically make?
8. **Related problems**: Similar question types to practice

Use clear formatting. Explain the reasoning at each step, not just the math."""
    
    response, source = ask_ai(prompt, temperature=0.3)
    
    if source in ["cohere", "zai", "minimax"]:
        response = f"{response}\n\n_[Solved by {source.title()}]_"
    
    return response

# ---------- 9. Experiment Analyzer ----------
def analyze_experiment(subject, experiment_description, question):
    """Analyze experiments and practical work with scientific reasoning"""
    if not experiment_description.strip():
        return "Describe the experiment!"
    
    prompt = f"""Analyze this IGCSE {subject} experiment with focus on SCIENTIFIC THINKING:

Experiment: {experiment_description}

Question: {question if question else "Analyze this experiment comprehensively"}

Provide:
1. **Aim**: What is being investigated and why?
2. **Science Behind It**: What principles/concepts does this test?
3. **Method Analysis**: Why is it done this way? What makes it valid?
4. **Variables**: Independent, dependent, control variables and why they matter
5. **Expected Results**: What should happen and WHY (predict using theory)
6. **Safety & Practical Tips**: Important precautions and techniques
7. **Possible Errors**: What could go wrong? How to minimize errors?
8. **Results Analysis**: How to interpret data scientifically
9. **Evaluation**: How could this experiment be improved?
10. **Exam Connection**: How might this be tested?

Think like a scientist - connect method to theory."""
    
    response, source = ask_ai(prompt, temperature=0.4)
    
    if source in ["cohere", "zai", "minimax"]:
        response = f"{response}\n\n_[Analyzed by {source.title()}]_"
    
    return response

# ---------- 10. Enhanced Practice Questions ----------
def generate_question(subject, topic, difficulty):
    """Generate practice questions with focus on understanding"""
    if not topic:
        return "Select a topic!", "", ""
    
    difficulty_guide = {
        "Easy": "Test basic understanding and recall. Simple calculations or describe questions.",
        "Medium": "Test application and analysis. Require explanations and connections.",
        "Hard": "Test evaluation and synthesis. Multi-step problems, novel scenarios."
    }
    
    pdf_context = ""
    for paper_id, content in pdf_content_storage.items():
        paper = next((p for p in papers_storage if p['id'] == paper_id), None)
        if paper and paper['subject'] == subject:
            pdf_context += f"\n\nReference: {paper['title']}:\n{content[:2000]}"
    
    prompt = f"""Create ONE high-quality IGCSE {subject} exam question on: "{topic}"

Difficulty: {difficulty} - {difficulty_guide[difficulty]}
{f"Base style on: {pdf_context[:1500]}" if pdf_context else "Create authentic exam-style question."}

The question should:
- Test UNDERSTANDING, not just recall
- Use appropriate command words (describe, explain, evaluate, calculate, etc.)
- Be worth 4-8 marks
- Include context/data if relevant
- Test ability to apply knowledge to new situations

Return ONLY valid JSON (no markdown):
{{
    "question": "complete question with all context",
    "marks": 6,
    "command_word": "explain/describe/calculate/etc",
    "expectedAnswer": "detailed key points with scientific reasoning",
    "markScheme": "specific mark allocations and what earns each mark",
    "understandingTips": "what concepts students need to understand to answer this"
}}"""
    
    response, source = ask_ai(prompt, temperature=0.4)
    
    try:
        clean_txt = response.replace("```json", "").replace("```", "").strip()
        data = json.loads(clean_txt)
        
        question_text = f"**[{data['marks']} marks] - {data['command_word'].upper()}**\n\n{data['question']}"
        expected = f"**Understanding Required:**\n{data.get('understandingTips', '')}\n\n**Key Points:**\n{data['expectedAnswer']}"
        marks = data['markScheme']
        
        return question_text, expected, marks
    except:
        return response, "", "Error parsing response"

def check_answer(question, expected, user_answer, subject):
    """Check answers with focus on understanding and reasoning"""
    if not user_answer.strip():
        return "Write your answer first!"
    
    prompt = f"""Evaluate this IGCSE {subject} answer focusing on UNDERSTANDING and SCIENTIFIC REASONING:

Question: {question}

Expected answer points: {expected}

Student's answer:
{user_answer}

Assess:
1. Scientific accuracy
2. Depth of understanding (not just memorization)
3. Use of scientific terminology
4. Logical reasoning and explanations
5. Answering the specific command word
6. Completeness

Return JSON (no markdown):
{{
    "score": 0-100,
    "marks": "X/8",
    "understanding_level": "surface/developing/strong/excellent",
    "feedback": "detailed feedback on scientific understanding",
    "strengths": "what shows good understanding",
    "improvements": "how to deepen understanding",
    "misconceptions": "any misunderstandings evident",
    "examTips": "exam technique advice",
    "followUpQuestion": "a question to test/extend understanding further"
}}"""
    
    response, source = ask_ai(prompt, temperature=0.3)
    
    try:
        clean_txt = response.replace("```json", "").replace("```", "").strip()
        fb = json.loads(clean_txt)
        
        result = f"""๐Ÿ“Š **Score: {fb['score']}% ({fb['marks']})**
**Understanding Level:** {fb['understanding_level'].upper()}

๐Ÿ“ **Detailed Feedback:**
{fb['feedback']}

โœ… **Your Strengths:**
{fb['strengths']}

๐Ÿ“ˆ **How to Deepen Understanding:**
{fb['improvements']}

โš ๏ธ **Misconceptions to Address:**
{fb.get('misconceptions', 'None identified')}

๐Ÿ’ก **Exam Tips:**
{fb['examTips']}

๐Ÿค” **Think Further:**
{fb.get('followUpQuestion', 'Keep practicing!')}"""
        
        if source in ["cohere", "zai", "minimax"]:
            result += f"\n\n_[Graded by {source.title()}]_"
        
        return result
    except:
        return response

# ---------- 11. Past Papers Browser ----------
def search_questions_by_topic(subject, topic):
    """Search for questions matching a specific topic"""
    if not questions_index:
        return "๐Ÿ“ญ No questions available yet. Admin needs to upload past papers first!"
    
    matching = [q for q in questions_index 
                if q['subject'] == subject and 
                (topic.lower() in q['topic'].lower() or topic.lower() in q['question_text'].lower())]
    
    if not matching:
        return f"๐Ÿ“ญ No questions found for {topic} in {subject}. Try a different topic or broader search."
    
    result = f"### ๐ŸŽฏ Found {len(matching)} question(s) on '{topic}' in {subject}\n\n"
    
    for i, q in enumerate(matching, 1):
        insert_note = " ๐Ÿ–ผ๏ธ **[Requires Insert]**" if q.get('requires_insert') else ""
        q_type = f" ({q.get('question_type', 'structured')})" if q.get('question_type') else ""
        
        paper_info = f"**{q['year']} {q['series']}** - Paper {q['paper_number']}"
        if q.get('variant') != 'Unknown':
            paper_info += f" Variant {q['variant']}"
        if q.get('syllabus_code') != 'Unknown':
            paper_info += f" ({q['syllabus_code']})"
        
        result += f"""**Question {i}** - {paper_info}
๐Ÿ“ **{q['question_number']}** [{q['marks']} marks]{q_type}{insert_note}
{q['question_text']}

{'โ”€'*80}
"""
    
    return result

def view_papers_student(subject):
    """View all papers for a subject"""
    filtered = [p for p in papers_storage if p["subject"] == subject]
    if not filtered:
        return f"๐Ÿ“ญ No {subject} papers available."
    
    result = ""
    for p in filtered:
        insert_note = " ๐Ÿ–ผ๏ธ Insert Available" if p['id'] in insert_storage else ""
        q_count = len([q for q in questions_index if q['paper_id'] == p['id']])
        
        paper_details = p.get('paper_details', {})
        year = paper_details.get('year', 'Unknown')
        series = paper_details.get('series', 'Unknown')
        variant = paper_details.get('variant', 'Unknown')
        paper_num = paper_details.get('paper_number', 'Unknown')
        syllabus = paper_details.get('syllabus_code', 'Unknown')
        
        paper_info = f"**{year} {series}** - Paper {paper_num}"
        if variant != 'Unknown':
            paper_info += f" Variant {variant}"
        if syllabus != 'Unknown':
            paper_info += f" ({syllabus})"
        
        result += f"""**{p['title']}** {'๐Ÿ“„ PDF' if p.get('has_pdf') else ''}{insert_note}
{paper_info}
โฐ Uploaded: {p['uploaded_at']} | ๐Ÿ“ {q_count} questions extracted
{p['content'][:200]}...

{'โ•'*80}
"""
    
    return result

# ---------- 12. Admin Functions ----------
def verify_admin_password(password):
    if password == ADMIN_PASSWORD:
        return gr.update(visible=True), gr.update(visible=False), "โœ… Access granted!"
    return gr.update(visible=False), gr.update(visible=True), "โŒ Incorrect password!"

def upload_paper(title, subject, content, pdf_file, insert_file):
    """Upload past papers with AI extraction"""
    if not all([title, subject, content]):
        return "โš  Please fill all required fields!", get_papers_list(), "๐Ÿ“Š Status: Waiting for upload"
    
    paper_id = len(papers_storage) + 1
    
    pdf_text = ""
    paper_details = {}
    if pdf_file is not None:
        pdf_text = extract_text_from_pdf(pdf_file)
        if pdf_text and not pdf_text.startswith("Error"):
            paper_details = identify_paper_details(pdf_text, pdf_file.name)
            pdf_content_storage[paper_id] = pdf_text
            
            detail_str = f"\n\n๐Ÿ“‹ **Paper Details:**"
            detail_str += f"\n- Year: {paper_details.get('year', 'Unknown')}"
            detail_str += f"\n- Series: {paper_details.get('series', 'Unknown')}"
            detail_str += f"\n- Paper: {paper_details.get('paper_number', 'Unknown')}"
            detail_str += f"\n- Variant: {paper_details.get('variant', 'Unknown')}"
            if paper_details.get('syllabus_code') != 'Unknown':
                detail_str += f"\n- Syllabus Code: {paper_details.get('syllabus_code')}"
            content += detail_str
            content += f"\n[๐Ÿ“„ PDF extracted: {len(pdf_text)} characters]"
    
    insert_data = None
    insert_type = None
    if insert_file is not None:
        insert_data, insert_type = process_insert_file(insert_file)
        if insert_data:
            insert_storage[paper_id] = (insert_data, insert_type)
            content += f"\n[๐Ÿ–ผ๏ธ Insert attached: {insert_type}]"
    
    papers_storage.append({
        "id": paper_id,
        "title": title,
        "subject": subject,
        "content": content,
        "has_pdf": bool(pdf_text and not pdf_text.startswith("Error")),
        "has_insert": bool(insert_data),
        "paper_details": paper_details,
        "uploaded_at": datetime.now().strftime("%Y-%m-%d %H:%M")
    })
    
    status_msg = "โœ… Paper uploaded!"
    if pdf_text and not pdf_text.startswith("Error"):
        status_msg += "\nโณ AI is extracting questions..."
        questions = extract_questions_from_text(pdf_text, paper_id, title, subject, paper_details)
        questions_index.extend(questions)
        
        paper_info = f"{paper_details.get('year', 'Unknown')} {paper_details.get('series', 'Unknown')}"
        if paper_details.get('variant') != 'Unknown':
            paper_info += f" Variant {paper_details.get('variant')}"
        
        status_msg += f"\nโœ… Extracted {len(questions)} questions from **{paper_info}**!"
        status_msg += f"\n๐Ÿ“‹ Identified as: {subject} Paper {paper_details.get('paper_number', 'Unknown')}"
    
    return status_msg, get_papers_list(), f"๐Ÿ“Š Total papers: {len(papers_storage)} | Total questions: {len(questions_index)}"

def get_papers_list():
    """Get formatted list of all papers"""
    if not papers_storage:
        return "No papers yet."
    
    result = []
    for p in papers_storage:
        paper_details = p.get('paper_details', {})
        year = paper_details.get('year', 'Unknown')
        series = paper_details.get('series', 'Unknown')
        variant = paper_details.get('variant', 'Unknown')
        paper_num = paper_details.get('paper_number', 'Unknown')
        
        paper_info = f"{year} {series} - Paper {paper_num}"
        if variant != 'Unknown':
            paper_info += f" V{variant}"
        
        insert_icon = '๐Ÿ–ผ๏ธ Insert' if p.get('has_insert') else ''
        pdf_icon = '๐Ÿ“„ PDF' if p.get('has_pdf') else ''
        
        result.append(f"**{p['title']}** ({p['subject']}) {pdf_icon} {insert_icon}\n{paper_info}\nโฐ {p['uploaded_at']}\n{p['content'][:120]}...\n{'โ”€'*60}")
    
    return "\n".join(result)

# ---------- 13. Gradio UI ----------
with gr.Blocks(theme=gr.themes.Soft(), title="IGCSE Science Platform") as app:
    gr.Markdown("""
    # ๐Ÿ”ฌ IGCSE Science Learning Platform
    Chemistry โš—๏ธ | Biology ๐Ÿงฌ
    _Deep Understanding Through AI-Powered Learning_
    """)

    with gr.Tabs():
        # โ”€โ”€โ”€โ”€โ”€ STUDENT PORTAL โ”€โ”€โ”€โ”€โ”€
        with gr.Tab("๐Ÿ‘จโ€๐ŸŽ“ Student Portal"):
            with gr.Tabs():
                # AI TUTOR
                with gr.Tab("๐Ÿค– AI Tutor - Deep Understanding"):
                    gr.Markdown("""### Chat with Your AI Science Tutor
*Focus on understanding WHY, not just memorizing facts*

**Tips for getting the most from your tutor:**
- Ask "why" and "how" questions
- Request step-by-step explanations
- Ask for real-world examples
- Challenge yourself with "what if" scenarios""")
                    
                    with gr.Row():
                        subj = gr.Radio(["Chemistry", "Biology"], label="Subject", value="Chemistry")
                        topc = gr.Dropdown(chemistry_topics, label="Topic (optional)", allow_custom_value=True)

                    def update_topics(s):
                        topics = {"Chemistry": chemistry_topics, "Biology": biology_topics}
                        return gr.Dropdown(choices=topics[s], value=None)
                    subj.change(update_topics, subj, topc)

                    chat = gr.Chatbot(height=500, show_label=False)
                    txt = gr.Textbox(placeholder="Ask anything... e.g., 'Why do ionic compounds conduct electricity when molten but not when solid?'", label="Message")
                    with gr.Row():
                        send = gr.Button("Send ๐Ÿ“ค", variant="primary")
                        clr = gr.Button("Clear ๐Ÿ—‘")
                    
                    send.click(ai_tutor_chat, [txt, chat, subj, topc], chat)
                    txt.submit(ai_tutor_chat, [txt, chat, subj, topc], chat)
                    clr.click(clear_chat, outputs=chat)

                # CONCEPT EXPLAINER
                with gr.Tab("๐Ÿ’ก Concept Explainer"):
                    gr.Markdown("""### Deep Dive into Scientific Concepts
*Get comprehensive explanations that build real understanding*""")
                    
                    with gr.Row():
                        ce_subj = gr.Radio(["Chemistry", "Biology"], label="Subject", value="Chemistry")
                        ce_concept = gr.Textbox(label="Concept to Explain", 
                                               placeholder="e.g., 'covalent bonding', 'osmosis', 'enzyme action'")
                    
                    ce_output = gr.Markdown(label="Explanation")
                    gr.Button("๐Ÿ” Explain Concept", variant="primary", size="lg").click(
                        explain_concept, [ce_subj, ce_concept], ce_output
                    )

                # CALCULATION HELPER
                with gr.Tab("๐Ÿงฎ Calculation Helper"):
                    gr.Markdown("""### Step-by-Step Problem Solving
*Understand the reasoning, not just the answer*""")
                    
                    calc_subj = gr.Radio(["Chemistry", "Biology"], label="Subject", value="Chemistry")
                    calc_problem = gr.Textbox(lines=4, label="Problem", 
                                            placeholder="e.g., 'Calculate the mass of calcium carbonate needed to produce 22g of carbon dioxide'")
                    calc_steps = gr.Checkbox(label="Show detailed steps", value=True)
                    calc_output = gr.Markdown(label="Solution")
                    
                    gr.Button("โœ๏ธ Solve Problem", variant="primary", size="lg").click(
                        solve_calculation, [calc_subj, calc_problem, calc_steps], calc_output
                    )

                # EXPERIMENT ANALYZER
                with gr.Tab("๐Ÿ”ฌ Experiment Analyzer"):
                    gr.Markdown("""### Understand Scientific Investigations
*Connect practical work to theory*""")
                    
                    exp_subj = gr.Radio(["Chemistry", "Biology"], label="Subject", value="Chemistry")
                    exp_desc = gr.Textbox(lines=5, label="Experiment Description", 
                                         placeholder="Describe the experiment setup and procedure...")
                    exp_q = gr.Textbox(label="Specific Question (optional)", 
                                      placeholder="e.g., 'Why must we use excess acid in this experiment?'")
                    exp_output = gr.Markdown(label="Analysis")
                    
                    gr.Button("๐Ÿ” Analyze Experiment", variant="primary", size="lg").click(
                        analyze_experiment, [exp_subj, exp_desc, exp_q], exp_output
                    )

                # PAST PAPERS BROWSER
                with gr.Tab("๐Ÿ“š Past Papers Browser"):
                    gr.Markdown("""### ๐ŸŽฏ Search Real Exam Questions by Topic
*Practice with actual IGCSE questions*""")
                    
                    with gr.Row():
                        pp_subject = gr.Radio(["Chemistry", "Biology"], label="Subject", value="Chemistry")
                        pp_topic = gr.Dropdown(chemistry_topics, label="Select Topic")
                    
                    pp_subject.change(update_topics, pp_subject, pp_topic)
                    
                    search_btn = gr.Button("๐Ÿ” Search Questions", variant="primary", size="lg")
                    questions_output = gr.Markdown(label="Questions Found", value="Select a topic and click Search")
                    
                    search_btn.click(search_questions_by_topic, [pp_subject, pp_topic], questions_output)
                    
                    gr.Markdown("---\n### ๐Ÿ“„ Browse All Papers")
                    browse_subject = gr.Radio(["Chemistry", "Biology"], label="Subject", value="Chemistry")
                    papers_display = gr.Markdown(label="Available Papers")
                    gr.Button("๐Ÿ“– Show All Papers").click(view_papers_student, browse_subject, papers_display)

                # PRACTICE QUESTIONS
                with gr.Tab("โœ Practice Questions"):
                    gr.Markdown("""### Generate & Practice Exam Questions
*Focus on understanding, not just correct answers*""")
                    
                    with gr.Row():
                        ps = gr.Radio(["Chemistry", "Biology"], label="Subject", value="Chemistry")
                        pt = gr.Dropdown(chemistry_topics, label="Topic")
                        diff = gr.Radio(["Easy", "Medium", "Hard"], label="Difficulty", value="Medium")
                    
                    ps.change(update_topics, ps, pt)

                    q = gr.Textbox(label="๐Ÿ“ Question", lines=8, interactive=False)
                    exp = gr.Textbox(label="Understanding Required & Expected Points", lines=6, interactive=False)
                    mark = gr.Textbox(label="๐Ÿ“Š Mark Scheme", lines=5, interactive=False)
                    ans = gr.Textbox(lines=12, label="โœ Your Answer", 
                                    placeholder="Write your answer here. Focus on explaining your reasoning...")
                    fb = gr.Textbox(lines=18, label="๐Ÿ“‹ Detailed Feedback", interactive=False)

                    with gr.Row():
                        gr.Button("๐ŸŽฒ Generate Question", variant="primary").click(
                            generate_question, [ps, pt, diff], [q, exp, mark]
                        )
                        gr.Button("โœ… Check Answer", variant="secondary").click(
                            check_answer, [q, exp, ans, ps], fb
                        )

        # โ”€โ”€โ”€โ”€โ”€ ADMIN PANEL โ”€โ”€โ”€โ”€โ”€
        with gr.Tab("๐Ÿ” Admin Panel"):
            with gr.Column() as login_section:
                gr.Markdown("### ๐Ÿ” Admin Login")
                pwd = gr.Textbox(label="Password", type="password", placeholder="Enter admin password")
                login_btn = gr.Button("๐Ÿ”“ Login", variant="primary")
                login_status = gr.Textbox(label="Status", interactive=False)
            
            with gr.Column(visible=False) as admin_section:
                gr.Markdown("""### ๐Ÿ“ค Upload Past Papers & Resources
                
**Instructions:**
1. **Title**: e.g., "Paper 2 Chemistry - June 2023"
2. **Subject**: Select Chemistry or Biology
3. **Content**: Add description, syllabus code (0620 Chemistry, 0610 Biology), or notes
4. **PDF**: Upload the actual past paper (questions will be auto-extracted)
5. **Insert**: Upload any accompanying insert/resource booklet

The AI will automatically:
- Identify paper details (year, series, variant)
- Extract all questions with topics
- Index them for student search
- Store insert materials for reference
""")
                
                with gr.Row():
                    with gr.Column():
                        t = gr.Textbox(label="๐Ÿ“‹ Title", placeholder="e.g., Paper 2 Chemistry - October/November 2023")
                        s = gr.Radio(["Chemistry", "Biology"], label="Subject", value="Chemistry")
                        c = gr.Textbox(lines=5, label="Content/Description", 
                                      placeholder="Add notes, syllabus code (0620/0610), or instructions...")
                        pdf = gr.File(label="๐Ÿ“„ Past Paper PDF (questions will be extracted)", file_types=[".pdf"])
                        insert = gr.File(label="๐Ÿ–ผ๏ธ Insert/Resource Booklet (optional)", 
                                        file_types=[".pdf", ".jpg", ".jpeg", ".png"])
                        
                        up = gr.Button("โฌ† Upload Paper", variant="primary", size="lg")
                        st = gr.Textbox(label="Upload Status", lines=4)
                        stats = gr.Textbox(label="๐Ÿ“Š Database Statistics", value="๐Ÿ“Š Status: No papers uploaded yet")
                    
                    with gr.Column():
                        gr.Markdown("### ๐Ÿ“š All Uploaded Papers")
                        lst = gr.Textbox(lines=26, label="Papers Database", value=get_papers_list(), 
                                        interactive=False, show_label=False)
                
                up.click(upload_paper, [t, s, c, pdf, insert], [st, lst, stats])
            
            login_btn.click(verify_admin_password, [pwd], [admin_section, login_section, login_status])

    gr.Markdown("""
    ---
    **System Status:** ๐ŸŸข Gemini AI (Primary) | ๐Ÿ”ต Cohere (Secondary) | ๐ŸŸข Z.ai (Tertiary) | ๐ŸŸฃ MiniMax (Fallback)
    
    **Key Features:**
    - ๐Ÿง  **Deep Understanding Focus**: AI emphasizes WHY, not just WHAT
    - ๐ŸŽฏ Smart question extraction and topic-based search
    - ๐Ÿ–ผ๏ธ Insert/resource support for diagrams and data
    - ๐Ÿ” Comprehensive concept explanations
    - ๐Ÿงฎ Step-by-step calculation support
    - ๐Ÿ”ฌ Experiment analysis with theory connections
    - ๐Ÿค– Multi-AI fallback system for reliability
    
    **Teaching Philosophy:**
    - Structure determines function
    - Understanding beats memorization
    - Connect concepts across topics
    - Apply knowledge to novel situations
    """)

app.launch()