Turbiling commited on
Commit
7b25f62
·
verified ·
1 Parent(s): 7d3c5aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -32
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import gradio as gr
3
  from docx import Document
@@ -5,41 +6,35 @@ from docx.oxml.ns import qn
5
  from docx.oxml import OxmlElement
6
  import datetime
7
  from groq import Groq
8
- from huggingface_hub import InferenceClient
9
 
10
- # Load API keys (must set in Secrets / environment)
11
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
12
- HF_TOKEN = os.getenv("HF_TOKEN")
13
 
14
- groq_client = Groq(api_key=GROQ_API_KEY)
15
- hf_client = InferenceClient(token=HF_TOKEN)
16
 
 
 
17
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
18
  try:
19
  if language == "English":
20
  prompt = f"""
21
- You are a lesson plan expert.
22
  Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
23
- Do not repeat the basic info (class, subject, topic, teacher etc.) again in headings or body.
24
- Include the following structured components only:
25
- 1. Bridge-In: a short, engaging paragraph.
26
  2. 3 Clear Learning Objectives.
27
  3. 2 Pre-Assessment Questions.
28
- 4. One detailed Participatory Learning Activity.
29
  5. 2–3 Post-Assessment Questions.
30
  6. Summary paragraph by the teacher.
31
  """
32
- response = groq_client.chat.completions.create(
33
- model="llama-3.1-8b-instant",
34
- messages=[{"role": "user", "content": prompt}],
35
- temperature=0.7,
36
- )
37
- lesson_text = response.choices[0].message.content.strip()
38
 
39
  else: # Urdu mode
40
  prompt = f"""
41
  آپ ایک ماہر اسباقی منصوبہ ساز ہیں۔
42
- ذیل کے موضوع "{topic}" پر BOPPPS ماڈل کے مطابق ایک مفصل اردو سبق منصوبہ تیار کریں۔
43
  براہ کرم درج ذیل حصے لازمی شامل کریں:
44
  1. تعارفی سرگرمی (Bridge-In)
45
  2. 3 واضح تعلیمی مقاصد
@@ -48,18 +43,21 @@ Include the following structured components only:
48
  5. 2 تا 3 بعد از تدریس سوالات (Post-assessment)
49
  6. سبق کا خلاصہ (Summary)
50
  """
51
- urdu_response = hf_client.text_generation(
52
- model="Imran1/gpt2-urdu-news",
53
- prompt=prompt,
54
- max_new_tokens=500,
55
- temperature=0.8,
56
- )
57
- if isinstance(urdu_response, str):
58
- lesson_text = urdu_response.strip()
59
- else:
60
- lesson_text = urdu_response.get("generated_text", "").strip()
61
-
62
- # Create the Word document
 
 
 
63
  doc = Document()
64
  doc.add_heading("BOPPPS Lesson Plan", 0)
65
 
@@ -79,15 +77,19 @@ Include the following structured components only:
79
  row_cells[1].text = str(value)
80
 
81
  doc.add_paragraph("\n")
 
82
  paragraph = doc.add_paragraph(lesson_text)
 
 
83
  if language == "Urdu":
84
- paragraph.alignment = 2 # right align
85
  rtl = OxmlElement('w:rtl')
86
  rtl.set(qn('w:val'), '1')
87
  paragraph._element.insert(0, rtl)
88
 
89
  doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
90
 
 
91
  filename = f"lesson_plan_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
92
  filepath = f"/tmp/{filename}"
93
  doc.save(filepath)
@@ -104,7 +106,7 @@ Include the following structured components only:
104
  return filepath
105
 
106
 
107
- # Gradio UI
108
  with gr.Blocks() as demo:
109
  gr.Markdown("## 🌍 Bilingual BOPPPS Lesson Plan Generator (English + Urdu)")
110
 
@@ -118,7 +120,7 @@ with gr.Blocks() as demo:
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")
121
- date = gr.Textbox(label="Date", placeholder="e.g., 2025-10-05")
122
  teacher = gr.Textbox(label="Teacher Name", placeholder="e.g., Najaf Ali Sharqi")
123
 
124
  generate_button = gr.Button("Generate Lesson Plan")
@@ -134,3 +136,4 @@ with gr.Blocks() as demo:
134
  gr.Markdown("🔹 Developed by **Najaf Ali Sharqi**")
135
 
136
  demo.launch()
 
 
1
+ # app.py
2
  import os
3
  import gradio as gr
4
  from docx import Document
 
6
  from docx.oxml import OxmlElement
7
  import datetime
8
  from groq import Groq
 
9
 
10
+ # ✅ Load API key from Hugging Face Secrets
11
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
 
12
 
13
+ # Initialize Groq client
14
+ client = Groq(api_key=GROQ_API_KEY)
15
 
16
+
17
+ # ✅ Text Generation Function
18
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
19
  try:
20
  if language == "English":
21
  prompt = f"""
22
+ You are an expert lesson plan creator.
23
  Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
24
+ Avoid repeating the basic info (class, subject, teacher, etc.) in headings.
25
+ Include only these sections:
26
+ 1. Bridge-In: short, engaging introduction.
27
  2. 3 Clear Learning Objectives.
28
  3. 2 Pre-Assessment Questions.
29
+ 4. 1 Participatory Learning Activity (detailed).
30
  5. 2–3 Post-Assessment Questions.
31
  6. Summary paragraph by the teacher.
32
  """
 
 
 
 
 
 
33
 
34
  else: # Urdu mode
35
  prompt = f"""
36
  آپ ایک ماہر اسباقی منصوبہ ساز ہیں۔
37
+ موضوع "{topic}" پر BOPPPS ماڈل کے مطابق ایک مفصل اردو سبق منصوبہ تیار کریں۔
38
  براہ کرم درج ذیل حصے لازمی شامل کریں:
39
  1. تعارفی سرگرمی (Bridge-In)
40
  2. 3 واضح تعلیمی مقاصد
 
43
  5. 2 تا 3 بعد از تدریس سوالات (Post-assessment)
44
  6. سبق کا خلاصہ (Summary)
45
  """
46
+
47
+ # ✅ Use the Groq client with the openai/gpt-oss-120b model
48
+ response = client.chat.completions.create(
49
+ model="openai/gpt-oss-120b",
50
+ messages=[{"role": "user", "content": prompt}],
51
+ temperature=0.8,
52
+ max_completion_tokens=2048,
53
+ top_p=1,
54
+ stream=False
55
+ )
56
+
57
+ # Extract generated text
58
+ lesson_text = response.choices[0].message.content.strip()
59
+
60
+ # ✅ Create the Word document
61
  doc = Document()
62
  doc.add_heading("BOPPPS Lesson Plan", 0)
63
 
 
77
  row_cells[1].text = str(value)
78
 
79
  doc.add_paragraph("\n")
80
+
81
  paragraph = doc.add_paragraph(lesson_text)
82
+
83
+ # Urdu formatting (right-aligned text)
84
  if language == "Urdu":
85
+ paragraph.alignment = 2
86
  rtl = OxmlElement('w:rtl')
87
  rtl.set(qn('w:val'), '1')
88
  paragraph._element.insert(0, rtl)
89
 
90
  doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
91
 
92
+ # Save to temporary file
93
  filename = f"lesson_plan_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
94
  filepath = f"/tmp/{filename}"
95
  doc.save(filepath)
 
106
  return filepath
107
 
108
 
109
+ # ✅ Gradio UI
110
  with gr.Blocks() as demo:
111
  gr.Markdown("## 🌍 Bilingual BOPPPS Lesson Plan Generator (English + Urdu)")
112
 
 
120
  students = gr.Textbox(label="Number of Students", placeholder="e.g., 30")
121
  with gr.Row():
122
  duration = gr.Textbox(label="Duration (minutes)", placeholder="e.g., 40")
123
+ date = gr.Textbox(label="Date", placeholder="e.g., 2025-10-07")
124
  teacher = gr.Textbox(label="Teacher Name", placeholder="e.g., Najaf Ali Sharqi")
125
 
126
  generate_button = gr.Button("Generate Lesson Plan")
 
136
  gr.Markdown("🔹 Developed by **Najaf Ali Sharqi**")
137
 
138
  demo.launch()
139
+