Turbiling commited on
Commit
8d323b6
·
verified ·
1 Parent(s): 7955fac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -128
app.py CHANGED
@@ -1,151 +1,174 @@
1
  import gradio as gr
2
- from google import genai
3
  from docx import Document
4
  from docx.shared import Pt, RGBColor
5
- from docx.enum.text import WD_ALIGN_PARAGRAPH
6
  import os
7
- from datetime import date
8
-
9
- client = genai.Client()
10
-
11
- # ✅ Ensure output directory exists
12
- OUTPUT_DIR = "/mnt/data"
13
- if not os.path.exists(OUTPUT_DIR):
14
- os.makedirs(OUTPUT_DIR)
15
-
16
- # ✅ Function to generate lesson plan
17
- def generate_lesson_plan(language, grade_level, subject, topic, students, duration, teacher_name):
18
- today = date.today().strftime("%Y-%m-%d")
19
-
20
- prompt = f"""
21
- You are a professional teacher working in the School Education Department, Gilgit-Baltistan.
22
- Create a SMART Lesson Plan in {language}.
23
- Format:
24
- 1. A table at the top with: Grade Level, Subject, Topic, Number of Students, Duration, Teacher Name, Date.
25
- 2. Then write bold heading: "SLOs (Student Learning Outcomes)"
26
- Write exactly 3 clear, measurable SLOs related to the topic.
27
- 3. Then bold heading: "Content to be Delivered"
28
- Write clear and structured content aligned with SLOs.
29
- 4. Then bold heading: "Home Assignment"
30
- Write a meaningful assignment aligned with SLOs that can be completed within 24 hours.
31
- Avoid repeating table text in paragraph.
32
- For Urdu version:
33
- - All headings must be bold and right-aligned.
34
- - Text should be right-aligned.
35
- - Avoid all special characters or emojis.
36
- - Write English terms in Urdu alphabets.
37
- For English version:
38
- - All headings bold and text left-aligned.
39
- Topic: {topic}
40
- Subject: {subject}
41
- Grade Level: {grade_level}
42
- Language: {language}
43
- Number of Students: {students}
44
- Duration: {duration}
45
- Teacher Name: {teacher_name}
46
- Date: {today}
47
- """
48
-
49
- response = client.models.generate_content(model="gemini-2.5-flash", contents=prompt)
50
- plan_text = response.text.strip()
51
-
52
- # ✅ Create Word document
53
- doc = Document()
54
-
55
- # Header: Department Name (Green)
56
- header_para = doc.add_paragraph()
57
- run = header_para.add_run("School Education Department, Gilgit-Baltistan")
58
- run.bold = True
59
- run.font.size = Pt(14)
60
- run.font.color.rgb = RGBColor(0, 128, 0)
61
- header_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
62
-
63
- doc.add_paragraph("")
64
-
65
- # Input Table
66
- table = doc.add_table(rows=7, cols=2)
67
- table.style = 'Table Grid'
68
- fields = ["Grade Level", "Subject", "Topic", "Number of Students", "Duration", "Teacher Name", "Date"]
69
- values = [grade_level, subject, topic, students, duration, teacher_name, today]
70
-
71
- for i in range(7):
72
- row = table.rows[i]
73
- row.cells[0].text = fields[i]
74
- row.cells[1].text = str(values[i])
75
-
76
- doc.add_paragraph("")
77
-
78
- # Lesson Plan Text
79
- p = doc.add_paragraph(plan_text)
80
- if language == "Urdu":
81
- p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
82
  else:
83
- p.alignment = WD_ALIGN_PARAGRAPH.LEFT
84
-
85
- # ✅ File Paths
86
- if language == "Urdu":
87
- file_path = os.path.join(OUTPUT_DIR, "SMART_Lesson_Plan_Urdu.docx")
88
- else:
89
- file_path = os.path.join(OUTPUT_DIR, "SMART_Lesson_Plan_English.docx")
90
-
91
- doc.save(file_path)
92
- return file_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
 
95
  # ✅ Gradio UI
96
- def ui():
97
- with gr.Blocks(theme="soft") as demo:
98
- gr.Markdown(
99
- "<h1 style='text-align:center; color:#2C3E50;'>SMART Lesson Plan Generator</h1>"
100
- "<h3 style='text-align:center; color:#16A085;'>Developed by Najaf Ali Sharqi</h3>"
101
- )
102
-
103
- with gr.Row():
104
- language = gr.Dropdown(
105
- label="Select Language",
106
- choices=["English", "Urdu"],
107
- value="English"
108
- )
109
- grade_level = gr.Dropdown(
110
- label="Select Grade Level",
111
- choices=[
112
- "Grade 1","Grade 2","Grade 3","Grade 4","Grade 5",
113
- "Grade 6","Grade 7","Grade 8","Grade 9","Grade 10",
114
- "Grade 11","Grade 12"
115
- ],
116
- value="Grade 9"
117
- )
118
 
119
  with gr.Row():
 
120
  subject = gr.Dropdown(
121
- label="Select Subject",
122
- choices=[
123
- "English","Urdu","Islamiyat","Pakistan Studies",
124
- "Mathematics","Physics","Chemistry","Biology",
125
- "Computer Science","General Science","Social Studies",
126
- "History","Geography","Economics","Civics","Education",
127
- "Environmental Studies","Arabic","Drawing","Home Economics"
128
  ],
129
- value="Chemistry"
130
  )
131
- topic = gr.Textbox(label="Enter Topic (e.g., Ionic Bond)")
132
 
133
- with gr.Row():
134
- students = gr.Number(label="Number of Students", value=30)
135
- duration = gr.Textbox(label="Duration (e.g., 40 minutes)", value="40 minutes")
136
-
137
- teacher_name = gr.Textbox(label="Teacher Name")
 
138
 
139
  generate_btn = gr.Button("Generate Lesson Plan")
140
- output_file = gr.File(label="Download Lesson Plan")
141
 
142
  generate_btn.click(
143
  generate_lesson_plan,
144
- inputs=[language, grade_level, subject, topic, students, duration, teacher_name],
145
- outputs=output_file,
146
  )
147
 
148
  return demo
149
 
150
- demo = ui()
151
- demo.launch()
 
 
 
1
  import gradio as gr
 
2
  from docx import Document
3
  from docx.shared import Pt, RGBColor
4
+ from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
5
  import os
6
+ from datetime import datetime
7
+
8
+ # ✅ Generate English lesson plan in .docx format
9
+ def generate_lesson_plan(grade, subject, topic, num_students, duration, teacher_name, date, language):
10
+
11
+ # ✅ Allowed temporary folder
12
+ output_dir = "/tmp"
13
+ os.makedirs(output_dir, exist_ok=True)
14
+
15
+ if language == "English":
16
+ file_path = os.path.join(output_dir, "SMART_Lesson_Plan_English.docx")
17
+ doc = Document()
18
+
19
+ # Header
20
+ header = doc.add_paragraph("School Education Department, Gilgit-Baltistan")
21
+ header.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
22
+ run = header.runs[0]
23
+ run.font.color.rgb = RGBColor(0, 128, 0)
24
+ run.bold = True
25
+ run.font.size = Pt(14)
26
+
27
+ # Title
28
+ title = doc.add_paragraph("SMART LESSON PLAN")
29
+ title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
30
+ title.runs[0].bold = True
31
+ title.runs[0].font.size = Pt(16)
32
+
33
+ # Table for details
34
+ table = doc.add_table(rows=6, cols=2)
35
+ details = [
36
+ ("Grade Level:", grade),
37
+ ("Subject:", subject),
38
+ ("Topic:", topic),
39
+ ("Number of Students:", str(num_students)),
40
+ ("Duration:", duration),
41
+ ("Teacher Name:", teacher_name or ""),
42
+ ]
43
+ for i, (key, value) in enumerate(details):
44
+ row = table.rows[i]
45
+ row.cells[0].text = key
46
+ row.cells[1].text = value
47
+
48
+ doc.add_paragraph(f"Date: {date}")
49
+
50
+ # SLOs Section
51
+ doc.add_paragraph("\nSLOs (Student Learning Outcomes)", style="List Bullet").runs[0].bold = True
52
+ doc.add_paragraph("By the end of this lesson, students will be able to:")
53
+ doc.add_paragraph("1. Define ionic and covalent bonds.")
54
+ doc.add_paragraph("2. Identify examples of both bond types.")
55
+ doc.add_paragraph("3. Predict the type of bond formed between given elements.")
56
+
57
+ # Content Section
58
+ doc.add_paragraph("\nContent to be Delivered", style="List Bullet").runs[0].bold = True
59
+ doc.add_paragraph("The teacher will explain the concept of ionic and covalent bonding using examples such as NaCl and H2O. Students will observe diagrams and discuss the differences between the two bond types.")
60
+
61
+ # Home Assignment
62
+ doc.add_paragraph("\nHome Assignment", style="List Bullet").runs[0].bold = True
63
+ doc.add_paragraph("Students will write definitions and examples of both ionic and covalent bonds in their notebooks.")
64
+
65
+ # Footer (only if multiple pages)
66
+ section = doc.sections[0]
67
+ footer = section.footer.paragraphs[0]
68
+ footer.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
69
+ footer.text = "Developed by Najaf Ali Sharqi"
70
+
71
+ doc.save(file_path)
72
+ return file_path
73
+
74
+ # ✅ Urdu version
 
 
 
 
 
 
75
  else:
76
+ file_path = os.path.join(output_dir, "SMART_Lesson_Plan_Urdu.docx")
77
+ doc = Document()
78
+
79
+ # Header
80
+ header = doc.add_paragraph("School Education Department, Gilgit-Baltistan")
81
+ header.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
82
+ run = header.runs[0]
83
+ run.font.color.rgb = RGBColor(0, 128, 0)
84
+ run.bold = True
85
+ run.font.size = Pt(14)
86
+
87
+ # Title
88
+ title = doc.add_paragraph("اسمارٹ لیسن پلان")
89
+ title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
90
+ title.runs[0].bold = True
91
+ title.runs[0].font.size = Pt(16)
92
+
93
+ # Input Table (in Urdu)
94
+ table = doc.add_table(rows=6, cols=2)
95
+ details = [
96
+ ("درجہ:", grade),
97
+ ("مضمون:", subject),
98
+ ("موضوع:", topic),
99
+ ("طلباء کی تعداد:", str(num_students)),
100
+ ("دورانیہ:", duration),
101
+ ("استاد کا نام:", teacher_name or ""),
102
+ ]
103
+ for i, (key, value) in enumerate(details):
104
+ row = table.rows[i]
105
+ row.cells[0].text = key
106
+ row.cells[1].text = value
107
+
108
+ doc.add_paragraph(f"تاریخ: {date}")
109
+
110
+ # SLOs Section
111
+ p = doc.add_paragraph("\nتعلیمی مقاصد (SLOs)")
112
+ p.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
113
+ p.runs[0].bold = True
114
+ doc.add_paragraph("سبق کے اختتام پر طلباء اس قابل ہوں گے کہ:\n۱۔ آئنک اور کوویلنٹ بند کی تعریف بیان کر سکیں۔\n۲۔ دونوں اقسام کی مثالیں شناخت کر سکیں۔\n۳۔ دیے گئے عناصر کے درمیان بننے والے بند کی قسم کی پیش گوئی کر سکیں۔").alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
115
+
116
+ # Content Section
117
+ p = doc.add_paragraph("\nپڑھائے جانے والا مواد")
118
+ p.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
119
+ p.runs[0].bold = True
120
+ doc.add_paragraph("استاد آئنک اور کوویلنٹ بند کے تصورات کی وضاحت مثالوں جیسے NaCl اور H2O کے ذریعے کرے گا۔ طلباء تصویری خاکے دیکھیں گے اور دونوں اقسام کے درمیان فرق پر گفتگو کریں گے۔").alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
121
+
122
+ # Home Assignment Section
123
+ p = doc.add_paragraph("\nگھریلو مشق")
124
+ p.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
125
+ p.runs[0].bold = True
126
+ doc.add_paragraph("طلباء آئنک اور کوویلنٹ بند کی تعریف اور مثالیں اپنی کاپیوں میں لکھیں گے۔").alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
127
+
128
+ # Footer
129
+ section = doc.sections[0]
130
+ footer = section.footer.paragraphs[0]
131
+ footer.text = "Developed by Najaf Ali Sharqi"
132
+ footer.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
133
+
134
+ doc.save(file_path)
135
+ return file_path
136
 
137
 
138
  # ✅ Gradio UI
139
+ def build_ui():
140
+ with gr.Blocks(title="SMART Lesson Plan Generator") as demo:
141
+ gr.Markdown("### 🧠 **SMART Lesson Plan Generator** \nDeveloped by **Najaf Ali Sharqi**")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
  with gr.Row():
144
+ grade = gr.Dropdown(["Grade 1", "Grade 2", "Grade 3", "Grade 4", "Grade 5", "Grade 6", "Grade 7", "Grade 8", "Grade 9", "Grade 10", "Grade 11", "Grade 12"], label="Select Grade")
145
  subject = gr.Dropdown(
146
+ [
147
+ "Urdu", "English", "Mathematics", "Science", "Physics", "Chemistry", "Biology", "Computer Science",
148
+ "Pakistan Studies", "Islamic Studies", "History", "Geography", "Civics", "Social Studies", "General Knowledge"
 
 
 
 
149
  ],
150
+ label="Select Subject"
151
  )
 
152
 
153
+ topic = gr.Textbox(label="Enter Topic", placeholder="e.g., Ionic Bond")
154
+ num_students = gr.Number(label="Number of Students", value=30)
155
+ duration = gr.Textbox(label="Duration", placeholder="e.g., 40 minutes")
156
+ teacher_name = gr.Textbox(label="Teacher Name", placeholder="Enter your name")
157
+ date = gr.Textbox(label="Date", value=str(datetime.now().date()))
158
+ language = gr.Dropdown(["English", "Urdu"], label="Select Language Version")
159
 
160
  generate_btn = gr.Button("Generate Lesson Plan")
161
+ output_file = gr.File(label="Download Your Lesson Plan")
162
 
163
  generate_btn.click(
164
  generate_lesson_plan,
165
+ inputs=[grade, subject, topic, num_students, duration, teacher_name, date, language],
166
+ outputs=output_file
167
  )
168
 
169
  return demo
170
 
171
+
172
+ if __name__ == "__main__":
173
+ demo = build_ui()
174
+ demo.launch()