Spaces:
Sleeping
Sleeping
File size: 41,335 Bytes
14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 21266a0 81f353b 14cbc25 81f353b 14cbc25 214386e 14cbc25 81f353b 14cbc25 81f353b 14cbc25 214386e 14cbc25 81f353b 14cbc25 81f353b 14cbc25 214386e 14cbc25 81f353b 14cbc25 81f353b 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 81f353b 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 4153280 14cbc25 214386e 4153280 214386e 4153280 214386e 4153280 214386e 4153280 214386e 4153280 214386e 4153280 214386e 4153280 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 81f353b 214386e 81f353b 14cbc25 214386e 81f353b 14cbc25 214386e 81f353b 14cbc25 214386e 81f353b 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 4153280 214386e 4153280 214386e 4153280 214386e 4153280 214386e 4153280 214386e 4153280 214386e 4153280 14cbc25 81f353b 214386e 4153280 14cbc25 214386e 14cbc25 214386e 14cbc25 4153280 14cbc25 4153280 14cbc25 4153280 214386e 14cbc25 214386e 4153280 14cbc25 214386e 4153280 214386e 4153280 214386e 4153280 214386e 14cbc25 4153280 14cbc25 4153280 214386e 3123760 14cbc25 81f353b 14cbc25 214386e 14cbc25 81f353b 14cbc25 81f353b 14cbc25 214386e 14cbc25 214386e 14cbc25 81f353b 14cbc25 214386e 14cbc25 81f353b 14cbc25 81f353b 14cbc25 214386e 14cbc25 81f353b 14cbc25 214386e 14cbc25 214386e 14cbc25 81f353b 14cbc25 214386e 14cbc25 81f353b 14cbc25 214386e 14cbc25 214386e 14cbc25 81f353b 14cbc25 214386e 14cbc25 81f353b 14cbc25 214386e 81f353b 14cbc25 81f353b 14cbc25 81f353b 214386e 14cbc25 81f353b 14cbc25 81f353b 14cbc25 214386e 14cbc25 81f353b 14cbc25 81f353b 14cbc25 81f353b 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e 14cbc25 214386e |
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 |
# --------------------------------------------------------------
# IGCSE Platform โ Enhanced Past Papers System with Question Search
# 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 = {} # Store insert images/documents
questions_index = [] # Index of all extracted questions
ADMIN_PASSWORD = "@mikaelJ46"
# ---------- 4. Topic lists ----------
geography_topics = [
"Population & Settlement", "Migration", "Urbanisation", "Rural Settlements",
"Natural Hazards: Earthquakes", "Natural Hazards: Volcanoes", "Natural Hazards: Tropical Storms",
"Rivers & Flooding", "Coasts & Coastal Management", "Weather & Climate",
"Climate Change", "Ecosystems: Tropical Rainforests", "Ecosystems: Hot Deserts",
"Economic Development", "Industry & Manufacturing", "Tourism", "Energy Resources",
"Water Resources", "Agriculture & Food Production", "Environmental Risks",
"Map Skills & Fieldwork", "GIS & Technology", "Sustainability"
]
history_topics = [
"WWI: Causes & Origins", "WWI: Key Events & Battles", "WWI: Treaty of Versailles",
"WWII: Causes", "WWII: Key Events", "WWII: Holocaust", "WWII: Impact & Consequences",
"Cold War: Origins", "Cold War: Key Events", "Cold War: Cuban Missile Crisis",
"Russian Revolution 1917", "Rise of Stalin", "Nazi Germany: Hitler's Rise",
"Nazi Germany: Policies", "Nazi Germany: Life in Nazi Germany",
"USA 1919-1941: Boom & Depression", "USA: New Deal", "USA: Civil Rights Movement",
"China: Communist Revolution", "China: Mao's China",
"Decolonisation", "United Nations", "Arab-Israeli Conflict",
"Vietnam War", "Historical Skills & Sources", "Causation & Consequence"
]
business_topics = [
"Business Activity & Types", "Business Objectives", "Stakeholders",
"Business Structure & Organisation", "Recruitment & Selection", "Training & Development",
"Motivation Theory", "Leadership & Management Styles", "Communication",
"Marketing: Market Research", "Marketing Mix: Product", "Marketing Mix: Price",
"Marketing Mix: Place", "Marketing Mix: Promotion", "Marketing Strategy",
"Production Methods", "Quality Management", "Location Decisions",
"Finance: Sources of Finance", "Finance: Cash Flow", "Finance: Break-even Analysis",
"Finance: Financial Statements", "Business Growth", "Economics: Supply & Demand",
"Economics: Market Structure", "International Business", "Business Ethics & Sustainability"
]
# ---------- 5. Enhanced PDF Processing with Question Extraction & Paper Intelligence ----------
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"""
# Extract first 2000 characters for analysis
sample_text = text[:2000] if len(text) > 2000 else text
prompt = f"""Analyze this IGCSE past paper and identify its details.
Filename: {filename}
Paper Text Sample:
{sample_text}
Identify and return ONLY a JSON object with:
- subject: The subject name (e.g., "Mathematics", "Physics", "Chemistry", "Biology", "Geography", "History", "Business")
- year: The year (e.g., "2023", "2022")
- series: The exam series (e.g., "June", "November", "March", "May/June")
- 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")
- syllabus_code: If visible (e.g., "0580", "0625", "0610")
Look for patterns like:
- "June 2023" or "November 2022"
- "Paper 2 Variant 1" or "Paper 21"
- Subject codes like "0580/21/M/J/23"
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}")
# Fallback to filename parsing
return parse_filename_for_details(filename)
def parse_filename_for_details(filename):
"""Fallback: Parse filename for paper details"""
import re
details = {
"subject": "Unknown",
"year": "Unknown",
"series": "Unknown",
"variant": "Unknown",
"paper_number": "Unknown",
"syllabus_code": "Unknown"
}
# Common patterns in IGCSE filenames
# e.g., "0580_w23_qp_21.pdf" or "Physics_June_2023_P2.pdf"
# Try to 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
# Try to 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)?|ON', filename):
details["series"] = "October/November"
elif re.search(r'[Mm]ar(ch)?|FM', filename):
details["series"] = "February/March"
# Try to 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)
# Try to extract syllabus code
code_match = re.search(r'\b(0\d{3})\b', filename)
if code_match:
details["syllabus_code"] = code_match.group(1)
# Map code to subject
code_subject_map = {
'0580': 'Mathematics', '0625': 'Physics', '0610': 'Biology',
'0620': 'Chemistry', '0460': 'Geography', '0455': 'Economics',
'0450': 'Business Studies', '0470': 'History'
}
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 (what IGCSE topic this relates to)
- requires_insert (true/false - does it reference Fig., Source, Table, etc?)
Return ONLY valid JSON array (no markdown):
[{{"question_number": "1(a)", "question_text": "...", "marks": 4, "topic": "...", "requires_insert": false}}, ...]
If text is incomplete, extract what you can find."""
try:
response, _ = ask_ai(prompt, temperature=0.2)
clean_txt = response.replace("```json", "").replace("```", "").strip()
questions = json.loads(clean_txt)
# Add metadata to each question including paper details
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}")
# Fallback: simple regex extraction
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 for question numbers like "1(a)", "2(b)(i)", "3", etc.
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\.|Source|Table|Insert|Photograph', q_text, re.IGNORECASE)),
'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':
# Extract text and try to get images from PDF
text = extract_text_from_pdf(insert_file)
return text, "pdf"
elif file_ext in ['jpg', 'jpeg', 'png', 'gif']:
# Store image
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. AI Tutor with Multi-Model Support ----------
def ai_tutor_chat(message, history, subject, topic):
if not message.strip():
return history
subject_context = {
"Geography": "You are an expert IGCSE Geography tutor. Focus on physical and human geography, case studies, map skills, and geographical skills. Use real-world examples and encourage critical thinking about global issues.",
"History": "You are an expert IGCSE History tutor. Focus on historical analysis, source evaluation, causation, consequence, and continuity/change. Encourage evidence-based arguments and historical context.",
"Business": "You are an expert IGCSE Business Studies tutor. Focus on business concepts, real-world applications, case studies, and analytical skills. Help students understand business decisions and their impacts."
}
system = f"""{subject_context[subject]}
Focus on {topic or 'any topic'}. Be encouraging, clear, and pedagogical.
Provide detailed explanations with examples and real-world applications.
Use case studies where appropriate and help students develop exam skills.
Encourage critical thinking and analytical skills."""
# Build conversation context
conversation = ""
for user_msg, bot_msg in history[-5:]:
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 not from Gemini
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. Case Study Generator ----------
def generate_case_study(subject, topic):
if not topic:
return "Select a topic first!"
prompt = f"""Create a detailed IGCSE {subject} case study for the topic: "{topic}".
The case study should include:
1. Real-world example or location (be specific)
2. Background context and key facts
3. Relevant data and statistics
4. Analysis points and key issues
5. Different perspectives/stakeholders
6. Impacts (economic, social, environmental where relevant)
7. Exam-style questions students could practice
Make it engaging, factual, and exam-relevant for IGCSE level."""
response, source = ask_ai(prompt, temperature=0.6)
if source in ["cohere", "zai", "minimax"]:
response = f"{response}\n\n_[Generated by {source.title()}]_"
return response
# ---------- 8. Source Analysis (for History) ----------
def analyze_source(source_text, question):
if not source_text.strip():
return "Enter a source to analyze."
prompt = f"""As an IGCSE History examiner, analyze this historical source:
SOURCE:
{source_text}
QUESTION: {question if question else "Evaluate the usefulness of this source for a historian studying this period."}
Provide:
1. **Content Analysis**: What information does the source provide?
2. **Provenance**: Origin, author, date, audience, purpose (if determinable)
3. **Reliability**: How reliable is this source? What are its limitations?
4. **Usefulness**: How useful is it for historians? What can/can't it tell us?
5. **Context**: What historical context is needed to understand this source?
6. **Cross-reference**: What other sources would help verify/challenge this?
Use IGCSE History exam criteria and techniques."""
response, source = ask_ai(prompt, temperature=0.4)
if source in ["cohere", "zai", "minimax"]:
response = f"{response}\n\n_[Analysis by {source.title()}]_"
return response
# ---------- 9. Business Calculator ----------
def business_calculator(calc_type, values):
"""Handle business calculations with AI explanation"""
calculations = {
"Break-even": "Fixed Costs รท (Selling Price - Variable Cost per unit)",
"Profit": "Total Revenue - Total Costs",
"Gross Profit Margin": "(Gross Profit รท Revenue) ร 100",
"Net Profit Margin": "(Net Profit รท Revenue) ร 100",
"Market Share": "(Company Sales รท Total Market Sales) ร 100",
"Return on Investment": "((Profit - Investment) รท Investment) ร 100"
}
prompt = f"""Explain how to calculate {calc_type} in IGCSE Business Studies.
Formula: {calculations.get(calc_type, "Standard business formula")}
Given values: {values}
Provide:
1. Step-by-step calculation
2. Clear explanation of what each component means
3. What the result tells us about the business
4. How this metric is used in business decisions
5. Limitations of this metric"""
response, source = ask_ai(prompt, temperature=0.3)
if source in ["cohere", "zai", "minimax"]:
response = f"{response}\n\n_[Explained by {source.title()}]_"
return response
# ---------- 10. Practice Questions (Enhanced with PDF context) ----------
def generate_question(subject, topic):
if not topic:
return "Select a topic!", "", ""
# Get relevant PDF content if available
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 material from {paper['title']}:\n{content[:3000]}"
question_guidance = {
"Geography": "Include command words (describe, explain, evaluate). May require case study knowledge or map interpretation.",
"History": "Include source material or ask about causation/consequence. Test historical analysis and evaluation skills.",
"Business": "Include a business scenario or data. Test application of business concepts and analytical skills."
}
prompt = f"""Create ONE high-quality IGCSE {subject} exam question on the topic: "{topic}".
{question_guidance[subject]}
{"Base the question style on this reference material:" + pdf_context if pdf_context else "Create an authentic exam-style question."}
The question should:
- Use appropriate exam command words
- Be worth 6-8 marks
- Test both knowledge and analysis/evaluation
- Include any necessary context or data
- Be answerable in 8-12 minutes
Return ONLY valid JSON (no markdown):
{{"question": "complete question with context", "expectedAnswer": "key points a good answer should include", "markScheme": "detailed marking criteria with mark allocations"}}"""
response, source = ask_ai(prompt, temperature=0.4)
try:
clean_txt = response.replace("```json", "").replace("```", "").strip()
data = json.loads(clean_txt)
return data["question"], data.get("expectedAnswer", ""), data.get("markScheme", "")
except Exception as e:
return response, "", f"Error: {e}"
def check_answer(question, expected, user_answer, subject):
if not user_answer.strip():
return "Write your answer first!"
subject_criteria = {
"Geography": "geographical knowledge, case study examples, use of terminology, analysis of causes/effects/solutions",
"History": "historical knowledge, use of sources/evidence, analysis of causation/consequence, balanced evaluation",
"Business": "business knowledge, application to scenarios, analysis using business concepts, justified recommendations"
}
prompt = f"""Evaluate this IGCSE {subject} answer using exam board criteria:
Question: {question}
Expected: {expected}
Student's answer:
{user_answer}
Assess based on: {subject_criteria[subject]}
Return JSON (no markdown):
{{"isCorrect": true/false, "score": 0-100, "marks": "X/8", "feedback": "detailed feedback on content and exam technique", "improvements": "specific suggestions", "strengths": "what was done well", "examTips": "exam-specific advice"}}"""
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.get('score', 'N/A')}% ({fb.get('marks', 'N/A')})
๐ Detailed Feedback:
{fb['feedback']}
โ
Your Strengths:
{fb.get('strengths', 'Good effort!')}
๐ How to Improve:
{fb['improvements']}
๐ก Exam Tips:
{fb.get('examTips', 'Keep practicing!')}"""
if source in ["cohere", "zai", "minimax"]:
result += f"\n\n_[Graded by {source.title()}]_"
return result
except Exception:
return response
# ---------- 11. Enhanced Past Papers System with Paper Intelligence ----------
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!"
# Filter questions
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."
# Format output with paper details
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 ""
# Enhanced paper info display
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]{insert_note}
{q['question_text']}
{'โ'*80}
"""
return result
def get_paper_insert(paper_id):
"""Retrieve insert for a specific paper"""
if paper_id in insert_storage:
insert_data, insert_type = insert_storage[paper_id]
if insert_type == "image":
return insert_data, "Image insert loaded โ
"
elif insert_type == "pdf":
return None, f"Insert text:\n\n{insert_data[:2000]}..."
return None, "No insert available for this paper"
def view_papers_student(subject):
"""View all papers for a subject with detailed information"""
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']])
# Get paper details
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 with Enhanced Paper Processing ----------
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):
"""Enhanced upload with AI-powered paper identification and question 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
# Process PDF
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"):
# Use AI to identify paper details
paper_details = identify_paper_details(pdf_text, pdf_file.name)
pdf_content_storage[paper_id] = pdf_text
# Add paper details to content
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]"
# Process Insert
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}]"
# Add paper to storage
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")
})
# Extract questions from PDF using AI
status_msg = "โ
Paper uploaded!"
if pdf_text and not pdf_text.startswith("Error"):
status_msg += "\nโณ AI is extracting questions and analyzing paper structure..."
questions = extract_questions_from_text(pdf_text, paper_id, title, subject, paper_details)
questions_index.extend(questions)
# Enhanced status with paper details
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 with detailed information"""
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 Platform") as app:
gr.Markdown("""
# ๐ IGCSE Learning Platform
Geography ๐ | History ๐ | Business ๐ผ
_Powered by Gemini AI with intelligent multi-model fallback system_
""")
with gr.Tabs():
# โโโโโ STUDENT PORTAL โโโโโ
with gr.Tab("๐จโ๐ Student Portal"):
with gr.Tabs():
# AI TUTOR
with gr.Tab("๐ค AI Tutor"):
gr.Markdown("### Chat with Your AI Tutor\n*Get expert help in Geography, History, or Business*")
with gr.Row():
subj = gr.Radio(["Geography", "History", "Business"], label="Subject", value="Geography")
topc = gr.Dropdown(geography_topics, label="Topic (optional)", allow_custom_value=True)
def update_topics(s):
topics = {"Geography": geography_topics, "History": history_topics, "Business": business_topics}
return gr.Dropdown(choices=topics[s], value=None)
subj.change(update_topics, subj, topc)
chat = gr.Chatbot(height=450, show_label=False)
txt = gr.Textbox(placeholder="Ask anything... e.g., 'Explain causes of earthquakes' or 'What led to WWI?'", 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)
# PAST PAPERS BROWSER - NEW!
with gr.Tab("๐ Past Papers Browser"):
gr.Markdown("""### ๐ฏ Search Real Exam Questions by Topic
*Find actual IGCSE questions from uploaded past papers*""")
with gr.Row():
pp_subject = gr.Radio(["Geography", "History", "Business"], label="Subject", value="Geography")
pp_topic = gr.Dropdown(geography_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(["Geography", "History", "Business"], label="Subject", value="Geography")
papers_display = gr.Markdown(label="Available Papers")
gr.Button("๐ Show All Papers").click(view_papers_student, browse_subject, papers_display)
# CASE STUDIES
with gr.Tab("๐ Case Studies"):
gr.Markdown("### Generate Detailed Case Studies")
with gr.Row():
cs_subj = gr.Radio(["Geography", "History", "Business"], label="Subject", value="Geography")
cs_topic = gr.Dropdown(geography_topics, label="Topic")
cs_subj.change(update_topics, cs_subj, cs_topic)
cs_output = gr.Textbox(lines=20, label="Case Study", interactive=False)
gr.Button("๐ Generate Case Study", variant="primary").click(
generate_case_study, [cs_subj, cs_topic], cs_output
)
# SOURCE ANALYSIS (History)
with gr.Tab("๐ Source Analysis"):
gr.Markdown("### Historical Source Analysis Tool")
source_text = gr.Textbox(lines=8, label="๐ Paste Historical Source",
placeholder="Paste the source text here (e.g., extract from a speech, letter, document...)")
source_q = gr.Textbox(label="โ Question (optional)",
placeholder="e.g., 'How useful is this source for understanding...?'")
source_output = gr.Textbox(lines=16, label="Analysis", interactive=False)
gr.Button("๐ Analyze Source", variant="primary").click(
analyze_source, [source_text, source_q], source_output
)
# BUSINESS CALCULATOR
with gr.Tab("๐ฐ Business Calculator"):
gr.Markdown("### Business Calculations & Explanations")
calc_type = gr.Dropdown(
["Break-even", "Profit", "Gross Profit Margin", "Net Profit Margin",
"Market Share", "Return on Investment"],
label="Calculation Type"
)
calc_values = gr.Textbox(lines=3, label="Values",
placeholder="e.g., Fixed Costs: $10,000, Price: $50, Variable Cost: $30")
calc_output = gr.Textbox(lines=14, label="Calculation & Explanation", interactive=False)
gr.Button("๐งฎ Calculate & Explain", variant="primary").click(
business_calculator, [calc_type, calc_values], calc_output
)
# PRACTICE QUESTIONS
with gr.Tab("โ Practice"):
gr.Markdown("### Generate & Practice Exam Questions")
with gr.Row():
ps = gr.Radio(["Geography", "History", "Business"], label="Subject", value="Geography")
pt = gr.Dropdown(geography_topics, label="Topic")
ps.change(update_topics, ps, pt)
q = gr.Textbox(label="๐ Question", lines=6, interactive=False)
exp = gr.Textbox(label="Expected Answer Points", lines=3, visible=False)
mark = gr.Textbox(label="๐ Mark Scheme", lines=4, interactive=False)
ans = gr.Textbox(lines=10, label="โ Your Answer", placeholder="Type your answer here...")
fb = gr.Textbox(lines=14, label="๐ Feedback", interactive=False)
with gr.Row():
gr.Button("๐ฒ Generate Question", variant="primary").click(
generate_question, [ps, pt], [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 Geography - June 2023"
2. **Subject**: Select Geography, History, or Business
3. **Content**: Add description, syllabus code, or notes
4. **PDF**: Upload the actual past paper (questions will be auto-extracted)
5. **Insert**: Upload any accompanying insert/resource booklet (images, maps, sources, etc.)
The system will automatically:
- Extract all questions from the PDF
- Index them by topic for student search
- Store insert materials for reference
""")
with gr.Row():
with gr.Column():
t = gr.Textbox(label="๐ Title", placeholder="e.g., Paper 2 - June 2023")
s = gr.Radio(["Geography", "History", "Business"], label="Subject", value="Geography")
c = gr.Textbox(lines=5, label="Content/Description",
placeholder="Add notes, syllabus code, 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=3)
stats = gr.Textbox(label="๐ Database Statistics", value="๐ Status: No papers uploaded yet")
with gr.Column():
gr.Markdown("### ๐ All Uploaded Papers")
lst = gr.Textbox(lines=24, 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)
**Features:**
- ๐ฏ Smart question extraction from past papers
- ๐ผ๏ธ Insert/resource support for visual materials
- ๐ Topic-based question search
- ๐ค Multi-AI fallback system for reliability
""")
app.launch() |