Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,306 +1,306 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import json
|
| 3 |
-
|
| 4 |
-
from openai_llm import LessonPlanGenerator
|
| 5 |
-
|
| 6 |
-
# Page configuration
|
| 7 |
-
st.set_page_config(
|
| 8 |
-
page_title="WizLab Lesson Plan Generator",
|
| 9 |
-
page_icon="📚",
|
| 10 |
-
layout="wide"
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
# Custom CSS with improved styling
|
| 14 |
-
st.markdown("""
|
| 15 |
-
<style>
|
| 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 {
|
| 25 |
-
margin: 15px 0;
|
| 26 |
-
padding: 10px 0;
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
.section-level-0 {
|
| 30 |
-
border-bottom: 2px solid #e8eaf6;
|
| 31 |
-
margin-bottom: 20px;
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
.section-level-1 {
|
| 35 |
-
margin-left: 20px;
|
| 36 |
-
border-left: 2px solid #e8eaf6;
|
| 37 |
-
padding-left: 15px;
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
.section-level-2 {
|
| 41 |
-
margin-left: 15px;
|
| 42 |
-
padding-left: 10px;
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
.section-header-main {
|
| 46 |
-
color: #1a237e;
|
| 47 |
-
font-size: 20px;
|
| 48 |
-
font-weight: 600;
|
| 49 |
-
margin-bottom: 15px;
|
| 50 |
-
padding-bottom: 5px;
|
| 51 |
-
border-bottom: 1px solid #e8eaf6;
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
.section-header-sub {
|
| 55 |
-
color: #283593;
|
| 56 |
-
font-size: 16px;
|
| 57 |
-
font-weight: 500;
|
| 58 |
-
margin: 10px 0;
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
.section-content {
|
| 62 |
-
color: #333;
|
| 63 |
-
margin: 10px 0 10px 20px;
|
| 64 |
-
line-height: 1.5;
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
.list-item {
|
| 68 |
-
margin: 8px 0;
|
| 69 |
-
color: #333;
|
| 70 |
-
padding-left: 20px;
|
| 71 |
-
position: relative;
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
.list-item:before {
|
| 75 |
-
content: "•";
|
| 76 |
-
position: absolute;
|
| 77 |
-
left: 0;
|
| 78 |
-
color: #3949ab;
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
.nested-content {
|
| 82 |
-
margin-left: 20px;
|
| 83 |
-
padding-left: 15px;
|
| 84 |
-
border-left: 2px solid #e8eaf6;
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
.timing-label {
|
| 88 |
-
color: #1565c0;
|
| 89 |
-
font-weight: 500;
|
| 90 |
-
font-style: italic;
|
| 91 |
-
}
|
| 92 |
-
</style>
|
| 93 |
-
""", unsafe_allow_html=True)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# Initialize generator
|
| 97 |
-
#@st.cache_resource
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
generator = LessonPlanGenerator()
|
| 103 |
-
|
| 104 |
-
# Header
|
| 105 |
-
st.title("WizLab Lesson Plan Generator")
|
| 106 |
-
st.markdown("We help you generate lesson plans tailored to your needs.")
|
| 107 |
-
|
| 108 |
-
# Input section
|
| 109 |
-
st.header("Input Your Requirements")
|
| 110 |
-
|
| 111 |
-
# Create two columns for input fields
|
| 112 |
-
col1, col2 = st.columns(2)
|
| 113 |
-
|
| 114 |
-
with col1:
|
| 115 |
-
topic = st.text_input("Topic", placeholder="e.g., Lesson Plan to teach Spanish numbers as third language to English Speaking students ")
|
| 116 |
-
|
| 117 |
-
age_group_options = ["Elementary (6-11)", "Middle School (12-14)",
|
| 118 |
-
"High School (15-18)", "Adult Learners", "Other"]
|
| 119 |
-
age_group_selection = st.selectbox("Target Age Group", age_group_options)
|
| 120 |
-
if age_group_selection == "Other":
|
| 121 |
-
age_group = st.text_input("Specify Age Group")
|
| 122 |
-
else:
|
| 123 |
-
age_group = age_group_selection
|
| 124 |
-
|
| 125 |
-
language_options = [
|
| 126 |
-
"English", "日本語 (Japanese)", "中文 (Mandarin Chinese)",
|
| 127 |
-
"Español (Spanish)", "हिंदी (Hindi)", "한국어 (Korean)",
|
| 128 |
-
"Deutsch (German)", "Français (French)", "Português (Portuguese)",
|
| 129 |
-
"العربية (Arabic)", "Русский (Russian)", "Other"
|
| 130 |
-
]
|
| 131 |
-
|
| 132 |
-
language_selection = st.selectbox("Select Output Language", language_options)
|
| 133 |
-
LANGUAGE_MAPPING = {
|
| 134 |
-
"日本語 (Japanese)": "Japanese",
|
| 135 |
-
"中文 (Mandarin Chinese)": "Mandarin Chinese",
|
| 136 |
-
"Español (Spanish)": "Spanish",
|
| 137 |
-
"हिंदी (Hindi)": "Hindi",
|
| 138 |
-
"한국어 (Korean)": "Korean",
|
| 139 |
-
"Deutsch (German)": "German",
|
| 140 |
-
"Français (French)": "French",
|
| 141 |
-
"Português (Portuguese)": "Portuguese",
|
| 142 |
-
"العربية (Arabic)": "Arabic",
|
| 143 |
-
"Русский (Russian)": "Russian"
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
# Then in your language selection code:
|
| 147 |
-
if language_selection == "Other":
|
| 148 |
-
language = st.text_input("Specify Language")
|
| 149 |
-
else:
|
| 150 |
-
# Use mapping if available, otherwise use the base language name
|
| 151 |
-
language = LANGUAGE_MAPPING.get(language_selection, language_selection)
|
| 152 |
-
|
| 153 |
-
#if language_selection == "Other":
|
| 154 |
-
# language = st.text_input("Specify Language")
|
| 155 |
-
# else:
|
| 156 |
-
# Extract the base language name without the native script
|
| 157 |
-
# language = language_selection.split(" (")[0] if " (" in language_selection else language_selection
|
| 158 |
-
|
| 159 |
-
with col2:
|
| 160 |
-
#duration_options = ["30 minutes", "45 minutes", "60 minutes", "90 minutes", "Other"]
|
| 161 |
-
#duration_selection = st.selectbox("Lesson Duration", duration_options)
|
| 162 |
-
#if duration_selection == "Other":
|
| 163 |
-
# duration = st.text_input("Specify Duration")
|
| 164 |
-
#else:
|
| 165 |
-
# duration = duration_selection
|
| 166 |
-
duration = st.text_input("Duration [in minutes]", placeholder = "e.g., 25 minutes")
|
| 167 |
-
proficiency_options = ["Beginner [A1]", "Elementary [A2]", "Intermediate[B1, B2]", "Advanced[C1]", "Proficient [C2]", "Other"]
|
| 168 |
-
proficiency_selection = st.selectbox("Proficiency Level [CEFR]", proficiency_options)
|
| 169 |
-
if proficiency_selection == "Other":
|
| 170 |
-
proficiency = st.text_input("Specify Proficiency Level")
|
| 171 |
-
else:
|
| 172 |
-
proficiency = proficiency_selection
|
| 173 |
-
|
| 174 |
-
tech_options = ["Interactive Whiteboard", "Computers/Laptops",
|
| 175 |
-
"Mobile Devices", "Internet Access", "None", "Other"]
|
| 176 |
-
tech_selection = st.multiselect("Accessible Tech Resources", tech_options)
|
| 177 |
-
|
| 178 |
-
if "Other" in tech_selection:
|
| 179 |
-
other_tech = st.text_input("Specify Other Technology Requirements")
|
| 180 |
-
tech_usage = [tech for tech in tech_selection if tech != "Other"] + [other_tech]
|
| 181 |
-
else:
|
| 182 |
-
tech_usage = tech_selection
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
def format_content(data):
|
| 186 |
-
html_content = '<div class="main-container">'
|
| 187 |
-
|
| 188 |
-
def format_key(key):
|
| 189 |
-
"""Format key string to be more readable"""
|
| 190 |
-
# Handle camelCase
|
| 191 |
-
key = ''.join(' ' + char if char.isupper() else char for char in key).strip()
|
| 192 |
-
# Replace underscores and normalize spaces
|
| 193 |
-
key = key.replace('_', ' ').title()
|
| 194 |
-
return key
|
| 195 |
-
|
| 196 |
-
def process_value(value, level=0):
|
| 197 |
-
if isinstance(value, dict):
|
| 198 |
-
return process_dict(value, level)
|
| 199 |
-
elif isinstance(value, list):
|
| 200 |
-
return process_list(value, level)
|
| 201 |
-
else:
|
| 202 |
-
return f'<div class="section-content">{value}</div>'
|
| 203 |
-
|
| 204 |
-
def process_dict(d, level):
|
| 205 |
-
content = ""
|
| 206 |
-
for key, value in d.items():
|
| 207 |
-
formatted_key = format_key(key)
|
| 208 |
-
|
| 209 |
-
section_class = "section-level-" + str(level)
|
| 210 |
-
content += f'<div class="section {section_class}">'
|
| 211 |
-
|
| 212 |
-
if level == 0:
|
| 213 |
-
content += f'<div class="section-header-main">{formatted_key}</div>'
|
| 214 |
-
else:
|
| 215 |
-
content += f'<div class="section-header-sub">{formatted_key}</div>'
|
| 216 |
-
|
| 217 |
-
if isinstance(value, dict) and any(isinstance(v, (dict, list)) for v in value.values()):
|
| 218 |
-
content += process_value(value, level + 1)
|
| 219 |
-
elif isinstance(value, list) and any(isinstance(item, dict) for item in value):
|
| 220 |
-
content += process_value(value, level + 1)
|
| 221 |
-
else:
|
| 222 |
-
content += process_value(value, level + 1)
|
| 223 |
-
|
| 224 |
-
content += '</div>'
|
| 225 |
-
return content
|
| 226 |
-
|
| 227 |
-
def process_list(lst, level):
|
| 228 |
-
content = '<div class="section-content">'
|
| 229 |
-
for item in lst:
|
| 230 |
-
if isinstance(item, dict):
|
| 231 |
-
content += process_dict(item, level + 1)
|
| 232 |
-
else:
|
| 233 |
-
content += f'<div class="list-item">{item}</div>'
|
| 234 |
-
content += '</div>'
|
| 235 |
-
return content
|
| 236 |
-
|
| 237 |
-
html_content += process_dict(data, 0)
|
| 238 |
-
html_content += '</div>'
|
| 239 |
-
return html_content
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
# Generate button
|
| 243 |
-
# Generate button
|
| 244 |
-
if st.button("Generate Lesson Plan", type="primary"):
|
| 245 |
-
if not topic:
|
| 246 |
-
st.error("Please enter a topic for the lesson plan.")
|
| 247 |
-
else:
|
| 248 |
-
with st.spinner("Generating your lesson plan..."):
|
| 249 |
-
# Set default values if not provided
|
| 250 |
-
if not duration:
|
| 251 |
-
duration = "30 minutes"
|
| 252 |
-
if not proficiency:
|
| 253 |
-
proficiency = "Intermediate [B1, B2]"
|
| 254 |
-
if not age_group:
|
| 255 |
-
age_group = "Elementary (6-11)"
|
| 256 |
-
if not tech_usage:
|
| 257 |
-
tech_usage = ["White Board", "Internet Access"]
|
| 258 |
-
if not language:
|
| 259 |
-
language = "English"
|
| 260 |
-
|
| 261 |
-
detailed_prompt = f"""
|
| 262 |
-
Create a lesson plan for teaching '{topic}' to {age_group} students.
|
| 263 |
-
Duration: {duration} minutes,
|
| 264 |
-
Proficiency Level [CEFR]: {proficiency},
|
| 265 |
-
Technology Requirements: {', '.join(tech_usage) if tech_usage else 'None'},
|
| 266 |
-
Generate response in {language} Language.
|
| 267 |
-
"""
|
| 268 |
-
|
| 269 |
-
try:
|
| 270 |
-
# Pass both the prompt and language
|
| 271 |
-
result = generator.generate_lesson_plan(detailed_prompt, language)
|
| 272 |
-
|
| 273 |
-
# Consistent parsing regardless of language
|
| 274 |
-
if isinstance(result, dict):
|
| 275 |
-
if "lessonPlan" in result:
|
| 276 |
-
lesson_content = result["lessonPlan"]
|
| 277 |
-
else:
|
| 278 |
-
lesson_content = result
|
| 279 |
-
|
| 280 |
-
st.success("Lesson plan generated successfully!")
|
| 281 |
-
|
| 282 |
-
# Create tabs for different views
|
| 283 |
-
tab1, tab2 = st.tabs(["Formatted View", "Raw JSON"])
|
| 284 |
-
|
| 285 |
-
with tab1:
|
| 286 |
-
st.markdown(format_content(lesson_content), unsafe_allow_html=True)
|
| 287 |
-
|
| 288 |
-
with tab2:
|
| 289 |
-
st.json(lesson_content)
|
| 290 |
-
|
| 291 |
-
# Download button
|
| 292 |
-
st.download_button(
|
| 293 |
-
label="Download Lesson Plan",
|
| 294 |
-
data=json.dumps(lesson_content, indent=2, ensure_ascii=False),
|
| 295 |
-
# ensure_ascii=False for proper Unicode handling
|
| 296 |
-
file_name=f"lesson_plan_{language}.json",
|
| 297 |
-
mime="application/json"
|
| 298 |
-
)
|
| 299 |
-
else:
|
| 300 |
-
st.error("Invalid response format")
|
| 301 |
-
|
| 302 |
-
except Exception as e:
|
| 303 |
-
st.error(f"An error occurred: {str(e)}")
|
| 304 |
-
|
| 305 |
-
# Footer
|
| 306 |
-
st.markdown("---")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
from openai_llm import LessonPlanGenerator
|
| 5 |
+
|
| 6 |
+
# Page configuration
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="WizLab Lesson Plan Generator",
|
| 9 |
+
page_icon="📚",
|
| 10 |
+
layout="wide"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Custom CSS with improved styling
|
| 14 |
+
st.markdown("""
|
| 15 |
+
<style>
|
| 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 {
|
| 25 |
+
margin: 15px 0;
|
| 26 |
+
padding: 10px 0;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
.section-level-0 {
|
| 30 |
+
border-bottom: 2px solid #e8eaf6;
|
| 31 |
+
margin-bottom: 20px;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
.section-level-1 {
|
| 35 |
+
margin-left: 20px;
|
| 36 |
+
border-left: 2px solid #e8eaf6;
|
| 37 |
+
padding-left: 15px;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
.section-level-2 {
|
| 41 |
+
margin-left: 15px;
|
| 42 |
+
padding-left: 10px;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
.section-header-main {
|
| 46 |
+
color: #1a237e;
|
| 47 |
+
font-size: 20px;
|
| 48 |
+
font-weight: 600;
|
| 49 |
+
margin-bottom: 15px;
|
| 50 |
+
padding-bottom: 5px;
|
| 51 |
+
border-bottom: 1px solid #e8eaf6;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
.section-header-sub {
|
| 55 |
+
color: #283593;
|
| 56 |
+
font-size: 16px;
|
| 57 |
+
font-weight: 500;
|
| 58 |
+
margin: 10px 0;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
.section-content {
|
| 62 |
+
color: #333;
|
| 63 |
+
margin: 10px 0 10px 20px;
|
| 64 |
+
line-height: 1.5;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
.list-item {
|
| 68 |
+
margin: 8px 0;
|
| 69 |
+
color: #333;
|
| 70 |
+
padding-left: 20px;
|
| 71 |
+
position: relative;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
.list-item:before {
|
| 75 |
+
content: "•";
|
| 76 |
+
position: absolute;
|
| 77 |
+
left: 0;
|
| 78 |
+
color: #3949ab;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
.nested-content {
|
| 82 |
+
margin-left: 20px;
|
| 83 |
+
padding-left: 15px;
|
| 84 |
+
border-left: 2px solid #e8eaf6;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
.timing-label {
|
| 88 |
+
color: #1565c0;
|
| 89 |
+
font-weight: 500;
|
| 90 |
+
font-style: italic;
|
| 91 |
+
}
|
| 92 |
+
</style>
|
| 93 |
+
""", unsafe_allow_html=True)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# Initialize generator
|
| 97 |
+
#@st.cache_resource
|
| 98 |
+
def get_generator():
|
| 99 |
+
return LessonPlanGenerator()
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
#generator = LessonPlanGenerator()
|
| 103 |
+
generator = get_generator()
|
| 104 |
+
# Header
|
| 105 |
+
st.title("WizLab Lesson Plan Generator")
|
| 106 |
+
st.markdown("We help you generate lesson plans tailored to your needs.")
|
| 107 |
+
|
| 108 |
+
# Input section
|
| 109 |
+
st.header("Input Your Requirements")
|
| 110 |
+
|
| 111 |
+
# Create two columns for input fields
|
| 112 |
+
col1, col2 = st.columns(2)
|
| 113 |
+
|
| 114 |
+
with col1:
|
| 115 |
+
topic = st.text_input("Topic", placeholder="e.g., Lesson Plan to teach Spanish numbers as third language to English Speaking students ")
|
| 116 |
+
|
| 117 |
+
age_group_options = ["Elementary (6-11)", "Middle School (12-14)",
|
| 118 |
+
"High School (15-18)", "Adult Learners", "Other"]
|
| 119 |
+
age_group_selection = st.selectbox("Target Age Group", age_group_options)
|
| 120 |
+
if age_group_selection == "Other":
|
| 121 |
+
age_group = st.text_input("Specify Age Group")
|
| 122 |
+
else:
|
| 123 |
+
age_group = age_group_selection
|
| 124 |
+
|
| 125 |
+
language_options = [
|
| 126 |
+
"English", "日本語 (Japanese)", "中文 (Mandarin Chinese)",
|
| 127 |
+
"Español (Spanish)", "हिंदी (Hindi)", "한국어 (Korean)",
|
| 128 |
+
"Deutsch (German)", "Français (French)", "Português (Portuguese)",
|
| 129 |
+
"العربية (Arabic)", "Русский (Russian)", "Other"
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
language_selection = st.selectbox("Select Output Language", language_options)
|
| 133 |
+
LANGUAGE_MAPPING = {
|
| 134 |
+
"日本語 (Japanese)": "Japanese",
|
| 135 |
+
"中文 (Mandarin Chinese)": "Mandarin Chinese",
|
| 136 |
+
"Español (Spanish)": "Spanish",
|
| 137 |
+
"हिंदी (Hindi)": "Hindi",
|
| 138 |
+
"한국어 (Korean)": "Korean",
|
| 139 |
+
"Deutsch (German)": "German",
|
| 140 |
+
"Français (French)": "French",
|
| 141 |
+
"Português (Portuguese)": "Portuguese",
|
| 142 |
+
"العربية (Arabic)": "Arabic",
|
| 143 |
+
"Русский (Russian)": "Russian"
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
# Then in your language selection code:
|
| 147 |
+
if language_selection == "Other":
|
| 148 |
+
language = st.text_input("Specify Language")
|
| 149 |
+
else:
|
| 150 |
+
# Use mapping if available, otherwise use the base language name
|
| 151 |
+
language = LANGUAGE_MAPPING.get(language_selection, language_selection)
|
| 152 |
+
|
| 153 |
+
#if language_selection == "Other":
|
| 154 |
+
# language = st.text_input("Specify Language")
|
| 155 |
+
# else:
|
| 156 |
+
# Extract the base language name without the native script
|
| 157 |
+
# language = language_selection.split(" (")[0] if " (" in language_selection else language_selection
|
| 158 |
+
|
| 159 |
+
with col2:
|
| 160 |
+
#duration_options = ["30 minutes", "45 minutes", "60 minutes", "90 minutes", "Other"]
|
| 161 |
+
#duration_selection = st.selectbox("Lesson Duration", duration_options)
|
| 162 |
+
#if duration_selection == "Other":
|
| 163 |
+
# duration = st.text_input("Specify Duration")
|
| 164 |
+
#else:
|
| 165 |
+
# duration = duration_selection
|
| 166 |
+
duration = st.text_input("Duration [in minutes]", placeholder = "e.g., 25 minutes")
|
| 167 |
+
proficiency_options = ["Beginner [A1]", "Elementary [A2]", "Intermediate[B1, B2]", "Advanced[C1]", "Proficient [C2]", "Other"]
|
| 168 |
+
proficiency_selection = st.selectbox("Proficiency Level [CEFR]", proficiency_options)
|
| 169 |
+
if proficiency_selection == "Other":
|
| 170 |
+
proficiency = st.text_input("Specify Proficiency Level")
|
| 171 |
+
else:
|
| 172 |
+
proficiency = proficiency_selection
|
| 173 |
+
|
| 174 |
+
tech_options = ["Interactive Whiteboard", "Computers/Laptops",
|
| 175 |
+
"Mobile Devices", "Internet Access", "None", "Other"]
|
| 176 |
+
tech_selection = st.multiselect("Accessible Tech Resources", tech_options)
|
| 177 |
+
|
| 178 |
+
if "Other" in tech_selection:
|
| 179 |
+
other_tech = st.text_input("Specify Other Technology Requirements")
|
| 180 |
+
tech_usage = [tech for tech in tech_selection if tech != "Other"] + [other_tech]
|
| 181 |
+
else:
|
| 182 |
+
tech_usage = tech_selection
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def format_content(data):
|
| 186 |
+
html_content = '<div class="main-container">'
|
| 187 |
+
|
| 188 |
+
def format_key(key):
|
| 189 |
+
"""Format key string to be more readable"""
|
| 190 |
+
# Handle camelCase
|
| 191 |
+
key = ''.join(' ' + char if char.isupper() else char for char in key).strip()
|
| 192 |
+
# Replace underscores and normalize spaces
|
| 193 |
+
key = key.replace('_', ' ').title()
|
| 194 |
+
return key
|
| 195 |
+
|
| 196 |
+
def process_value(value, level=0):
|
| 197 |
+
if isinstance(value, dict):
|
| 198 |
+
return process_dict(value, level)
|
| 199 |
+
elif isinstance(value, list):
|
| 200 |
+
return process_list(value, level)
|
| 201 |
+
else:
|
| 202 |
+
return f'<div class="section-content">{value}</div>'
|
| 203 |
+
|
| 204 |
+
def process_dict(d, level):
|
| 205 |
+
content = ""
|
| 206 |
+
for key, value in d.items():
|
| 207 |
+
formatted_key = format_key(key)
|
| 208 |
+
|
| 209 |
+
section_class = "section-level-" + str(level)
|
| 210 |
+
content += f'<div class="section {section_class}">'
|
| 211 |
+
|
| 212 |
+
if level == 0:
|
| 213 |
+
content += f'<div class="section-header-main">{formatted_key}</div>'
|
| 214 |
+
else:
|
| 215 |
+
content += f'<div class="section-header-sub">{formatted_key}</div>'
|
| 216 |
+
|
| 217 |
+
if isinstance(value, dict) and any(isinstance(v, (dict, list)) for v in value.values()):
|
| 218 |
+
content += process_value(value, level + 1)
|
| 219 |
+
elif isinstance(value, list) and any(isinstance(item, dict) for item in value):
|
| 220 |
+
content += process_value(value, level + 1)
|
| 221 |
+
else:
|
| 222 |
+
content += process_value(value, level + 1)
|
| 223 |
+
|
| 224 |
+
content += '</div>'
|
| 225 |
+
return content
|
| 226 |
+
|
| 227 |
+
def process_list(lst, level):
|
| 228 |
+
content = '<div class="section-content">'
|
| 229 |
+
for item in lst:
|
| 230 |
+
if isinstance(item, dict):
|
| 231 |
+
content += process_dict(item, level + 1)
|
| 232 |
+
else:
|
| 233 |
+
content += f'<div class="list-item">{item}</div>'
|
| 234 |
+
content += '</div>'
|
| 235 |
+
return content
|
| 236 |
+
|
| 237 |
+
html_content += process_dict(data, 0)
|
| 238 |
+
html_content += '</div>'
|
| 239 |
+
return html_content
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
# Generate button
|
| 243 |
+
# Generate button
|
| 244 |
+
if st.button("Generate Lesson Plan", type="primary"):
|
| 245 |
+
if not topic:
|
| 246 |
+
st.error("Please enter a topic for the lesson plan.")
|
| 247 |
+
else:
|
| 248 |
+
with st.spinner("Generating your lesson plan..."):
|
| 249 |
+
# Set default values if not provided
|
| 250 |
+
if not duration:
|
| 251 |
+
duration = "30 minutes"
|
| 252 |
+
if not proficiency:
|
| 253 |
+
proficiency = "Intermediate [B1, B2]"
|
| 254 |
+
if not age_group:
|
| 255 |
+
age_group = "Elementary (6-11)"
|
| 256 |
+
if not tech_usage:
|
| 257 |
+
tech_usage = ["White Board", "Internet Access"]
|
| 258 |
+
if not language:
|
| 259 |
+
language = "English"
|
| 260 |
+
|
| 261 |
+
detailed_prompt = f"""
|
| 262 |
+
Create a lesson plan for teaching '{topic}' to {age_group} students.
|
| 263 |
+
Duration: {duration} minutes,
|
| 264 |
+
Proficiency Level [CEFR]: {proficiency},
|
| 265 |
+
Technology Requirements: {', '.join(tech_usage) if tech_usage else 'None'},
|
| 266 |
+
Generate response in {language} Language.
|
| 267 |
+
"""
|
| 268 |
+
|
| 269 |
+
try:
|
| 270 |
+
# Pass both the prompt and language
|
| 271 |
+
result = generator.generate_lesson_plan(detailed_prompt, language)
|
| 272 |
+
|
| 273 |
+
# Consistent parsing regardless of language
|
| 274 |
+
if isinstance(result, dict):
|
| 275 |
+
if "lessonPlan" in result:
|
| 276 |
+
lesson_content = result["lessonPlan"]
|
| 277 |
+
else:
|
| 278 |
+
lesson_content = result
|
| 279 |
+
|
| 280 |
+
st.success("Lesson plan generated successfully!")
|
| 281 |
+
|
| 282 |
+
# Create tabs for different views
|
| 283 |
+
tab1, tab2 = st.tabs(["Formatted View", "Raw JSON"])
|
| 284 |
+
|
| 285 |
+
with tab1:
|
| 286 |
+
st.markdown(format_content(lesson_content), unsafe_allow_html=True)
|
| 287 |
+
|
| 288 |
+
with tab2:
|
| 289 |
+
st.json(lesson_content)
|
| 290 |
+
|
| 291 |
+
# Download button
|
| 292 |
+
st.download_button(
|
| 293 |
+
label="Download Lesson Plan",
|
| 294 |
+
data=json.dumps(lesson_content, indent=2, ensure_ascii=False),
|
| 295 |
+
# ensure_ascii=False for proper Unicode handling
|
| 296 |
+
file_name=f"lesson_plan_{language}.json",
|
| 297 |
+
mime="application/json"
|
| 298 |
+
)
|
| 299 |
+
else:
|
| 300 |
+
st.error("Invalid response format")
|
| 301 |
+
|
| 302 |
+
except Exception as e:
|
| 303 |
+
st.error(f"An error occurred: {str(e)}")
|
| 304 |
+
|
| 305 |
+
# Footer
|
| 306 |
+
st.markdown("---")
|