Turbiling commited on
Commit
3db928d
·
verified ·
1 Parent(s): 3fc2369

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -68
app.py CHANGED
@@ -1,103 +1,165 @@
1
  import os
2
  import gradio as gr
 
 
 
3
  from groq import Groq
4
 
5
- # ✅ Load Groq API Key
6
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
7
 
8
- # ✅ Define models
9
  URDU_MODEL = "openai/gpt-oss-120b"
10
  ENGLISH_MODEL = "llama-3.1-8b-instant"
11
 
12
 
13
- # ✅ Core Function
14
- def generate_content(language, plan_type, feature, grade, subject, topic, duration, student_level):
15
  if language == "Urdu":
16
  model = URDU_MODEL
17
  prompt = f"""
18
- آپ ایک ماہر استاد ہیں۔ براہ کرم نیچے دی گئی معلومات کی بنیاد پر SMART طرز کا {plan_type} تیار کریں۔
19
  درج ذیل حصے شامل ہوں:
20
  1. معلوماتی جدول (Input Table)
21
- 2. SMART سیکھنے کے نتائج (SLOs)
22
- 3. "پڑھایا جانے والا مواد" (Content to be delivered) — تعریفیں، کلیدی تصورات اور بنیادی اصطلاحات کے ساتھ۔
23
- 4. گھریلو کام (Home Assignment) — SLOs کے مطابق۔
24
- 5. سرپرست یا پرنسپل کے تاثرات کے لیے جگہ (Feedback by Mentor/Principal)
25
 
26
  جماعت: {grade}
27
  مضمون: {subject}
28
  موضوع: {topic}
29
  دورانیہ: {duration} منٹ
30
- طلبہ کی سطح: {student_level}
31
- اگر {feature} منتخب کیا گیا ہے تو اس کے مطابق مواد بھی تیار کریں۔
32
  """
33
  else:
34
  model = ENGLISH_MODEL
35
  prompt = f"""
36
- You are an experienced Pakistani school teacher.
37
- Please create a SMART-style {plan_type} lesson plan based on the given details.
38
 
39
- Include only the following sections:
40
- 1. Input Table (with basic information)
41
  2. SMART Learning Outcomes (SLOs)
42
- 3. "Content to be Delivered" — include definitions, key terms, and core concepts.
43
- 4. Home Assignment aligned with SLOs.
44
- 5. Feedback by Mentor/Principal (leave blank space for handwritten feedback).
45
 
46
  Grade: {grade}
47
  Subject: {subject}
48
  Topic: {topic}
49
  Duration: {duration} minutes
50
  Student Level: {student_level}
51
- Also, if {feature} is selected, generate relevant {feature} content aligned with SLOs.
52
  """
53
 
54
- try:
55
- response = client.chat.completions.create(
56
- model=model,
57
- messages=[{"role": "user", "content": prompt}],
58
- temperature=0.6,
59
- max_tokens=1800
60
- )
61
- return response.choices[0].message.content.strip()
62
- except Exception as e:
63
- return f"❌ Error: {str(e)}"
64
 
 
 
 
 
65
 
66
- # ✅ Build Gradio Interface
67
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="cyan")) as demo:
68
- gr.Markdown("## 🧠 SMART LESSON PLAN GENERATOR")
69
- gr.Markdown("**Developed by: Najaf Ali**")
 
 
70
 
71
- with gr.Row():
72
- language = gr.Dropdown(
73
- ["English", "Urdu"],
74
- label="Select Language",
75
- value="English"
76
- )
77
 
78
- plan_type = gr.Dropdown(
79
- ["SMART Lesson Plan", "Daily Lesson Plan", "Weekly Lesson Plan", "Unit Plan"],
80
- label="Select Lesson Plan Type",
81
- value="SMART Lesson Plan"
82
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- feature = gr.Dropdown(
85
- ["None", "Flashcards", "Quiz"],
86
- label="Select Additional Feature",
87
- value="None"
88
- )
89
 
90
  with gr.Row():
 
91
  grade = gr.Dropdown(
92
- [
93
- "Grade 1", "Grade 2", "Grade 3", "Grade 4", "Grade 5",
94
- "Grade 6", "Grade 7", "Grade 8", "Grade 9", "Grade 10",
95
- "Grade 11", "Grade 12"
96
- ],
97
- label="Select Class/Grade",
98
- value="Grade 8"
99
  )
100
-
101
  subject = gr.Dropdown(
102
  [
103
  "English", "Urdu", "Mathematics", "Science", "Computer",
@@ -109,22 +171,33 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="cyan")) as demo:
109
  )
110
 
111
  with gr.Row():
112
- topic = gr.Textbox(label="Enter Topic (e.g., Photosynthesis / Noun Types)")
113
- duration = gr.Number(label="Lesson Duration (minutes)", value=40)
114
- student_level = gr.Dropdown(
115
- ["Beginner", "Average", "Advanced"],
116
- label="Student Level",
117
- value="Average"
118
- )
119
 
120
  generate_btn = gr.Button("🚀 Generate SMART Lesson Plan")
121
- output = gr.Textbox(label="Generated Output", lines=25)
 
 
 
 
 
 
 
 
122
 
 
 
 
 
123
  generate_btn.click(
124
- generate_content,
125
- inputs=[language, plan_type, feature, grade, subject, topic, duration, student_level],
126
- outputs=output
127
  )
128
 
 
 
 
129
  # ✅ Launch App
130
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ import datetime
4
+ from tempfile import NamedTemporaryFile
5
+ from docx import Document
6
  from groq import Groq
7
 
8
+ # ✅ Initialize Groq client
9
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
10
 
 
11
  URDU_MODEL = "openai/gpt-oss-120b"
12
  ENGLISH_MODEL = "llama-3.1-8b-instant"
13
 
14
 
15
+ # ✅ Function: Generate SMART Lesson Plan
16
+ def generate_lesson_plan(language, grade, subject, topic, duration, student_level):
17
  if language == "Urdu":
18
  model = URDU_MODEL
19
  prompt = f"""
20
+ آپ ایک ماہر استاد ہیں۔ دی گئی معلومات کی بنیاد پر ایک SMART طرز کا تدریسی منصوبہ تیار کریں۔
21
  درج ذیل حصے شامل ہوں:
22
  1. معلوماتی جدول (Input Table)
23
+ 2. SMART تعلیمی مقاصد (SLOs)
24
+ 3. "پڑھایا جانے والا مواد" (Content to be delivered): تعریفیں، کلیدی اصطلاحات اور تصورات۔
25
+ 4. گھریلو کام (Home Assignment) — جو SLOs سے مطابقت رکھتا ہو۔
26
+ 5. سرپرست یا پرنسپل کی رائے کے لیے خالی جگہ (Feedback by Mentor/Principal)
27
 
28
  جماعت: {grade}
29
  مضمون: {subject}
30
  موضوع: {topic}
31
  دورانیہ: {duration} منٹ
32
+ طلباء کی سطح: {student_level}
 
33
  """
34
  else:
35
  model = ENGLISH_MODEL
36
  prompt = f"""
37
+ You are an expert Pakistani school teacher. Create a SMART-style lesson plan with the following structure:
 
38
 
39
+ 1. Input Table
 
40
  2. SMART Learning Outcomes (SLOs)
41
+ 3. Content to be Delivered — include definitions, key terms, and key concepts related to the topic.
42
+ 4. Home Assignment — must directly align with the SLOs.
43
+ 5. Feedback by Mentor/Principal (leave blank space for handwritten notes)
44
 
45
  Grade: {grade}
46
  Subject: {subject}
47
  Topic: {topic}
48
  Duration: {duration} minutes
49
  Student Level: {student_level}
 
50
  """
51
 
52
+ response = client.chat.completions.create(
53
+ model=model,
54
+ messages=[{"role": "user", "content": prompt}],
55
+ temperature=0.7,
56
+ max_tokens=1800
57
+ )
58
+ content = response.choices[0].message.content.strip()
 
 
 
59
 
60
+ # ✅ Create downloadable file
61
+ if language == "English":
62
+ doc = Document()
63
+ doc.add_heading("SMART LESSON PLAN", level=1)
64
 
65
+ table = doc.add_table(rows=5, cols=2)
66
+ headers = ["Grade", "Subject", "Topic", "Duration", "Student Level"]
67
+ values = [grade, subject, topic, f"{duration} minutes", student_level]
68
+ for i in range(5):
69
+ table.cell(i, 0).text = headers[i]
70
+ table.cell(i, 1).text = str(values[i])
71
 
72
+ doc.add_paragraph("\n" + content)
73
+ doc.add_paragraph("\nDeveloped by: Najaf Ali")
 
 
 
 
74
 
75
+ temp = NamedTemporaryFile(delete=False, suffix=".docx")
76
+ doc.save(temp.name)
77
+ file_path = temp.name
78
+ message = f"✅ English SMART Lesson Plan generated successfully for {subject} ({grade})."
79
+
80
+ else:
81
+ md_text = f"""
82
+ <div dir="rtl" align="right">
83
+
84
+ # 📘 SMART LESSON PLAN
85
+
86
+ | کلیدی معلومات | تفصیل |
87
+ |----------------|--------|
88
+ | جماعت | {grade} |
89
+ | مضمون | {subject} |
90
+ | عنوان | {topic} |
91
+ | دورانیہ | {duration} منٹ |
92
+ | طلبہ کی سطح | {student_level} |
93
+
94
+ ---
95
+
96
+ {content}
97
+
98
+ ---
99
+
100
+ **مرتب کردہ: نجف علی**
101
+ </div>
102
+ """
103
+ temp = NamedTemporaryFile(delete=False, suffix=".md", mode="w", encoding="utf-8")
104
+ temp.write(md_text)
105
+ temp.close()
106
+ file_path = temp.name
107
+ message = f"✅ اردو تدریسی منصوبہ ({subject}) کامیابی سے تیار ہو گیا۔"
108
+
109
+ return message, file_path, content
110
+
111
+
112
+ # ✅ Function: Generate Flashcards
113
+ def generate_flashcards(language, topic, slos_text):
114
+ model = URDU_MODEL if language == "Urdu" else ENGLISH_MODEL
115
+
116
+ prompt = f"""
117
+ Based on the topic "{topic}" and the following SLOs:
118
+ {slos_text}
119
+ Generate 5 short and clear **Flashcards** (Question/Term on one side and Answer/Definition on the other side).
120
+ Ensure alignment with the SLOs and curriculum context of Pakistan.
121
+ """
122
+ response = client.chat.completions.create(
123
+ model=model,
124
+ messages=[{"role": "user", "content": prompt}],
125
+ temperature=0.6,
126
+ max_tokens=800
127
+ )
128
+ return response.choices[0].message.content.strip()
129
+
130
+
131
+ # ✅ Function: Generate Quiz
132
+ def generate_quiz(language, topic, slos_text):
133
+ model = URDU_MODEL if language == "Urdu" else ENGLISH_MODEL
134
+
135
+ prompt = f"""
136
+ Based on the topic "{topic}" and these SLOs:
137
+ {slos_text}
138
+ Create a short quiz including:
139
+ - 5 Multiple Choice Questions (MCQs)
140
+ - 2 Short Answer Questions
141
+ - 2 Fill-in-the-Blank Questions
142
+ Ensure all are aligned with the SLOs and Pakistani curriculum.
143
+ """
144
+ response = client.chat.completions.create(
145
+ model=model,
146
+ messages=[{"role": "user", "content": prompt}],
147
+ temperature=0.7,
148
+ max_tokens=800
149
+ )
150
+ return response.choices[0].message.content.strip()
151
 
152
+
153
+ # ✅ Build Gradio Interface
154
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="teal")) as demo:
155
+ gr.Markdown("## 🧠 SMART LESSON PLAN GENERATOR (Bilingual)")
156
+ gr.Markdown("**Developed by: Najaf Ali**")
157
 
158
  with gr.Row():
159
+ language = gr.Dropdown(["English", "Urdu"], label="Select Language", value="English")
160
  grade = gr.Dropdown(
161
+ [f"Grade {i}" for i in range(1, 13)], label="Select Grade", value="Grade 9"
 
 
 
 
 
 
162
  )
 
163
  subject = gr.Dropdown(
164
  [
165
  "English", "Urdu", "Mathematics", "Science", "Computer",
 
171
  )
172
 
173
  with gr.Row():
174
+ topic = gr.Textbox(label="Enter Topic (e.g., Photosynthesis, Fractions, Noun Types)")
175
+ duration = gr.Number(label="Duration (minutes)", value=40)
176
+ student_level = gr.Dropdown(["Beginner", "Average", "Advanced"], label="Student Level", value="Average")
 
 
 
 
177
 
178
  generate_btn = gr.Button("🚀 Generate SMART Lesson Plan")
179
+ output_message = gr.HTML(label="Status")
180
+ file_output = gr.File(label="⬇️ Download Lesson Plan")
181
+ lesson_textbox = gr.Textbox(label="Generated Lesson Plan Text", lines=20, visible=False)
182
+
183
+ # ✅ Flashcards and Quiz buttons
184
+ gr.Markdown("### 🎯 Generate Additional Materials (Aligned with Lesson Plan)")
185
+ with gr.Row():
186
+ flash_btn = gr.Button("🃏 Generate Flashcards")
187
+ quiz_btn = gr.Button("📝 Generate Quiz")
188
 
189
+ flash_output = gr.Textbox(label="Generated Flashcards", lines=10)
190
+ quiz_output = gr.Textbox(label="Generated Quiz", lines=12)
191
+
192
+ # ✅ Button connections
193
  generate_btn.click(
194
+ generate_lesson_plan,
195
+ inputs=[language, grade, subject, topic, duration, student_level],
196
+ outputs=[output_message, file_output, lesson_textbox]
197
  )
198
 
199
+ flash_btn.click(generate_flashcards, inputs=[language, topic, lesson_textbox], outputs=flash_output)
200
+ quiz_btn.click(generate_quiz, inputs=[language, topic, lesson_textbox], outputs=quiz_output)
201
+
202
  # ✅ Launch App
203
  demo.launch()