Turbiling commited on
Commit
970f969
·
verified ·
1 Parent(s): deff2da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -14
app.py CHANGED
@@ -2,13 +2,15 @@ import os
2
  import gradio as gr
3
  import datetime
4
  from groq import Groq
 
 
5
 
6
  # ✅ Initialize Groq API
7
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
8
  client = Groq(api_key=GROQ_API_KEY)
9
 
10
  # ✅ Function to generate lesson plan
11
- def generate_lesson_plan(language, grade, subject, topic, students, duration, date, teacher):
12
  if language == "Urdu":
13
  model = "openai/gpt-oss-120b"
14
  prompt = f"""
@@ -41,7 +43,7 @@ def generate_lesson_plan(language, grade, subject, topic, students, duration, da
41
  else:
42
  model = "llama-3.1-8b-instant"
43
  prompt = f"""
44
- You are a professional teacher. Based on the following details, generate a complete **Lesson Plan in English** using the **BOPPPS Model**.
45
 
46
  Class: {grade}
47
  Subject: {subject}
@@ -61,6 +63,7 @@ Lesson Plan Structure:
61
  7. Home Assignment aligned with SLOs (reasonable length, logical order, creative, interesting, suitable for students’ level, and completable in 1–2 hours)
62
  """
63
 
 
64
  completion = client.chat.completions.create(
65
  model=model,
66
  messages=[
@@ -70,12 +73,35 @@ Lesson Plan Structure:
70
  )
71
 
72
  response = completion.choices[0].message.content
73
-
74
- # Add footer
75
  footer = "\n\n---\n**Developed by Najaf Ali Sharqi**"
76
- return response + footer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- # ✅ Gradio Interface
 
 
79
  grades = [f"Grade {i}" for i in range(1, 17)]
80
  subjects = [
81
  "Urdu", "English", "Arabic", "Science", "Mathematics",
@@ -83,33 +109,36 @@ subjects = [
83
  "Civics", "Islamiat"
84
  ]
85
 
86
- with gr.Blocks(title="Lesson Plan Generator") as app:
87
- gr.Markdown("## 📘 AI Lesson Plan Generator (English / Urdu)\nDeveloped by **Najaf Ali Sharqi**")
 
88
 
89
  with gr.Row():
90
  language = gr.Dropdown(["English", "Urdu"], label="Language / زبان", value="English")
91
  grade = gr.Dropdown(grades, label="Class / جماعت")
92
  subject = gr.Dropdown(subjects, label="Subject / مضمون")
 
93
 
94
  with gr.Row():
95
  topic = gr.Textbox(label="Topic / عنوان", placeholder="Enter topic")
96
  students = gr.Slider(1, 200, step=1, label="No. of Students / طلبہ کی تعداد")
97
  duration = gr.Slider(30, 60, step=5, label="Time Duration (minutes) / دورانیہ (منٹ)")
98
 
99
- # ✅ Replaced gr.Calendar() with gr.Textbox (date input)
100
  with gr.Row():
101
  date = gr.Textbox(label="Date / تاریخ", placeholder="YYYY-MM-DD", value=str(datetime.date.today()))
102
  teacher = gr.Textbox(label="Teacher's Name / استاد کا نام")
103
 
104
- generate_btn = gr.Button("Generate Lesson Plan / سبق کا منصوبہ تیار کریں")
105
 
106
- output = gr.Markdown(label="Generated Lesson Plan")
 
107
 
108
  generate_btn.click(
109
  fn=generate_lesson_plan,
110
- inputs=[language, grade, subject, topic, students, duration, date, teacher],
111
- outputs=output,
112
  )
113
 
114
- # ✅ Launch
115
  app.launch()
 
 
2
  import gradio as gr
3
  import datetime
4
  from groq import Groq
5
+ from docx import Document
6
+ import tempfile
7
 
8
  # ✅ Initialize Groq API
9
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
10
  client = Groq(api_key=GROQ_API_KEY)
11
 
12
  # ✅ Function to generate lesson plan
13
+ def generate_lesson_plan(language, grade, subject, topic, students, duration, date, teacher, theme):
14
  if language == "Urdu":
15
  model = "openai/gpt-oss-120b"
16
  prompt = f"""
 
43
  else:
44
  model = "llama-3.1-8b-instant"
45
  prompt = f"""
46
+ You are a professional teacher. Based on the following details, generate a complete Lesson Plan in English using the BOPPPS Model.
47
 
48
  Class: {grade}
49
  Subject: {subject}
 
63
  7. Home Assignment aligned with SLOs (reasonable length, logical order, creative, interesting, suitable for students’ level, and completable in 1–2 hours)
64
  """
65
 
66
+ # ✅ Generate via Groq
67
  completion = client.chat.completions.create(
68
  model=model,
69
  messages=[
 
73
  )
74
 
75
  response = completion.choices[0].message.content
 
 
76
  footer = "\n\n---\n**Developed by Najaf Ali Sharqi**"
77
+ final_text = response + footer
78
+
79
+ # ✅ Save as Markdown for Urdu / DOCX for English
80
+ if language == "Urdu":
81
+ file_ext = ".md"
82
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=file_ext)
83
+ with open(temp_file.name, "w", encoding="utf-8") as f:
84
+ f.write(final_text)
85
+ else:
86
+ file_ext = ".docx"
87
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=file_ext)
88
+ doc = Document()
89
+ doc.add_heading("Lesson Plan", level=1)
90
+ doc.add_paragraph(final_text)
91
+ doc.add_paragraph("\nDeveloped by Najaf Ali Sharqi")
92
+ doc.save(temp_file.name)
93
+
94
+ # ✅ Adjust background color theme
95
+ if theme == "Dark":
96
+ theme_css = "background-color:#111;color:#fff;padding:10px;border-radius:10px;"
97
+ else:
98
+ theme_css = "background-color:#fdfdfd;color:#000;padding:10px;border-radius:10px;"
99
+
100
+ display_html = f"<div style='{theme_css}'>{final_text.replace('\n', '<br>')}</div>"
101
 
102
+ return display_html, temp_file.name
103
+
104
+ # ✅ Dropdown options
105
  grades = [f"Grade {i}" for i in range(1, 17)]
106
  subjects = [
107
  "Urdu", "English", "Arabic", "Science", "Mathematics",
 
109
  "Civics", "Islamiat"
110
  ]
111
 
112
+ # ✅ Build the Gradio app
113
+ with gr.Blocks(title="AI Lesson Plan Generator") as app:
114
+ gr.Markdown("## 🧠 **AI Lesson Plan Generator (English / Urdu)**\nDeveloped by **Najaf Ali Sharqi**")
115
 
116
  with gr.Row():
117
  language = gr.Dropdown(["English", "Urdu"], label="Language / زبان", value="English")
118
  grade = gr.Dropdown(grades, label="Class / جماعت")
119
  subject = gr.Dropdown(subjects, label="Subject / مضمون")
120
+ theme = gr.Radio(["Light", "Dark"], label="Screen Theme / سکرین موڈ", value="Light")
121
 
122
  with gr.Row():
123
  topic = gr.Textbox(label="Topic / عنوان", placeholder="Enter topic")
124
  students = gr.Slider(1, 200, step=1, label="No. of Students / طلبہ کی تعداد")
125
  duration = gr.Slider(30, 60, step=5, label="Time Duration (minutes) / دورانیہ (منٹ)")
126
 
 
127
  with gr.Row():
128
  date = gr.Textbox(label="Date / تاریخ", placeholder="YYYY-MM-DD", value=str(datetime.date.today()))
129
  teacher = gr.Textbox(label="Teacher's Name / استاد کا نام")
130
 
131
+ generate_btn = gr.Button("🚀 Generate Lesson Plan / سبق کا منصوبہ تیار کریں")
132
 
133
+ output_display = gr.HTML(label="Generated Lesson Plan")
134
+ file_output = gr.File(label="Download Lesson Plan File")
135
 
136
  generate_btn.click(
137
  fn=generate_lesson_plan,
138
+ inputs=[language, grade, subject, topic, students, duration, date, teacher, theme],
139
+ outputs=[output_display, file_output],
140
  )
141
 
142
+ # ✅ Launch the app
143
  app.launch()
144
+