Turbiling commited on
Commit
13b08b6
·
verified ·
1 Parent(s): b025ad2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -78
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
- # Build the structured prompt
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
- Follow the exact format and structure below without using any symbols or special characters.
16
- Use clear, teacher-friendly, classroom-appropriate language.
17
- If the selected language is Urdu, write in right-to-left direction using Jameel Noori Nastaleeq font style and write English terms in Urdu script (for example, "Science" as "سائنس", "Homework" as "ہوم ورک").
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 clear and measurable SLO aligned with the topic "{topic}".
27
- 2. Write the second clear and measurable SLO aligned with the topic "{topic}".
28
- 3. Write the third clear and measurable SLO aligned with the topic "{topic}".
29
 
30
  B) Content to be delivered:
31
- Write detailed and meaningful content based on "{topic}" and the SLOs, including clear definitions, short explanations, and at least one example that can be taught in the classroom.
32
- Ensure the content is conceptually sound and suitable for grade {grade} students.
33
 
34
  C) Home Assignment:
35
- Create a simple, practical, and creative home assignment aligned with the above SLOs.
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 for handwritten feedback with approximately 2.5 line spacing.)
41
- """
42
 
43
- # Generate lesson content using Gemini
44
  response = client.models.generate_content(
45
  model="gemini-2.5-flash",
46
  contents=prompt,
47
  )
48
- lesson_text = response.text
49
-
50
- # Create Word Document
51
- doc = Document()
52
- doc.add_heading("SMART LESSON PLAN", level=1)
53
-
54
- # Input Table
55
- table = doc.add_table(rows=8, cols=2)
56
- table.style = 'Table Grid'
57
- fields = [
58
- ("Language", language),
59
- ("Grade Level", grade),
60
- ("Subject", subject),
61
- ("Topic", topic),
62
- ("Number of Students", students),
63
- ("Duration", f"{duration} minutes"),
64
- ("Teacher Name", teacher_name),
65
- ("Date", str(date.today()))
66
- ]
67
- for i, (label, value) in enumerate(fields):
68
- table.cell(i, 0).text = label
69
- table.cell(i, 1).text = value
70
-
71
- # Add lesson sections
72
- doc.add_paragraph()
73
- doc.add_paragraph("A) Students Learning Outcomes:", style='Heading2')
74
- doc.add_paragraph("By the end of this lesson, students will be able to:")
75
- doc.add_paragraph(lesson_text.split("B)")[0].strip())
76
-
77
- doc.add_paragraph()
78
- doc.add_paragraph("B) Content to be delivered:", style='Heading2')
79
- content_section = lesson_text.split("B)")[1].split("C)")[0].strip()
80
- doc.add_paragraph(content_section)
81
-
82
- doc.add_paragraph()
83
- doc.add_paragraph("C) Home Assignment:", style='Heading2')
84
- assignment_section = lesson_text.split("C)")[1].split("D)")[0].strip()
85
- doc.add_paragraph(assignment_section)
86
-
87
- doc.add_paragraph()
88
- doc.add_paragraph("D) Feedback by the Mentor/ Principal:", style='Heading2')
89
- for _ in range(4):
90
- doc.add_paragraph(" ")
91
-
92
- # Save document
93
- file_name = f"SMART_Lesson_Plan_{topic.replace(' ', '_')}.docx"
94
- doc.save(file_name)
95
-
96
- return lesson_text, file_name
97
-
98
-
99
- # Gradio UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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;'>Generate complete, structured lesson plans in Urdu or English for Pakistani classrooms.</p>")
103
 
104
  with gr.Row():
105
  language = gr.Dropdown(["English", "Urdu"], label="Select Language")
106
- grade = gr.Textbox(label="Grade Level (e.g., Grade 8)")
107
- subject = gr.Textbox(label="Subject (e.g., Science)")
 
 
 
 
 
 
 
 
 
108
 
109
  with gr.Row():
110
- topic = gr.Textbox(label="Topic (e.g., Chemical Bonding)")
111
- students = gr.Textbox(label="Number of Students")
112
- duration = gr.Textbox(label="Duration (in minutes)")
 
 
 
 
 
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 MS Word File")
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