# ======================================================== # UNEB Primary 7/6 Exam Preparation Platform # Multi-AI System: Gemini (Primary) → Cohere → HF (Fallback) # ======================================================== import os import json from datetime import datetime import gradio as gr import time from io import BytesIO from PIL import Image import csv from pathlib import Path import re # ---------- 1. Configure AI Systems ---------- try: import google.generativeai as genai genai.configure(api_key=os.getenv("GEMINI_API_KEY")) gemini_model = genai.GenerativeModel('gemini-2.0-flash-exp') print("✓ Gemini AI initialized (PRIMARY with Vision)") except Exception as e: print(f"✗ Gemini Error: {e}") gemini_model = None try: import cohere cohere_client = cohere.Client(os.getenv("COHERE_API_KEY")) print("✓ Cohere initialized (SECONDARY)") except Exception as e: print(f"✗ Cohere Error: {e}") cohere_client = None try: from huggingface_hub import InferenceClient hf_client = InferenceClient(api_key=os.environ.get("HF_TOKEN")) print("✓ Hugging Face initialized (FALLBACK)") except Exception as e: print(f"✗ HF Error: {e}") hf_client = None def ask_ai(prompt, temperature=0.7, max_retries=2, image=None): """Try models: Gemini → Cohere → HF If image provided, only Gemini can process it""" # Try Gemini first (Primary) - handles both text and images if gemini_model: for attempt in range(max_retries): try: if image: response = gemini_model.generate_content( [prompt, image], generation_config=genai.types.GenerationConfig( temperature=temperature, ) ) else: response = gemini_model.generate_content( prompt, generation_config=genai.types.GenerationConfig( temperature=temperature, ) ) return response.text, "gemini" except Exception as e: print(f"✗ Gemini attempt {attempt+1} failed: {e}") if attempt < max_retries - 1: time.sleep(1) if image: return " Image analysis requires Gemini AI. Please check API configuration.", "error" # Try Cohere (Secondary) - text only 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: print(f"✗ Cohere attempt {attempt+1} failed: {e}") if attempt < max_retries - 1: time.sleep(1) # Try Hugging Face (Fallback) - text only if hf_client: try: completion = hf_client.chat.completions.create( model="Qwen/Qwen2.5-72B-Instruct", messages=[{"role": "user", "content": prompt}], temperature=temperature, max_tokens=2000 ) return completion.choices[0].message.content, "hf" except Exception as e: print(f"✗ HF failed: {e}") return " All AI services failed. Please try again later.", "error" # ---------- 3. Uganda Primary Curriculum Syllabus (P6 & P7 Only, by Subject) ---------- # Topics below are expanded and aligned to typical NCDC/UNEB primary curriculum themes # (see https://ncdc.go.ug/ and Ministry of Education resources for official documents). syllabus_topics = { "Primary 6": { "Mathematics": [ "Whole Numbers - Addition & Subtraction", "Whole Numbers - Multiplication & Division", "Factors, Multiples and Prime Numbers", "Fractions & Decimals", "Money & Making Change", "Measurement - Length, Mass & Capacity", "Time - Hours, Minutes & Conversion", "Geometry - Shapes, Symmetry & Angles (basic)", "Data Handling - Tables, Bar Graphs & Pictograms", "Ratio & Proportion", "Introduction to Algebra - Simple Equations", "Basic Percentages and Problem Solving" ], "English": [ "Reading Comprehension - Short Passages", "Grammar - Tenses, Parts of Speech, Sentence Structure", "Vocabulary Building & Spelling", "Composition - Story and Letter Writing", "Punctuation & Capitalization", "Clarity in Expression - Cohesion and Coherence", "Cloze Tests & Short Answer Questions", "Listening and Speaking Basics" ], "Social Studies": [ "Local Community - Roles, Services & Leaders", "Local History & Traditions", "Civics - Rights, Responsibilities & Good Citizenship", "Map Skills - Directions, Symbols, Scale (basic)", "Resources and Local Economy - Farming, Trade, Markets", "Culture, Customs and Heritage", "Environment & Conservation - Local Examples", "Health, Sanitation and Community Wellbeing", "Basic Local Government Structures and Participation", "Road Safety and Community Rules" ], "Science": [ "Living & Non-Living Things - Characteristics", "Plants - Parts and Functions", "Animals - Habitats and Adaptations", "Human Body - Health, Nutrition & Hygiene", "Materials & Their Properties", "Forces and Motion - Simple Examples", "Light, Heat and Sound (basic concepts)", "Environment and Natural Resources", "Simple Experiments and Observations" ] }, "Primary 7": { "Mathematics": [ "Integers & Operations", "Fractions - Addition, Subtraction, Multiplication & Division", "Decimals & Percentages", "Ratio, Rate & Proportion", "Algebraic Expressions & Simple Equations", "Geometry - Angles, Triangles and Quadrilaterals", "Mensuration - Area, Perimeter and Volume (basic)", "Statistics & Probability - Averages and Data Interpretation", "Coordinate Geometry - Introduction", "Number Theory - Factors, HCF & LCM", "Problem Solving Strategies" ], "English": [ "Comprehension - Longer Passages & Questioning", "Grammar - Sentence Transformation, Tenses, Agreement", "Composition - Stories, Letters, Reports and Dialogues", "Cloze & Summary Writing", "Vocabulary - Synonyms, Antonyms & Contextual Use", "Listening Skills and Oral Expression", "Directed Writing and Examination Techniques" ], "Social Studies": [ "History - Key Events in Uganda and East Africa (pre-colonial, colonial, independence)", "Civics and Governance - Structure of Government, Roles and Rights", "Geography - Maps, Physical Features, Weather, Climate and Resources", "Economy - Agriculture, Trade, Markets, Production and Consumption", "Community Development - Projects, Participation and Leadership", "Citizenship Education - Rights, Responsibilities and Human Rights", "Culture, National Symbols and Heritage", "Local and National Government - Functions and Services", "Environmental Issues - Conservation, Deforestation, Pollution", "Global Connections - Trade, Aid and Regional Cooperation" ], "Science": [ "Living Things - Classification, Life Cycles and Ecosystems", "Plants and Animals - Structure and Function", "Human Body Systems - Digestive, Respiratory, Circulatory (basic)", "Health and Disease Prevention", "Forces, Magnets and Motion", "Energy - Sources and Uses", "Materials and Their Uses (including mixtures and separation)", "Environment - Habitats, Conservation and Sustainable Use", "Simple Scientific Investigation and Reporting" ] } } # ---------- 4. Generate Practice Questions (Customizable) ---------- def generate_sample_questions(grade_level, subject, topic, num_questions=10): """Fallback: Generate sample questions locally when AI is unavailable. This function is subject-aware and supplies simple sample questions for common topics.""" # Basic sample banks keyed by subject/topic samples = { "Mathematics": { "Integers & Operations": [ "Q1. Calculate the sum of -20 and -15.", "Q2. A farmer bought 40 oranges and then sold 25 of them. What is the difference between the number of oranges bought and sold?", "Q3. Simplify: 36 - (-10) + 5.", "Q4. Find the value of -2(8) + 15.", "Q5. A car is parked at -10 meters. If it moves up 15 meters, what is its final position?", "Q6. Calculate the product of -4 and 5.", "Q7. A boat descends 12 meters, then rises 8 meters. What is its final position relative to sea level?", "Q8. Simplify: 2(-3) + 5(-2).", "Q9. A rabbit hops 7 meters forward and then 4 meters backward. What is the net distance covered?", "Q10. A plane descends 300 meters and then ascends 200 meters. What is the plane's final position?", ], "Fractions - Addition & Subtraction": [ "Q1. Add 1/4 and 1/3.", "Q2. Subtract 2/5 from 3/5.", "Q3. What is 1/2 + 1/4 + 1/8?", "Q4. Calculate 7/8 - 1/4.", "Q5. Find the sum of 2/3 and 1/6.", "Q6. Subtract 3/10 from 9/10.", "Q7. Add 1/5, 2/5, and 1/5.", "Q8. What is 5/6 - 1/3?", "Q9. Calculate 3/4 + 2/8.", "Q10. Find 11/12 - 1/4.", ], }, "English": { "Comprehension - Passages & Questions": [ "Q1. Read the passage and answer: What is the main idea of paragraph 2?", "Q2. From the passage, extract two reasons the author gives for saving water.", "Q3. What does the word 'frugal' mean in the passage?", "Q4. Give a title for the passage in not more than five words.", "Q5. Why did the character decide to leave home?", ], "Grammar - Sentence Transformation & Tenses": [ "Q1. Change to passive voice: 'The teacher marked the tests.'", "Q2. Fill in the blank with the correct tense: 'She ___ (go) to school yesterday.'", "Q3. Correct the sentence: 'He don't like vegetables.'", "Q4. Combine the sentences: 'He ran fast. He missed the bus.'", "Q5. Rewrite in reported speech: 'She said, \"I will come.\"'", ] }, "Science": { "Living Things - Classification": [ "Q1. State two differences between plants and animals.", "Q2. Name three groups of living organisms.", "Q3. How do leaves help plants to survive?", "Q4. What is photosynthesis? Give a simple definition.", "Q5. Explain why animals need oxygen.", ], "Forces, Magnets and Motion": [ "Q1. Define force with an example.", "Q2. What does a magnet attract?", "Q3. Give one example of a push and one example of a pull.", ] }, "Social Studies": { "History - Uganda & East Africa": [ "Q1. Name one important event in Uganda's history and explain why it is important.", "Q2. Who was the first person to unite (example) ...?", ], "Geography - Maps, Weather & Resources": [ "Q1. Give two uses of a map.", "Q2. What are the main types of weather in Uganda?", ] } } subject_bank = samples.get(subject, {}) # Try to match topic within subject bank topic_key = None for key in subject_bank: if key.lower() in topic.lower() or topic.lower() in key.lower(): topic_key = key break if topic_key: qs = subject_bank[topic_key][:num_questions] else: # Generic fallback per subject qs = [f"Q{i+1}. Sample {subject} question {i+1} on {topic}." for i in range(num_questions)] return "\n\n".join(qs) def generate_practice_questions(grade_level, subject, topic, num_questions=10): """Generate multiple questions with robust text parsing (not JSON). Subject-aware prompt.""" if not topic: return None, "⚠️ Please select a topic first!" prompt = f"""Generate exactly {num_questions} UNEB-style {subject} questions for {grade_level} students on: "{topic}" Format your response EXACTLY like this: Q1. [Question text here] Q2. [Question text here] Q3. [Question text here] ... and so on up to Q{num_questions} Each question should be: - Clear and exam-like - Appropriate difficulty for {grade_level} - Self-contained (includes all necessary information) - Solvable in 2-5 minutes for short-answer questions Start immediately with Q1. Do not include any introduction or explanation. """ response, source = ask_ai(prompt, temperature=0.6) # Fallback: if AI fails or returns empty, use local generator (now subject-aware) if "All AI services failed" in response or not response or response.strip() == "": response = generate_sample_questions(grade_level, subject, topic, num_questions) # Parse questions robustly using regex to support multi-digit numbers (Q1..Q10..) questions = [] current_question = None q_re = re.compile(r'^Q(\d+)\.?\s*(.*)', re.IGNORECASE) for raw_line in response.splitlines(): line = raw_line.strip() if not line: continue m = q_re.match(line) if m: # Start of a new question if current_question: questions.append(current_question.strip()) # Include the rest of the line after the Qn. prefix rest = m.group(2) or '' current_question = f"Q{m.group(1)}. {rest.strip()}" else: # Continuation of previous question (append with space) if current_question: current_question += ' ' + line if current_question: questions.append(current_question.strip()) # Ensure we got questions if not questions: return None, " Failed to generate questions. Please try again." questions = questions[:num_questions] # Format for display formatted = "\n\n".join(questions) return questions, formatted # ---------- 5. Grade/Mark Student Answers ---------- def grade_student_answers(questions, student_answers, grade_level, topic, subject=None): """Grade all student answers and provide AI feedback - NO STRICT FORMAT REQUIRED""" if not questions: return " No questions to grade." feedback_list = [] for i, question_text in enumerate(questions): student_answer = student_answers.get(i, None) # No answer submitted for this question if student_answer is None: # Ask AI for a short guidance/hint for this question (best-effort) hint_prompt = f"""You are an experienced UNEB examiner for {grade_level} students. Question {i+1}: {question_text} The student did not submit an answer. Provide a very brief (2-3 line) approach or hint that would help the student answer this question.""" hint_resp, _ = ask_ai(hint_prompt, temperature=0.3) if not hint_resp or "All AI services failed" in hint_resp: hint_resp = "No answer submitted. Expected: show step-by-step working and final answer; include units where applicable." feedback_list.append(f"Q{i+1}: [NOT ANSWERED]\nGuidance: {hint_resp}\n") continue # Handle image answers (canvas/uploaded photos) try: from PIL import Image as PILImage is_image = isinstance(student_answer, PILImage.Image) except Exception: is_image = False if is_image: grading_prompt = f"""You are an experienced UNEB examiner for {grade_level} students. Subject: {subject or 'General'} Question {i+1}: {question_text} The student submitted handwritten work as an image. Analyse the image, extract the student's answer for this question, and then: - State whether the answer is correct, partially correct, or incorrect - Provide a mark/score - Give brief constructive feedback and the correct approach if needed If you cannot analyse images, state that image analysis requires a vision-capable model.""" grade_response, _ = ask_ai(grading_prompt, temperature=0.3, image=student_answer) feedback_list.append(f"Q{i+1}:\n{grade_response}\n") continue # Treat as text answer text_ans = str(student_answer).strip() if not text_ans: # empty string case hint_prompt = f"""You are an experienced UNEB examiner for {grade_level} students. Question {i+1}: {question_text} The student submitted an empty/text-missing answer. Provide a very brief (2-3 line) approach or hint that would help the student answer this question.""" hint_resp, _ = ask_ai(hint_prompt, temperature=0.3) if not hint_resp or "All AI services failed" in hint_resp: hint_resp = "No answer submitted. Expected: show step-by-step working and final answer; include units where applicable." feedback_list.append(f"Q{i+1}: [NOT ANSWERED]\nGuidance: {hint_resp}\n") continue grading_prompt = f"""You are an experienced UNEB examiner for {grade_level} students. Subject: {subject or 'General'} Question {i+1}: {question_text} Student's Answer: {text_ans} Evaluate this answer: - Is it correct, partially correct, or incorrect? - Provide a mark/score - Give feedback explaining what's good and what needs improvement - If incorrect, briefly explain the correct approach Accept answers in ANY format/style as long as they address the question. Be constructive and encouraging.""" grade_response, _ = ask_ai(grading_prompt, temperature=0.3) feedback_list.append(f"Q{i+1}:\n{grade_response}\n") full_feedback = "\n" + "="*60 + "\n".join(feedback_list) return full_feedback # ---------- 6. Session Tracking ---------- class StudentSession: def __init__(self, student_name="Student"): self.student_name = student_name self.current_questions = [] self.current_answers = {} self.current_topic = None self.current_grade = None self.last_feedback = None self.correction_conversation = [] # list of {'role':'user'|'assistant','content':str} def clear_session(self): self.current_questions = [] self.current_answers = {} self.current_topic = None self.current_grade = None self.last_feedback = None session = StudentSession() # In-memory submissions store for student results submissions_storage = [] # ---------- 7. Download Functions ---------- def download_questions_file(questions_list, topic, grade_level, subject=None): """Download questions as text file""" if not questions_list: return None def sanitize_filename(name: str) -> str: # Replace unsafe characters with underscore return re.sub(r"[^A-Za-z0-9_.()-]", "_", str(name)) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") safe_grade = sanitize_filename(grade_level) safe_topic = sanitize_filename(topic) safe_subject = sanitize_filename(subject) if subject else "" subject_tag = f"_{safe_subject}" if safe_subject else "" filename = f"questions_{safe_grade}{subject_tag}_{safe_topic}_{timestamp}.txt" content = f"""UNEB EXAM PRACTICE QUESTIONS {'='*50} Grade Level: {grade_level} Subject: {subject or 'N/A'} Topic: {topic} Generated: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Total Questions: {len(questions_list)} {'='*50} """ for question in questions_list: content += question + "\n\n" content += f""" {'='*50} INSTRUCTIONS: 1. Write your working clearly for each question 2. Show all steps in your solution 3. Once complete, submit to AI for correction 4. Review the feedback to improve Good luck! """ filepath = os.path.join("downloads", filename) os.makedirs("downloads", exist_ok=True) with open(filepath, "w", encoding="utf-8") as f: f.write(content) return os.path.abspath(filepath) def download_questions_pdf(questions_list, topic, grade_level, subject=None): """Generate a simple PDF with the questions, return filepath.""" try: from reportlab.lib.pagesizes import A4 from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer from reportlab.lib.units import mm except Exception: return None def sanitize_filename(name: str) -> str: return re.sub(r"[^A-Za-z0-9_.()-]", "_", str(name)) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") safe_grade = sanitize_filename(grade_level) safe_topic = sanitize_filename(topic) safe_subject = sanitize_filename(subject) if subject else "" subject_tag = f"_{safe_subject}" if safe_subject else "" filename = f"questions_{safe_grade}{subject_tag}_{safe_topic}_{timestamp}.pdf" filepath = os.path.join("downloads", filename) os.makedirs("downloads", exist_ok=True) doc = SimpleDocTemplate(filepath, pagesize=A4, rightMargin=20*mm, leftMargin=20*mm, topMargin=20*mm, bottomMargin=20*mm) styles = getSampleStyleSheet() normal = styles['Normal'] heading = ParagraphStyle('Heading', parent=styles['Heading1'], alignment=0) elems = [] elems.append(Paragraph("UNEB EXAM PRACTICE QUESTIONS", styles['Title'])) elems.append(Spacer(1, 4*mm)) meta = f"Grade Level: {grade_level} Subject: {subject or 'N/A'} Topic: {topic} Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" elems.append(Paragraph(meta, normal)) elems.append(Spacer(1, 6*mm)) for i, q in enumerate(questions_list): # Remove any leading Qn. if present q_text = re.sub(r'^Q\d+\.?\s*', '', q) elems.append(Paragraph(f"Q{i+1}. {q_text}", normal)) elems.append(Spacer(1, 3*mm)) try: doc.build(elems) return os.path.abspath(filepath) except Exception: return None def download_feedback_file(feedback_text, topic, grade_level, subject=None): """Download AI feedback/corrections""" if not feedback_text: return None def sanitize_filename(name: str) -> str: return re.sub(r"[^A-Za-z0-9_.()-]", "_", str(name)) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") safe_grade = sanitize_filename(grade_level) safe_topic = sanitize_filename(topic) safe_subject = sanitize_filename(subject) if subject else "" subject_tag = f"_{safe_subject}" if safe_subject else "" filename = f"feedback_{safe_grade}{subject_tag}_{safe_topic}_{timestamp}.txt" content = f"""AI CORRECTION & FEEDBACK {'='*50} Student: {session.student_name} Grade Level: {grade_level} Subject: {subject or 'N/A'} Topic: {topic} Date: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} {'='*50} {feedback_text} {'='*50} Review this feedback carefully to understand where you went wrong. Practice similar questions to strengthen this topic. """ filepath = os.path.join("downloads", filename) os.makedirs("downloads", exist_ok=True) with open(filepath, "w", encoding="utf-8") as f: f.write(content) return os.path.abspath(filepath) # ---------- 8. Gradio UI ---------- css_styles = """ .header-section { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 30px; border-radius: 15px; color: white; margin-bottom: 20px; } .tab-content { padding: 20px; } .question-display { background: #ffffff; padding: 25px; border-radius: 10px; border-left: 5px solid #667eea; margin: 15px 0; color: #000000 !important; font-size: 22px !important; font-weight: 700 !important; line-height: 1.9 !important; overflow-wrap: break-word; } .question-display ol, .question-display li { color: #000000 !important; font-size: 22px !important; font-weight: 700 !important; line-height: 1.9 !important; margin: 14px 0 !important; } .feedback-box { background: #ffffff; padding: 25px; border-radius: 10px; border-left: 5px solid #10b981; color: #000000 !important; font-size: 20px !important; font-weight: 700 !important; line-height: 1.9 !important; overflow-wrap: break-word; } .answer-section { background: #fef9e7; padding: 20px; border-radius: 10px; margin: 20px 0; } /* Significantly larger text for iPad and tablets */ @media (max-width: 1024px) { .question-display { font-size: 26px !important; padding: 30px !important; } .question-display ol, .question-display li { font-size: 26px !important; } .feedback-box { font-size: 24px !important; padding: 30px !important; } } /* Extra large text for small phones */ @media (max-width: 600px) { .question-display { font-size: 28px !important; padding: 30px !important; } .question-display ol, .question-display li { font-size: 28px !important; } } """ with gr.Blocks(title="UNEB Exam Prep - Primary 6 & 7", theme=gr.themes.Soft(), css=css_styles) as app: gr.Markdown(""" # UNEB Exam Practice ## Primary 6 & 7 — Multiple Subjects """) # Global state for questions questions_state = gr.State(value=[]) # Student name input with gr.Group(): student_name_input = gr.Textbox( label="Your Name", placeholder="Enter your name", value="Student" ) def update_student_name(name): session.student_name = name if name else "Student" return f"Welcome, {session.student_name}!" student_name_input.change(update_student_name, student_name_input, None) # Grade, Subject, Topic selection (shared across all tabs) with gr.Group(): gr.Markdown("### Choose Your Topic") with gr.Row(): grade_select = gr.Dropdown( choices=["Primary 6", "Primary 7"], label="Grade Level", value="Primary 7" ) subject_select = gr.Dropdown( choices=["Mathematics", "English", "Social Studies", "Science"], label="Subject", value="Mathematics" ) topic_select = gr.Dropdown( label="Topic", choices=syllabus_topics["Primary 7"]["Mathematics"], value=syllabus_topics["Primary 7"]["Mathematics"][0] ) # Update topics when grade or subject changes def update_topics_shared(grade, subject): topics = syllabus_topics.get(grade, {}).get(subject, []) if not topics: topics = ["General - " + subject] return gr.Dropdown(choices=topics, value=topics[0]) grade_select.change(update_topics_shared, [grade_select, subject_select], topic_select) subject_select.change(update_topics_shared, [grade_select, subject_select], topic_select) # Shared question display (placed below name and above the tabs to avoid large empty header) questions_display = gr.HTML(value="", elem_classes="question-display") with gr.Tabs(): # ===== TAB 1: GENERATE QUESTIONS ===== with gr.Tab("1️⃣ Generate Questions"): gr.Markdown("### Step 1: Generate Practice Questions\n\nSet the Number of Questions (1–100) and click Generate.") with gr.Row(): num_questions_input = gr.Slider(minimum=1, maximum=100, step=1, value=20, label="Number of Questions") generate_btn = gr.Button(" Generate Questions", variant="primary", size="lg") # (questions_display is shared above - no local questions_output here) status_output = gr.Textbox( label="Status", interactive=False, lines=1 ) with gr.Row(): download_questions_btn = gr.DownloadButton(" Download Questions (PDF)") copy_btn = gr.Button(" Copy Questions") # Generate questions handler def generate_and_display(grade, subject, topic, num_questions): questions_list, formatted_text = generate_practice_questions(grade, subject, topic, num_questions=num_questions) if questions_list is None: return "", questions_state.value, " Generation failed" session.current_questions = questions_list session.current_grade = grade session.current_topic = topic # Also store subject session.current_subject = subject # Build accessible HTML list with bold black text for iPad/Safari visibility items_html = "".join([f"