Turbiling commited on
Commit
cebdff9
·
verified ·
1 Parent(s): af8dd84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -70
app.py CHANGED
@@ -2,76 +2,73 @@ import os
2
  import gradio as gr
3
  from docx import Document
4
  import datetime
5
- from groq import Groq
6
  from huggingface_hub import InferenceClient
7
 
8
- # ✅ Initialize Groq client (for English)
9
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
10
- client_groq = Groq(api_key=GROQ_API_KEY)
11
 
12
- # ✅ Initialize Zephyr model client (for Urdu)
13
- urdu_model = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
 
 
 
14
 
15
- def generate_lesson_plan(class_, subject, topic, students, duration, date, teacher, language):
16
- try:
17
- # ========================= Urdu MODE ==========================
18
- if language == "Urdu":
19
- prompt = f"""
20
  آپ ایک تجربہ کار استاد ہیں۔
21
- موضوع "{topic}" کے لیے BOPPPS ماڈل کے مطابق ایک مکمل سبق کا منصوبہ (Lesson Plan) تیار کریں۔
22
- سبق کا متن فصیح اور درست اردو میں ہو، اور درج ذیل حصے شامل ہوں:
23
- 1. Bridge-In: سبق کے آغاز کے لیے ایک دلچسپ اور متوجہ کرنے والا پیراگراف۔
24
- 2. تین واضح تعلیمی مقاصد۔
25
- 3. دو پیشگی سوالات (Pre-Assessment)۔
26
- 4. ایک تفصیلی تدریسی سرگرمی (Participatory Learning Activity)۔
27
- 5. دو سے تین بعد از تدریس سوالات (Post-Assessment)۔
28
- 6. استاد کی طرف سے ایک مختصر اختتامی خلاصہ پیراگراف۔
29
- نوٹ: بنیادی معلومات (جیسے کلاس، موضوع، استاد کا نام وغیرہ) کو دوبارہ متن میں شامل نہ کریں۔
30
- """
31
- response = urdu_model.text_generation(
32
- prompt,
33
- max_new_tokens=900,
34
- temperature=0.7,
35
- do_sample=True
36
- )
37
- lesson_text = response.strip()
38
-
39
- # ========================= English MODE ==========================
40
- else:
41
- prompt = f"""
42
- You are a professional lesson plan designer.
43
  Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
44
- Do not repeat the basic info (class, subject, topic, teacher etc.) in headings or body.
45
- Include only these structured sections:
46
- 1. Bridge-In: engaging introduction.
47
- 2. 3 clear Learning Objectives.
48
  3. 2 Pre-Assessment Questions.
49
  4. One detailed Participatory Learning Activity.
50
  5. 2–3 Post-Assessment Questions.
51
- 6. Summary paragraph by the teacher.
52
- """
53
- response = client_groq.chat.completions.create(
54
- model="llama-3.1-8b-instant",
55
- messages=[{"role": "user", "content": prompt}],
56
- temperature=0.7,
57
- )
58
- lesson_text = response.choices[0].message.content.strip()
 
 
 
 
59
 
60
  # ✅ Create Word document
61
  doc = Document()
62
- doc.add_heading("BOPPPS سبق کا منصوبہ" if language == "Urdu" else "BOPPPS Lesson Plan", 0)
63
 
64
- # ✅ Metadata Table
65
  table = doc.add_table(rows=0, cols=2)
66
  data = {
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
  for key, value in data.items():
76
  row_cells = table.add_row().cells
77
  row_cells[0].text = key
@@ -81,13 +78,14 @@ Include only these structured sections:
81
  doc.add_paragraph(lesson_text)
82
  doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
83
 
84
- filename = f"lesson_plan_{language.lower()}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
85
  filepath = f"/tmp/{filename}"
86
  doc.save(filepath)
 
87
  return filepath
88
 
89
  except Exception as e:
90
- # Return error file instead of crash
91
  error_doc = Document()
92
  error_doc.add_heading("Error in Lesson Plan Generator", 0)
93
  error_doc.add_paragraph(str(e))
@@ -97,30 +95,30 @@ Include only these structured sections:
97
  return filepath
98
 
99
 
100
- # ✅ Gradio UI
101
  with gr.Blocks() as demo:
102
- gr.Markdown("## 📘 BOPPPS Lesson Plan Generator (English + Urdu — Accurate Urdu Version)")
103
 
104
  with gr.Group():
 
 
105
  with gr.Row():
106
- class_ = gr.Textbox(label="Class / جماعت", placeholder="e.g., 9th / نہم")
107
- subject = gr.Textbox(label="Subject / مضمون", placeholder="e.g., Physics / طبیعیات")
108
  with gr.Row():
109
- topic = gr.Textbox(label="Topic / موضوع", placeholder="e.g., Newton’s First Law / نیوٹن کا پہلا قانون")
110
- students = gr.Textbox(label="Number of Students / طلبہ کی تعداد", placeholder="e.g., 30")
111
  with gr.Row():
112
- duration = gr.Textbox(label="Duration / دورانیہ", placeholder="e.g., 40")
113
- date = gr.Textbox(label="Date / تاریخ", placeholder="e.g., 2025-10-05")
114
- teacher = gr.Textbox(label="Teacher / استاد کا نام", placeholder="e.g., Najaf Ali Sharqi")
115
 
116
- language = gr.Radio(["English", "Urdu"], label="Select Language / زبان منتخب کریں", value="English")
117
-
118
- generate_button = gr.Button("Generate Lesson Plan / سبق کا منصوبہ تیار کریں")
119
- output_file = gr.File(label="📄 Download Word File / فائل ڈاؤن لوڈ کریں")
120
 
121
  generate_button.click(
122
  fn=generate_lesson_plan,
123
- inputs=[class_, subject, topic, students, duration, date, teacher, language],
124
  outputs=output_file
125
  )
126
 
@@ -129,3 +127,4 @@ with gr.Blocks() as demo:
129
 
130
  demo.launch()
131
 
 
 
2
  import gradio as gr
3
  from docx import Document
4
  import datetime
 
5
  from huggingface_hub import InferenceClient
6
 
7
+ # ✅ Load your Hugging Face token from environment (set it in HF Spaces or locally)
8
+ HF_TOKEN = os.getenv("HF_TOKEN")
 
9
 
10
+ # ✅ Initialize Hugging Face client
11
+ client = InferenceClient(
12
+ model="tiiuae/falcon-7b-instruct",
13
+ token=HF_TOKEN
14
+ )
15
 
16
+ def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
17
+ if language == "Urdu":
18
+ prompt = f"""
 
 
19
  آپ ایک تجربہ کار استاد ہیں۔
20
+ براہ کرم درج ذیل موضوع پر ایک مکمل BOPPPS لیسن پلان اردو زبان میں تیار کریں۔
21
+ موضوع: "{topic}"
22
+ منصوبہ میں درج ذیل حصے شامل ہوں:
23
+ 1. Bridge-In: ایک مختصر دلچسپ تمہیدی پیراگراف۔
24
+ 2. 3 واضح تعلیمی مقاصد۔
25
+ 3. 2 قبل از جائزہ (Pre-Assessment) سوالات۔
26
+ 4. ایک مفصل تدریسی سرگرمی (Participatory Learning)۔
27
+ 5. 2 تا 3 بعد از جائزہ (Post-Assessment) سوالات۔
28
+ 6. سبق کا خلاصہ (Summary)۔
29
+ براہ کرم متن کو قدرتی اور تعلیمی انداز میں اردو میں لکھیں۔
30
+ """
31
+ else:
32
+ prompt = f"""
33
+ You are a professional lesson plan expert.
 
 
 
 
 
 
 
 
34
  Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
35
+ Do not repeat the basic info (class, subject, topic, teacher etc.) again in headings or body.
36
+ Include only the following:
37
+ 1. Bridge-In: a short, engaging paragraph.
38
+ 2. 3 Clear Learning Objectives.
39
  3. 2 Pre-Assessment Questions.
40
  4. One detailed Participatory Learning Activity.
41
  5. 2–3 Post-Assessment Questions.
42
+ 6. A short Summary paragraph.
43
+ """
44
+
45
+ try:
46
+ # ✅ Generate lesson text
47
+ response = client.text_generation(
48
+ prompt,
49
+ max_new_tokens=800,
50
+ temperature=0.7,
51
+ )
52
+
53
+ lesson_text = response.strip()
54
 
55
  # ✅ Create Word document
56
  doc = Document()
57
+ doc.add_heading("BOPPPS Lesson Plan", 0)
58
 
59
+ # Metadata table
60
  table = doc.add_table(rows=0, cols=2)
61
  data = {
62
+ "Language": language,
63
+ "Class": class_,
64
+ "Subject": subject,
65
+ "Topic": topic,
66
+ "No. of Students": students,
67
+ "Duration": duration,
68
+ "Date": date,
69
+ "Teacher": teacher
70
  }
71
+
72
  for key, value in data.items():
73
  row_cells = table.add_row().cells
74
  row_cells[0].text = key
 
78
  doc.add_paragraph(lesson_text)
79
  doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
80
 
81
+ filename = f"lesson_plan_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
82
  filepath = f"/tmp/{filename}"
83
  doc.save(filepath)
84
+
85
  return filepath
86
 
87
  except Exception as e:
88
+ # Error handling
89
  error_doc = Document()
90
  error_doc.add_heading("Error in Lesson Plan Generator", 0)
91
  error_doc.add_paragraph(str(e))
 
95
  return filepath
96
 
97
 
98
+ # ✅ Build Gradio UI
99
  with gr.Blocks() as demo:
100
+ gr.Markdown("## 📘 BOPPPS Lesson Plan Generator (English + Urdu)")
101
 
102
  with gr.Group():
103
+ language = gr.Radio(["English", "Urdu"], label="Select Language", value="English")
104
+
105
  with gr.Row():
106
+ class_ = gr.Textbox(label="Class", placeholder="e.g., 9th")
107
+ subject = gr.Textbox(label="Subject", placeholder="e.g., Physics")
108
  with gr.Row():
109
+ topic = gr.Textbox(label="Topic", placeholder="e.g., Laws of Motion")
110
+ students = gr.Textbox(label="Number of Students", placeholder="e.g., 30")
111
  with gr.Row():
112
+ duration = gr.Textbox(label="Duration (minutes)", placeholder="e.g., 40")
113
+ date = gr.Textbox(label="Date", placeholder="e.g., 2025-10-05")
114
+ teacher = gr.Textbox(label="Teacher Name", placeholder="e.g., Najaf Ali Sharqi")
115
 
116
+ generate_button = gr.Button("Generate Lesson Plan")
117
+ output_file = gr.File(label="📄 Download Word File")
 
 
118
 
119
  generate_button.click(
120
  fn=generate_lesson_plan,
121
+ inputs=[language, class_, subject, topic, students, duration, date, teacher],
122
  outputs=output_file
123
  )
124
 
 
127
 
128
  demo.launch()
129
 
130
+