Turbiling commited on
Commit
305367c
·
verified ·
1 Parent(s): b1bbb0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -149
app.py CHANGED
@@ -1,176 +1,115 @@
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": "عنوان",
17
- "Students": "طلباء",
18
- "Duration": "دورانیہ",
19
- "Teacher": "معلم",
20
- "Date": "تاریخ",
21
- }
22
-
23
- def translate_to_urdu_script(text):
24
- for eng, urdu in transliteration_map.items():
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}
48
  Topic: {topic}
49
  Number of Students: {students}
50
- Duration: {duration}
51
  Date: {date}
52
- Teacher: {teacher}
53
- """
54
- else:
55
- prompt = f"""
56
- آپ ایک ماہر استاد اور سبق منصوبہ ساز ہیں۔
57
- ذیل کی معلومات کی بنیاد پر ایک مکمل **BOPPPS ماڈل** کے مطابق روزانہ کا تدریسی منصوبہ اردو زبان میں تیار کریں۔
58
- متن میں انگریزی حروف کا استعمال نہ کریں۔
59
-
60
- منصوبے کے درج ذیل حصے شامل ہوں:
61
- ۱۔ ابتدائی تعارف
62
- ۲۔ ابتدائی تشخیص
63
- ۳۔ تعلیمی مقاصد
64
- ۴۔ شراکتی تعلیمی سرگرمیاں
65
- ۵۔ بعد از تدریس تشخیص
66
- ۶۔ خلاصہ
67
- ۷۔ گھریلو کام (SLOs کے مطابق): ایسا کام جو مناسب طوالت، منطقی ترتیب، تخلیقی، دلچسپ، ذہنی سطح کے مطابق اور ایک تا دو گھنٹوں میں مکمل ہو۔
68
-
69
- جماعت: {class_}
70
- مضمون: {subject}
71
- عنوان: {topic}
72
- طلباء کی تعداد: {students}
73
- دورانیہ: {duration}
74
- تاریخ: {date}
75
- معلم: {teacher}
76
  """
77
 
78
- # ✅ Generate from Groq
79
- response = client.chat.completions.create(
80
- model=model_name,
81
- messages=[{"role": "user", "content": prompt}],
82
- temperature=0.7,
 
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
 
92
- # یومیہ تدریسی منصوبہ (BOPPPS ماڈل)
 
 
93
 
94
- | کلیدی معلومات | تفصیل |
95
- |----------------|--------|
96
- | جماعت | {class_} |
97
- | مضمون | {subject} |
98
- | عنوان | {topic} |
99
- | طلباء کی تعداد | {students} |
100
- | دورانیہ | {duration} |
101
- | تاریخ | {date} |
102
- | معلم | {teacher} |
103
 
104
- ---
 
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
 
 
 
 
1
  import os
2
  import gradio as gr
3
  import datetime
 
4
  from groq import Groq
5
 
6
+ # ✅ Initialize Groq API
7
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
8
  client = Groq(api_key=GROQ_API_KEY)
9
 
10
+ # ✅ Function to generate lesson plan
11
+ def generate_lesson_plan(language, grade, subject, topic, students, duration, date, teacher):
12
+ if language == "Urdu":
13
+ model = "openai/gpt-oss-120b"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  prompt = f"""
15
+ آپ ایک پیشہ ور استاد ہیں۔ درج ذیل معلومات کی بنیاد پر ایک مکمل سبق کا منصوبہ اردو میں تیار کریں۔
16
+ براہ کرم ہر لفظ اردو حروف میں لکھیں، انگریزی حروف استعمال نہ کریں۔
 
 
 
 
 
 
 
17
 
18
+ جماعت: {grade}
19
+ مضمون: {subject}
20
+ عنوان: {topic}
21
+ طلبہ کی تعداد: {students}
22
+ دورانیہ: {duration} منٹ
23
+ تاریخ: {date}
24
+ استاد کا نام: {teacher}
25
+
26
+ سبق کا منصوبہ BOPPPS ماڈل کے مطابق ہو:
27
+ 1. توجہ حاصل کرنے کا مرحلہ (Bridge-in)
28
+ 2. پیشگی جانچ (Pre-Assessment)
29
+ 3. سیکھنے کے نتائج (Learning Outcomes)
30
+ 4. شراکتی سیکھنے کی سرگرمیاں (Participatory Learning Activities)
31
+ 5. بعد از تدریس جانچ (Post-Assessment)
32
+ 6. خلاصہ (Summary)
33
+ 7. گھریلو کام (Home Assignment) جو درج ذیل شرائط پوری کرے:
34
+ - مناسب طوالت ہو
35
+ - منطقی ترتیب ہو
36
+ - طلبہ میں تخلیقی صلاحیت پیدا کرے
37
+ - دلچسپ ہو
38
+ - ذہنی و جماعتی سطح کے مطابق ہو
39
+ - ۱ تا ۲ گھنٹوں میں مکمل ہوسکے
40
+ """
41
+ else:
42
+ model = "llama-3.1-8b-instant"
43
+ prompt = f"""
44
+ You are a professional teacher. Based on the following details, generate a complete **Lesson Plan in English** using the **BOPPPS Model**.
45
+
46
+ Class: {grade}
47
  Subject: {subject}
48
  Topic: {topic}
49
  Number of Students: {students}
50
+ Duration: {duration} minutes
51
  Date: {date}
52
+ Teacher's Name: {teacher}
53
+
54
+ Lesson Plan Structure:
55
+ 1. Bridge-in
56
+ 2. Pre-Assessment
57
+ 3. Learning Outcomes
58
+ 4. Participatory Learning Activities
59
+ 5. Post-Assessment
60
+ 6. Summary
61
+ 7. Home Assignment aligned with SLOs (reasonable length, logical order, creative, interesting, suitable for students’ level, and completable in 1–2 hours)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  """
63
 
64
+ completion = client.chat.completions.create(
65
+ model=model,
66
+ messages=[
67
+ {"role": "system", "content": "You are a professional lesson plan creator."},
68
+ {"role": "user", "content": prompt}
69
+ ],
70
  )
 
71
 
72
+ response = completion.choices[0].message.content
 
 
 
 
73
 
74
+ # Add footer
75
+ footer = "\n\n---\n**Developed by Najaf Ali Sharqi**"
76
+ return response + footer
77
 
78
+ # ✅ Gradio Interface
79
+ grades = [f"Grade {i}" for i in range(1, 17)]
80
+ subjects = [
81
+ "Urdu", "English", "Arabic", "Science", "Mathematics",
82
+ "Biology", "Chemistry", "Physics", "Zoology", "Botany",
83
+ "Civics", "Islamiat"
84
+ ]
 
 
85
 
86
+ with gr.Blocks(title="Lesson Plan Generator") as app:
87
+ gr.Markdown("## 📘 AI Lesson Plan Generator (English / Urdu)\nDeveloped by **Najaf Ali Sharqi**")
88
 
89
+ with gr.Row():
90
+ language = gr.Dropdown(["English", "Urdu"], label="Language / زبان", value="English")
91
+ grade = gr.Dropdown(grades, label="Class / جماعت")
92
+ subject = gr.Dropdown(subjects, label="Subject / مضمون")
93
 
94
+ with gr.Row():
95
+ topic = gr.Textbox(label="Topic / عنوان", placeholder="Enter topic")
96
+ students = gr.Slider(1, 200, step=1, label="No. of Students / طلبہ کی تعداد")
97
+ duration = gr.Slider(30, 60, step=5, label="Time Duration (minutes) / دورانیہ (منٹ)")
98
 
99
+ # ✅ Replaced gr.Calendar() with gr.Textbox (date input)
100
+ with gr.Row():
101
+ date = gr.Textbox(label="Date / تاریخ", placeholder="YYYY-MM-DD", value=str(datetime.date.today()))
102
+ teacher = gr.Textbox(label="Teacher's Name / استاد کا نام")
103
 
104
+ generate_btn = gr.Button("Generate Lesson Plan / سبق کا منصوبہ تیار کریں")
 
 
 
 
 
105
 
106
+ output = gr.Markdown(label="Generated Lesson Plan")
107
+
108
+ generate_btn.click(
109
+ fn=generate_lesson_plan,
110
+ inputs=[language, grade, subject, topic, students, duration, date, teacher],
111
+ outputs=output,
112
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
+ # ✅ Launch
115
+ app.launch()