Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,176 +1,115 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import datetime
|
| 4 |
-
from docx import Document
|
| 5 |
from groq import Groq
|
| 6 |
|
| 7 |
-
# ✅
|
| 8 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
client = Groq(api_key=GROQ_API_KEY)
|
| 10 |
|
| 11 |
-
# ✅
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"Subject": "مضمون",
|
| 16 |
-
"Topic": "عنوان",
|
| 17 |
-
"Students": "طلباء",
|
| 18 |
-
"Duration": "دورانیہ",
|
| 19 |
-
"Teacher": "معلم",
|
| 20 |
-
"Date": "تاریخ",
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
def translate_to_urdu_script(text):
|
| 24 |
-
for eng, urdu in transliteration_map.items():
|
| 25 |
-
text = text.replace(eng, urdu)
|
| 26 |
-
return text
|
| 27 |
-
|
| 28 |
-
# ✅ Lesson Plan Generator
|
| 29 |
-
def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
|
| 30 |
-
# Select model
|
| 31 |
-
model_name = "openai/gpt-oss-120b" if language == "Urdu" else "llama-3.1-8b-instant"
|
| 32 |
-
|
| 33 |
-
# Prompts
|
| 34 |
-
if language == "English":
|
| 35 |
prompt = f"""
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
1. Bridge-in
|
| 39 |
-
2. Pre-Assessment
|
| 40 |
-
3. Learning Outcomes
|
| 41 |
-
4. Participatory Learning Activities
|
| 42 |
-
5. Post-Assessment
|
| 43 |
-
6. Summary
|
| 44 |
-
7. Home Assignment aligned with SLOs (reasonable length, logical, creative, interesting, grade-appropriate, within 1–2 hours)
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
Subject: {subject}
|
| 48 |
Topic: {topic}
|
| 49 |
Number of Students: {students}
|
| 50 |
-
Duration: {duration}
|
| 51 |
Date: {date}
|
| 52 |
-
Teacher: {teacher}
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
۲۔ ابتدائی تشخیص
|
| 63 |
-
۳۔ تعلیمی مقاصد
|
| 64 |
-
۴۔ شراکتی تعلیمی سرگرمیاں
|
| 65 |
-
۵۔ بعد از تدریس تشخیص
|
| 66 |
-
۶۔ خلاصہ
|
| 67 |
-
۷۔ گھریلو کام (SLOs کے مطابق): ایسا کام جو مناسب طوالت، منطقی ترتیب، تخلیقی، دلچسپ، ذہنی سطح کے مطابق اور ایک تا دو گھنٹوں میں مکمل ہو۔
|
| 68 |
-
|
| 69 |
-
جماعت: {class_}
|
| 70 |
-
مضمون: {subject}
|
| 71 |
-
عنوان: {topic}
|
| 72 |
-
طلباء کی تعداد: {students}
|
| 73 |
-
دورانیہ: {duration}
|
| 74 |
-
تاریخ: {date}
|
| 75 |
-
معلم: {teacher}
|
| 76 |
"""
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
)
|
| 84 |
-
lesson_text = response.choices[0].message.content.strip()
|
| 85 |
|
| 86 |
-
|
| 87 |
-
if language == "Urdu":
|
| 88 |
-
lesson_text = translate_to_urdu_script(lesson_text)
|
| 89 |
-
urdu_md = f"""
|
| 90 |
-
<div dir="rtl" align="right">
|
| 91 |
|
| 92 |
-
#
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
| تاریخ | {date} |
|
| 102 |
-
| معلم | {teacher} |
|
| 103 |
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
"""
|
| 114 |
-
file_name = f"Lesson_Plan_Urdu_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
|
| 115 |
-
with open(file_name, "w", encoding="utf-8") as f:
|
| 116 |
-
f.write(urdu_md)
|
| 117 |
-
return file_name
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
"Class": class_,
|
| 127 |
-
"Subject": subject,
|
| 128 |
-
"Topic": topic,
|
| 129 |
-
"Number of Students": students,
|
| 130 |
-
"Duration": duration,
|
| 131 |
-
"Date": date,
|
| 132 |
-
"Teacher": teacher
|
| 133 |
-
}
|
| 134 |
-
for key, value in data.items():
|
| 135 |
-
row = table.add_row().cells
|
| 136 |
-
row[0].text = key
|
| 137 |
-
row[1].text = str(value)
|
| 138 |
-
|
| 139 |
-
doc.add_paragraph("\n")
|
| 140 |
-
doc.add_paragraph(lesson_text)
|
| 141 |
-
doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
|
| 142 |
-
|
| 143 |
-
file_name = f"Lesson_Plan_English_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
|
| 144 |
-
doc.save(file_name)
|
| 145 |
-
return file_name
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
# ✅ Dropdown options
|
| 149 |
-
classes = [f"Grade {i}" for i in range(1, 17)]
|
| 150 |
-
subjects = [
|
| 151 |
-
"Urdu", "English", "Arabic", "Science", "Mathematics", "Biology", "Chemistry",
|
| 152 |
-
"Physics", "Zoology", "Botany", "Civics", "Islamiat"
|
| 153 |
-
]
|
| 154 |
-
students = [str(i) for i in range(1, 201)]
|
| 155 |
-
durations = [f"{i} minutes" for i in range(30, 61, 5)]
|
| 156 |
-
|
| 157 |
-
# ✅ Gradio UI
|
| 158 |
-
demo = gr.Interface(
|
| 159 |
-
fn=generate_lesson_plan,
|
| 160 |
-
inputs=[
|
| 161 |
-
gr.Radio(["English", "Urdu"], label="Select Language / زبان کا انتخاب"),
|
| 162 |
-
gr.Dropdown(choices=classes, label="Class / جماعت"),
|
| 163 |
-
gr.Dropdown(choices=subjects, label="Subject / مضمون"),
|
| 164 |
-
gr.Textbox(label="Topic / عنوان"),
|
| 165 |
-
gr.Dropdown(choices=students, label="Number of Students / طلباء کی تعداد"),
|
| 166 |
-
gr.Dropdown(choices=durations, label="Duration / دورانیہ"),
|
| 167 |
-
gr.Calendar(label="Date / تاریخ"),
|
| 168 |
-
gr.Textbox(label="Teacher Name / معلم کا نام"),
|
| 169 |
-
],
|
| 170 |
-
outputs=gr.File(label="📥 Download Lesson Plan"),
|
| 171 |
-
title="📘 AI BOPPPS Lesson Plan Generator (Urdu + English)",
|
| 172 |
-
description="یہ ایپ انگریزی یا اردو میں مکمل BOPPPS ماڈل کے مطابق لیسن پلان تیار کرتی ہے۔ اردو ورژن مکمل اردو رسم الخط میں ہوتا ہے۔",
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
demo.launch()
|
| 176 |
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import datetime
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
+
# ✅ Initialize Groq API
|
| 7 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 8 |
client = Groq(api_key=GROQ_API_KEY)
|
| 9 |
|
| 10 |
+
# ✅ Function to generate lesson plan
|
| 11 |
+
def generate_lesson_plan(language, grade, subject, topic, students, duration, date, teacher):
|
| 12 |
+
if language == "Urdu":
|
| 13 |
+
model = "openai/gpt-oss-120b"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
prompt = f"""
|
| 15 |
+
آپ ایک پیشہ ور استاد ہیں۔ درج ذیل معلومات کی بنیاد پر ایک مکمل سبق کا منصوبہ اردو میں تیار کریں۔
|
| 16 |
+
براہ کرم ہر لفظ اردو حروف میں لکھیں، انگریزی حروف استعمال نہ کریں۔
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
جماعت: {grade}
|
| 19 |
+
مضمون: {subject}
|
| 20 |
+
عنوان: {topic}
|
| 21 |
+
طلبہ کی تعداد: {students}
|
| 22 |
+
دورانیہ: {duration} منٹ
|
| 23 |
+
تاریخ: {date}
|
| 24 |
+
استاد کا نام: {teacher}
|
| 25 |
+
|
| 26 |
+
سبق کا منصوبہ BOPPPS ماڈل کے مطابق ہو:
|
| 27 |
+
1. توجہ حاصل کرنے کا مرحلہ (Bridge-in)
|
| 28 |
+
2. پیشگی جانچ (Pre-Assessment)
|
| 29 |
+
3. سیکھنے کے نتائج (Learning Outcomes)
|
| 30 |
+
4. شراکتی سیکھنے کی سرگرمیاں (Participatory Learning Activities)
|
| 31 |
+
5. بعد از تدریس جانچ (Post-Assessment)
|
| 32 |
+
6. خلاصہ (Summary)
|
| 33 |
+
7. گھریلو کام (Home Assignment) جو درج ذیل شرائط پوری کرے:
|
| 34 |
+
- مناسب طوالت ہو
|
| 35 |
+
- منطقی ترتیب ہو
|
| 36 |
+
- طلبہ میں تخلیقی صلاحیت پیدا کرے
|
| 37 |
+
- دلچسپ ہو
|
| 38 |
+
- ذہنی و جماعتی سطح کے مطابق ہو
|
| 39 |
+
- ۱ تا ۲ گھنٹوں میں مکمل ہوسکے
|
| 40 |
+
"""
|
| 41 |
+
else:
|
| 42 |
+
model = "llama-3.1-8b-instant"
|
| 43 |
+
prompt = f"""
|
| 44 |
+
You are a professional teacher. Based on the following details, generate a complete **Lesson Plan in English** using the **BOPPPS Model**.
|
| 45 |
+
|
| 46 |
+
Class: {grade}
|
| 47 |
Subject: {subject}
|
| 48 |
Topic: {topic}
|
| 49 |
Number of Students: {students}
|
| 50 |
+
Duration: {duration} minutes
|
| 51 |
Date: {date}
|
| 52 |
+
Teacher's Name: {teacher}
|
| 53 |
+
|
| 54 |
+
Lesson Plan Structure:
|
| 55 |
+
1. Bridge-in
|
| 56 |
+
2. Pre-Assessment
|
| 57 |
+
3. Learning Outcomes
|
| 58 |
+
4. Participatory Learning Activities
|
| 59 |
+
5. Post-Assessment
|
| 60 |
+
6. Summary
|
| 61 |
+
7. Home Assignment aligned with SLOs (reasonable length, logical order, creative, interesting, suitable for students’ level, and completable in 1–2 hours)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
"""
|
| 63 |
|
| 64 |
+
completion = client.chat.completions.create(
|
| 65 |
+
model=model,
|
| 66 |
+
messages=[
|
| 67 |
+
{"role": "system", "content": "You are a professional lesson plan creator."},
|
| 68 |
+
{"role": "user", "content": prompt}
|
| 69 |
+
],
|
| 70 |
)
|
|
|
|
| 71 |
|
| 72 |
+
response = completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Add footer
|
| 75 |
+
footer = "\n\n---\n**Developed by Najaf Ali Sharqi**"
|
| 76 |
+
return response + footer
|
| 77 |
|
| 78 |
+
# ✅ Gradio Interface
|
| 79 |
+
grades = [f"Grade {i}" for i in range(1, 17)]
|
| 80 |
+
subjects = [
|
| 81 |
+
"Urdu", "English", "Arabic", "Science", "Mathematics",
|
| 82 |
+
"Biology", "Chemistry", "Physics", "Zoology", "Botany",
|
| 83 |
+
"Civics", "Islamiat"
|
| 84 |
+
]
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
with gr.Blocks(title="Lesson Plan Generator") as app:
|
| 87 |
+
gr.Markdown("## 📘 AI Lesson Plan Generator (English / Urdu)\nDeveloped by **Najaf Ali Sharqi**")
|
| 88 |
|
| 89 |
+
with gr.Row():
|
| 90 |
+
language = gr.Dropdown(["English", "Urdu"], label="Language / زبان", value="English")
|
| 91 |
+
grade = gr.Dropdown(grades, label="Class / جماعت")
|
| 92 |
+
subject = gr.Dropdown(subjects, label="Subject / مضمون")
|
| 93 |
|
| 94 |
+
with gr.Row():
|
| 95 |
+
topic = gr.Textbox(label="Topic / عنوان", placeholder="Enter topic")
|
| 96 |
+
students = gr.Slider(1, 200, step=1, label="No. of Students / طلبہ کی تعداد")
|
| 97 |
+
duration = gr.Slider(30, 60, step=5, label="Time Duration (minutes) / دورانیہ (منٹ)")
|
| 98 |
|
| 99 |
+
# ✅ Replaced gr.Calendar() with gr.Textbox (date input)
|
| 100 |
+
with gr.Row():
|
| 101 |
+
date = gr.Textbox(label="Date / تاریخ", placeholder="YYYY-MM-DD", value=str(datetime.date.today()))
|
| 102 |
+
teacher = gr.Textbox(label="Teacher's Name / استاد کا نام")
|
| 103 |
|
| 104 |
+
generate_btn = gr.Button("Generate Lesson Plan / سبق کا منصوبہ تیار کریں")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
output = gr.Markdown(label="Generated Lesson Plan")
|
| 107 |
+
|
| 108 |
+
generate_btn.click(
|
| 109 |
+
fn=generate_lesson_plan,
|
| 110 |
+
inputs=[language, grade, subject, topic, students, duration, date, teacher],
|
| 111 |
+
outputs=output,
|
| 112 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
# ✅ Launch
|
| 115 |
+
app.launch()
|