Turbiling commited on
Commit
b1bbb0c
·
verified ·
1 Parent(s): 12a724e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -84
app.py CHANGED
@@ -1,15 +1,16 @@
1
  import os
2
  import gradio as gr
3
  import datetime
 
4
  from groq import Groq
5
 
6
- # ✅ Get Groq API Key
7
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
8
  client = Groq(api_key=GROQ_API_KEY)
9
 
10
- # ✅ English → Urdu transliteration for keywords
11
  transliteration_map = {
12
- "Lesson Plan": "لیسن پلان",
13
  "Class": "جماعت",
14
  "Subject": "مضمون",
15
  "Topic": "عنوان",
@@ -17,13 +18,6 @@ transliteration_map = {
17
  "Duration": "دورانیہ",
18
  "Teacher": "معلم",
19
  "Date": "تاریخ",
20
- "Bridge-in": "ابتدائی تعارف",
21
- "Pre-Assessment": "ابتدائی تشخیص",
22
- "Learning Outcomes": "تعلیمی مقاصد",
23
- "Participatory Learning Activities": "شراکتی تعلیمی سرگرمیاں",
24
- "Post-Assessment": "بعد از تدریس تشخیص",
25
- "Summary": "خلاصہ",
26
- "Home Assignment aligned with SLOs": "گھریلو کام (SLOs کے مطابق)",
27
  }
28
 
29
  def translate_to_urdu_script(text):
@@ -31,26 +25,23 @@ def translate_to_urdu_script(text):
31
  text = text.replace(eng, urdu)
32
  return text
33
 
34
-
35
- # ✅ Lesson Plan Generator Function
36
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
37
- if language == "Urdu":
38
- model_name = "openai/gpt-oss-120b"
39
- else:
40
- model_name = "llama-3.1-8b-instant"
41
 
42
- # ✅ Prompts for both languages
43
  if language == "English":
44
  prompt = f"""
45
- You are an expert lesson plan designer.
46
- Create a complete **BOPPPS Model Lesson Plan** in English with the following sections:
47
- 1. Bridge-in: a short and engaging introduction
48
- 2. Pre-Assessment: 2 diagnostic questions
49
- 3. Learning Outcomes: 3 measurable objectives
50
- 4. Participatory Learning Activities: detailed student-centered activity
51
- 5. Post-Assessment: 2–3 evaluative questions
52
- 6. Summary: teacher-led closure paragraph
53
- 7. Home Assignment aligned with SLOs (reasonable length, logical, creative, interesting, grade-appropriate, 1–2 hours)
54
 
55
  Class: {class_}
56
  Subject: {subject}
@@ -64,21 +55,16 @@ Teacher: {teacher}
64
  prompt = f"""
65
  آپ ایک ماہر استاد اور سبق منصوبہ ساز ہیں۔
66
  ذیل کی معلومات کی بنیاد پر ایک مکمل **BOPPPS ماڈل** کے مطابق روزانہ کا تدریسی منصوبہ اردو زبان میں تیار کریں۔
67
- متن میں انگریزی حروف کا استعمال ہرگز نہ کریں، صرف اردو رسم الخط استعمال کریں۔
68
-
69
- منصوبے کے درج ذیل حصے لازمی شامل ہوں:
70
- ۱۔ ابتدائی تعارف (Bridge-in): ایک مختصر دلچسپ آغاز جو سبق سے جوڑ پیدا کرے۔
71
- ۲۔ ابتدائی تشخیص (Pre-assessment): دو سوالات جو طلباء کے پیشگی علم کو پرکھیں۔
72
- ۳۔ تعلیمی مقاصد (Learning Outcomes): تین واضح اور قابلِ پیمائش مقاصد۔
73
- ۴۔ شراکتی تعلیمی سرگرمیاں (Participatory Learning Activities): ایک تفصیلی اور دلچسپ سرگرمی جس میں طلباء متحرک طور پر شامل ہوں۔
74
- ۵۔ بعد از تدریس تشخیص (Post-assessment): دو تا تین سوالات جو سبق کے نتائج جانچیں۔
75
- ۶۔ خلاصہ (Summary): استاد کی جانب سے سبق کا نچوڑ اور اہم نکات کا بیان۔
76
- ۷۔ گھریلو کام (SLOs کے مطابق): ایسا کام جو درج ذیل خوبیوں کا حامل ہو:
77
- • مناسب طوالت رکھتا ہو۔
78
- • منطقی ترتیب رکھتا ہو۔
79
- • تخلیقی اور دلچسپ ہو۔
80
- • طلباء کی ذہنی سطح کے مطابق ہو۔
81
- • ایک تا دو گھنٹوں میں مکمل کیا جا سکے۔
82
 
83
  جماعت: {class_}
84
  مضمون: {subject}
@@ -89,7 +75,7 @@ Teacher: {teacher}
89
  معلم: {teacher}
90
  """
91
 
92
- # ✅ Generate from Groq API
93
  response = client.chat.completions.create(
94
  model=model_name,
95
  messages=[{"role": "user", "content": prompt}],
@@ -97,10 +83,9 @@ Teacher: {teacher}
97
  )
98
  lesson_text = response.choices[0].message.content.strip()
99
 
100
- # ✅ Urdu Post-processing
101
  if language == "Urdu":
102
  lesson_text = translate_to_urdu_script(lesson_text)
103
-
104
  urdu_md = f"""
105
  <div dir="rtl" align="right">
106
 
@@ -120,61 +105,72 @@ Teacher: {teacher}
120
 
121
  {lesson_text}
122
 
123
- </div>
124
- """
125
- file_name = f"Lesson_Plan_Urdu_BOPPPS_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
126
- with open(file_name, "w", encoding="utf-8") as f:
127
- f.write(urdu_md)
128
-
129
- else:
130
- english_md = f"""
131
- # BOPPPS Lesson Plan
132
-
133
- | Key Information | Details |
134
- |------------------|----------|
135
- | Class | {class_} |
136
- | Subject | {subject} |
137
- | Topic | {topic} |
138
- | Number of Students | {students} |
139
- | Duration | {duration} |
140
- | Date | {date} |
141
- | Teacher | {teacher} |
142
-
143
  ---
144
 
145
- ## 1. Bridge-in
146
- ## 2. Pre-Assessment
147
- ## 3. Learning Outcomes
148
- ## 4. Participatory Learning Activities
149
- ## 5. Post-Assessment
150
- ## 6. Summary
151
- ## 7. Home Assignment aligned with SLOs
152
 
153
- {lesson_text}
154
  """
155
- file_name = f"Lesson_Plan_English_BOPPPS_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
156
  with open(file_name, "w", encoding="utf-8") as f:
157
- f.write(english_md)
158
-
159
- return file_name
160
-
161
 
162
- # ✅ Gradio Interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  demo = gr.Interface(
164
  fn=generate_lesson_plan,
165
  inputs=[
166
  gr.Radio(["English", "Urdu"], label="Select Language / زبان کا انتخاب"),
167
- gr.Textbox(label="Class / جماعت"),
168
- gr.Textbox(label="Subject / مضمون"),
169
  gr.Textbox(label="Topic / عنوان"),
170
- gr.Textbox(label="Number of Students / طلباء کی تعداد"),
171
- gr.Textbox(label="Duration (minutes) / دورانیہ"),
172
- gr.Textbox(label="Date / تاریخ", value=str(datetime.date.today())),
173
  gr.Textbox(label="Teacher Name / معلم کا نام"),
174
  ],
175
- outputs=gr.File(label="📄 Download Lesson Plan (.md)"),
176
  title="📘 AI BOPPPS Lesson Plan Generator (Urdu + English)",
177
  description="یہ ایپ انگریزی یا اردو میں مکمل BOPPPS ماڈل کے مطابق لیسن پلان تیار کرتی ہے۔ اردو ورژن مکمل اردو رسم الخط میں ہوتا ہے۔",
178
  )
179
 
180
  demo.launch()
 
 
1
  import os
2
  import gradio as gr
3
  import datetime
4
+ from docx import Document
5
  from groq import Groq
6
 
7
+ # ✅ API Key
8
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
9
  client = Groq(api_key=GROQ_API_KEY)
10
 
11
+ # ✅ Urdu translation map (for minor replacements)
12
  transliteration_map = {
13
+ "Lesson Plan": "سبق کا منصوبہ",
14
  "Class": "جماعت",
15
  "Subject": "مضمون",
16
  "Topic": "عنوان",
 
18
  "Duration": "دورانیہ",
19
  "Teacher": "معلم",
20
  "Date": "تاریخ",
 
 
 
 
 
 
 
21
  }
22
 
23
  def translate_to_urdu_script(text):
 
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
+ You are an expert lesson plan designer.
37
+ Create a complete **BOPPPS Model Lesson Plan** in English with the following sections only:
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
  Class: {class_}
47
  Subject: {subject}
 
55
  prompt = f"""
56
  آپ ایک ماہر استاد اور سبق منصوبہ ساز ہیں۔
57
  ذیل کی معلومات کی بنیاد پر ایک مکمل **BOPPPS ماڈل** کے مطابق روزانہ کا تدریسی منصوبہ اردو زبان میں تیار کریں۔
58
+ متن میں انگریزی حروف کا استعمال نہ کریں۔
59
+
60
+ منصوبے کے درج ذیل حصے شامل ہوں:
61
+ ۱۔ ابتدائی تعارف
62
+ ۲۔ ابتدائی تشخیص
63
+ ۳۔ تعلیمی مقاصد
64
+ ۴۔ شراکتی تعلیمی سرگرمیاں
65
+ ۵۔ بعد از تدریس تشخیص
66
+ ۶۔ خلاصہ
67
+ ۷۔ گھریلو کام (SLOs کے مطابق): ایسا کام جو مناسب طوالت، منطقی ترتیب، تخلیقی، دلچسپ، ذہنی سطح کے مطابق اور ایک تا دو گھنٹوں میں مکمل ہو۔
 
 
 
 
 
68
 
69
  جماعت: {class_}
70
  مضمون: {subject}
 
75
  معلم: {teacher}
76
  """
77
 
78
+ # ✅ Generate from Groq
79
  response = client.chat.completions.create(
80
  model=model_name,
81
  messages=[{"role": "user", "content": prompt}],
 
83
  )
84
  lesson_text = response.choices[0].message.content.strip()
85
 
86
+ # ✅ Urdu version → Markdown
87
  if language == "Urdu":
88
  lesson_text = translate_to_urdu_script(lesson_text)
 
89
  urdu_md = f"""
90
  <div dir="rtl" align="right">
91
 
 
105
 
106
  {lesson_text}
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  ---
109
 
110
+ *تیار کردہ: نجف علی شرقی*
 
 
 
 
 
 
111
 
112
+ </div>
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
+ # ✅ English version → Word (.docx)
120
+ else:
121
+ doc = Document()
122
+ doc.add_heading("BOPPPS Lesson Plan", 0)
123
+
124
+ table = doc.add_table(rows=0, cols=2)
125
+ data = {
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
+