Turbiling commited on
Commit
2021d53
·
verified ·
1 Parent(s): 970f969

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -107
app.py CHANGED
@@ -3,142 +3,173 @@ import gradio as gr
3
  import datetime
4
  from groq import Groq
5
  from docx import Document
6
- import tempfile
 
7
 
8
- # ✅ Initialize Groq API
9
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
10
  client = Groq(api_key=GROQ_API_KEY)
11
 
 
12
  # ✅ Function to generate lesson plan
13
- def generate_lesson_plan(language, grade, subject, topic, students, duration, date, teacher, theme):
14
  if language == "Urdu":
15
- model = "openai/gpt-oss-120b"
16
- prompt = f"""
17
- آپ ایک پیشہ ور استاد ہیں۔ درج ذیل معلومات کی بنیاد پر ایک مکمل سبق کا منصوبہ اردو میں تیار کریں۔
18
- براہ کرم ہر لفظ اردو حروف میں لکھیں، انگریزی حروف استعمال نہ کریں۔
19
-
20
- جماعت: {grade}
21
- مضمون: {subject}
22
- عنوان: {topic}
23
- طلبہ کی تعداد: {students}
24
- دورانیہ: {duration} منٹ
25
- تاریخ: {date}
26
- استاد کا نام: {teacher}
27
-
28
- سبق کا منصوبہ BOPPPS ماڈل کے مطابق ہو:
29
- 1. توجہ حاصل کرنے کا مرحلہ (Bridge-in)
30
- 2. پیشگی جانچ (Pre-Assessment)
31
- 3. سیکھنے کے نتائج (Learning Outcomes)
32
- 4. شراکتی سیکھنے کی سرگرمیاں (Participatory Learning Activities)
33
- 5. بعد از تدریس جانچ (Post-Assessment)
34
- 6. خلاصہ (Summary)
35
- 7. گھریلو کام (Home Assignment) جو درج ذیل شرائط پوری کرے:
36
- - مناسب طوالت ہو
37
- - منطقی ترتیب ہو
38
- - طلبہ میں تخلیقی صلاحیت پیدا کرے
39
- - دلچسپ ہو
40
- - ذہنی و جماعتی سطح کے مطابق ہو
41
- - ۱ تا ۲ گھنٹوں میں مکمل ہوسکے
42
- """
43
  else:
44
- model = "llama-3.1-8b-instant"
45
- prompt = f"""
46
- You are a professional teacher. Based on the following details, generate a complete Lesson Plan in English using the BOPPPS Model.
47
 
48
- Class: {grade}
49
- Subject: {subject}
50
- Topic: {topic}
51
- Number of Students: {students}
52
- Duration: {duration} minutes
53
- Date: {date}
54
- Teacher's Name: {teacher}
55
-
56
- Lesson Plan Structure:
57
- 1. Bridge-in
58
  2. Pre-Assessment
59
  3. Learning Outcomes
60
  4. Participatory Learning Activities
61
  5. Post-Assessment
62
  6. Summary
63
- 7. Home Assignment aligned with SLOs (reasonable length, logical order, creative, interesting, suitable for students’ level, and completable in 1–2 hours)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  """
65
 
66
- # ✅ Generate via Groq
67
- completion = client.chat.completions.create(
68
- model=model,
69
- messages=[
70
- {"role": "system", "content": "You are a professional lesson plan creator."},
71
- {"role": "user", "content": prompt}
72
- ],
73
  )
 
74
 
75
- response = completion.choices[0].message.content
76
- footer = "\n\n---\n**Developed by Najaf Ali Sharqi**"
77
- final_text = response + footer
78
-
79
- # ✅ Save as Markdown for Urdu / DOCX for English
80
- if language == "Urdu":
81
- file_ext = ".md"
82
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=file_ext)
83
- with open(temp_file.name, "w", encoding="utf-8") as f:
84
- f.write(final_text)
85
- else:
86
- file_ext = ".docx"
87
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=file_ext)
88
  doc = Document()
89
- doc.add_heading("Lesson Plan", level=1)
90
- doc.add_paragraph(final_text)
 
 
 
 
 
 
 
 
 
91
  doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
92
- doc.save(temp_file.name)
93
 
94
- # ✅ Adjust background color theme
95
- if theme == "Dark":
96
- theme_css = "background-color:#111;color:#fff;padding:10px;border-radius:10px;"
97
- else:
98
- theme_css = "background-color:#fdfdfd;color:#000;padding:10px;border-radius:10px;"
99
 
100
- display_html = f"<div style='{theme_css}'>{final_text.replace('\n', '<br>')}</div>"
 
101
 
102
- return display_html, temp_file.name
 
 
 
103
 
104
- # ✅ Dropdown options
105
- grades = [f"Grade {i}" for i in range(1, 17)]
106
- subjects = [
107
- "Urdu", "English", "Arabic", "Science", "Mathematics",
108
- "Biology", "Chemistry", "Physics", "Zoology", "Botany",
109
- "Civics", "Islamiat"
110
- ]
111
 
112
- # ✅ Build the Gradio app
113
- with gr.Blocks(title="AI Lesson Plan Generator") as app:
114
- gr.Markdown("## 🧠 **AI Lesson Plan Generator (English / Urdu)**\nDeveloped by **Najaf Ali Sharqi**")
 
 
 
 
 
 
115
 
116
- with gr.Row():
117
- language = gr.Dropdown(["English", "Urdu"], label="Language / زبان", value="English")
118
- grade = gr.Dropdown(grades, label="Class / جماعت")
119
- subject = gr.Dropdown(subjects, label="Subject / مضمون")
120
- theme = gr.Radio(["Light", "Dark"], label="Screen Theme / سکرین موڈ", value="Light")
121
 
122
- with gr.Row():
123
- topic = gr.Textbox(label="Topic / عنوان", placeholder="Enter topic")
124
- students = gr.Slider(1, 200, step=1, label="No. of Students / طلبہ کی تعداد")
125
- duration = gr.Slider(30, 60, step=5, label="Time Duration (minutes) / دورانیہ (منٹ)")
126
 
127
- with gr.Row():
128
- date = gr.Textbox(label="Date / تاریخ", placeholder="YYYY-MM-DD", value=str(datetime.date.today()))
129
- teacher = gr.Textbox(label="Teacher's Name / استاد کا نام")
130
 
131
- generate_btn = gr.Button("🚀 Generate Lesson Plan / سبق کا منصوبہ تیار کریں")
 
 
 
 
 
132
 
133
- output_display = gr.HTML(label="Generated Lesson Plan")
134
- file_output = gr.File(label="Download Lesson Plan File")
135
 
136
- generate_btn.click(
137
- fn=generate_lesson_plan,
138
- inputs=[language, grade, subject, topic, students, duration, date, teacher, theme],
139
- outputs=[output_display, file_output],
140
- )
141
 
142
- # ✅ Launch the app
143
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
 
3
  import datetime
4
  from groq import Groq
5
  from docx import Document
6
+ from docx.shared import Pt
7
+ from tempfile import NamedTemporaryFile
8
 
9
+ # ✅ Groq API Key
10
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
11
  client = Groq(api_key=GROQ_API_KEY)
12
 
13
+
14
  # ✅ Function to generate lesson plan
15
+ def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher, theme):
16
  if language == "Urdu":
17
+ model_name = "openai/gpt-oss-120b"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  else:
19
+ model_name = "llama-3.1-8b-instant"
 
 
20
 
21
+ # ✅ Prompt templates
22
+ if language == "English":
23
+ prompt = f"""
24
+ You are an expert lesson plan designer.
25
+ Create a complete **BOPPPS Model Lesson Plan** in English with the following sections:
26
+ 1. Bridge-in
 
 
 
 
27
  2. Pre-Assessment
28
  3. Learning Outcomes
29
  4. Participatory Learning Activities
30
  5. Post-Assessment
31
  6. Summary
32
+ 7. Home Assignment aligned with SLOs
33
+
34
+ Make it logical, creative, and suitable for grade level.
35
+ Do not repeat input details in body text.
36
+ Include only one concise header table of details.
37
+
38
+ Class: {class_}
39
+ Subject: {subject}
40
+ Topic: {topic}
41
+ Students: {students}
42
+ Duration: {duration}
43
+ Date: {date}
44
+ Teacher: {teacher}
45
+ """
46
+ else:
47
+ prompt = f"""
48
+ آپ ایک ماہر استاد ہیں۔ درج ذیل معلومات کی بنیاد پر **BOPPPS ماڈل** کے مطابق ایک مکمل روزانہ کا تدریسی منصوبہ اردو زبان میں تیار کریں۔
49
+ انگریزی حروف استعمال نہ کریں، صرف اردو رسم الخط استعمال کریں۔
50
+ درج ذیل حصے لازمی شامل ہوں:
51
+ ۱۔ ابتدائی تعارف (Bridge-in)
52
+ ۲۔ ابتدائی تشخیص (Pre-Assessment)
53
+ ۳۔ تعلیمی مقاصد (Learning Outcomes)
54
+ ۴۔ شراکتی تعلیمی سرگرمیاں (Participatory Learning Activities)
55
+ ۵۔ بعد از تدریس تشخیص (Post-Assessment)
56
+ ۶۔ خلاصہ (Summary)
57
+ ۷۔ گھریلو کام (SLOs کے مطابق)
58
+
59
+ جماعت: {class_}
60
+ مضمون: {subject}
61
+ عنوان: {topic}
62
+ طلباء کی تعداد: {students}
63
+ دورانیہ: {duration}
64
+ تاریخ: {date}
65
+ معلم: {teacher}
66
  """
67
 
68
+ # ✅ Generate text
69
+ response = client.chat.completions.create(
70
+ model=model_name,
71
+ messages=[{"role": "user", "content": prompt}],
72
+ temperature=0.7,
 
 
73
  )
74
+ lesson_text = response.choices[0].message.content.strip()
75
 
76
+ # ✅ ENGLISH VERSION — create .docx
77
+ if language == "English":
 
 
 
 
 
 
 
 
 
 
 
78
  doc = Document()
79
+ doc.add_heading("BOPPPS Lesson Plan", level=1)
80
+
81
+ table = doc.add_table(rows=7, cols=2)
82
+ table.style = "Table Grid"
83
+ headers = ["Class", "Subject", "Topic", "Students", "Duration", "Date", "Teacher"]
84
+ values = [class_, subject, topic, students, duration, date, teacher]
85
+ for i in range(7):
86
+ table.cell(i, 0).text = headers[i]
87
+ table.cell(i, 1).text = str(values[i])
88
+
89
+ doc.add_paragraph("\n" + lesson_text)
90
  doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
 
91
 
92
+ temp_file = NamedTemporaryFile(delete=False, suffix=".docx")
93
+ doc.save(temp_file.name)
 
 
 
94
 
95
+ final_text = f"✅ English lesson plan generated successfully for {subject} (Grade {class_})."
96
+ file_output = temp_file.name
97
 
98
+ # ✅ URDU VERSION — create .md
99
+ else:
100
+ urdu_md = f"""
101
+ <div dir="rtl" align="right">
102
 
103
+ # یومیہ تدریسی منصوبہ (BOPPPS ماڈل)
 
 
 
 
 
 
104
 
105
+ | کلیدی معلومات | تفصیل |
106
+ |----------------|--------|
107
+ | جماعت | {class_} |
108
+ | مضمون | {subject} |
109
+ | عنوان | {topic} |
110
+ | طلباء کی تعداد | {students} |
111
+ | دورانیہ | {duration} |
112
+ | تاریخ | {date} |
113
+ | معلم | {teacher} |
114
 
115
+ ---
 
 
 
 
116
 
117
+ {lesson_text}
 
 
 
118
 
119
+ ---
 
 
120
 
121
+ **مرتب کردہ: نجف علی شرقی**
122
+ </div>
123
+ """
124
+ temp_file = NamedTemporaryFile(delete=False, suffix=".md", mode="w", encoding="utf-8")
125
+ temp_file.write(urdu_md)
126
+ temp_file.close()
127
 
128
+ final_text = f"✅ اردو تدریسی منصوبہ برائے مضمون {subject} کامیابی سے تیار ہو گیا۔"
129
+ file_output = temp_file.name
130
 
131
+ # ✅ Theme customization (brightness)
132
+ if theme == "Dark":
133
+ theme_css = "background-color:#111;color:#fff;padding:10px;border-radius:10px;"
134
+ else:
135
+ theme_css = "background-color:#fdfdfd;color:#000;padding:10px;border-radius:10px;"
136
 
137
+ safe_html = final_text.replace("\n", "<br>")
138
+ display_html = f"<div style='{theme_css}'>{safe_html}</div>"
139
+
140
+ return display_html, file_output
141
+
142
+
143
+ # ✅ Dropdowns and Interface
144
+ demo = gr.Interface(
145
+ fn=generate_lesson_plan,
146
+ inputs=[
147
+ gr.Radio(["English", "Urdu"], label="Select Language / زبان کا انتخاب"),
148
+ gr.Dropdown([f"Grade {i}" for i in range(1, 17)], label="Class / جماعت"),
149
+ gr.Dropdown(
150
+ ["Urdu", "English", "Arabic", "Science", "Mathematics", "Biology",
151
+ "Chemistry", "Physics", "Zoology", "Botany", "Civics", "Islamiat"],
152
+ label="Subject / مضمون"
153
+ ),
154
+ gr.Textbox(label="Topic / عنوان"),
155
+ gr.Slider(1, 200, value=30, step=1, label="Number of Students / طلباء کی تعداد"),
156
+ gr.Slider(30, 60, value=40, step=5, label="Duration (minutes) / دورانیہ (منٹ)"),
157
+ gr.Textbox(label="Date / تاریخ", value=str(datetime.date.today())),
158
+ gr.Textbox(label="Teacher Name / معلم کا نام"),
159
+ gr.Radio(["Light", "Dark"], label="Theme / پس منظر کا انتخاب", value="Light"),
160
+ ],
161
+ outputs=[
162
+ gr.HTML(label="📄 Lesson Plan Preview"),
163
+ gr.File(label="⬇️ Download File (.docx or .md)")
164
+ ],
165
+ title="📘 AI BOPPPS Lesson Plan Generator (Urdu + English)",
166
+ description=(
167
+ "⚙️ Developed by **Najaf Ali Sharqi**. <br>"
168
+ "یہ ایپ خودکار طور پر اردو یا انگریزی میں مکمل BOPPPS ماڈل کے مطابق یومیہ تدریسی منصوبہ تیار کرتی ہے۔<br>"
169
+ "اردو ورژن `.md` فارمیٹ میں جبکہ انگریزی ورژن `.docx` میں ڈاؤن لوڈ ہوتا ہے۔"
170
+ ),
171
+ theme="default"
172
+ )
173
+
174
+ demo.launch()
175