import gradio as gr from google import genai from docx import Document from docx.shared import Pt, RGBColor from docx.enum.text import WD_ALIGN_PARAGRAPH import os # Initialize Gemini client from google import genai client = genai.Client(api_key="AIzaSyBNy1L8y-hA8ZV8sgL4Cknw0wStV8koHgg") def generate_lesson_plan(language, grade, subject, section, topic, teacher, date, students, slo): prompt = f""" You are a qualified and experienced school teacher in Pakistan, fully familiar with the National Curriculum and classroom context. Create a SMART LESSON PLAN in {language} for the topic "{topic}" with the following details: Class: {grade},Section: {section} Subject: {subject}, Teacher: {teacher}, Date: {date}, Students: {students}, SLO: {slo} Rules: 1. Use the logo in center before header in center from the link "https://banner2.cleanpng.com/lnd/20250115/kl/b8b7a261362e1cc2ad46527307e685.webp" 2. Write the header as: "School Education Department Gilgit-Baltistan" ,font syle bold and size 18. 3. Write Second heading and underline "School Education Department Gilgit-Baltistan", font size 24. 4. Include an input table at the top with: Class, Section, Subject, Date, Teacher, Number of Students, Topic. 5. Do NOT repeat this input data after the table. 6. Write the given SLO only with SLO heading 7. After writing SLOs, add a new heading **Content Knowledge** and and use definition and describe key concepts need for each SLO. 8. After content, add an optional **Home Assignment** section that is SMART and aligned with the SLOs. 9. Avoid any extra lines or repetition. Write all text clearly and professionally. 10. English version: All text in English with bold headings. Urdu version: All text in Urdu with English terms in Urdu script, right aligned, and no special characters. Return full formatted lesson plan text only (no developer credit inside the content). """ response = client.models.generate_content( model="gemini-2.5-flash", contents=prompt ) lesson_text = response.text.strip() if language.lower() == "english": # Create Word document for English version doc = Document() # Header header = doc.add_paragraph() run = header.add_run("School Education Department Gilgit-Baltistan") run.bold = True run.font.color.rgb = RGBColor(0, 128, 0) header.alignment = WD_ALIGN_PARAGRAPH.CENTER # Input table table = doc.add_table(rows=4, cols=4) table.style = 'Table Grid' cells = table.rows[0].cells cells[0].text = "Class" cells[1].text = str(grade) cells[2].text = "Section" cells[3].text = str(section) cells = table.rows[1].cells cells[0].text = "Subject" cells[1].text = str(subject) cells[2].text = "Date" cells[3].text = str(date) cells = table.rows[2].cells cells[0].text = "Teacher Name" cells[1].text = str(teacher) cells[2].text = "Number of Students" cells[3].text = str(students) cells = table.rows[3].cells cells[0].text = "Topic" # Merge columns 1, 2, 3 into a single cell merged_cell = cells[1].merge(cells[2]) merged_cell = merged_cell.merge(cells[3]) # Write the topic in merged cell merged_cell.text = str(topic) # Add lesson content for line in lesson_text.split("\n"): if line.strip(): # Write SLO once before each lesson point slo_para = doc.add_paragraph(f"SLO: {slo}") slo_para.space_after = Pt(12) # Write the lesson line para = doc.add_paragraph(line.strip()) para.paragraph_format.space_after = Pt(6) # Bold headings if needed if any(line.strip().startswith(h) for h in ["Content Knowledge", "Home Assignment"]): para.runs[0].bold = True # Save English file file_path = "/tmp/SMART_Lesson_Plan_SED.docx" doc.save(file_path) else: # Urdu version in Markdown file_path = "/tmp/SMART_Lesson_Plan_Urdu.md" with open(file_path, "w", encoding="utf-8") as f: f.write("School Education Department Gilgit-Baltistan\n\n") f.write("| درجہ | مضمون | موضوع | استاد |\n") f.write(f"| {grade} | {subject} | {topic} | {teacher} |\n\n") f.write(f"**تاریخ:** {date} **طلبہ کی تعداد:** {students} **دورانیہ:** {duration} منٹ\n\n") for line in lesson_text.split("\n"): if line.strip(): if any(h in line for h in ["SLOs", "Content Knowledge", "Home Assignment"]): f.write(f"**{line.strip()}**\n\n") else: f.write(f"{line.strip()}\n\n") return file_path # Create Gradio UI with gr.Blocks() as demo: gr.Markdown("SMART Lesson Plan (SED)") gr.Markdown("Developed by Ashuftah Shigri") with gr.Row(): language = gr.Dropdown(["English", "Urdu"], label="Language") grade = gr.Dropdown([f"Class {i}" for i in range(1, 13)], label="Class") subject = gr.Dropdown(["English","Urdu", "Islamiyat", "Mathematics", "Science", "Computer", "Chemistry", "Physics", "Biology", "Pakistan Studies"], label="Subject") section = gr.Textbox(label="Section") topic = gr.Textbox(label="Topic") teacher = gr.Textbox(label="Teacher Name") date = gr.Textbox(label="Date (DD-MM-YYYY)") students = gr.Number(label="Number of Students", value=50) slo = gr.Textbox(label="Write SLO") btn = gr.Button("Generate Lesson Plan") output_file = gr.File(label="Download Lesson Plan") btn.click(generate_lesson_plan, inputs=[language, grade, subject, section, topic, teacher, date, students, slo], outputs=output_file) demo.launch()