Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,30 +3,45 @@ import gradio as gr
|
|
| 3 |
from docx import Document
|
| 4 |
import datetime
|
| 5 |
from groq import Groq
|
|
|
|
| 6 |
|
| 7 |
-
# ✅
|
| 8 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
|
| 9 |
|
| 10 |
-
client
|
|
|
|
| 11 |
|
| 12 |
# ✅ Function to generate lesson plan
|
| 13 |
def generate_lesson_plan(class_, subject, topic, students, duration, date, teacher, language):
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
آپ ایک تجربہ کار استاد ہیں۔
|
| 17 |
-
موضوع "{topic}" کے لیے BOPPPS ماڈل کے تحت ایک
|
| 18 |
-
یہ منصوبہ مکمل طور پر اردو زبان میں ہو۔
|
| 19 |
-
مندرجہ ذیل حصے
|
| 20 |
-
1. Bridge-In: دلچسپ آغاز کے لیے ایک
|
| 21 |
2. تین واضح تعلیمی مقاصد۔
|
| 22 |
3. دو پیشگی تشخیصی سوالات۔
|
| 23 |
4. ایک تفصیلی تدریسی سرگرمی (Participatory Learning Activity)۔
|
| 24 |
5. دو سے تین بعد از تدریس سوالات۔
|
| 25 |
6. استاد کی طرف سے ایک خلاصہ پیراگراف۔
|
| 26 |
-
نوٹ: بنیادی معلومات (کلاس، مضمون، موضوع
|
| 27 |
"""
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
You are a lesson plan expert.
|
| 31 |
Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
|
| 32 |
Do not repeat the basic info (class, subject, topic, teacher etc.) again in headings or body.
|
|
@@ -38,16 +53,13 @@ Include the following structured components only:
|
|
| 38 |
5. 2–3 Post-Assessment Questions.
|
| 39 |
6. Summary paragraph by the teacher.
|
| 40 |
"""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
lesson_text = response.choices[0].message.content.strip()
|
| 51 |
|
| 52 |
# ✅ Create Word document
|
| 53 |
doc = Document()
|
|
@@ -75,10 +87,7 @@ Include the following structured components only:
|
|
| 75 |
|
| 76 |
doc.add_paragraph("\n")
|
| 77 |
doc.add_paragraph(lesson_text)
|
| 78 |
-
|
| 79 |
-
# ✅ Add footer credit
|
| 80 |
-
doc.add_paragraph("\n")
|
| 81 |
-
doc.add_paragraph("Developed by Najaf Ali Sharqi")
|
| 82 |
|
| 83 |
filename = f"lesson_plan_{language.lower()}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
|
| 84 |
filepath = f"/tmp/{filename}"
|
|
@@ -87,7 +96,6 @@ Include the following structured components only:
|
|
| 87 |
return filepath
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
-
# ✅ Return error document instead of crashing
|
| 91 |
error_doc = Document()
|
| 92 |
error_doc.add_heading("Error in Lesson Plan Generator", 0)
|
| 93 |
error_doc.add_paragraph(str(e))
|
|
@@ -99,7 +107,7 @@ Include the following structured components only:
|
|
| 99 |
|
| 100 |
# ✅ Build 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():
|
|
@@ -109,9 +117,9 @@ with gr.Blocks() as demo:
|
|
| 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
|
| 113 |
-
date = gr.Textbox(label="Date / تاریخ", placeholder="e.g., 2025-
|
| 114 |
-
teacher = gr.Textbox(label="Teacher
|
| 115 |
|
| 116 |
language = gr.Radio(["English", "Urdu"], label="Select Language / زبان منتخب کریں", value="English")
|
| 117 |
|
|
@@ -128,3 +136,4 @@ with gr.Blocks() as demo:
|
|
| 128 |
gr.Markdown("🔹 Developed by **Najaf Ali Sharqi**")
|
| 129 |
|
| 130 |
demo.launch()
|
|
|
|
|
|
| 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 Falcon model client for Urdu
|
| 13 |
+
falcon_client = InferenceClient(model="mlx-community/Falcon-H1-7B-Instruct-6bit")
|
| 14 |
|
| 15 |
# ✅ Function to generate lesson plan
|
| 16 |
def generate_lesson_plan(class_, subject, topic, students, duration, date, teacher, language):
|
| 17 |
+
try:
|
| 18 |
+
if language == "Urdu":
|
| 19 |
+
# Urdu prompt for Falcon model
|
| 20 |
+
prompt = f"""
|
| 21 |
آپ ایک تجربہ کار استاد ہیں۔
|
| 22 |
+
موضوع "{topic}" کے لیے BOPPPS ماڈل کے تحت ایک مکمل سبق کا منصوبہ تیار کریں۔
|
| 23 |
+
یہ منصوبہ مکمل طور پر قدرتی اور درست اردو زبان میں ہو۔
|
| 24 |
+
مندرجہ ذیل حصے شامل کریں:
|
| 25 |
+
1. Bridge-In: دلچسپ آغاز کے لیے ایک پیراگراف۔
|
| 26 |
2. تین واضح تعلیمی مقاصد۔
|
| 27 |
3. دو پیشگی تشخیصی سوالات۔
|
| 28 |
4. ایک تفصیلی تدریسی سرگرمی (Participatory Learning Activity)۔
|
| 29 |
5. دو سے تین بعد از تدریس سوالات۔
|
| 30 |
6. استاد کی طرف سے ایک خلاصہ پیراگراف۔
|
| 31 |
+
نوٹ: بنیادی معلومات (کلاس، مضمون، موضوع وغیرہ) دوبارہ متن میں شامل نہ کریں۔
|
| 32 |
"""
|
| 33 |
+
# ✅ Use Falcon model for Urdu
|
| 34 |
+
response = falcon_client.text_generation(
|
| 35 |
+
prompt,
|
| 36 |
+
max_new_tokens=800,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
do_sample=True
|
| 39 |
+
)
|
| 40 |
+
lesson_text = response.strip()
|
| 41 |
+
|
| 42 |
+
else:
|
| 43 |
+
# English prompt for Groq model
|
| 44 |
+
prompt = f"""
|
| 45 |
You are a lesson plan expert.
|
| 46 |
Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
|
| 47 |
Do not repeat the basic info (class, subject, topic, teacher etc.) again in headings or body.
|
|
|
|
| 53 |
5. 2–3 Post-Assessment Questions.
|
| 54 |
6. Summary paragraph by the teacher.
|
| 55 |
"""
|
| 56 |
+
# ✅ Use Groq model for English
|
| 57 |
+
response = client_groq.chat.completions.create(
|
| 58 |
+
model="llama-3.1-8b-instant",
|
| 59 |
+
messages=[{"role": "user", "content": prompt}],
|
| 60 |
+
temperature=0.7,
|
| 61 |
+
)
|
| 62 |
+
lesson_text = response.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# ✅ Create Word document
|
| 65 |
doc = Document()
|
|
|
|
| 87 |
|
| 88 |
doc.add_paragraph("\n")
|
| 89 |
doc.add_paragraph(lesson_text)
|
| 90 |
+
doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
filename = f"lesson_plan_{language.lower()}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
|
| 93 |
filepath = f"/tmp/{filename}"
|
|
|
|
| 96 |
return filepath
|
| 97 |
|
| 98 |
except Exception as e:
|
|
|
|
| 99 |
error_doc = Document()
|
| 100 |
error_doc.add_heading("Error in Lesson Plan Generator", 0)
|
| 101 |
error_doc.add_paragraph(str(e))
|
|
|
|
| 107 |
|
| 108 |
# ✅ Build Gradio UI
|
| 109 |
with gr.Blocks() as demo:
|
| 110 |
+
gr.Markdown("## 📘 BOPPPS Lesson Plan Generator (English + Urdu Improved)")
|
| 111 |
|
| 112 |
with gr.Group():
|
| 113 |
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 / دورانیہ", placeholder="e.g., 40")
|
| 121 |
+
date = gr.Textbox(label="Date / تاریخ", placeholder="e.g., 2025-10-05")
|
| 122 |
+
teacher = gr.Textbox(label="Teacher / استاد کا نام", placeholder="e.g., Najaf Ali Sharqi")
|
| 123 |
|
| 124 |
language = gr.Radio(["English", "Urdu"], label="Select Language / زبان منتخب کریں", value="English")
|
| 125 |
|
|
|
|
| 136 |
gr.Markdown("🔹 Developed by **Najaf Ali Sharqi**")
|
| 137 |
|
| 138 |
demo.launch()
|
| 139 |
+
|