Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,173 +1,166 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
from docx import Document
|
| 5 |
-
from docx.
|
|
|
|
| 6 |
from docx.oxml import OxmlElement
|
| 7 |
-
from docx.
|
| 8 |
import datetime
|
| 9 |
from groq import Groq
|
| 10 |
|
| 11 |
-
# ✅
|
| 12 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 13 |
-
|
| 14 |
-
# Initialize Groq client
|
| 15 |
client = Groq(api_key=GROQ_API_KEY)
|
| 16 |
|
| 17 |
|
| 18 |
-
# ✅
|
| 19 |
-
def
|
| 20 |
-
"""Apply right-to-left Nastaleeq font to a paragraph"""
|
| 21 |
-
run = paragraph.runs[0] if paragraph.runs else paragraph.add_run()
|
| 22 |
run.font.name = "Jameel Noori Nastaleeq"
|
| 23 |
-
r = run._element
|
| 24 |
-
r.rFonts.set(qn(
|
| 25 |
-
r.rFonts.set(qn("w:cs"), "Jameel Noori Nastaleeq")
|
| 26 |
-
paragraph.alignment = 2 # right alignment
|
| 27 |
-
rtl = OxmlElement("w:rtl")
|
| 28 |
-
rtl.set(qn("w:val"), "1")
|
| 29 |
-
paragraph._element.insert(0, rtl)
|
| 30 |
|
| 31 |
|
| 32 |
-
# ✅
|
| 33 |
def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
|
| 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 |
if language == "Urdu":
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
"مضمون": subject,
|
| 89 |
-
"عنوان": topic,
|
| 90 |
-
"طلباء کی تعداد": students,
|
| 91 |
-
"دورانیہ (منٹوں میں)": duration,
|
| 92 |
-
"تاریخ": date,
|
| 93 |
-
"استاد کا نام": teacher
|
| 94 |
-
}
|
| 95 |
else:
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
"Subject": subject,
|
| 99 |
-
"Topic": topic,
|
| 100 |
-
"No. of Students": students,
|
| 101 |
-
"Duration": duration,
|
| 102 |
-
"Date": date,
|
| 103 |
-
"Teacher Name": teacher
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
for key, value in data.items():
|
| 107 |
-
row_cells = table.add_row().cells
|
| 108 |
-
row_cells[0].text = key
|
| 109 |
-
row_cells[1].text = str(value)
|
| 110 |
-
|
| 111 |
-
# Apply Urdu formatting to both cells if Urdu mode
|
| 112 |
-
if language == "Urdu":
|
| 113 |
-
for cell in row_cells:
|
| 114 |
-
for paragraph in cell.paragraphs:
|
| 115 |
-
apply_urdu_format(paragraph)
|
| 116 |
-
|
| 117 |
-
doc.add_paragraph("\n")
|
| 118 |
-
|
| 119 |
-
# Add generated text paragraph
|
| 120 |
-
paragraph = doc.add_paragraph(lesson_text)
|
| 121 |
-
|
| 122 |
-
# Apply Urdu formatting
|
| 123 |
-
if language == "Urdu":
|
| 124 |
-
apply_urdu_format(paragraph)
|
| 125 |
-
|
| 126 |
-
doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
-
error_doc = Document()
|
| 136 |
-
error_doc.add_heading("Error in Lesson Plan Generator", 0)
|
| 137 |
-
error_doc.add_paragraph(str(e))
|
| 138 |
-
filename = f"error_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
|
| 139 |
-
filepath = f"/tmp/{filename}"
|
| 140 |
-
error_doc.save(filepath)
|
| 141 |
-
return filepath
|
| 142 |
|
| 143 |
|
| 144 |
# ✅ Gradio UI
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
generate_button.click(
|
| 165 |
-
fn=generate_lesson_plan,
|
| 166 |
-
inputs=[language, class_, subject, topic, students, duration, date, teacher],
|
| 167 |
-
outputs=output_file
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
gr.Markdown("---")
|
| 171 |
-
gr.Markdown("🔹 Developed by **Najaf Ali Sharqi**")
|
| 172 |
-
|
| 173 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from docx import Document
|
| 4 |
+
from docx.shared import Pt, Inches
|
| 5 |
+
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
| 6 |
from docx.oxml import OxmlElement
|
| 7 |
+
from docx.oxml.ns import qn
|
| 8 |
import datetime
|
| 9 |
from groq import Groq
|
| 10 |
|
| 11 |
+
# ✅ Get Groq API Key
|
| 12 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
|
|
|
|
| 13 |
client = Groq(api_key=GROQ_API_KEY)
|
| 14 |
|
| 15 |
|
| 16 |
+
# ✅ Urdu Nastaliq Font Style (Jameel or similar)
|
| 17 |
+
def set_urdu_font(run):
|
|
|
|
|
|
|
| 18 |
run.font.name = "Jameel Noori Nastaleeq"
|
| 19 |
+
r = run._element
|
| 20 |
+
r.rPr.rFonts.set(qn('w:eastAsia'), "Jameel Noori Nastaleeq")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
+
# ✅ Generate Lesson Plan Function
|
| 24 |
def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
|
| 25 |
+
# ✅ Define model based on language
|
| 26 |
+
if language == "Urdu":
|
| 27 |
+
model_name = "openai/gpt-oss-120b"
|
| 28 |
+
else:
|
| 29 |
+
model_name = "llama-3.1-8b-instant"
|
| 30 |
+
|
| 31 |
+
# ✅ Create prompt
|
| 32 |
+
if language == "English":
|
| 33 |
+
prompt = f"""
|
| 34 |
+
Generate a comprehensive daily lesson plan in English.
|
| 35 |
+
Class: {class_}
|
| 36 |
+
Subject: {subject}
|
| 37 |
+
Topic: {topic}
|
| 38 |
+
Students: {students}
|
| 39 |
+
Duration: {duration}
|
| 40 |
+
Date: {date}
|
| 41 |
+
Teacher: {teacher}
|
| 42 |
+
|
| 43 |
+
The plan should include:
|
| 44 |
+
1. Learning Outcomes (SLOs)
|
| 45 |
+
2. Introduction
|
| 46 |
+
3. Teaching Methodology
|
| 47 |
+
4. Activities
|
| 48 |
+
5. Assessment
|
| 49 |
+
6. Reflection
|
| 50 |
+
7. Home Assignment aligned with SLOs.
|
| 51 |
+
"""
|
| 52 |
+
else:
|
| 53 |
+
prompt = f"""
|
| 54 |
+
درج ذیل تفصیلات کی بنیاد پر ایک جامع، خوبصورت اور بامعنی روزانہ اسباق کا منصوبہ اردو زبان میں تیار کریں۔
|
| 55 |
+
جماعت: {class_}
|
| 56 |
+
مضمون: {subject}
|
| 57 |
+
عنوان: {topic}
|
| 58 |
+
طلباء کی تعداد: {students}
|
| 59 |
+
دورانیہ: {duration}
|
| 60 |
+
تاریخ: {date}
|
| 61 |
+
معلم: {teacher}
|
| 62 |
+
|
| 63 |
+
منصوبے میں درج ذیل اجزاء لازمی شامل ہوں:
|
| 64 |
+
۱۔ تعلیمی مقاصد (SLOs)
|
| 65 |
+
۲۔ تعارف سبق
|
| 66 |
+
۳۔ تدریسی طریقہ کار
|
| 67 |
+
۴۔ تعلیمی سرگرمیاں
|
| 68 |
+
۵۔ تشخیص
|
| 69 |
+
۶۔ عکاسی (Reflection)
|
| 70 |
+
۷۔ گھریلو کام (Home Assignment) جو درج ذیل خوبیوں کا حامل ہو:
|
| 71 |
+
• مناسب طوالت رکھتا ہو۔
|
| 72 |
+
• منطقی ترتیب رکھتا ہو۔
|
| 73 |
+
• طلباء میں تخلیقی صلاحیت پیدا کرے۔
|
| 74 |
+
• دلچسپ اور پرکشش ہو۔
|
| 75 |
+
• طلباء کی ذہنی سطح اور جماعت کے مطابق ہو۔
|
| 76 |
+
• ایک سے دو گھنٹوں میں مکمل کیا جا سکے۔
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
# ✅ Get response from Groq API
|
| 80 |
+
response = client.chat.completions.create(
|
| 81 |
+
model=model_name,
|
| 82 |
+
messages=[{"role": "user", "content": prompt}],
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
lesson_text = response.choices[0].message.content.strip()
|
| 86 |
+
|
| 87 |
+
# ✅ Create Word Document
|
| 88 |
+
doc = Document()
|
| 89 |
+
|
| 90 |
+
if language == "English":
|
| 91 |
+
doc.add_heading("Daily Lesson Plan", level=1)
|
| 92 |
+
else:
|
| 93 |
+
p = doc.add_paragraph("یومیہ تدریسی منصوبہ")
|
| 94 |
+
p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
|
| 95 |
+
run = p.runs[0]
|
| 96 |
+
set_urdu_font(run)
|
| 97 |
+
run.bold = True
|
| 98 |
+
|
| 99 |
+
# ✅ Table (Top Info)
|
| 100 |
+
table = doc.add_table(rows=6, cols=2)
|
| 101 |
+
table.autofit = True
|
| 102 |
+
|
| 103 |
+
if language == "English":
|
| 104 |
+
info = [
|
| 105 |
+
("Class:", class_),
|
| 106 |
+
("Subject:", subject),
|
| 107 |
+
("Topic:", topic),
|
| 108 |
+
("Number of Students:", students),
|
| 109 |
+
("Duration:", duration),
|
| 110 |
+
("Teacher:", teacher),
|
| 111 |
+
]
|
| 112 |
+
else:
|
| 113 |
+
info = [
|
| 114 |
+
("جماعت:", class_),
|
| 115 |
+
("مضمون:", subject),
|
| 116 |
+
("عنوان:", topic),
|
| 117 |
+
("طلباء کی تعداد:", students),
|
| 118 |
+
("دورانیہ:", duration),
|
| 119 |
+
("معلم:", teacher),
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
for i, (key, value) in enumerate(info):
|
| 123 |
+
cells = table.rows[i].cells
|
| 124 |
if language == "Urdu":
|
| 125 |
+
cells[0].text = value
|
| 126 |
+
cells[1].text = key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
else:
|
| 128 |
+
cells[0].text = key
|
| 129 |
+
cells[1].text = value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
# ✅ Add Lesson Plan Text
|
| 132 |
+
doc.add_paragraph("")
|
| 133 |
+
if language == "English":
|
| 134 |
+
p = doc.add_paragraph(lesson_text)
|
| 135 |
+
else:
|
| 136 |
+
p = doc.add_paragraph(lesson_text)
|
| 137 |
+
p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
|
| 138 |
+
for run in p.runs:
|
| 139 |
+
set_urdu_font(run)
|
| 140 |
|
| 141 |
+
# ✅ Save File
|
| 142 |
+
file_name = f"Lesson_Plan_{language}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
|
| 143 |
+
doc.save(file_name)
|
| 144 |
|
| 145 |
+
return file_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
|
| 148 |
# ✅ Gradio UI
|
| 149 |
+
interface = gr.Interface(
|
| 150 |
+
fn=generate_lesson_plan,
|
| 151 |
+
inputs=[
|
| 152 |
+
gr.Dropdown(["English", "Urdu"], label="Select Language"),
|
| 153 |
+
gr.Textbox(label="Class / جماعت"),
|
| 154 |
+
gr.Textbox(label="Subject / مضمون"),
|
| 155 |
+
gr.Textbox(label="Topic / عنوان"),
|
| 156 |
+
gr.Textbox(label="Number of Students / طلباء کی تعداد"),
|
| 157 |
+
gr.Textbox(label="Duration / دورانیہ"),
|
| 158 |
+
gr.Textbox(label="Date / تاریخ", value=str(datetime.date.today())),
|
| 159 |
+
gr.Textbox(label="Teacher Name / معلم کا نام"),
|
| 160 |
+
],
|
| 161 |
+
outputs=gr.File(label="Download Lesson Plan"),
|
| 162 |
+
title="AI Lesson Plan Generator",
|
| 163 |
+
description="Generate bilingual (English / Urdu) lesson plans powered by Groq API and open models.",
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|