Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,122 +1,165 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from google import genai
|
| 3 |
from docx import Document
|
|
|
|
|
|
|
|
|
|
| 4 |
from datetime import date
|
| 5 |
|
| 6 |
# Initialize Gemini client
|
| 7 |
client = genai.Client()
|
| 8 |
|
| 9 |
def generate_lesson_plan(language, grade, subject, topic, students, duration, teacher_name):
|
| 10 |
-
#
|
| 11 |
prompt = f"""
|
| 12 |
You are a qualified and experienced school teacher in Pakistan, well-versed in the National Curriculum of Pakistan.
|
| 13 |
Your task is to create a complete and structured "SMART LESSON PLAN" in the language requested by the user (either Urdu or English).
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
Ensure the lesson content aligns with the given topic and is relevant to Pakistani classrooms.
|
| 19 |
-
|
| 20 |
-
Generate the lesson plan as follows:
|
| 21 |
|
| 22 |
SMART LESSON PLAN
|
| 23 |
|
| 24 |
A) Students Learning Outcomes:
|
| 25 |
By the end of this lesson, students will be able to:
|
| 26 |
-
1. Write the first
|
| 27 |
-
2. Write the second
|
| 28 |
-
3. Write the third
|
| 29 |
|
| 30 |
B) Content to be delivered:
|
| 31 |
-
Write detailed and
|
| 32 |
-
Ensure
|
| 33 |
|
| 34 |
C) Home Assignment:
|
| 35 |
-
Create a simple
|
| 36 |
-
The assignment should be doable within 24 hours and promote understanding and application of the topic.
|
| 37 |
-
Write actual directions that a teacher would give to students.
|
| 38 |
|
| 39 |
D) Feedback by the Mentor/ Principal:
|
| 40 |
-
(Leave four continuous blank lines
|
| 41 |
-
|
| 42 |
|
| 43 |
-
# Generate
|
| 44 |
response = client.models.generate_content(
|
| 45 |
model="gemini-2.5-flash",
|
| 46 |
contents=prompt,
|
| 47 |
)
|
| 48 |
-
lesson_text = response.text
|
| 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 |
-
doc.add_paragraph(
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
with gr.Blocks(title="SMART Lesson Plan Generator") as demo:
|
| 101 |
gr.Markdown("<h2 style='text-align:center;'>SMART Lesson Plan Generator</h2>")
|
| 102 |
-
gr.Markdown("<p style='text-align:center;'>
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
language = gr.Dropdown(["English", "Urdu"], label="Select Language")
|
| 106 |
-
grade = gr.
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
with gr.Row():
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
with gr.Row():
|
| 115 |
teacher_name = gr.Textbox(label="Teacher Name")
|
| 116 |
|
| 117 |
generate_btn = gr.Button("Generate Lesson Plan")
|
| 118 |
output_text = gr.Textbox(label="Generated Lesson Plan", lines=25)
|
| 119 |
-
download_file = gr.File(label="Download
|
| 120 |
|
| 121 |
generate_btn.click(
|
| 122 |
generate_lesson_plan,
|
|
@@ -124,7 +167,7 @@ with gr.Blocks(title="SMART Lesson Plan Generator") as demo:
|
|
| 124 |
outputs=[output_text, download_file],
|
| 125 |
)
|
| 126 |
|
| 127 |
-
gr.Markdown("<hr><p style='text-align:center;'>Developed by <b>Najaf Ali Sharqi</b></p>")
|
| 128 |
|
| 129 |
demo.launch()
|
| 130 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from google import genai
|
| 3 |
from docx import Document
|
| 4 |
+
from docx.oxml import parse_xml
|
| 5 |
+
from docx.oxml.ns import qn
|
| 6 |
+
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
| 7 |
from datetime import date
|
| 8 |
|
| 9 |
# Initialize Gemini client
|
| 10 |
client = genai.Client()
|
| 11 |
|
| 12 |
def generate_lesson_plan(language, grade, subject, topic, students, duration, teacher_name):
|
| 13 |
+
# ===== Prompt =====
|
| 14 |
prompt = f"""
|
| 15 |
You are a qualified and experienced school teacher in Pakistan, well-versed in the National Curriculum of Pakistan.
|
| 16 |
Your task is to create a complete and structured "SMART LESSON PLAN" in the language requested by the user (either Urdu or English).
|
| 17 |
|
| 18 |
+
If Urdu is selected, write right-to-left in Jameel Noori Nastaleeq style, with English words in Urdu script (for example, "Science" as "سائنس").
|
| 19 |
+
Avoid all symbols, bullets, or special characters.
|
| 20 |
+
Generate only the following sections once, clearly separated and formatted:
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
SMART LESSON PLAN
|
| 23 |
|
| 24 |
A) Students Learning Outcomes:
|
| 25 |
By the end of this lesson, students will be able to:
|
| 26 |
+
1. Write the first SLO related to "{topic}".
|
| 27 |
+
2. Write the second SLO related to "{topic}".
|
| 28 |
+
3. Write the third SLO related to "{topic}".
|
| 29 |
|
| 30 |
B) Content to be delivered:
|
| 31 |
+
Write detailed and clear content (definitions, explanations, and one example) about "{topic}" aligned with the SLOs.
|
| 32 |
+
Ensure it fits Grade {grade} Pakistani context.
|
| 33 |
|
| 34 |
C) Home Assignment:
|
| 35 |
+
Create a simple and creative home assignment aligned with SLOs that can be completed within 24 hours.
|
|
|
|
|
|
|
| 36 |
|
| 37 |
D) Feedback by the Mentor/ Principal:
|
| 38 |
+
(Leave four continuous blank lines.)
|
| 39 |
+
"""
|
| 40 |
|
| 41 |
+
# ===== Generate Response =====
|
| 42 |
response = client.models.generate_content(
|
| 43 |
model="gemini-2.5-flash",
|
| 44 |
contents=prompt,
|
| 45 |
)
|
| 46 |
+
lesson_text = response.text.strip()
|
| 47 |
+
|
| 48 |
+
# ===== File Generation Based on Language =====
|
| 49 |
+
if language == "English":
|
| 50 |
+
# Create Word Document
|
| 51 |
+
doc = Document()
|
| 52 |
+
|
| 53 |
+
# Header
|
| 54 |
+
section = doc.sections[0]
|
| 55 |
+
header = section.header
|
| 56 |
+
header_para = header.paragraphs[0]
|
| 57 |
+
header_para.text = "School Education Department, Gilgit-Baltistan"
|
| 58 |
+
header_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
| 59 |
+
|
| 60 |
+
# Footer with page numbers
|
| 61 |
+
footer = section.footer
|
| 62 |
+
footer_para = footer.paragraphs[0]
|
| 63 |
+
footer_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
| 64 |
+
footer_para.add_run("Page ").add_field('PAGE')
|
| 65 |
+
footer_para.add_run(" of ").add_field('NUMPAGES')
|
| 66 |
+
|
| 67 |
+
# Title
|
| 68 |
+
doc.add_heading("SMART LESSON PLAN", level=1).alignment = WD_ALIGN_PARAGRAPH.CENTER
|
| 69 |
+
|
| 70 |
+
# Input Table
|
| 71 |
+
table = doc.add_table(rows=8, cols=2)
|
| 72 |
+
table.style = 'Table Grid'
|
| 73 |
+
fields = [
|
| 74 |
+
("Language", language),
|
| 75 |
+
("Grade Level", grade),
|
| 76 |
+
("Subject", subject),
|
| 77 |
+
("Topic", topic),
|
| 78 |
+
("Number of Students", students),
|
| 79 |
+
("Duration", f"{duration} minutes"),
|
| 80 |
+
("Teacher Name", teacher_name),
|
| 81 |
+
("Date", str(date.today()))
|
| 82 |
+
]
|
| 83 |
+
for i, (label, value) in enumerate(fields):
|
| 84 |
+
table.cell(i, 0).text = label
|
| 85 |
+
table.cell(i, 1).text = value
|
| 86 |
+
|
| 87 |
+
# Add Lesson Content
|
| 88 |
+
doc.add_paragraph()
|
| 89 |
+
for line in lesson_text.splitlines():
|
| 90 |
+
doc.add_paragraph(line)
|
| 91 |
+
|
| 92 |
+
# Save DOCX
|
| 93 |
+
file_name = f"SMART_Lesson_Plan_{topic.replace(' ', '_')}.docx"
|
| 94 |
+
doc.save(file_name)
|
| 95 |
+
return lesson_text, file_name
|
| 96 |
+
|
| 97 |
+
else:
|
| 98 |
+
# Urdu version: Markdown format (Right-to-Left)
|
| 99 |
+
md_content = f"""
|
| 100 |
+
SMART LESSON PLAN
|
| 101 |
+
|
| 102 |
+
زبان: اردو
|
| 103 |
+
جماعت: {grade}
|
| 104 |
+
مضمون: {subject}
|
| 105 |
+
موضوع: {topic}
|
| 106 |
+
طلباء کی تعداد: {students}
|
| 107 |
+
دورانیہ: {duration} منٹ
|
| 108 |
+
استاد کا نام: {teacher_name}
|
| 109 |
+
تاریخ: {date.today()}
|
| 110 |
+
|
| 111 |
+
---
|
| 112 |
+
|
| 113 |
+
{lesson_text}
|
| 114 |
+
|
| 115 |
+
---
|
| 116 |
+
|
| 117 |
+
ڈیزائن کیا گیا از: نجف علی شرقی
|
| 118 |
+
School Education Department, Gilgit-Baltistan
|
| 119 |
+
"""
|
| 120 |
+
# Save as Markdown
|
| 121 |
+
file_name = f"SMART_Lesson_Plan_{topic.replace(' ', '_')}.md"
|
| 122 |
+
with open(file_name, "w", encoding="utf-8") as f:
|
| 123 |
+
f.write(md_content)
|
| 124 |
+
|
| 125 |
+
return lesson_text, file_name
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
# ===== Gradio UI =====
|
| 129 |
with gr.Blocks(title="SMART Lesson Plan Generator") as demo:
|
| 130 |
gr.Markdown("<h2 style='text-align:center;'>SMART Lesson Plan Generator</h2>")
|
| 131 |
+
gr.Markdown("<p style='text-align:center;'>Create structured lesson plans in Urdu or English for Pakistani classrooms.</p>")
|
| 132 |
|
| 133 |
with gr.Row():
|
| 134 |
language = gr.Dropdown(["English", "Urdu"], label="Select Language")
|
| 135 |
+
grade = gr.Dropdown(
|
| 136 |
+
["Grade 1", "Grade 2", "Grade 3", "Grade 4", "Grade 5", "Grade 6", "Grade 7", "Grade 8", "Grade 9", "Grade 10", "Grade 11", "Grade 12"],
|
| 137 |
+
label="Select Grade Level"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
with gr.Row():
|
| 141 |
+
subject = gr.Dropdown(
|
| 142 |
+
["Science", "Mathematics", "English", "Urdu", "Islamiyat", "Computer", "Physics", "Chemistry", "Biology", "Social Studies"],
|
| 143 |
+
label="Select Subject"
|
| 144 |
+
)
|
| 145 |
+
topic = gr.Textbox(label="Enter Topic (e.g., Chemical Bonding)")
|
| 146 |
|
| 147 |
with gr.Row():
|
| 148 |
+
students = gr.Dropdown(
|
| 149 |
+
["20", "25", "30", "35", "40", "45", "50+"],
|
| 150 |
+
label="Number of Students"
|
| 151 |
+
)
|
| 152 |
+
duration = gr.Dropdown(
|
| 153 |
+
["30", "35", "40", "45", "50", "60"],
|
| 154 |
+
label="Duration (in minutes)"
|
| 155 |
+
)
|
| 156 |
|
| 157 |
with gr.Row():
|
| 158 |
teacher_name = gr.Textbox(label="Teacher Name")
|
| 159 |
|
| 160 |
generate_btn = gr.Button("Generate Lesson Plan")
|
| 161 |
output_text = gr.Textbox(label="Generated Lesson Plan", lines=25)
|
| 162 |
+
download_file = gr.File(label="Download File")
|
| 163 |
|
| 164 |
generate_btn.click(
|
| 165 |
generate_lesson_plan,
|
|
|
|
| 167 |
outputs=[output_text, download_file],
|
| 168 |
)
|
| 169 |
|
| 170 |
+
gr.Markdown("<hr><p style='text-align:center;'>Developed by <b>Najaf Ali Sharqi</b> | School Education Department, Gilgit-Baltistan</p>")
|
| 171 |
|
| 172 |
demo.launch()
|
| 173 |
|