Spaces:
Sleeping
Sleeping
Commit
·
fd59f22
1
Parent(s):
f5a842e
Added app [Not required, just for reference], llm handler and streamlit_app files, requirements is yet to be added
Browse files- Streamlit_app.py +194 -0
- app.py +30 -0
- openai_llm.py +59 -0
Streamlit_app.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
from openai_llm import LessonPlanGenerator
|
| 4 |
+
|
| 5 |
+
# Page configuration
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="Lesson Plan Generator",
|
| 8 |
+
# page_icon="📚",
|
| 9 |
+
layout="wide"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# Custom CSS with corrected container styling
|
| 13 |
+
st.markdown("""
|
| 14 |
+
<style>
|
| 15 |
+
/* Main container */
|
| 16 |
+
.main-container {
|
| 17 |
+
background-color: white;
|
| 18 |
+
border-radius: 8px;
|
| 19 |
+
padding: 25px;
|
| 20 |
+
margin: 20px 0;
|
| 21 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
/* Section styling */
|
| 25 |
+
.section {
|
| 26 |
+
margin: 15px 0;
|
| 27 |
+
padding: 10px 0;
|
| 28 |
+
border-bottom: 1px solid #eee;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
/* Header styling */
|
| 32 |
+
.section-header {
|
| 33 |
+
color: #1a237e;
|
| 34 |
+
font-size: 18px;
|
| 35 |
+
font-weight: 600;
|
| 36 |
+
margin-bottom: 10px;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/* Content styling */
|
| 40 |
+
.section-content {
|
| 41 |
+
color: #333;
|
| 42 |
+
margin-left: 20px;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
/* List item styling */
|
| 46 |
+
.list-item {
|
| 47 |
+
margin: 5px 0;
|
| 48 |
+
color: #333;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
/* Nested content */
|
| 52 |
+
.nested-content {
|
| 53 |
+
margin-left: 20px;
|
| 54 |
+
padding-left: 10px;
|
| 55 |
+
border-left: 2px solid #e8eaf6;
|
| 56 |
+
}
|
| 57 |
+
</style>
|
| 58 |
+
""", unsafe_allow_html=True)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# Initialize generator
|
| 62 |
+
@st.cache_resource
|
| 63 |
+
def get_generator():
|
| 64 |
+
return LessonPlanGenerator()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
generator = get_generator()
|
| 68 |
+
|
| 69 |
+
# Header
|
| 70 |
+
st.title("WizLab Lesson Plan Generator")
|
| 71 |
+
st.markdown("We help you generate lesson plans tailored to your needs.")
|
| 72 |
+
|
| 73 |
+
# Input section
|
| 74 |
+
st.header("📝 Input Your Requirements")
|
| 75 |
+
|
| 76 |
+
# Create two columns for input fields
|
| 77 |
+
col1, col2 = st.columns(2)
|
| 78 |
+
|
| 79 |
+
with col1:
|
| 80 |
+
topic = st.text_input("Topic", placeholder="e.g., Introduction to Photosynthesis")
|
| 81 |
+
|
| 82 |
+
age_group_options = ["Elementary (6-11)", "Middle School (12-14)",
|
| 83 |
+
"High School (15-18)", "Adult Learners", "Other"]
|
| 84 |
+
age_group_selection = st.selectbox("Target Age Group", age_group_options)
|
| 85 |
+
if age_group_selection == "Other":
|
| 86 |
+
age_group = st.text_input("Specify Age Group")
|
| 87 |
+
else:
|
| 88 |
+
age_group = age_group_selection
|
| 89 |
+
|
| 90 |
+
with col2:
|
| 91 |
+
duration_options = ["30 minutes", "45 minutes", "60 minutes", "90 minutes", "Other"]
|
| 92 |
+
duration_selection = st.selectbox("Lesson Duration", duration_options)
|
| 93 |
+
if duration_selection == "Other":
|
| 94 |
+
duration = st.text_input("Specify Duration")
|
| 95 |
+
else:
|
| 96 |
+
duration = duration_selection
|
| 97 |
+
|
| 98 |
+
proficiency_options = ["Beginner", "Intermediate", "Advanced", "Other"]
|
| 99 |
+
proficiency_selection = st.selectbox("Proficiency Level", proficiency_options)
|
| 100 |
+
if proficiency_selection == "Other":
|
| 101 |
+
proficiency = st.text_input("Specify Proficiency Level")
|
| 102 |
+
else:
|
| 103 |
+
proficiency = proficiency_selection
|
| 104 |
+
|
| 105 |
+
tech_options = ["Interactive Whiteboard", "Computers/Laptops",
|
| 106 |
+
"Mobile Devices", "Internet Access", "None", "Other"]
|
| 107 |
+
tech_selection = st.multiselect("Technology Requirements", tech_options)
|
| 108 |
+
|
| 109 |
+
if "Other" in tech_selection:
|
| 110 |
+
other_tech = st.text_input("Specify Other Technology Requirements")
|
| 111 |
+
tech_usage = [tech for tech in tech_selection if tech != "Other"] + [other_tech]
|
| 112 |
+
else:
|
| 113 |
+
tech_usage = tech_selection
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def format_content(data):
|
| 117 |
+
html_content = '<div class="main-container">'
|
| 118 |
+
|
| 119 |
+
def process_value(value, level=0):
|
| 120 |
+
if isinstance(value, dict):
|
| 121 |
+
return process_dict(value, level)
|
| 122 |
+
elif isinstance(value, list):
|
| 123 |
+
return process_list(value, level)
|
| 124 |
+
else:
|
| 125 |
+
return f'<div class="section-content">{value}</div>'
|
| 126 |
+
|
| 127 |
+
def process_dict(d, level):
|
| 128 |
+
content = ""
|
| 129 |
+
for key, value in d.items():
|
| 130 |
+
formatted_key = key.replace("_", " ").title()
|
| 131 |
+
content += f'<div class="section">'
|
| 132 |
+
content += f'<div class="section-header">{formatted_key}</div>'
|
| 133 |
+
content += process_value(value, level + 1)
|
| 134 |
+
content += '</div>'
|
| 135 |
+
return content
|
| 136 |
+
|
| 137 |
+
def process_list(lst, level):
|
| 138 |
+
content = '<div class="section-content">'
|
| 139 |
+
for item in lst:
|
| 140 |
+
if isinstance(item, dict):
|
| 141 |
+
content += process_dict(item, level + 1)
|
| 142 |
+
else:
|
| 143 |
+
content += f'<div class="list-item">• {item}</div>'
|
| 144 |
+
content += '</div>'
|
| 145 |
+
return content
|
| 146 |
+
|
| 147 |
+
html_content += process_dict(data, 0)
|
| 148 |
+
html_content += '</div>'
|
| 149 |
+
return html_content
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
# Generate button
|
| 153 |
+
if st.button("Generate Lesson Plan", type="primary"):
|
| 154 |
+
if not topic:
|
| 155 |
+
st.error("Please enter a topic for the lesson plan.")
|
| 156 |
+
else:
|
| 157 |
+
with st.spinner("Generating your lesson plan..."):
|
| 158 |
+
detailed_prompt = f"""
|
| 159 |
+
Create a lesson plan for teaching '{topic}' to {age_group} students.
|
| 160 |
+
Duration: {duration}
|
| 161 |
+
Proficiency Level: {proficiency}
|
| 162 |
+
Technology Requirements: {', '.join(tech_usage) if tech_usage else 'None'}
|
| 163 |
+
"""
|
| 164 |
+
|
| 165 |
+
try:
|
| 166 |
+
result = generator.generate_lesson_plan(detailed_prompt)
|
| 167 |
+
st.success("Lesson plan generated successfully!")
|
| 168 |
+
|
| 169 |
+
# Create tabs for different views
|
| 170 |
+
tab1, tab2 = st.tabs(["Formatted View", "Raw JSON"])
|
| 171 |
+
|
| 172 |
+
with tab1:
|
| 173 |
+
if isinstance(result, dict):
|
| 174 |
+
# Display formatted content in white container
|
| 175 |
+
st.markdown(format_content(result), unsafe_allow_html=True)
|
| 176 |
+
else:
|
| 177 |
+
st.error("Invalid response format")
|
| 178 |
+
|
| 179 |
+
with tab2:
|
| 180 |
+
st.json(result)
|
| 181 |
+
|
| 182 |
+
# Download button
|
| 183 |
+
st.download_button(
|
| 184 |
+
label="Download Lesson Plan",
|
| 185 |
+
data=json.dumps(result, indent=2),
|
| 186 |
+
file_name="lesson_plan.json",
|
| 187 |
+
mime="application/json"
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
except Exception as e:
|
| 191 |
+
st.error(f"An error occurred: {str(e)}")
|
| 192 |
+
|
| 193 |
+
# Footer
|
| 194 |
+
st.markdown("---")
|
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from openai_llm import LessonPlanGenerator
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
CORS(app)
|
| 7 |
+
|
| 8 |
+
# Initialize the lesson plan generator
|
| 9 |
+
lesson_generator = LessonPlanGenerator()
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@app.route('/generate-lesson-plan', methods=['POST'])
|
| 13 |
+
def generate_lesson_plan():
|
| 14 |
+
try:
|
| 15 |
+
data = request.get_json()
|
| 16 |
+
|
| 17 |
+
if not data or 'topic' not in data:
|
| 18 |
+
return jsonify({"error": "No topic provided"}), 400
|
| 19 |
+
|
| 20 |
+
topic = data['topic']
|
| 21 |
+
lesson_plan = lesson_generator.generate_lesson_plan(topic)
|
| 22 |
+
|
| 23 |
+
return jsonify(lesson_plan)
|
| 24 |
+
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return jsonify({"error": str(e)}), 500
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
if __name__ == '__main__':
|
| 30 |
+
app.run(debug=True)
|
openai_llm.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
class LessonPlanGenerator:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
# Load environment variables
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Initialize OpenAI client
|
| 12 |
+
self.client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
| 13 |
+
|
| 14 |
+
# System prompt for lesson plan generation
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def generate_lesson_plan(self, topic):
|
| 18 |
+
system_prompt = """
|
| 19 |
+
You are an advanced lesson plan generator. When provided with an input text prompt that requests a lesson plan on a specific topic, along with parameters such as learner age, proficiency level, duration, and tech usage, follow these steps:
|
| 20 |
+
1. Extract Input Parameters:
|
| 21 |
+
• Identify the topic, target learner group (age and proficiency), lesson duration, and any technology requirements.
|
| 22 |
+
2. Determine the Appropriate Framework:
|
| 23 |
+
• For young learners with clear sequential activities, select an activity-based framework such as PPP (Presentation, Practice, Production) or one of the ESA variants (Straight Arrow, Boomerang, or Patchwork).
|
| 24 |
+
• For adult learners or contexts that require diagnostic assessment followed by targeted instruction, consider the TTT (Test, Teach, Test) framework.
|
| 25 |
+
• For high school learners or lessons focused on collaborative tasks and discussion, consider using the TBL (Task-Based Learning) framework.
|
| 26 |
+
• For lessons that require comprehensive, backward-designed planning with a focus on understanding, essential questions, and transfer of learning, use the UbD (Understanding by Design) framework. In this case, structure the lesson plan into three stages: Stage 1 – Desired Results, Stage 2 – Assessment Evidence, and Stage 3 – Learning Plan.
|
| 27 |
+
3. Generate a JSON Structure:
|
| 28 |
+
• Your output should be a well-formed JSON object containing sections for:
|
| 29 |
+
- Objectives and learning outcomes
|
| 30 |
+
- Essential questions (if using UbD)
|
| 31 |
+
- Detailed activities and timings
|
| 32 |
+
- Assessment strategies
|
| 33 |
+
- Materials and resources
|
| 34 |
+
- Any other relevant details based on the chosen framework
|
| 35 |
+
4. Ensure Adaptability:
|
| 36 |
+
While the examples provided (PPP, ESA, TTT, TBL, and UbD) serve as guidance,
|
| 37 |
+
you are not restricted to these frameworks. If another lesson plan structure or innovative approach better aligns with the input parameters,
|
| 38 |
+
feel free to choose and implement that structure.
|
| 39 |
+
5. Ensure that your output is organized
|
| 40 |
+
in a clear JSON format with sections for objectives, assessments, learning activities, and resources.
|
| 41 |
+
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
response = self.client.chat.completions.create(
|
| 45 |
+
model="gpt-4o-mini",
|
| 46 |
+
messages=[
|
| 47 |
+
{"role": "system", "content": system_prompt},
|
| 48 |
+
{"role": "user", "content": f"Create a lesson plan for: {topic}"}
|
| 49 |
+
],
|
| 50 |
+
response_format={"type": "json_object"} # This ensures JSON output
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# If you want to strictly parse the response
|
| 54 |
+
import json
|
| 55 |
+
return json.loads(response.choices[0].message.content, strict=True)
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return {"error": str(e)}
|
| 59 |
+
|