Turbiling commited on
Commit
9ebc2ee
·
verified ·
1 Parent(s): 3c454c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -43
app.py CHANGED
@@ -5,48 +5,31 @@ import datetime
5
  from groq import Groq
6
  from huggingface_hub import InferenceClient
7
 
8
- # ✅ Load API keys
9
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
 
12
- # ✅ Initialize clients
13
  groq_client = Groq(api_key=GROQ_API_KEY)
14
- hf_client = InferenceClient(model="facebook/m2m100-1.2B", token=HF_TOKEN)
15
 
 
 
 
16
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
17
  try:
18
- if language == "Urdu":
19
- # Prompt in English → translated to Urdu by model
20
  prompt = f"""
21
- Write a complete BOPPPS lesson plan in **Urdu language** for the topic: "{topic}".
22
- Include the following sections clearly in Urdu:
23
- 1. Bridge-In (interesting introduction)
24
- 2. 3 Learning Objectives
25
- 3. 2 Pre-assessment questions
26
- 4. One detailed participatory learning activity
27
- 5. 2–3 Post-assessment questions
28
- 6. A concluding Summary paragraph
29
- Make the tone natural and educational for teachers.
30
- """
31
-
32
- response = hf_client.text_generation(
33
- prompt,
34
- max_new_tokens=900,
35
- temperature=0.8,
36
- )
37
- lesson_text = response.strip()
38
-
39
- else:
40
- prompt = f"""
41
- You are a professional lesson plan expert.
42
  Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
43
- Include:
44
- 1. Bridge-In (short, engaging paragraph)
45
- 2. 3 Clear Learning Objectives
46
- 3. 2 Pre-Assessment Questions
47
- 4. One detailed Participatory Learning Activity
48
- 5. 2–3 Post-Assessment Questions
49
- 6. A short Summary paragraph
 
50
  """
51
 
52
  response = groq_client.chat.completions.create(
@@ -54,23 +37,43 @@ Include:
54
  messages=[{"role": "user", "content": prompt}],
55
  temperature=0.7,
56
  )
57
-
58
  lesson_text = response.choices[0].message.content.strip()
59
 
60
- # ✅ Create Word Document
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  doc = Document()
62
  doc.add_heading("BOPPPS Lesson Plan", 0)
63
 
 
64
  table = doc.add_table(rows=0, cols=2)
65
  data = {
66
- "Language": language,
67
  "Class": class_,
68
  "Subject": subject,
69
  "Topic": topic,
70
  "No. of Students": students,
71
  "Duration": duration,
72
  "Date": date,
73
- "Teacher": teacher
74
  }
75
 
76
  for key, value in data.items():
@@ -89,6 +92,7 @@ Include:
89
  return filepath
90
 
91
  except Exception as e:
 
92
  error_doc = Document()
93
  error_doc.add_heading("Error in Lesson Plan Generator", 0)
94
  error_doc.add_paragraph(str(e))
@@ -98,18 +102,19 @@ Include:
98
  return filepath
99
 
100
 
101
- # ✅ Build Gradio UI
 
 
102
  with gr.Blocks() as demo:
103
- gr.Markdown("## 📘 BOPPPS Lesson Plan Generator (English + Urdu)")
104
 
105
  with gr.Group():
106
  language = gr.Radio(["English", "Urdu"], label="Select Language", value="English")
107
-
108
  with gr.Row():
109
- class_ = gr.Textbox(label="Class", placeholder="e.g., 9th")
110
- subject = gr.Textbox(label="Subject", placeholder="e.g., Physics")
111
  with gr.Row():
112
- topic = gr.Textbox(label="Topic", placeholder="e.g., Newton’s Laws")
113
  students = gr.Textbox(label="Number of Students", placeholder="e.g., 30")
114
  with gr.Row():
115
  duration = gr.Textbox(label="Duration (minutes)", placeholder="e.g., 40")
 
5
  from groq import Groq
6
  from huggingface_hub import InferenceClient
7
 
8
+ # Load API keys
9
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
 
12
+ # Initialize clients
13
  groq_client = Groq(api_key=GROQ_API_KEY)
14
+ hf_client = InferenceClient(api_key=HF_TOKEN)
15
 
16
+ # -----------------------------
17
+ # Urdu + English Lesson Plan Generator
18
+ # -----------------------------
19
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
20
  try:
21
+ if language == "English":
 
22
  prompt = f"""
23
+ You are a lesson plan expert.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
25
+ Do not repeat the basic info (class, subject, topic, teacher etc.) again in headings or body.
26
+ Include the following structured components only:
27
+ 1. Bridge-In: a short, engaging paragraph.
28
+ 2. 3 Clear Learning Objectives.
29
+ 3. 2 Pre-Assessment Questions.
30
+ 4. One detailed Participatory Learning Activity.
31
+ 5. 2–3 Post-Assessment Questions.
32
+ 6. Summary paragraph by the teacher.
33
  """
34
 
35
  response = groq_client.chat.completions.create(
 
37
  messages=[{"role": "user", "content": prompt}],
38
  temperature=0.7,
39
  )
 
40
  lesson_text = response.choices[0].message.content.strip()
41
 
42
+ else: # Urdu mode
43
+ prompt = f"""
44
+ آپ ایک ماہر اسباقی منصوبہ ساز ہیں۔
45
+ ذیل کے موضوع "{topic}" پر BOPPPS ماڈل کے مطابق ایک مفصل اردو سبق منصوبہ تیار کریں۔
46
+ براہ کرم درج ذیل حصے لازمی شامل کریں:
47
+ 1. تعارفی سرگرمی (Bridge-In)
48
+ 2. 3 واضح تعلیمی مقاصد
49
+ 3. 2 ابتدائی سوالات (Pre-assessment)
50
+ 4. ایک تفصیلی سرگرمی (Participatory Learning Activity)
51
+ 5. 2 تا 3 بعد از تدریس سوالات (Post-assessment)
52
+ 6. سبق کا خلاصہ (Summary)
53
+ """
54
+
55
+ urdu_response = hf_client.text_generation(
56
+ model="MuhammadYaseen77999/urdu-islamic-bot",
57
+ inputs=prompt,
58
+ max_new_tokens=500,
59
+ temperature=0.7,
60
+ )
61
+ lesson_text = urdu_response.strip()
62
+
63
+ # Create Word document
64
  doc = Document()
65
  doc.add_heading("BOPPPS Lesson Plan", 0)
66
 
67
+ # Metadata table
68
  table = doc.add_table(rows=0, cols=2)
69
  data = {
 
70
  "Class": class_,
71
  "Subject": subject,
72
  "Topic": topic,
73
  "No. of Students": students,
74
  "Duration": duration,
75
  "Date": date,
76
+ "Teacher Name": teacher
77
  }
78
 
79
  for key, value in data.items():
 
92
  return filepath
93
 
94
  except Exception as e:
95
+ # Return error file
96
  error_doc = Document()
97
  error_doc.add_heading("Error in Lesson Plan Generator", 0)
98
  error_doc.add_paragraph(str(e))
 
102
  return filepath
103
 
104
 
105
+ # -----------------------------
106
+ # Gradio UI
107
+ # -----------------------------
108
  with gr.Blocks() as demo:
109
+ gr.Markdown("## 🌍 Bilingual BOPPPS Lesson Plan Generator (English + Urdu)")
110
 
111
  with gr.Group():
112
  language = gr.Radio(["English", "Urdu"], label="Select Language", value="English")
 
113
  with gr.Row():
114
+ class_ = gr.Textbox(label="Class", placeholder="e.g., 9th / نہم")
115
+ subject = gr.Textbox(label="Subject", placeholder="e.g., Physics / فزکس")
116
  with gr.Row():
117
+ topic = gr.Textbox(label="Topic", placeholder="e.g., Newton’s First Law / حرکیات کا پہلا قانون")
118
  students = gr.Textbox(label="Number of Students", placeholder="e.g., 30")
119
  with gr.Row():
120
  duration = gr.Textbox(label="Duration (minutes)", placeholder="e.g., 40")