Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import datetime | |
| from groq import Groq | |
| from docx import Document | |
| from docx.shared import Pt | |
| from tempfile import NamedTemporaryFile | |
| # ✅ Groq API Key | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| client = Groq(api_key=GROQ_API_KEY) | |
| # ✅ Function to generate lesson plan | |
| def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher, theme): | |
| if language == "Urdu": | |
| model_name = "openai/gpt-oss-120b" | |
| else: | |
| model_name = "llama-3.1-8b-instant" | |
| # ✅ Prompt templates | |
| if language == "English": | |
| prompt = f""" | |
| You are an expert lesson plan designer. | |
| Create a complete **BOPPPS Model Lesson Plan** in English with the following sections: | |
| 1. Bridge-in | |
| 2. Pre-Assessment | |
| 3. Learning Outcomes | |
| 4. Participatory Learning Activities | |
| 5. Post-Assessment | |
| 6. Summary | |
| 7. Home Assignment aligned with SLOs | |
| Make it logical, creative, and suitable for grade level. | |
| Do not repeat input details in body text. | |
| Include only one concise header table of details. | |
| Class: {class_} | |
| Subject: {subject} | |
| Topic: {topic} | |
| Students: {students} | |
| Duration: {duration} | |
| Date: {date} | |
| Teacher: {teacher} | |
| """ | |
| else: | |
| prompt = f""" | |
| آپ ایک ماہر استاد ہیں۔ درج ذیل معلومات کی بنیاد پر **BOPPPS ماڈل** کے مطابق ایک مکمل روزانہ کا تدریسی منصوبہ اردو زبان میں تیار کریں۔ | |
| انگریزی حروف استعمال نہ کریں، صرف اردو رسم الخط استعمال کریں۔ | |
| درج ذیل حصے لازمی شامل ہوں: | |
| ۱۔ ابتدائی تعارف (Bridge-in) | |
| ۲۔ ابتدائی تشخیص (Pre-Assessment) | |
| ۳۔ تعلیمی مقاصد (Learning Outcomes) | |
| ۴۔ شراکتی تعلیمی سرگرمیاں (Participatory Learning Activities) | |
| ۵۔ بعد از تدریس تشخیص (Post-Assessment) | |
| ۶۔ خلاصہ (Summary) | |
| ۷۔ گھریلو کام (SLOs کے مطابق) | |
| جماعت: {class_} | |
| مضمون: {subject} | |
| عنوان: {topic} | |
| طلباء کی تعداد: {students} | |
| دورانیہ: {duration} | |
| تاریخ: {date} | |
| معلم: {teacher} | |
| """ | |
| # ✅ Generate text | |
| response = client.chat.completions.create( | |
| model=model_name, | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0.7, | |
| ) | |
| lesson_text = response.choices[0].message.content.strip() | |
| # ✅ ENGLISH VERSION — create .docx | |
| if language == "English": | |
| doc = Document() | |
| doc.add_heading("BOPPPS Lesson Plan", level=1) | |
| table = doc.add_table(rows=7, cols=2) | |
| table.style = "Table Grid" | |
| headers = ["Class", "Subject", "Topic", "Students", "Duration", "Date", "Teacher"] | |
| values = [class_, subject, topic, students, duration, date, teacher] | |
| for i in range(7): | |
| table.cell(i, 0).text = headers[i] | |
| table.cell(i, 1).text = str(values[i]) | |
| doc.add_paragraph("\n" + lesson_text) | |
| doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi") | |
| temp_file = NamedTemporaryFile(delete=False, suffix=".docx") | |
| doc.save(temp_file.name) | |
| final_text = f"✅ English lesson plan generated successfully for {subject} (Grade {class_})." | |
| file_output = temp_file.name | |
| # ✅ URDU VERSION — create .md | |
| else: | |
| urdu_md = f""" | |
| <div dir="rtl" align="right"> | |
| # یومیہ تدریسی منصوبہ (BOPPPS ماڈل) | |
| | کلیدی معلومات | تفصیل | | |
| |----------------|--------| | |
| | جماعت | {class_} | | |
| | مضمون | {subject} | | |
| | عنوان | {topic} | | |
| | طلباء کی تعداد | {students} | | |
| | دورانیہ | {duration} | | |
| | تاریخ | {date} | | |
| | معلم | {teacher} | | |
| --- | |
| {lesson_text} | |
| --- | |
| **مرتب کردہ: نجف علی شرقی** | |
| </div> | |
| """ | |
| temp_file = NamedTemporaryFile(delete=False, suffix=".md", mode="w", encoding="utf-8") | |
| temp_file.write(urdu_md) | |
| temp_file.close() | |
| final_text = f"✅ اردو تدریسی منصوبہ برائے مضمون {subject} کامیابی سے تیار ہو گیا۔" | |
| file_output = temp_file.name | |
| # ✅ Theme customization (brightness) | |
| if theme == "Dark": | |
| theme_css = "background-color:#111;color:#fff;padding:10px;border-radius:10px;" | |
| else: | |
| theme_css = "background-color:#fdfdfd;color:#000;padding:10px;border-radius:10px;" | |
| safe_html = final_text.replace("\n", "<br>") | |
| display_html = f"<div style='{theme_css}'>{safe_html}</div>" | |
| return display_html, file_output | |
| # ✅ Dropdowns and Interface | |
| demo = gr.Interface( | |
| fn=generate_lesson_plan, | |
| inputs=[ | |
| gr.Radio(["English", "Urdu"], label="Select Language / زبان کا انتخاب"), | |
| gr.Dropdown([f"Grade {i}" for i in range(1, 17)], label="Class / جماعت"), | |
| gr.Dropdown( | |
| ["Urdu", "English", "Arabic", "Science", "Mathematics", "Biology", | |
| "Chemistry", "Physics", "Zoology", "Botany", "Civics", "Islamiat"], | |
| label="Subject / مضمون" | |
| ), | |
| gr.Textbox(label="Topic / عنوان"), | |
| gr.Slider(1, 200, value=30, step=1, label="Number of Students / طلباء کی تعداد"), | |
| gr.Slider(30, 60, value=40, step=5, label="Duration (minutes) / دورانیہ (منٹ)"), | |
| gr.Textbox(label="Date / تاریخ", value=str(datetime.date.today())), | |
| gr.Textbox(label="Teacher Name / معلم کا نام"), | |
| gr.Radio(["Light", "Dark"], label="Theme / پس منظر کا انتخاب", value="Light"), | |
| ], | |
| outputs=[ | |
| gr.HTML(label="📄 Lesson Plan Preview"), | |
| gr.File(label="⬇️ Download File (.docx or .md)") | |
| ], | |
| title="📘 AI BOPPPS Lesson Plan Generator (Urdu + English)", | |
| description=( | |
| "⚙️ Developed by **Najaf Ali Sharqi**. <br>" | |
| "یہ ایپ خودکار طور پر اردو یا انگریزی میں مکمل BOPPPS ماڈل کے مطابق یومیہ تدریسی منصوبہ تیار کرتی ہے۔<br>" | |
| "اردو ورژن `.md` فارمیٹ میں جبکہ انگریزی ورژن `.docx` میں ڈاؤن لوڈ ہوتا ہے۔" | |
| ), | |
| theme="default" | |
| ) | |
| demo.launch() | |