Turbiling commited on
Commit
1f9daab
·
verified ·
1 Parent(s): 210b980

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -10
app.py CHANGED
@@ -3,6 +3,10 @@ import os
3
  from datetime import datetime
4
  from google import genai
5
  from google.genai import types
 
 
 
 
6
 
7
  # ✅ Initialize Gemini client
8
  client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
@@ -51,10 +55,15 @@ Inputs (to be considered while generating the plan):
51
  Generate the final lesson plan in the requested language, following the exact format above.
52
  """
53
 
54
- # ✅ Function to generate SMART Lesson Plan using Gemini 2.5 Flash
 
 
 
 
 
 
55
  def generate_smart_lesson(language, grade, subject, topic, num_students, duration, teacher_name, date):
56
  try:
57
- # Combine user inputs into a structured query
58
  user_input = f"""
59
  Language: {language}
60
  Grade Level: {grade}
@@ -68,7 +77,6 @@ Date: {date}
68
 
69
  full_prompt = BASE_PROMPT + "\n\n" + user_input
70
 
71
- # Generate content using Gemini model
72
  response = client.models.generate_content(
73
  model="gemini-2.5-flash",
74
  contents=full_prompt,
@@ -78,15 +86,44 @@ Date: {date}
78
  )
79
  )
80
 
81
- return response.text.strip()
 
 
82
 
83
  except Exception as e:
84
- return f"❌ Error generating lesson plan: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- # ✅ Gradio UI
87
  with gr.Blocks(title="SMART Lesson Plan Generator (Gemini 2.5 Flash)") as app:
88
  gr.Markdown("## 🧠 SMART LESSON PLAN GENERATOR (Gemini 2.5 Flash)")
89
- gr.Markdown("Generate complete Urdu or English lesson plans aligned with Pakistan’s National Curriculum.")
90
 
91
  with gr.Row():
92
  language = gr.Radio(["English", "Urdu"], label="Select Language", value="English")
@@ -101,14 +138,14 @@ with gr.Blocks(title="SMART Lesson Plan Generator (Gemini 2.5 Flash)") as app:
101
  date = gr.Textbox(label="Date", value=datetime.now().strftime("%Y-%m-%d"))
102
 
103
  generate_btn = gr.Button("🎓 Generate SMART Lesson Plan")
104
- output_box = gr.Textbox(label="Generated Lesson Plan", lines=30)
 
105
 
106
  generate_btn.click(
107
  fn=generate_smart_lesson,
108
  inputs=[language, grade, subject, topic, num_students, duration, teacher_name, date],
109
- outputs=output_box
110
  )
111
 
112
- # ✅ Launch app
113
  if __name__ == "__main__":
114
  app.launch()
 
3
  from datetime import datetime
4
  from google import genai
5
  from google.genai import types
6
+ from docx import Document
7
+ from docx.shared import Pt
8
+ from docx.enum.text import WD_ALIGN_PARAGRAPH
9
+ import re
10
 
11
  # ✅ Initialize Gemini client
12
  client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
 
55
  Generate the final lesson plan in the requested language, following the exact format above.
56
  """
57
 
58
+ # ✅ Function: clean text (remove unwanted symbols)
59
+ def clean_text(text):
60
+ text = re.sub(r"[*_•\-–—]", "", text)
61
+ text = text.replace("❌", "").replace("✅", "").strip()
62
+ return text
63
+
64
+ # ✅ Generate SMART Lesson Plan using Gemini
65
  def generate_smart_lesson(language, grade, subject, topic, num_students, duration, teacher_name, date):
66
  try:
 
67
  user_input = f"""
68
  Language: {language}
69
  Grade Level: {grade}
 
77
 
78
  full_prompt = BASE_PROMPT + "\n\n" + user_input
79
 
 
80
  response = client.models.generate_content(
81
  model="gemini-2.5-flash",
82
  contents=full_prompt,
 
86
  )
87
  )
88
 
89
+ lesson_text = clean_text(response.text.strip())
90
+ word_file = create_word_doc(language, lesson_text, topic)
91
+ return lesson_text, word_file
92
 
93
  except Exception as e:
94
+ return f"❌ Error generating lesson plan: {e}", None
95
+
96
+
97
+ # ✅ Create Word / Markdown file
98
+ def create_word_doc(language, lesson_text, topic):
99
+ file_path = f"SMART_Lesson_{topic.replace(' ', '_')}.docx" if language == "English" else f"SMART_Lesson_{topic.replace(' ', '_')}.md"
100
+
101
+ # For English version: Create Word document
102
+ if language == "English":
103
+ doc = Document()
104
+ style = doc.styles['Normal']
105
+ style.font.name = 'Times New Roman'
106
+ style.font.size = Pt(12)
107
+
108
+ for line in lesson_text.split("\n"):
109
+ p = doc.add_paragraph(line.strip())
110
+ p.alignment = WD_ALIGN_PARAGRAPH.LEFT
111
+
112
+ doc.save(file_path)
113
+ return file_path
114
+
115
+ # For Urdu version: Markdown file (Right-to-Left)
116
+ else:
117
+ urdu_text = f"<div dir='rtl' style='font-family:Jameel Noori Nastaleeq; text-align:right;'>\n{lesson_text}\n</div>"
118
+ with open(file_path, "w", encoding="utf-8") as f:
119
+ f.write(urdu_text)
120
+ return file_path
121
+
122
 
123
+ # ✅ Gradio Interface
124
  with gr.Blocks(title="SMART Lesson Plan Generator (Gemini 2.5 Flash)") as app:
125
  gr.Markdown("## 🧠 SMART LESSON PLAN GENERATOR (Gemini 2.5 Flash)")
126
+ gr.Markdown("Generate complete Urdu or English lesson plans aligned with Pakistan’s National Curriculum. Download them as Word or Markdown files.")
127
 
128
  with gr.Row():
129
  language = gr.Radio(["English", "Urdu"], label="Select Language", value="English")
 
138
  date = gr.Textbox(label="Date", value=datetime.now().strftime("%Y-%m-%d"))
139
 
140
  generate_btn = gr.Button("🎓 Generate SMART Lesson Plan")
141
+ output_text = gr.Textbox(label="Generated Lesson Plan", lines=25)
142
+ download_file = gr.File(label="Download Lesson Plan")
143
 
144
  generate_btn.click(
145
  fn=generate_smart_lesson,
146
  inputs=[language, grade, subject, topic, num_students, duration, teacher_name, date],
147
+ outputs=[output_text, download_file]
148
  )
149
 
 
150
  if __name__ == "__main__":
151
  app.launch()