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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import datetime
|
| 4 |
+
from groq import Groq
|
| 5 |
+
from docx import Document
|
| 6 |
+
from docx.shared import Pt
|
| 7 |
+
from tempfile import NamedTemporaryFile
|
| 8 |
+
|
| 9 |
+
# ✅ Groq API Key (ensure it's set in your environment)
|
| 10 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 11 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _call_groq(model_name, prompt, temperature=0.7, max_tokens=1500):
|
| 15 |
+
"""Helper to call Groq chat completions and return text."""
|
| 16 |
+
response = client.chat.completions.create(
|
| 17 |
+
model=model_name,
|
| 18 |
+
messages=[{"role": "user", "content": prompt}],
|
| 19 |
+
temperature=temperature,
|
| 20 |
+
)
|
| 21 |
+
return response.choices[0].message.content.strip()
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def generate_lesson_plan(
|
| 25 |
+
class_, subject, topic, students, duration, date, teacher, theme, assessments
|
| 26 |
+
):
|
| 27 |
+
"""
|
| 28 |
+
Generate bilingual (English + Urdu) BOPPPS-style lesson plan:
|
| 29 |
+
- 3 SMART SLOs aligned with Pakistan National Curriculum (GB context)
|
| 30 |
+
- Content to be delivered (definitions, key terms, key concepts)
|
| 31 |
+
- Home Assignment aligned with SLOs
|
| 32 |
+
- Feedback area for Mentor/Principal (blank space)
|
| 33 |
+
- Optional: generate selected assessments (Pre/Post/Flashcards/Quizzes/Short response/Fill-in/Match/MCQs)
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
# Prepare English prompt
|
| 37 |
+
english_prompt = f"""
|
| 38 |
+
You are an expert lesson-plan designer familiar with the National Curriculum of Pakistan and teaching context of Gilgit-Baltistan.
|
| 39 |
+
Using the BOPPPS approach, create a focused lesson plan for the following inputs:
|
| 40 |
+
|
| 41 |
+
Class: {class_}
|
| 42 |
+
Subject: {subject}
|
| 43 |
+
Topic: {topic}
|
| 44 |
+
Number of students: {students}
|
| 45 |
+
Duration (minutes): {duration}
|
| 46 |
+
Date: {date}
|
| 47 |
+
Teacher: {teacher}
|
| 48 |
+
|
| 49 |
+
Requirements (English):
|
| 50 |
+
1) Provide exactly THREE SMART Student Learning Outcomes (SLOs). Each must be Specific, Measurable, Achievable, Relevant, Time-bound and explicitly mention an observable student behavior or product.
|
| 51 |
+
2) After the SLOs add a major heading "Content to be delivered" and under it provide:
|
| 52 |
+
- Short definition(s) relevant to the topic
|
| 53 |
+
- Key terms (with one-line explanations)
|
| 54 |
+
- Key concepts (bullet points that teachers will use to help students achieve the SLOs)
|
| 55 |
+
3) Add a heading "Home Assignment" with tasks aligned to the SLOs.
|
| 56 |
+
4) Add a final heading "Feedback by the Mentor/Principal" and leave space or lines for handwritten feedback.
|
| 57 |
+
5) When asked for assessments (see list below), produce them aligned to the topic and SLOs:
|
| 58 |
+
- Pre-assessment: 5 short diagnostic items
|
| 59 |
+
- Post-assessment: 5 items with an answer key
|
| 60 |
+
- Flash cards: 8 cards (term/question — answer)
|
| 61 |
+
- Quiz (short): a set of up to 6 items mixing MCQs and short response
|
| 62 |
+
- Short Response: 5 open-ended items
|
| 63 |
+
- Fill-in-the-blank: 6 items
|
| 64 |
+
- Match the column: 6 pairs
|
| 65 |
+
- MCQs: 8 MCQs with 4 options each and answer key
|
| 66 |
+
|
| 67 |
+
Produce only the sections requested. For sections not requested, do not produce content. Be concise but teacher-friendly.
|
| 68 |
+
Return clean plain text (no JSON). Use clear headings and bullets.
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
# Prepare Urdu prompt (Urdu-only output; use Urdu script)
|
| 72 |
+
urdu_prompt = f"""
|
| 73 |
+
آپ پاکستان کے قومی نصاب اور گلگت بلتستان کے تعلیمی سیاق و سباق سے واقف ماہر معلم ہیں۔
|
| 74 |
+
ذیل میں دی گئی معلومات کے لئے BOPPPS انداز میں ایک مکمل روزانہ تدریسی منصوبہ اردو زبان میں تیار کریں:
|
| 75 |
+
|
| 76 |
+
جماعت: {class_}
|
| 77 |
+
مضمون: {subject}
|
| 78 |
+
عنوان: {topic}
|
| 79 |
+
طلباء کی تعداد: {students}
|
| 80 |
+
دورانیہ (منٹ): {duration}
|
| 81 |
+
تاریخ: {date}
|
| 82 |
+
معلم: {teacher}
|
| 83 |
+
|
| 84 |
+
شرائط (اردو):
|
| 85 |
+
1) بالکل تین (3) SMART تعلیمی مقاصد (SLOs) تیار کریں۔ ہر مقصد مخصوص، قابلِ پیمائش، قابلِ حصول، متعلقہ اور وقت بند ہو۔ ہر SLO میں طالب علم کا مشاہداتی فعل یا نتیجہ واضح ہو۔
|
| 86 |
+
2) SLOs کے بعد بڑا ہیڈنگ "فراہمیِ مواد / Content to be delivered" رکھیں اور اس کے نیچے:
|
| 87 |
+
- موضوع کی تعریف(یں) مختصراً
|
| 88 |
+
- کلیدی اصطلاحات (ہر ایک کی ایک سطری وضاحت)
|
| 89 |
+
- کلیدی تصورات (بلٹ پوائنٹس) جو ان SLOs کو حاصل کرنے میں مدد دیں گے
|
| 90 |
+
3) "گھریلو کام / Home Assignment" عنوان کے تحت SLOs کے مطابق کام لکھیں۔
|
| 91 |
+
4) آخری عنوان "مشیر/پرنسپل کی رائے / Feedback by the Mentor/Principal" رکھیں اور دستی رائے کے لیے جگہ چھوڑیں۔
|
| 92 |
+
5) اگر assessments مانگے گئے ہوں تو وہ SLOs اور موضوع کے مطابق تیار کریں:
|
| 93 |
+
- Pre-assessment: 5 مختصر آئٹمز
|
| 94 |
+
- Post-assessment: 5 آئٹمز اور جوابات
|
| 95 |
+
- Flash cards: 8 کارڈز (سوال-جواب)
|
| 96 |
+
- Quiz: 6 آئٹمز (MCQ اور مختصر جوابات)
|
| 97 |
+
- Short Response: 5 اوپن اینڈڈ سوالات
|
| 98 |
+
- Fill-in-the-blank: 6 آئٹمز
|
| 99 |
+
- Match the column: 6 جوڑ
|
| 100 |
+
- MCQs: 8 MCQs ہر ایک کے 4 آپشنز اور جواب
|
| 101 |
+
|
| 102 |
+
جو سیکشنز UI میں منتخب ہوں، صرف وہی سیکشنز بنائیں۔ غیر منتخب سیکشنز نہ ڈالیں۔
|
| 103 |
+
مختصر، واضح اور استاد دوست انداز میں تحریر کریں۔
|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
+
# Which assessments user selected (list)
|
| 107 |
+
selected = assessments or []
|
| 108 |
+
# Build an assessments instruction suffix to tell the model which to include
|
| 109 |
+
sel_text = ", ".join(selected) if selected else "None"
|
| 110 |
+
|
| 111 |
+
# Append selection instruction to prompts so model only returns chosen sections
|
| 112 |
+
english_prompt += f"\n\nAssessments requested: {sel_text}\n"
|
| 113 |
+
urdu_prompt += f"\n\nدرخواست کردہ تشخیصی اشیاء: {sel_text}\n"
|
| 114 |
+
|
| 115 |
+
# Call Groq for English and Urdu
|
| 116 |
+
try:
|
| 117 |
+
english_text = _call_groq("llama-3.1-8b-instant", english_prompt)
|
| 118 |
+
except Exception as e:
|
| 119 |
+
english_text = f"⚠️ Failed to generate English text: {e}"
|
| 120 |
+
|
| 121 |
+
try:
|
| 122 |
+
urdu_text = _call_groq("openai/gpt-oss-120b", urdu_prompt)
|
| 123 |
+
except Exception as e:
|
| 124 |
+
urdu_text = f"⚠️ اردو مواد بنانے میں مسئلہ: {e}"
|
| 125 |
+
|
| 126 |
+
# Create .docx with bilingual content; keep top table of inputs
|
| 127 |
+
doc = Document()
|
| 128 |
+
doc.styles["Normal"].font.name = "Times New Roman"
|
| 129 |
+
doc.styles["Normal"].font.size = Pt(11)
|
| 130 |
+
|
| 131 |
+
# Heading
|
| 132 |
+
doc.add_heading(f"BOPPPS Lesson Plan — {subject} — {topic}", level=1)
|
| 133 |
+
|
| 134 |
+
# Top input table (retain)
|
| 135 |
+
table = doc.add_table(rows=7, cols=2)
|
| 136 |
+
table.style = "Table Grid"
|
| 137 |
+
headers = ["Class / جماعت", "Subject / مضمون", "Topic / عنوان", "Students / طلباء", "Duration / دورانیہ", "Date / تاریخ", "Teacher / معلم"]
|
| 138 |
+
values = [class_, subject, topic, students, f"{duration} minutes", date, teacher]
|
| 139 |
+
for i in range(7):
|
| 140 |
+
table.cell(i, 0).text = headers[i]
|
| 141 |
+
table.cell(i, 1).text = str(values[i])
|
| 142 |
+
|
| 143 |
+
doc.add_paragraph("") # spacing
|
| 144 |
+
|
| 145 |
+
# Insert English section
|
| 146 |
+
doc.add_heading("English Section", level=2)
|
| 147 |
+
for line in english_text.split("\n"):
|
| 148 |
+
doc.add_paragraph(line)
|
| 149 |
+
|
| 150 |
+
doc.add_page_break()
|
| 151 |
+
|
| 152 |
+
# Insert Urdu section (right-to-left is not natively handled in docx; we still include Urdu text)
|
| 153 |
+
doc.add_heading("سیکشن اردو / Urdu Section", level=2)
|
| 154 |
+
for line in urdu_text.split("\n"):
|
| 155 |
+
# ensure Urdu lines are added
|
| 156 |
+
para = doc.add_paragraph(line)
|
| 157 |
+
# optionally set font to an Urdu-capable font (if available on the system)
|
| 158 |
+
try:
|
| 159 |
+
run = para.runs[0]
|
| 160 |
+
run.font.name = "Jameel Noori Nastaleeq"
|
| 161 |
+
run._element.rPr.rFonts.set(qn("w:eastAsia"), "Jameel Noori Nastaleeq")
|
| 162 |
+
except Exception:
|
| 163 |
+
pass
|
| 164 |
+
|
| 165 |
+
# Add footer credit
|
| 166 |
+
doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
|
| 167 |
+
|
| 168 |
+
# Save file
|
| 169 |
+
temp_file = NamedTemporaryFile(delete=False, suffix=".docx")
|
| 170 |
+
doc.save(temp_file.name)
|
| 171 |
+
|
| 172 |
+
# Theme HTML display
|
| 173 |
+
final_text = f"✅ Bilingual lesson plan generated for {subject} — {topic} (Class: {class_}).<br>Assessments generated: {sel_text or 'None'}."
|
| 174 |
+
if theme == "Dark":
|
| 175 |
+
theme_css = "background-color:#111;color:#fff;padding:10px;border-radius:10px;"
|
| 176 |
+
else:
|
| 177 |
+
theme_css = "background-color:#fdfdfd;color:#000;padding:10px;border-radius:10px;"
|
| 178 |
+
display_html = f"<div style='{theme_css}'>{final_text}</div>"
|
| 179 |
+
|
| 180 |
+
return display_html, temp_file.name
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
# -------------------------
|
| 184 |
+
# Gradio UI (keeps other features, adds assessment checkboxes)
|
| 185 |
+
# -------------------------
|
| 186 |
+
assessment_options = [
|
| 187 |
+
"Pre-assessment",
|
| 188 |
+
"Post-assessment",
|
| 189 |
+
"Flash cards",
|
| 190 |
+
"Quizzes",
|
| 191 |
+
"Short Response questions",
|
| 192 |
+
"Fill-in the blank",
|
| 193 |
+
"Match the column",
|
| 194 |
+
"MCQs test",
|
| 195 |
+
]
|
| 196 |
+
|
| 197 |
+
demo = gr.Interface(
|
| 198 |
+
fn=generate_lesson_plan,
|
| 199 |
+
inputs=[
|
| 200 |
+
# Language selection kept only for user information; generation is bilingual by default
|
| 201 |
+
gr.Dropdown(["Bilingual (English + اردو)"], value="Bilingual (English + اردو)", label="Language / زبان (بِیلِنگوئل)"),
|
| 202 |
+
gr.Dropdown([f"Grade {i}" for i in range(1, 13)], label="Class / جماعت"),
|
| 203 |
+
gr.Dropdown(
|
| 204 |
+
["Urdu", "English", "Arabic", "Science", "Mathematics", "Biology",
|
| 205 |
+
"Chemistry", "Physics", "Zoology", "Botany", "Civics", "Islamiat"],
|
| 206 |
+
label="Subject / مضمون"
|
| 207 |
+
),
|
| 208 |
+
gr.Textbox(label="Topic / عنوان"),
|
| 209 |
+
gr.Slider(1, 200, value=30, step=1, label="Number of Students / طلباء کی تعداد"),
|
| 210 |
+
gr.Slider(20, 120, value=40, step=5, label="Duration (minutes) / دورانیہ (منٹ)"),
|
| 211 |
+
gr.Textbox(label="Date / تاریخ", value=str(datetime.date.today())),
|
| 212 |
+
gr.Textbox(label="Teacher Name / معلم کا نام"),
|
| 213 |
+
gr.CheckboxGroup(assessment_options, label="Select assessments to generate / منتخب کریں (تشخیصی آئٹمز)"),
|
| 214 |
+
gr.Radio(["Light", "Dark"], label="Theme / پس منظر کا انتخاب", value="Light"),
|
| 215 |
+
],
|
| 216 |
+
outputs=[
|
| 217 |
+
gr.HTML(label="��� Lesson Plan Preview"),
|
| 218 |
+
gr.File(label="⬇️ Download File (.docx)")
|
| 219 |
+
],
|
| 220 |
+
title="📘 AI BOPPPS Lesson Plan Generator — SMART SLOs (Bilingual)",
|
| 221 |
+
description=(
|
| 222 |
+
"⚙️ Developed by **Najaf Ali Sharqi**. <br>"
|
| 223 |
+
"یہ ایپ قومی نصابِ پاکستان (گلگت-بلتستان کے سیاق) کے مطابق صرف تین SMART SLOs تیار کرتی ہے، اور پھر 'Content to be delivered'، 'Home Assignment'، 'Feedback by Mentor/Principal' اور منتخب کردہ تشخیصی آئٹمز (Pre/Post/Flashcards/Quizzes وغیرہ) بناتی ہے — دونوں زبانوں میں۔"
|
| 224 |
+
),
|
| 225 |
+
theme="default",
|
| 226 |
+
allow_flagging="never"
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
if __name__ == "__main__":
|
| 230 |
+
demo.launch()
|