Spaces:
No application file

Turbiling commited on
Commit
510ac2f
·
verified ·
1 Parent(s): afaf86f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -122
app.py CHANGED
@@ -1,10 +1,5 @@
1
  import os
2
  import gradio as gr
3
- from docx import Document
4
- from docx.shared import Pt, Inches
5
- from docx.enum.text import WD_ALIGN_PARAGRAPH
6
- from docx.oxml import OxmlElement
7
- from docx.oxml.ns import qn
8
  import datetime
9
  from groq import Groq
10
 
@@ -12,155 +7,175 @@ from groq import Groq
12
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
13
  client = Groq(api_key=GROQ_API_KEY)
14
 
15
-
16
- # ✅ Urdu Nastaliq Font Style (Jameel or similar)
17
- def set_urdu_font(run):
18
- run.font.name = "Jameel Noori Nastaleeq"
19
- r = run._element
20
- r.rPr.rFonts.set(qn('w:eastAsia'), "Jameel Noori Nastaleeq")
21
-
22
-
23
- # ✅ Generate Lesson Plan Function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
25
- # ✅ Define model based on language
26
  if language == "Urdu":
27
  model_name = "openai/gpt-oss-120b"
28
  else:
29
  model_name = "llama-3.1-8b-instant"
30
 
31
- # ✅ Create prompt
32
  if language == "English":
33
  prompt = f"""
34
- Generate a comprehensive daily lesson plan in English.
35
- Class: {class_}
36
- Subject: {subject}
37
- Topic: {topic}
38
- Students: {students}
39
- Duration: {duration}
40
- Date: {date}
41
- Teacher: {teacher}
42
-
43
- The plan should include:
44
- 1. Learning Outcomes (SLOs)
45
- 2. Introduction
46
- 3. Teaching Methodology
47
- 4. Activities
48
- 5. Assessment
49
- 6. Reflection
50
- 7. Home Assignment aligned with SLOs.
51
- """
52
  else:
53
  prompt = f"""
54
- درج ذیل تفصیلات کی بنیاد پر ایک جامع، خوبصورت اور بامعنی روزانہ اسباق کا منصوبہ اردو زبان میں تیار کریں۔
55
- جماعت: {class_}
56
- مضمون: {subject}
57
- عنوان: {topic}
58
- طلباء کی تعداد: {students}
59
- دورانیہ: {duration}
60
- تاریخ: {date}
61
- معلم: {teacher}
62
-
63
- منصوبے میں درج ذیل اجزاء لازمی شامل ہوں:
64
- ۱۔ تعلیمی مقاصد (SLOs)
65
- ۲۔ تعارف سبق
66
- ۳۔ تدریسی طریقہ کار
67
- ۴۔ تعلیمی سرگرمیاں
68
- ۵۔ تشخیص
69
- ۶۔ عکاسی (Reflection)
70
- ۷۔ گھریلو کام (Home Assignment) جو درج ذیل خوبیوں کا حامل ہو:
71
- • مناسب طوالت رکھتا ہو۔
72
- • منطقی ترتیب رکھتا ہو۔
73
- • طلباء میں تخلیقی صلاحیت پیدا کرے۔
74
- • دلچسپ اور پرکشش ہو۔
75
- • طلباء کی ذہنی سطح اور جماعت کے مطابق ہو۔
76
- • ایک سے دو گھنٹوں میں مکمل کیا جا سکے۔
77
- """
78
-
79
- # ✅ Get response from Groq API
 
 
80
  response = client.chat.completions.create(
81
  model=model_name,
82
  messages=[{"role": "user", "content": prompt}],
 
83
  )
84
-
85
  lesson_text = response.choices[0].message.content.strip()
86
 
87
- # ✅ Create Word Document
88
- doc = Document()
 
89
 
90
- if language == "English":
91
- doc.add_heading("Daily Lesson Plan", level=1)
92
- else:
93
- p = doc.add_paragraph("یومیہ تدریسی منصوبہ")
94
- p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
95
- run = p.runs[0]
96
- set_urdu_font(run)
97
- run.bold = True
98
 
99
- # ✅ Table (Top Info)
100
- table = doc.add_table(rows=6, cols=2)
101
- table.autofit = True
102
 
103
- if language == "English":
104
- info = [
105
- ("Class:", class_),
106
- ("Subject:", subject),
107
- ("Topic:", topic),
108
- ("Number of Students:", students),
109
- ("Duration:", duration),
110
- ("Teacher:", teacher),
111
- ]
112
- else:
113
- info = [
114
- ("جماعت:", class_),
115
- ("مضمون:", subject),
116
- ("عنوان:", topic),
117
- ("طلباء کی تعداد:", students),
118
- ("دورانیہ:", duration),
119
- ("معلم:", teacher),
120
- ]
121
-
122
- for i, (key, value) in enumerate(info):
123
- cells = table.rows[i].cells
124
- if language == "Urdu":
125
- cells[0].text = value
126
- cells[1].text = key
127
- else:
128
- cells[0].text = key
129
- cells[1].text = value
130
-
131
- # ✅ Add Lesson Plan Text
132
- doc.add_paragraph("")
133
- if language == "English":
134
- p = doc.add_paragraph(lesson_text)
135
- else:
136
- p = doc.add_paragraph(lesson_text)
137
- p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
138
- for run in p.runs:
139
- set_urdu_font(run)
140
 
141
- # ✅ Save File
142
- file_name = f"Lesson_Plan_{language}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
143
- doc.save(file_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
  return file_name
146
 
147
 
148
- # ✅ Gradio UI
149
- interface = gr.Interface(
150
  fn=generate_lesson_plan,
151
  inputs=[
152
- gr.Dropdown(["English", "Urdu"], label="Select Language"),
153
  gr.Textbox(label="Class / جماعت"),
154
  gr.Textbox(label="Subject / مضمون"),
155
  gr.Textbox(label="Topic / عنوان"),
156
  gr.Textbox(label="Number of Students / طلباء کی تعداد"),
157
- gr.Textbox(label="Duration / دورانیہ"),
158
  gr.Textbox(label="Date / تاریخ", value=str(datetime.date.today())),
159
  gr.Textbox(label="Teacher Name / معلم کا نام"),
160
  ],
161
- outputs=gr.File(label="Download Lesson Plan"),
162
- title="AI Lesson Plan Generator",
163
- description="Generate bilingual (English / Urdu) lesson plans powered by Groq API and open models.",
164
  )
165
 
166
- interface.launch()
 
 
1
  import os
2
  import gradio as gr
 
 
 
 
 
3
  import datetime
4
  from groq import Groq
5
 
 
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": "عنوان",
16
+ "Students": "طلباء",
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):
30
+ for eng, urdu in transliteration_map.items():
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}
57
+ Topic: {topic}
58
+ Number of Students: {students}
59
+ Duration: {duration}
60
+ Date: {date}
61
+ Teacher: {teacher}
62
+ """
63
  else:
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}
85
+ عنوان: {topic}
86
+ طلباء کی تعداد: {students}
87
+ دورانیہ: {duration}
88
+ تاریخ: {date}
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}],
96
+ temperature=0.7,
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
 
107
+ # یومیہ تدریسی منصوبہ (BOPPPS ماڈل)
 
 
108
 
109
+ | کلیدی معلومات | تفصیل |
110
+ |----------------|--------|
111
+ | جماعت | {class_} |
112
+ | مضمون | {subject} |
113
+ | عنوان | {topic} |
114
+ | طلباء کی تعداد | {students} |
115
+ | دورانیہ | {duration} |
116
+ | تاریخ | {date} |
117
+ | معلم | {teacher} |
118
+
119
+ ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
181
+