Turbiling commited on
Commit
c042089
·
verified ·
1 Parent(s): 23f6294

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -28
app.py CHANGED
@@ -1,10 +1,10 @@
1
  # app.py
2
- # app.py
3
  import os
4
  import gradio as gr
5
  from docx import Document
6
  from docx.oxml.ns import qn
7
  from docx.oxml import OxmlElement
 
8
  import datetime
9
  from groq import Groq
10
 
@@ -15,6 +15,20 @@ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
15
  client = Groq(api_key=GROQ_API_KEY)
16
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # ✅ Text Generation Function
19
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
20
  try:
@@ -49,7 +63,7 @@ Include only these sections:
49
  6. سبق کا خلاصہ (Summary)
50
  """
51
 
52
- # ✅ Generate text using selected model
53
  response = client.chat.completions.create(
54
  model=model_name,
55
  messages=[{"role": "user", "content": prompt}],
@@ -58,42 +72,59 @@ Include only these sections:
58
  top_p=1,
59
  stream=False
60
  )
61
-
62
  lesson_text = response.choices[0].message.content.strip()
63
 
64
- # ✅ Create the Word document
65
  doc = Document()
66
  doc.add_heading("BOPPPS Lesson Plan", 0)
67
 
 
68
  table = doc.add_table(rows=0, cols=2)
69
- data = {
70
- "Class": class_,
71
- "Subject": subject,
72
- "Topic": topic,
73
- "No. of Students": students,
74
- "Duration": duration,
75
- "Date": date,
76
- "Teacher Name": teacher
77
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  for key, value in data.items():
79
  row_cells = table.add_row().cells
80
  row_cells[0].text = key
81
  row_cells[1].text = str(value)
82
 
 
 
 
 
 
 
83
  doc.add_paragraph("\n")
84
 
 
85
  paragraph = doc.add_paragraph(lesson_text)
86
 
87
- # Urdu formatting (right-aligned)
88
  if language == "Urdu":
89
- paragraph.alignment = 2
90
- rtl = OxmlElement('w:rtl')
91
- rtl.set(qn('w:val'), '1')
92
- paragraph._element.insert(0, rtl)
93
 
94
  doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
95
 
96
- # Save file
97
  filename = f"lesson_plan_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
98
  filepath = f"/tmp/{filename}"
99
  doc.save(filepath)
@@ -117,18 +148,18 @@ with gr.Blocks() as demo:
117
  with gr.Group():
118
  language = gr.Radio(["English", "Urdu"], label="Select Language", value="English")
119
  with gr.Row():
120
- class_ = gr.Textbox(label="Class", placeholder="e.g., 9th / نہم")
121
- subject = gr.Textbox(label="Subject", placeholder="e.g., Physics / فزکس")
122
  with gr.Row():
123
- topic = gr.Textbox(label="Topic", placeholder="e.g., Newton’s First Law / حرکیات کا پہلا قانون")
124
- students = gr.Textbox(label="Number of Students", placeholder="e.g., 30")
125
  with gr.Row():
126
- duration = gr.Textbox(label="Duration (minutes)", placeholder="e.g., 40")
127
- date = gr.Textbox(label="Date", placeholder="e.g., 2025-10-07")
128
- teacher = gr.Textbox(label="Teacher Name", placeholder="e.g., Najaf Ali Sharqi")
129
 
130
- generate_button = gr.Button("Generate Lesson Plan")
131
- output_file = gr.File(label="📄 Download Word File")
132
 
133
  generate_button.click(
134
  fn=generate_lesson_plan,
 
1
  # app.py
 
2
  import os
3
  import gradio as gr
4
  from docx import Document
5
  from docx.oxml.ns import qn
6
  from docx.oxml import OxmlElement
7
+ from docx.shared import Pt
8
  import datetime
9
  from groq import Groq
10
 
 
15
  client = Groq(api_key=GROQ_API_KEY)
16
 
17
 
18
+ # ✅ Function to apply Urdu font and RTL formatting
19
+ def apply_urdu_format(paragraph):
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.rPr
24
+ r.rFonts.set(qn("w:eastAsia"), "Jameel Noori Nastaleeq")
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
  # ✅ Text Generation Function
33
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
34
  try:
 
63
  6. سبق کا خلاصہ (Summary)
64
  """
65
 
66
+ # ✅ Generate text
67
  response = client.chat.completions.create(
68
  model=model_name,
69
  messages=[{"role": "user", "content": prompt}],
 
72
  top_p=1,
73
  stream=False
74
  )
 
75
  lesson_text = response.choices[0].message.content.strip()
76
 
77
+ # ✅ Create Word document
78
  doc = Document()
79
  doc.add_heading("BOPPPS Lesson Plan", 0)
80
 
81
+ # Create info table
82
  table = doc.add_table(rows=0, cols=2)
83
+
84
+ # Urdu field labels if language == Urdu
85
+ if language == "Urdu":
86
+ data = {
87
+ "کلاس": class_,
88
+ "مضمون": subject,
89
+ "عنوان": topic,
90
+ "طلباء کی تعداد": students,
91
+ "دورانیہ (منٹوں میں)": duration,
92
+ "تاریخ": date,
93
+ "استاد کا نام": teacher
94
+ }
95
+ else:
96
+ data = {
97
+ "Class": class_,
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
  filename = f"lesson_plan_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
129
  filepath = f"/tmp/{filename}"
130
  doc.save(filepath)
 
148
  with gr.Group():
149
  language = gr.Radio(["English", "Urdu"], label="Select Language", value="English")
150
  with gr.Row():
151
+ class_ = gr.Textbox(label="Class / کلاس", placeholder="e.g., 9th / نہم")
152
+ subject = gr.Textbox(label="Subject / مضمون", placeholder="e.g., Physics / فزکس")
153
  with gr.Row():
154
+ topic = gr.Textbox(label="Topic / عنوان", placeholder="e.g., Newton’s First Law / حرکیات کا پہلا قانون")
155
+ students = gr.Textbox(label="Number of Students / طلباء کی تعداد", placeholder="e.g., 30")
156
  with gr.Row():
157
+ duration = gr.Textbox(label="Duration (minutes) / دورانیہ", placeholder="e.g., 40")
158
+ date = gr.Textbox(label="Date / تاریخ", placeholder="e.g., 2025-10-07")
159
+ teacher = gr.Textbox(label="Teacher Name / استاد کا نام", placeholder="e.g., Najaf Ali Sharqi / نجف علی شرقی")
160
 
161
+ generate_button = gr.Button("Generate Lesson Plan / سبق منصوبہ تیار کریں")
162
+ output_file = gr.File(label="📄 Download Word File / فائل ڈاؤن لوڈ کریں")
163
 
164
  generate_button.click(
165
  fn=generate_lesson_plan,