Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,118 +8,215 @@ from groq import Groq
|
|
| 8 |
from docx import Document
|
| 9 |
from docx.shared import Pt, Inches
|
| 10 |
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
|
|
|
|
|
| 11 |
from datetime import datetime
|
| 12 |
import re
|
| 13 |
|
| 14 |
-
# Initialize client
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# ------------------ DATA ------------------
|
| 18 |
SUBJECTS_BY_GRADE = {
|
| 19 |
-
"Grade 1-5": ["English", "Urdu", "Mathematics"],
|
| 20 |
-
"Grade 6-8": ["English", "Urdu", "Mathematics"],
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
-
DISTRICTS_GB = [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
return None, "Topic and Subject required"
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
|
|
|
| 47 |
Grade: {full_grade}
|
| 48 |
-
Subject: {subject}
|
| 49 |
-
Topic: {topic[:100]}
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
"""
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
2. علمی مواد
|
| 70 |
-
3. گھر کا کام
|
| 71 |
|
| 72 |
-
|
|
|
|
| 73 |
"""
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
# ------------------ FIXED API CALL ------------------
|
| 77 |
-
completion = client.chat.completions.create(
|
| 78 |
-
model="openai/gpt-oss-120b",
|
| 79 |
-
messages=[
|
| 80 |
-
{"role": "system", "content": "You are a professional teacher."},
|
| 81 |
-
{"role": "user", "content": prompt[:2000]}
|
| 82 |
-
],
|
| 83 |
-
temperature=0.6,
|
| 84 |
-
max_completion_tokens=800,
|
| 85 |
-
top_p=0.9,
|
| 86 |
-
stream=False
|
| 87 |
-
)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
| 93 |
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
doc.add_paragraph(f"Topic: {topic}")
|
| 102 |
-
doc.add_paragraph(f"Teacher: {teacher_name}")
|
| 103 |
-
doc.add_paragraph(f"School: {school_name}")
|
| 104 |
-
doc.add_paragraph(f"Date: {formatted_date}")
|
| 105 |
|
| 106 |
-
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
doc.save(file_path)
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
language = gr.Dropdown(["English", "Urdu"], value="English")
|
| 121 |
-
grade_range = gr.Dropdown(["Grade 1-5", "Grade 6-8"])
|
| 122 |
-
grade_specific = gr.Dropdown([f"Grade {i}" for i in range(1,
|
|
|
|
| 123 |
subject = gr.Dropdown([])
|
| 124 |
topic = gr.Textbox()
|
| 125 |
|
|
@@ -127,19 +224,18 @@ with gr.Blocks() as demo:
|
|
| 127 |
teacher_name = gr.Textbox()
|
| 128 |
district = gr.Dropdown(DISTRICTS_GB)
|
| 129 |
|
| 130 |
-
date_input = gr.Textbox()
|
| 131 |
students = gr.Number(value=30)
|
| 132 |
-
duration = gr.Dropdown([40, 45])
|
| 133 |
|
| 134 |
-
teaching_method = gr.
|
| 135 |
-
assessment_type = gr.
|
| 136 |
|
| 137 |
-
include_resources = gr.Checkbox()
|
| 138 |
-
include_differentiation = gr.Checkbox()
|
| 139 |
|
| 140 |
btn = gr.Button("Generate")
|
| 141 |
|
| 142 |
-
|
| 143 |
status = gr.Textbox()
|
| 144 |
|
| 145 |
grade_range.change(update_subjects, grade_range, subject)
|
|
@@ -147,10 +243,10 @@ with gr.Blocks() as demo:
|
|
| 147 |
btn.click(
|
| 148 |
generate_lesson_plan,
|
| 149 |
inputs=[language, grade_range, grade_specific, subject, topic,
|
| 150 |
-
school_name, teacher_name, district,
|
| 151 |
-
|
| 152 |
include_resources, include_differentiation],
|
| 153 |
-
outputs=[
|
| 154 |
)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
|
|
|
| 8 |
from docx import Document
|
| 9 |
from docx.shared import Pt, Inches
|
| 10 |
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
| 11 |
+
from docx.oxml.ns import qn
|
| 12 |
+
from docx.oxml import OxmlElement
|
| 13 |
from datetime import datetime
|
| 14 |
import re
|
| 15 |
|
| 16 |
+
# Initialize Groq client
|
| 17 |
+
try:
|
| 18 |
+
client = Groq()
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Groq Error: {e}")
|
| 21 |
+
client = None
|
| 22 |
+
|
| 23 |
+
# ---------------- DATA ---------------- #
|
| 24 |
|
|
|
|
| 25 |
SUBJECTS_BY_GRADE = {
|
| 26 |
+
"Grade 1-5": ["English", "Urdu", "Mathematics", "General Science", "Islamiyat", "Pakistan Studies", "Computer"],
|
| 27 |
+
"Grade 6-8": ["English", "Urdu", "Mathematics", "General Science", "Islamiyat", "Pakistan Studies", "Computer"],
|
| 28 |
+
"Grade 9-10": ["English", "Urdu", "Mathematics", "Physics", "Chemistry", "Biology", "Islamiyat", "Pakistan Studies", "Computer Science"],
|
| 29 |
+
"Grade 11-12": ["English", "Urdu", "Mathematics", "Physics", "Chemistry", "Biology", "Islamiyat", "Pakistan Studies", "Computer Science", "Economics", "Statistics"]
|
| 30 |
}
|
| 31 |
|
| 32 |
+
DISTRICTS_GB = [
|
| 33 |
+
"Gilgit", "Ghizer", "Hunza", "Nagar", "Skardu", "Shigar",
|
| 34 |
+
"Kharmang", "Ghanche", "Astore", "Diamer", "Darel", "Tangir",
|
| 35 |
+
"Gupis Yasin", "Roundu"
|
| 36 |
+
]
|
| 37 |
|
| 38 |
+
TEACHING_METHODS = [
|
| 39 |
+
"Lecture Method", "Discussion Method", "Activity-Based Learning",
|
| 40 |
+
"Group Work", "Inquiry-Based Learning"
|
| 41 |
+
]
|
| 42 |
|
| 43 |
+
ASSESSMENT_TYPES = [
|
| 44 |
+
"Formative Assessment", "Summative Assessment",
|
| 45 |
+
"Quiz", "Worksheet", "Oral Questions"
|
| 46 |
+
]
|
| 47 |
|
| 48 |
+
# ---------------- SAFE GENERATION ---------------- #
|
| 49 |
+
|
| 50 |
+
def safe_generate(messages, max_tokens=500):
|
| 51 |
+
"""
|
| 52 |
+
Token-safe API call
|
| 53 |
+
"""
|
| 54 |
+
try:
|
| 55 |
+
response = client.chat.completions.create(
|
| 56 |
+
model="openai/gpt-oss-120b",
|
| 57 |
+
messages=messages,
|
| 58 |
+
temperature=0.7,
|
| 59 |
+
max_completion_tokens=max_tokens,
|
| 60 |
+
top_p=0.9
|
| 61 |
+
)
|
| 62 |
+
return response.choices[0].message.content.strip()
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"Error: {str(e)}"
|
| 65 |
|
| 66 |
+
# ---------------- HELPERS ---------------- #
|
|
|
|
| 67 |
|
| 68 |
+
def update_subjects(grade_range):
|
| 69 |
+
if not grade_range:
|
| 70 |
+
return gr.Dropdown(choices=[], value=None)
|
| 71 |
+
return gr.Dropdown(choices=SUBJECTS_BY_GRADE.get(grade_range, []), value=None)
|
| 72 |
+
|
| 73 |
+
def validate_inputs(language, grade_range, subject, topic, teacher_name, school_name):
|
| 74 |
+
errors = []
|
| 75 |
+
if not language: errors.append("Language required")
|
| 76 |
+
if not grade_range: errors.append("Grade required")
|
| 77 |
+
if not subject: errors.append("Subject required")
|
| 78 |
+
if not topic or len(topic.strip()) < 3: errors.append("Topic too short")
|
| 79 |
+
if not teacher_name: errors.append("Teacher name required")
|
| 80 |
+
if not school_name: errors.append("School name required")
|
| 81 |
+
return errors
|
| 82 |
+
def generate_content(language, full_grade, subject, topic,
|
| 83 |
+
include_resources, include_differentiation):
|
| 84 |
+
|
| 85 |
+
# -------- SLO -------- #
|
| 86 |
+
slo_prompt = f"""
|
| 87 |
+
Generate 3 SMART SLOs for:
|
| 88 |
+
Grade: {full_grade}
|
| 89 |
+
Subject: {subject}
|
| 90 |
+
Topic: {topic}
|
| 91 |
+
"""
|
| 92 |
|
| 93 |
+
slos = safe_generate([
|
| 94 |
+
{"role": "system", "content": f"Write in {language} only"},
|
| 95 |
+
{"role": "user", "content": slo_prompt}
|
| 96 |
+
], 300)
|
| 97 |
|
| 98 |
+
# -------- CONTENT -------- #
|
| 99 |
+
content_prompt = f"""
|
| 100 |
+
Explain topic for students:
|
| 101 |
|
| 102 |
+
Topic: {topic}
|
| 103 |
Grade: {full_grade}
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
Include:
|
| 106 |
+
- Key concepts
|
| 107 |
+
- Definitions
|
| 108 |
+
- Examples
|
| 109 |
+
{"- Resources" if include_resources else ""}
|
| 110 |
+
{"- Differentiation" if include_differentiation else ""}
|
| 111 |
+
"""
|
| 112 |
+
|
| 113 |
+
content = safe_generate([
|
| 114 |
+
{"role": "system", "content": f"Write in {language} only"},
|
| 115 |
+
{"role": "user", "content": content_prompt}
|
| 116 |
+
], 600)
|
| 117 |
|
| 118 |
+
# -------- ASSIGNMENT -------- #
|
| 119 |
+
assignment_prompt = f"""
|
| 120 |
+
Create homework:
|
| 121 |
+
Topic: {topic}
|
| 122 |
+
Grade: {full_grade}
|
| 123 |
+
3-5 questions
|
| 124 |
"""
|
| 125 |
|
| 126 |
+
assignment = safe_generate([
|
| 127 |
+
{"role": "system", "content": f"Write in {language} only"},
|
| 128 |
+
{"role": "user", "content": assignment_prompt}
|
| 129 |
+
], 300)
|
| 130 |
|
| 131 |
+
# -------- MERGE -------- #
|
| 132 |
+
return f"""
|
| 133 |
+
SMART STUDENT LEARNING OUTCOMES
|
| 134 |
+
{slos}
|
| 135 |
|
| 136 |
+
CONTENT KNOWLEDGE
|
| 137 |
+
{content}
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
HOME ASSIGNMENT
|
| 140 |
+
{assignment}
|
| 141 |
"""
|
| 142 |
+
def create_doc(language, lesson_text, subject, topic, full_grade, teacher_name, school_name, district, duration, students):
|
| 143 |
|
| 144 |
+
doc = Document()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
title = doc.add_paragraph("SMART LESSON PLAN")
|
| 147 |
+
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
| 148 |
|
| 149 |
+
table = doc.add_table(rows=4, cols=2)
|
| 150 |
|
| 151 |
+
table.cell(0,0).text = "Grade"
|
| 152 |
+
table.cell(0,1).text = full_grade
|
| 153 |
|
| 154 |
+
table.cell(1,0).text = "Subject"
|
| 155 |
+
table.cell(1,1).text = subject
|
| 156 |
|
| 157 |
+
table.cell(2,0).text = "Teacher"
|
| 158 |
+
table.cell(2,1).text = teacher_name
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
table.cell(3,0).text = "School"
|
| 161 |
+
table.cell(3,1).text = school_name
|
| 162 |
|
| 163 |
+
doc.add_paragraph("")
|
|
|
|
| 164 |
|
| 165 |
+
for line in lesson_text.split("\n"):
|
| 166 |
+
if line.strip():
|
| 167 |
+
para = doc.add_paragraph(line.strip())
|
| 168 |
+
para.paragraph_format.space_after = Pt(6)
|
| 169 |
|
| 170 |
+
safe_topic = re.sub(r'[^\w\s-]', '', topic)[:20]
|
| 171 |
+
file_path = f"/tmp/LP_{safe_topic}.docx"
|
| 172 |
+
doc.save(file_path)
|
| 173 |
+
|
| 174 |
+
return file_path
|
| 175 |
+
def generate_lesson_plan(language, grade_range, grade_specific, subject, topic,
|
| 176 |
+
school_name, teacher_name, district, date_input,
|
| 177 |
+
students, duration, teaching_method, assessment_type,
|
| 178 |
+
include_resources, include_differentiation):
|
| 179 |
|
| 180 |
+
errors = validate_inputs(language, grade_range, subject, topic, teacher_name, school_name)
|
| 181 |
+
if errors:
|
| 182 |
+
return None, "\n".join(errors)
|
| 183 |
|
| 184 |
+
if client is None:
|
| 185 |
+
return None, "API not initialized"
|
| 186 |
+
|
| 187 |
+
full_grade = grade_specific if grade_specific else grade_range.split('-')[0]
|
| 188 |
+
|
| 189 |
+
# -------- GENERATE CONTENT -------- #
|
| 190 |
+
lesson_text = generate_content(
|
| 191 |
+
language,
|
| 192 |
+
full_grade,
|
| 193 |
+
subject,
|
| 194 |
+
topic,
|
| 195 |
+
include_resources,
|
| 196 |
+
include_differentiation
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
# -------- CREATE DOC -------- #
|
| 200 |
+
file_path = create_doc(
|
| 201 |
+
language,
|
| 202 |
+
lesson_text,
|
| 203 |
+
subject,
|
| 204 |
+
topic,
|
| 205 |
+
full_grade,
|
| 206 |
+
teacher_name,
|
| 207 |
+
school_name,
|
| 208 |
+
district,
|
| 209 |
+
duration,
|
| 210 |
+
students
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
return file_path, "Lesson Plan Generated Successfully!"
|
| 214 |
+
with gr.Blocks() as demo:
|
| 215 |
|
| 216 |
language = gr.Dropdown(["English", "Urdu"], value="English")
|
| 217 |
+
grade_range = gr.Dropdown(["Grade 1-5", "Grade 6-8", "Grade 9-10", "Grade 11-12"])
|
| 218 |
+
grade_specific = gr.Dropdown([f"Grade {i}" for i in range(1,13)])
|
| 219 |
+
|
| 220 |
subject = gr.Dropdown([])
|
| 221 |
topic = gr.Textbox()
|
| 222 |
|
|
|
|
| 224 |
teacher_name = gr.Textbox()
|
| 225 |
district = gr.Dropdown(DISTRICTS_GB)
|
| 226 |
|
|
|
|
| 227 |
students = gr.Number(value=30)
|
| 228 |
+
duration = gr.Dropdown([40, 45, 60])
|
| 229 |
|
| 230 |
+
teaching_method = gr.Dropdown(TEACHING_METHODS)
|
| 231 |
+
assessment_type = gr.Dropdown(ASSESSMENT_TYPES)
|
| 232 |
|
| 233 |
+
include_resources = gr.Checkbox(value=True)
|
| 234 |
+
include_differentiation = gr.Checkbox(value=True)
|
| 235 |
|
| 236 |
btn = gr.Button("Generate")
|
| 237 |
|
| 238 |
+
output_file = gr.File()
|
| 239 |
status = gr.Textbox()
|
| 240 |
|
| 241 |
grade_range.change(update_subjects, grade_range, subject)
|
|
|
|
| 243 |
btn.click(
|
| 244 |
generate_lesson_plan,
|
| 245 |
inputs=[language, grade_range, grade_specific, subject, topic,
|
| 246 |
+
school_name, teacher_name, district, "", students,
|
| 247 |
+
duration, teaching_method, assessment_type,
|
| 248 |
include_resources, include_differentiation],
|
| 249 |
+
outputs=[output_file, status]
|
| 250 |
)
|
| 251 |
|
| 252 |
if __name__ == "__main__":
|