File size: 22,305 Bytes
92f356a
 
 
 
 
b384740
63ce019
1f9daab
dfdf429
ff1591a
dd1de57
 
acc2409
b6f8b4b
31b89f7
dd1de57
 
 
dfdf429
dd1de57
dfdf429
dd1de57
 
dfdf429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1de57
31b89f7
acc2409
dd1de57
 
 
 
acc2409
b1d497e
dd1de57
 
 
 
 
2ee2a0c
dd1de57
dfdf429
 
 
 
 
dd1de57
92f356a
dd1de57
dfdf429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1de57
92f356a
dd1de57
92f356a
dfdf429
 
 
 
 
 
 
 
 
 
dd1de57
 
 
 
 
dfdf429
dd1de57
 
dfdf429
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1de57
 
dfdf429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1de57
 
 
92f356a
dfdf429
 
 
 
 
 
92f356a
dfdf429
 
 
 
 
 
ee87489
dd1de57
 
 
dfdf429
dd1de57
 
 
dfdf429
 
 
ee87489
dfdf429
 
 
 
 
dd1de57
 
27e0d71
dfdf429
 
 
63ce019
dfdf429
dd1de57
 
92f356a
dd1de57
 
ee87489
dd1de57
 
ee87489
 
dfdf429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92f356a
dfdf429
 
92f356a
dfdf429
 
 
92f356a
dfdf429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92f356a
dfdf429
 
 
 
 
 
 
 
 
 
 
 
92f356a
dfdf429
92f356a
dfdf429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92f356a
dfdf429
 
92f356a
dfdf429
 
 
 
 
 
92f356a
dfdf429
 
dd1de57
dfdf429
 
 
 
92f356a
dfdf429
 
 
 
92f356a
dfdf429
dd1de57
dfdf429
dd1de57
dfdf429
 
dd1de57
dfdf429
 
 
dd1de57
dfdf429
 
92f356a
dfdf429
 
dd1de57
dfdf429
 
92f356a
dfdf429
 
 
92f356a
dfdf429
 
 
92f356a
dfdf429
 
 
 
 
 
92f356a
dfdf429
92f356a
dfdf429
92f356a
dfdf429
92f356a
dfdf429
 
ee87489
92f356a
dfdf429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acc2409
 
dfdf429
 
 
 
 
acc2409
dfdf429
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
import os
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
os.environ["HF_HUB_OFFLINE"] = "1"
os.environ["GRADIO_TEMP_DIR"] = "/tmp"

import gradio as gr
from groq import Groq
from docx import Document
from docx.shared import Pt, RGBColor, Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
from datetime import datetime
import re

# Initialize Groq client
try:
    client = Groq()
    print("Groq client initialized successfully")
except Exception as e:
    print(f"Warning: Groq client initialization failed: {e}")
    client = None

# ---------------- TOKEN SAFE GENERATION ---------------- #

def safe_generate(messages, max_tokens=400):
    """
    Controlled generation to avoid token overflow
    """
    try:
        response = client.chat.completions.create(
            model="openai/gpt-oss-120b",
            messages=messages,
            temperature=0.7,
            max_completion_tokens=max_tokens,
            top_p=0.9
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        return f"Error: {str(e)}"


# ---------------- DATA ---------------- #

SUBJECTS_BY_GRADE = {
    "Grade 1-5": ["English", "Urdu", "Mathematics", "General Science", "Islamiyat", "Pakistan Studies", "Computer"],
    "Grade 6-8": ["English", "Urdu", "Mathematics", "General Science", "Islamiyat", "Pakistan Studies", "Computer"],
    "Grade 9-10": ["English", "Urdu", "Mathematics", "Physics", "Chemistry", "Biology", "Islamiyat", "Pakistan Studies", "Computer Science"],
    "Grade 11-12": ["English", "Urdu", "Mathematics", "Physics", "Chemistry", "Biology", "Islamiyat", "Pakistan Studies", "Computer Science", "Economics", "Statistics"]
}

DISTRICTS_GB = [
    "Gilgit", "Ghizer", "Hunza", "Nagar", "Skardu", "Shigar",
    "Kharmang", "Ghanche", "Astore", "Diamer", "Darel", "Tangir",
    "Gupis Yasin", "Roundu"
]

TEACHING_METHODS = [
    "Lecture Method", "Discussion Method", "Demonstration Method",
    "Activity-Based Learning", "Inquiry-Based Learning", "Group Work",
    "Project-Based Learning", "Problem-Based Learning", "Hands-on Activities",
    "Visual Learning", "Interactive Learning", "Storytelling",
    "Cooperative Learning", "Flipped Classroom", "Think-Pair-Share"
]

ASSESSMENT_TYPES = [
    "Formative Assessment", "Summative Assessment", "Oral Questions",
    "Written Test", "Practical Work", "Class Participation",
    "Quiz", "Worksheet", "Group Presentation", "Individual Assignment",
    "Peer Assessment", "Self Assessment", "Portfolio Assessment"
# ---------------- URDU TRANSLATIONS ---------------- #

URDU_TRANSLATIONS = {
    "English": "انگریزی",
    "Urdu": "اردو",
    "Mathematics": "ریاضی",
    "General Science": "عمومی سائنس",
    "Islamiyat": "اسلامیات",
    "Pakistan Studies": "پاکستان کی تاریخ و جغرافیہ",
    "Computer": "کمپیوٹر",
    "Physics": "طبیعیات",
    "Chemistry": "کیمیا",
    "Biology": "حیاتیات",
    "Computer Science": "کمپیوٹر سائنس",
    "Economics": "معاشیات",
    "Statistics": "شماریات",

    "Lecture Method": "لیکچر کا طریقہ",
    "Discussion Method": "گفتگو کا طریقہ",
    "Demonstration Method": "عملی مظاہرہ",
    "Activity-Based Learning": "سرگرمی پر مبنی تعلیم",
    "Inquiry-Based Learning": "تحقیقی تعلیم",
    "Group Work": "اجتماعی کام",
    "Project-Based Learning": "منصوبہ پر مبنی تعلیم",
    "Problem-Based Learning": "مسئلہ حل کرنے کی تعلیم",
    "Hands-on Activities": "عملی سرگرمیاں",
    "Visual Learning": "بصری تعلیم",
    "Interactive Learning": "تعاملی تعلیم",
    "Storytelling": "کہانی سنانا",
    "Cooperative Learning": "باہمی تعاون سے سیکھنا",
    "Flipped Classroom": "تبدیل شدہ کلاس روم",
    "Think-Pair-Share": "سوچیں، جوڑے بنائیں، شیئر کریں",

    "Formative Assessment": "تشکیلی تشخیص",
    "Summative Assessment": "اختتامی تشخیص",
    "Oral Questions": "زبانی سوالات",
    "Written Test": "تحریری امتحان",
    "Practical Work": "عملی کام",
    "Class Participation": "کلاسی شرکت",
    "Quiz": "مختصر امتحان",
    "Worksheet": "ورک شیٹ",
    "Group Presentation": "اجتماعی پیشکش",
    "Individual Assignment": "انفرادی تفویض",
    "Peer Assessment": "ہم جماعت کی تشخیص",
    "Self Assessment": "خود تشخیص",
    "Portfolio Assessment": "پورٹ فولیو کی تشخیص",

    "Grade 1": "جماعت اول",
    "Grade 2": "جماعت دوم",
    "Grade 3": "جماعت سوم",
    "Grade 4": "جماعت چہارم",
    "Grade 5": "جماعت پنجم",
    "Grade 6": "جماعت ششم",
    "Grade 7": "جماعت ہفتم",
    "Grade 8": "جماعت ہشتم",
    "Grade 9": "جماعت نہم",
    "Grade 10": "جماعت دہم",
    "Grade 11": "جماعت یازدہم",
    "Grade 12": "جماعت دوازدہم",

    "District": "ضلع",
    "School": "تعلیمی ادارہ",
    "Teacher": "استاد کا نام",
    "Date": "تاریخ",
    "Topic": "عنوانِ سبق",
    "Duration": "دورانیہ",
    "No. of Students": "طلبہ کی تعداد",
    "Teaching Method": "تدریسی طریقہ",
    "Assessment Type": "تشخیصی طریقہ",
    "minutes": "منٹ"
}


# ---------------- HELPERS ---------------- #

def translate_to_urdu(text):
    if not text:
        return text
    if text in URDU_TRANSLATIONS:
        return URDU_TRANSLATIONS[text]

    words = str(text).split()
    return " ".join([URDU_TRANSLATIONS.get(w, w) for w in words])


def update_subjects(grade_range):
    if not grade_range:
        return gr.Dropdown(choices=[], value=None)
    return gr.Dropdown(choices=SUBJECTS_BY_GRADE.get(grade_range, []), value=None)


def validate_inputs(language, grade_range, subject, topic, teacher_name, school_name):
    errors = []

    if not language:
        errors.append("Language is required")
    if not grade_range:
        errors.append("Grade range is required")
    if not subject:
        errors.append("Subject is required")
    if not topic or len(topic.strip()) < 3:
        errors.append("Topic too short")
    if not teacher_name:
        errors.append("Teacher name required")
    if not school_name:
        errors.append("School name required")

    return errors


# ---------------- DOCX UTILITIES ---------------- #

def add_table_border(table):
    tbl = table._tbl
    tblPr = tbl.tblPr

    if tblPr is None:
        tblPr = OxmlElement('w:tblPr')
        tbl.insert(0, tblPr)

    borders = OxmlElement('w:tblBorders')

    for name in ['top', 'left', 'bottom', 'right', 'insideH', 'insideV']:
        border = OxmlElement(f'w:{name}')
        border.set(qn('w:val'), 'single')
        border.set(qn('w:sz'), '4')
        border.set(qn('w:color'), '000000')
        borders.append(border)

    tblPr.append(borders)


def apply_rtl_to_paragraph(para, alignment=WD_ALIGN_PARAGRAPH.RIGHT):
    para.alignment = alignment
    para.paragraph_format.right_to_left = True

    pPr = para._element.get_or_add_pPr()
    bidi = OxmlElement('w:bidi')
    pPr.append(bidi)


def apply_rtl_to_run(run):
    run.font.rtl = True
    run.font.name = 'Arial'


def create_rtl_table_cell(cell, text, bold=False):
    cell.text = ''
    para = cell.paragraphs[0]
    run = para.add_run(str(text))

    run.font.size = Pt(11)
    run.font.name = 'Arial'
    run.font.rtl = True

    if bold:
        run.bold = True

    apply_rtl_to_paragraph(para)
def generate_lesson_plan(language, grade_range, grade_specific, subject, topic,
                         school_name, teacher_name, district, date_input,
                         students, duration, teaching_method, assessment_type,
                         include_resources, include_differentiation):

    # ---------- VALIDATION ----------
    errors = validate_inputs(language, grade_range, subject, topic, teacher_name, school_name)
    if errors:
        return None, "\n".join(errors)

    if client is None:
        return None, "Groq API not initialized"

    # ---------- DATE ----------
    try:
        formatted_date = datetime.strptime(date_input, "%Y-%m-%d").strftime("%Y-%m-%d")
    except:
        formatted_date = datetime.now().strftime("%Y-%m-%d")

    # ---------- GRADE ----------
    full_grade = grade_specific if grade_specific else grade_range.split('-')[0]

    # ---------- URDU PREP ----------
    if language == "Urdu":
        full_grade_urdu = translate_to_urdu(full_grade)
        subject_urdu = translate_to_urdu(subject)
        district_urdu = translate_to_urdu(district)
        teaching_method_urdu = translate_to_urdu(teaching_method)
        assessment_type_urdu = translate_to_urdu(assessment_type)
        topic_urdu = topic

    # ============================================================
    # 🔥 CHUNKED GENERATION STARTS HERE
    # ============================================================

    print(f"Generating {language} lesson plan (chunked mode)...")

    # ---------- ENGLISH VERSION ----------
    if language == "English":

        # 1️⃣ SLOs
        slos = safe_generate([
            {"role": "system", "content": "Generate in English only"},
            {"role": "user", "content": f"""
Write 3 SMART SLOs:
Grade: {full_grade}
Subject: {subject}
Topic: {topic}

Use:
1 Knowledge
2 Understanding
3 Application
"""}
        ], max_tokens=250)

        # 2️⃣ CONTENT
        content = safe_generate([
            {"role": "system", "content": "Generate in English only"},
            {"role": "user", "content": f"""
Write CONTENT KNOWLEDGE for:
{topic} (Grade {full_grade})

Include:
- Key concepts
- Definitions
- Examples (Pakistan context)
{"- Resources" if include_resources else ""}
{"- Differentiation" if include_differentiation else ""}

No teaching steps.
"""}
        ], max_tokens=600)

        # 3️⃣ ASSIGNMENT
        assignment = safe_generate([
            {"role": "system", "content": "Generate in English only"},
            {"role": "user", "content": f"""
Create HOME ASSIGNMENT:
Topic: {topic}
Grade: {full_grade}

3-5 questions.
"""}
        ], max_tokens=250)

        lesson_text = f"""
SMART STUDENT LEARNING OUTCOMES
{slos}

CONTENT KNOWLEDGE
{content}

HOME ASSIGNMENT
{assignment}
"""

    # ---------- URDU VERSION ----------
    else:

        slos = safe_generate([
            {"role": "system", "content": "صرف اردو میں لکھیں"},
            {"role": "user", "content": f"""
3 اسمارٹ نتائج لکھیں:
جماعت: {full_grade_urdu}
مضمون: {subject_urdu}
عنوان: {topic_urdu}
"""}
        ], max_tokens=250)

        content = safe_generate([
            {"role": "system", "content": "صرف اردو میں لکھیں"},
            {"role": "user", "content": f"""
علمی مواد لکھیں:
{topic_urdu} (جماعت {full_grade_urdu})

شامل کریں:
- بنیادی تصورات
- تعریفات
- مثالیں
{"- وسائل" if include_resources else ""}
{"- مختلف طلبہ کے لیے حکمت عملی" if include_differentiation else ""}
"""}
        ], max_tokens=600)

        assignment = safe_generate([
            {"role": "system", "content": "صرف اردو میں لکھیں"},
            {"role": "user", "content": f"""
گھر کا کام بنائیں:
{topic_urdu}

3-5 سوالات شامل کریں۔
"""}
        ], max_tokens=250)

        lesson_text = f"""
اسمارٹ طلبہ سیکھنے کے نتائج
{slos}

علمی مواد
{content}

گھر کی تفویض
{assignment}
"""

    # Clean extra noise
    lesson_text = lesson_text.strip()

    # ============================================================
    # ✅ RETURN TEXT FOR DOC GENERATION (PART 4)
    # ============================================================

    return lesson_text, {
        "formatted_date": formatted_date,
        "full_grade": full_grade,
        "topic": topic,
        "subject": subject,
        "district": district,
        "students": students,
        "duration": duration,
        "teacher_name": teacher_name,
        "school_name": school_name,
        "teaching_method": teaching_method,
        "assessment_type": assessment_type,
        "language": language,
        "topic_urdu": topic if language == "Urdu" else None
    }

def build_docx(lesson_text, meta):
    """
    DOCX builder using already generated safe text (NO API CALLS HERE)
    """

    language = meta["language"]
    topic = meta["topic"]
    subject = meta["subject"]
    district = meta["district"]
    teacher_name = meta["teacher_name"]
    school_name = meta["school_name"]
    full_grade = meta["full_grade"]
    students = meta["students"]
    duration = meta["duration"]
    teaching_method = meta["teaching_method"]
    assessment_type = meta["assessment_type"]
    formatted_date = meta["formatted_date"]

    doc = Document()

    # ============================================================
    # ===================== ENGLISH DOC ==========================
    # ============================================================
    if language == "English":

        section = doc.sections[0]
        section.top_margin = Inches(0.75)
        section.bottom_margin = Inches(0.75)
        section.left_margin = Inches(0.75)
        section.right_margin = Inches(0.75)

        # Header
        header = doc.add_paragraph()
        run = header.add_run("Government of Gilgit-Baltistan\nSchool Education Department")
        run.bold = True
        run.font.size = Pt(13)
        header.alignment = WD_ALIGN_PARAGRAPH.CENTER

        # Title
        title = doc.add_paragraph()
        run = title.add_run("SMART LESSON PLAN")
        run.bold = True
        run.font.size = Pt(14)
        title.alignment = WD_ALIGN_PARAGRAPH.CENTER

        doc.add_paragraph()

        # Table
        table = doc.add_table(rows=6, cols=4)
        add_table_border(table)

        def cell(r, c, text, bold=False):
            cell = table.rows[r].cells[c]
            cell.text = str(text)
            if bold:
                for p in cell.paragraphs:
                    for run in p.runs:
                        run.bold = True

        cell(0, 0, "Grade", True)
        cell(0, 1, full_grade)
        cell(0, 2, "Subject", True)
        cell(0, 3, subject)

        cell(1, 0, "Topic", True)
        cell(1, 1, topic)
        cell(1, 2, "Duration", True)
        cell(1, 3, f"{duration} min")

        cell(2, 0, "Teacher", True)
        cell(2, 1, teacher_name)
        cell(2, 2, "Date", True)
        cell(2, 3, formatted_date)

        cell(3, 0, "School", True)
        cell(3, 1, school_name)
        cell(3, 2, "District", True)
        cell(3, 3, district)

        cell(4, 0, "Students", True)
        cell(4, 1, students)
        cell(4, 2, "Method", True)
        cell(4, 3, teaching_method)

        cell(5, 0, "Assessment", True)
        table.rows[5].cells[1].merge(table.rows[5].cells[3])
        cell(5, 1, assessment_type)

        doc.add_paragraph()

        # Content sections (SAFE TEXT ONLY)
        for line in lesson_text.split("\n"):
            if line.strip():
                para = doc.add_paragraph(line.strip())
                para.alignment = WD_ALIGN_PARAGRAPH.LEFT

        file_path = f"/tmp/LP_{subject}_{topic[:20]}_{formatted_date}.docx"
        doc.save(file_path)

        return file_path, "English Lesson Plan Generated Successfully"

    # ============================================================
    # ======================= URDU DOC ===========================
    # ============================================================
    else:

        section = doc.sections[0]
        section.top_margin = Inches(0.75)
        section.bottom_margin = Inches(0.75)
        section.left_margin = Inches(0.75)
        section.right_margin = Inches(1)

        # Header
        header = doc.add_paragraph()
        run = header.add_run("حکومتِ گلگت بلتستان\nمحکمہ تعلیم")
        run.bold = True
        run.font.size = Pt(13)
        apply_rtl_to_paragraph(header)

        # Title
        title = doc.add_paragraph()
        run = title.add_run("اسمارٹ سبق کا منصوبہ")
        run.bold = True
        run.font.size = Pt(14)
        apply_rtl_to_paragraph(title)

        doc.add_paragraph()

        # Table (simplified safe version)
        table = doc.add_table(rows=6, cols=4)
        add_table_border(table)

        def urdu_cell(r, c, text, bold=False):
            cell = table.rows[r].cells[c]
            cell.text = str(text)
            para = cell.paragraphs[0]
            apply_rtl_to_paragraph(para)

        urdu_cell(0, 0, translate_to_urdu(subject))
        urdu_cell(0, 1, translate_to_urdu("Subject"), True)
        urdu_cell(0, 2, translate_to_urdu(full_grade))
        urdu_cell(0, 3, translate_to_urdu("Grade"), True)

        urdu_cell(1, 0, topic)
        urdu_cell(1, 1, translate_to_urdu("Topic"), True)
        urdu_cell(1, 2, f"{duration} {translate_to_urdu('minutes')}")
        urdu_cell(1, 3, translate_to_urdu("Duration"), True)

        urdu_cell(2, 0, teacher_name)
        urdu_cell(2, 1, translate_to_urdu("Teacher"), True)
        urdu_cell(2, 2, formatted_date)
        urdu_cell(2, 3, translate_to_urdu("Date"), True)

        urdu_cell(3, 0, district)
        urdu_cell(3, 1, translate_to_urdu("District"), True)
        urdu_cell(3, 2, school_name)
        urdu_cell(3, 3, translate_to_urdu("School"), True)

        urdu_cell(4, 0, students)
        urdu_cell(4, 1, translate_to_urdu("Students"), True)
        urdu_cell(4, 2, teaching_method)
        urdu_cell(4, 3, translate_to_urdu("Method"), True)

        urdu_cell(5, 0, assessment_type)
        table.rows[5].cells[0].merge(table.rows[5].cells[3])

        doc.add_paragraph()

        # Content (SAFE TEXT ONLY)
        for line in lesson_text.split("\n"):
            if line.strip():
                para = doc.add_paragraph(line.strip())
                apply_rtl_to_paragraph(para)

        file_path = f"/tmp/LP_Urdu_{topic[:20]}_{formatted_date}.docx"
        doc.save(file_path)

        return file_path, "اردو سبق کا منصوبہ تیار ہو گیا"

# ============================================================
# ================= GRADIO INTERFACE =========================
# ============================================================

custom_css = """
.gradio-container {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
}
.main-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 2rem;
    border-radius: 10px;
    color: white;
    margin-bottom: 2rem;
}
.section-header {
    background-color: #f8f9fa;
    padding: 1rem;
    border-left: 4px solid #667eea;
    margin: 1.5rem 0 1rem 0;
    border-radius: 5px;
}
"""

with gr.Blocks(theme=gr.themes.Soft(), css=custom_css,
               title="SMART Lesson Plan Generator") as demo:

    gr.HTML("""
    <div class="main-header">
        <h1 style="margin:0;">SMART Lesson Plan Generator</h1>
        <p>AI Powered Lesson Planning System (Pakistan Curriculum)</p>
    </div>
    """)

    # ---------------- INPUTS ---------------- #
    language = gr.Dropdown(["English", "Urdu"], value="English", label="Language")

    grade_range = gr.Dropdown(
        ["Grade 1-5", "Grade 6-8", "Grade 9-10", "Grade 11-12"],
        label="Grade Range"
    )

    grade_specific = gr.Dropdown(
        [f"Grade {i}" for i in range(1, 13)],
        label="Specific Grade (Optional)"
    )

    subject = gr.Dropdown([], label="Subject")

    topic = gr.Textbox(label="Topic")

    school_name = gr.Textbox(label="School Name")
    teacher_name = gr.Textbox(label="Teacher Name")

    district = gr.Dropdown(DISTRICTS_GB, label="District")

    date_input = gr.Textbox(label="Date", value=datetime.now().strftime("%Y-%m-%d"))

    students = gr.Number(label="Students", value=30)
    duration = gr.Number(label="Duration (minutes)", value=40)

    teaching_method = gr.Dropdown(TEACHING_METHODS, label="Teaching Method")
    assessment_type = gr.Dropdown(ASSESSMENT_TYPES, label="Assessment Type")

    include_resources = gr.Checkbox(label="Include Resources", value=True)
    include_differentiation = gr.Checkbox(label="Include Differentiation", value=True)

    # ---------------- OUTPUT ---------------- #
    file_output = gr.File(label="Download Lesson Plan")
    status_output = gr.Textbox(label="Status")

    # ============================================================
    # ===================== EVENT PIPELINE =======================
    # ============================================================

    def pipeline(*inputs):
        """
        FULL FIXED PIPELINE:
        1. Generate chunked lesson text
        2. Build DOCX
        """

        lesson_text, meta = generate_lesson_plan(*inputs)

        file_path, status = build_docx(lesson_text, meta)

        return file_path, status


    # Grade → Subject update
    grade_range.change(update_subjects, grade_range, subject)

    # Generate button
    gr.Button("Generate Lesson Plan", variant="primary").click(
        pipeline,
        inputs=[
            language,
            grade_range,
            grade_specific,
            subject,
            topic,
            school_name,
            teacher_name,
            district,
            date_input,
            students,
            duration,
            teaching_method,
            assessment_type,
            include_resources,
            include_differentiation
        ],
        outputs=[file_output, status_output]
    )


# ============================================================
# ===================== LAUNCH APP ============================
# ============================================================

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        show_error=True,
        allowed_paths=["/tmp"]
    )