Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,93 @@
|
|
| 1 |
import os
|
| 2 |
-
import requests
|
| 3 |
import gradio as gr
|
| 4 |
from docx import Document
|
| 5 |
-
from docx.shared import Pt
|
| 6 |
import datetime
|
|
|
|
| 7 |
|
| 8 |
-
# Groq API
|
| 9 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY"
|
| 10 |
-
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
prompt = f"""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
- Class: {class_level}
|
| 18 |
-
- Subject: {subject}
|
| 19 |
-
- Topic: {topic}
|
| 20 |
-
- Number of students: {students}
|
| 21 |
-
- Duration: {duration} minutes
|
| 22 |
-
- Date: {date}
|
| 23 |
-
- Teacher Name: {teacher}
|
| 24 |
-
|
| 25 |
-
Include the following in proper sequence:
|
| 26 |
-
1. **Bridge-In**: Introduction to grab students' attention related to the topic.
|
| 27 |
-
2. **Objectives**: Three specific learning objectives.
|
| 28 |
-
3. **Pre-Assessment**: Two questions to assess prior knowledge related to the topic.
|
| 29 |
-
4. **Participatory Learning**: Detailed instruction for interactive/participatory learning.
|
| 30 |
-
5. **Post-Assessment**: Questions or activities to evaluate understanding.
|
| 31 |
-
6. **Summary**: Wrap-up by the teacher, summarizing key points of the lesson.
|
| 32 |
-
|
| 33 |
-
Output must be in proper English and suitable for inclusion in a Word document.
|
| 34 |
-
"""
|
| 35 |
|
| 36 |
-
|
| 37 |
-
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 38 |
-
"Content-Type": "application/json"
|
| 39 |
-
}
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
try:
|
| 48 |
-
response =
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Create Word document
|
| 54 |
doc = Document()
|
| 55 |
-
doc.add_heading("Lesson Plan
|
| 56 |
-
|
| 57 |
-
#
|
| 58 |
-
table = doc.add_table(rows=
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
run.font.size = Pt(11)
|
| 79 |
-
|
| 80 |
-
doc.add_paragraph("\n") # spacing
|
| 81 |
-
doc.add_paragraph(lesson_plan)
|
| 82 |
-
|
| 83 |
-
filename = f"LessonPlan_{topic.replace(' ', '_')}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
|
| 84 |
-
filepath = os.path.join("/tmp", filename)
|
| 85 |
doc.save(filepath)
|
| 86 |
-
|
| 87 |
return filepath
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
return f"API request failed: {str(e)}"
|
| 91 |
-
except Exception as ex:
|
| 92 |
-
return f"Error generating lesson plan: {str(ex)}"
|
| 93 |
|
| 94 |
# Gradio UI
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
-
gr.Markdown("## 📘 BOPPPS Lesson Plan Generator
|
| 97 |
|
| 98 |
with gr.Row():
|
| 99 |
-
|
| 100 |
-
subject = gr.Textbox(label="Subject
|
| 101 |
-
topic = gr.Textbox(label="Topic
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
| 105 |
teacher = gr.Textbox(label="Teacher Name")
|
| 106 |
|
| 107 |
-
|
| 108 |
-
output_file = gr.File(label="
|
| 109 |
|
| 110 |
-
|
| 111 |
fn=generate_lesson_plan,
|
| 112 |
-
inputs=[
|
| 113 |
outputs=output_file
|
| 114 |
)
|
| 115 |
|
| 116 |
demo.launch()
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from docx import Document
|
|
|
|
| 4 |
import datetime
|
| 5 |
+
from groq import Groq
|
| 6 |
|
| 7 |
+
# Load Groq API key
|
| 8 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Set this in Hugging Face secrets
|
|
|
|
| 9 |
|
| 10 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
+
|
| 12 |
+
def generate_lesson_plan(class_, subject, topic, students, duration, date, teacher):
|
| 13 |
+
# Construct the prompt — instruct model NOT to repeat inputs
|
| 14 |
prompt = f"""
|
| 15 |
+
You are an expert lesson plan developer.
|
| 16 |
+
|
| 17 |
+
Write ONLY the **lesson body** using the BOPPPS model for the topic: "{topic}".
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
Do NOT restate these inputs (class, subject, topic, date, teacher name, etc.) — only write the lesson content.
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
The plan must be in English and include the following:
|
| 22 |
+
|
| 23 |
+
1. **Bridge-In**: A short paragraph that introduces the topic engagingly.
|
| 24 |
+
2. **Learning Objectives**: List 3 clear objectives.
|
| 25 |
+
3. **Pre-Assessment Questions**: Write 2 questions to assess prior knowledge.
|
| 26 |
+
4. **Participatory Learning**: Describe an engaging activity.
|
| 27 |
+
5. **Post-Assessment**: 2–3 questions to evaluate student understanding.
|
| 28 |
+
6. **Summary**: A concluding paragraph by the teacher.
|
| 29 |
+
"""
|
| 30 |
|
| 31 |
try:
|
| 32 |
+
response = client.chat.completions.create(
|
| 33 |
+
model="llama3-8b-8192",
|
| 34 |
+
messages=[{"role": "user", "content": prompt}],
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
)
|
| 37 |
+
lesson_text = response.choices[0].message.content
|
| 38 |
|
|
|
|
| 39 |
doc = Document()
|
| 40 |
+
doc.add_heading(f"BOPPPS Lesson Plan - {topic}", 0)
|
| 41 |
+
|
| 42 |
+
# Table for metadata (not repeated in content)
|
| 43 |
+
table = doc.add_table(rows=0, cols=2)
|
| 44 |
+
fields = {
|
| 45 |
+
"Class": class_,
|
| 46 |
+
"Subject": subject,
|
| 47 |
+
"Topic": topic,
|
| 48 |
+
"No. of Students": students,
|
| 49 |
+
"Duration": duration,
|
| 50 |
+
"Date": date,
|
| 51 |
+
"Teacher Name": teacher
|
| 52 |
+
}
|
| 53 |
+
for key, value in fields.items():
|
| 54 |
+
row_cells = table.add_row().cells
|
| 55 |
+
row_cells[0].text = key
|
| 56 |
+
row_cells[1].text = value
|
| 57 |
+
|
| 58 |
+
doc.add_paragraph("\n") # space after table
|
| 59 |
+
doc.add_paragraph(lesson_text)
|
| 60 |
+
|
| 61 |
+
filename = f"lesson_plan_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.docx"
|
| 62 |
+
filepath = f"/tmp/{filename}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
doc.save(filepath)
|
|
|
|
| 64 |
return filepath
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return f"Error: {e}"
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Gradio UI
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("## 📘 BOPPPS Lesson Plan Generator (English Only)")
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
+
class_ = gr.Textbox(label="Class")
|
| 74 |
+
subject = gr.Textbox(label="Subject")
|
| 75 |
+
topic = gr.Textbox(label="Topic")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
students = gr.Textbox(label="Number of Students")
|
| 79 |
+
duration = gr.Textbox(label="Duration (in minutes)")
|
| 80 |
+
date = gr.Textbox(label="Date")
|
| 81 |
teacher = gr.Textbox(label="Teacher Name")
|
| 82 |
|
| 83 |
+
generate_btn = gr.Button("Generate Lesson Plan")
|
| 84 |
+
output_file = gr.File(label="Download Lesson Plan")
|
| 85 |
|
| 86 |
+
generate_btn.click(
|
| 87 |
fn=generate_lesson_plan,
|
| 88 |
+
inputs=[class_, subject, topic, students, duration, date, teacher],
|
| 89 |
outputs=output_file
|
| 90 |
)
|
| 91 |
|
| 92 |
demo.launch()
|
| 93 |
+
|