Spaces:
No application file
No application file
Update app.py
Browse files
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 |
-
# ✅
|
| 9 |
-
|
| 10 |
-
client_groq = Groq(api_key=GROQ_API_KEY)
|
| 11 |
|
| 12 |
-
# ✅ Initialize
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
def generate_lesson_plan(class_, subject, topic, students, duration, date, teacher
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if language == "Urdu":
|
| 19 |
-
prompt = f"""
|
| 20 |
آپ ایک تجربہ کار استاد ہیں۔
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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
|
| 46 |
-
1. Bridge-In: engaging
|
| 47 |
-
2. 3
|
| 48 |
3. 2 Pre-Assessment Questions.
|
| 49 |
4. One detailed Participatory Learning Activity.
|
| 50 |
5. 2–3 Post-Assessment Questions.
|
| 51 |
-
6.
|
| 52 |
-
"""
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# ✅ Create Word document
|
| 61 |
doc = Document()
|
| 62 |
-
doc.add_heading("BOPPPS
|
| 63 |
|
| 64 |
-
#
|
| 65 |
table = doc.add_table(rows=0, cols=2)
|
| 66 |
data = {
|
| 67 |
-
"
|
| 68 |
-
"
|
| 69 |
-
"
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
"
|
|
|
|
| 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_{
|
| 85 |
filepath = f"/tmp/{filename}"
|
| 86 |
doc.save(filepath)
|
|
|
|
| 87 |
return filepath
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
-
#
|
| 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
|
| 103 |
|
| 104 |
with gr.Group():
|
|
|
|
|
|
|
| 105 |
with gr.Row():
|
| 106 |
-
class_ = gr.Textbox(label="Class
|
| 107 |
-
subject = gr.Textbox(label="Subject
|
| 108 |
with gr.Row():
|
| 109 |
-
topic = gr.Textbox(label="Topic
|
| 110 |
-
students = gr.Textbox(label="Number of Students
|
| 111 |
with gr.Row():
|
| 112 |
-
duration = gr.Textbox(label="Duration
|
| 113 |
-
date = gr.Textbox(label="Date
|
| 114 |
-
teacher = gr.Textbox(label="Teacher
|
| 115 |
|
| 116 |
-
|
| 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
|
| 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 |
+
|