dhanvanth183 commited on
Commit
d0ed37e
·
verified ·
1 Parent(s): 560c197

Update app.py

Browse files

updated the streamlit for changes made for lesson plan

Files changed (1) hide show
  1. app.py +88 -83
app.py CHANGED
@@ -1,7 +1,14 @@
1
  import streamlit as st
2
  import json
 
 
 
3
 
4
- from openai_llm import LessonPlanGenerator
 
 
 
 
5
 
6
  # Page configuration
7
  st.set_page_config(
@@ -93,13 +100,14 @@ st.markdown("""
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
 
104
  # Header
105
  st.title("WizLab Lesson Plan Generator")
@@ -112,7 +120,8 @@ st.header("Input Your Requirements")
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"]
@@ -123,32 +132,23 @@ with col1:
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
  skills_section = ["Reading", "Writing", "Listening", "Speaking", "Other"]
154
 
@@ -160,22 +160,10 @@ with col1:
160
  if other_skills:
161
  skill_focus.append(other_skills) # Add user-specified skills
162
 
163
- #st.write("Selected Skills:", skill_focus)
164
- #if language_selection == "Other":
165
- # language = st.text_input("Specify Language")
166
- # else:
167
- # Extract the base language name without the native script
168
- # language = language_selection.split(" (")[0] if " (" in language_selection else language_selection
169
-
170
  with col2:
171
- #duration_options = ["30 minutes", "45 minutes", "60 minutes", "90 minutes", "Other"]
172
- #duration_selection = st.selectbox("Lesson Duration", duration_options)
173
- #if duration_selection == "Other":
174
- # duration = st.text_input("Specify Duration")
175
- #else:
176
- # duration = duration_selection
177
- duration = st.text_input("Duration [in minutes]", placeholder = "e.g., 25 minutes")
178
- proficiency_options = ["Beginner [A1]", "Elementary [A2]", "Intermediate[B1]", "Upper-Intermediate [B2]", "Advanced[C1]", "Proficient [C2]", "Other"]
179
  proficiency_selection = st.selectbox("Proficiency Level [CEFR]", proficiency_options)
180
  if proficiency_selection == "Other":
181
  proficiency = st.text_input("Specify Proficiency Level")
@@ -186,8 +174,7 @@ with col2:
186
  "Mobile Devices", "Internet Access", "None", "Other"]
187
  tech_selection = st.multiselect("Accessible Tech Resources", tech_options)
188
 
189
- num_students = st.text_input("Number of students", placeholder= "e.g., 30 students")
190
-
191
 
192
  if "Other" in tech_selection:
193
  other_tech = st.text_input("Specify Other Technology Requirements")
@@ -253,13 +240,12 @@ def format_content(data):
253
  return html_content
254
 
255
 
256
- # Generate button
257
  # Generate button
258
  if st.button("Generate Lesson Plan", type="primary"):
259
  if not topic:
260
  st.error("Please enter a topic for the lesson plan.")
261
  else:
262
- with st.spinner("Generating your lesson plan..."):
263
  # Set default values if not provided
264
  if not duration:
265
  duration = "30 minutes"
@@ -273,50 +259,69 @@ if st.button("Generate Lesson Plan", type="primary"):
273
  language = "English"
274
 
275
  detailed_prompt = f"""
276
- Create a lesson plan for teaching '{topic}' to {age_group} students, for a
277
- Duration: {duration} minutes,
278
- Number of students in the class: {num_students},
279
- SKills to focus in generating lesson plan: {skill_focus}
280
- Proficiency Level [CEFR]: {proficiency},
281
- Technology Requirements: {', '.join(tech_usage) if tech_usage else 'None'},
282
- Generate response in {language} Language.
283
- """
 
 
 
284
 
285
  try:
286
- # Pass both the prompt and language
287
- result = generator.generate_lesson_plan(detailed_prompt, language)
288
-
289
- # Consistent parsing regardless of language
290
- if isinstance(result, dict):
291
- if "lessonPlan" in result:
292
- lesson_content = result["lessonPlan"]
293
- else:
294
- lesson_content = result
295
-
296
- st.success("Lesson plan generated successfully!")
297
-
298
- # Create tabs for different views
299
- tab1, tab2 = st.tabs(["Formatted View", "Raw JSON"])
300
-
301
- with tab1:
302
- st.markdown(format_content(lesson_content), unsafe_allow_html=True)
303
-
304
- with tab2:
305
- st.json(lesson_content)
306
-
307
- # Download button
308
- st.download_button(
309
- label="Download Lesson Plan",
310
- data=json.dumps(lesson_content, indent=2, ensure_ascii=False),
311
- # ensure_ascii=False for proper Unicode handling
312
- file_name=f"lesson_plan_{language}.json",
313
- mime="application/json"
314
- )
315
- else:
316
- st.error("Invalid response format")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
  except Exception as e:
319
  st.error(f"An error occurred: {str(e)}")
 
320
 
321
  # Footer
322
  st.markdown("---")
 
 
1
  import streamlit as st
2
  import json
3
+ import os
4
+ from dotenv import load_dotenv
5
+ from typing import Dict, Any
6
 
7
+ # Import the LLM Handler directly
8
+ from llms_handler import LLMHandler
9
+
10
+ # Load environment variables for OpenAI API key
11
+ load_dotenv()
12
 
13
  # Page configuration
14
  st.set_page_config(
 
100
  """, unsafe_allow_html=True)
101
 
102
 
103
+ # Initialize the LLM Handler (using Streamlit caching)
104
+ @st.cache_resource
105
+ def get_llm_handler():
106
+ """Create and cache the LLM Handler."""
107
+ return LLMHandler()
108
 
109
 
110
+ llm_handler = get_llm_handler()
111
 
112
  # Header
113
  st.title("WizLab Lesson Plan Generator")
 
120
  col1, col2 = st.columns(2)
121
 
122
  with col1:
123
+ topic = st.text_input("Topic",
124
+ placeholder="e.g., Lesson Plan to teach Spanish numbers as third language to English Speaking students ")
125
 
126
  age_group_options = ["Elementary (6-11)", "Middle School (12-14)",
127
  "High School (15-18)", "Adult Learners", "Other"]
 
132
  age_group = age_group_selection
133
 
134
  language_options = [
135
+ "English", "中文 (Mandarin Chinese)"
136
+
 
 
137
  ]
138
 
139
  language_selection = st.selectbox("Select Output Language", language_options)
140
  LANGUAGE_MAPPING = {
141
+
142
+ "中文 (Mandarin Chinese)": "Mandarin Chinese"
143
+
 
 
 
 
 
 
 
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
  skills_section = ["Reading", "Writing", "Listening", "Speaking", "Other"]
154
 
 
160
  if other_skills:
161
  skill_focus.append(other_skills) # Add user-specified skills
162
 
 
 
 
 
 
 
 
163
  with col2:
164
+ duration = st.text_input("Duration [in minutes]", placeholder="e.g., 25 minutes")
165
+ proficiency_options = ["Beginner [A1]", "Elementary [A2]", "Intermediate[B1]", "Upper-Intermediate [B2]",
166
+ "Advanced[C1]", "Proficient [C2]", "Other"]
 
 
 
 
 
167
  proficiency_selection = st.selectbox("Proficiency Level [CEFR]", proficiency_options)
168
  if proficiency_selection == "Other":
169
  proficiency = st.text_input("Specify Proficiency Level")
 
174
  "Mobile Devices", "Internet Access", "None", "Other"]
175
  tech_selection = st.multiselect("Accessible Tech Resources", tech_options)
176
 
177
+ num_students = st.text_input("Number of students", placeholder="e.g., 30 students")
 
178
 
179
  if "Other" in tech_selection:
180
  other_tech = st.text_input("Specify Other Technology Requirements")
 
240
  return html_content
241
 
242
 
 
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... This may take a minute or two."):
249
  # Set default values if not provided
250
  if not duration:
251
  duration = "30 minutes"
 
259
  language = "English"
260
 
261
  detailed_prompt = f"""
262
+ Create a lesson plan for teaching '{topic}' to {age_group} students, for a
263
+ Duration: {duration},
264
+ Number of students in the class: {num_students},
265
+ Skills to focus in generating lesson plan: {', '.join(skill_focus) if skill_focus else 'All language skills'},
266
+ Proficiency Level [CEFR]: {proficiency},
267
+ Technology Requirements: {', '.join(tech_usage) if tech_usage else 'None'},
268
+ Generate response in {language} Language.
269
+ """
270
+
271
+ # Add debug output to see what's happening
272
+ progress_placeholder = st.empty()
273
 
274
  try:
275
+ # Direct integration with LLM Handler
276
+
277
+ # Step 1: Extract and validate input parameters
278
+ progress_placeholder.text("Step 1/4: Extracting and validating inputs...")
279
+ input_data = llm_handler.input_extraction_validation(detailed_prompt)
280
+ progress_placeholder.text("Step 1/4: Input extraction completed")
281
+
282
+ # Step 2: Determine lesson plan structure
283
+ progress_placeholder.text("Step 2/4: Determining lesson structure...")
284
+ structure_info = llm_handler.lesson_plan_structure(input_data)
285
+ progress_placeholder.text("Step 2/4: Structure determination completed")
286
+
287
+ # Step 3: Select activity templates
288
+ progress_placeholder.text("Step 3/4: Selecting activity templates...")
289
+ activity_info = llm_handler.activity_template_selection(input_data, structure_info)
290
+ progress_placeholder.text("Step 3/4: Activity template selection completed")
291
+
292
+ # Step 4: Generate lesson plan
293
+ progress_placeholder.text("Step 4/4: Generating final lesson plan...")
294
+ lesson_plan = llm_handler.lesson_plan_generation(input_data, structure_info, activity_info)
295
+ progress_placeholder.text("Step 4/4: Lesson plan generation completed")
296
+
297
+ # Clear the progress placeholder
298
+ progress_placeholder.empty()
299
+
300
+ # Notify success
301
+ st.success("Lesson plan generated successfully!")
302
+
303
+ # Create tabs for different views
304
+ tab1, tab2 = st.tabs(["Formatted View", "Raw JSON"])
305
+
306
+ with tab1:
307
+ st.markdown(format_content(lesson_plan), unsafe_allow_html=True)
308
+
309
+ with tab2:
310
+ st.json(lesson_plan)
311
+
312
+ # Download button
313
+ st.download_button(
314
+ label="Download Lesson Plan",
315
+ data=json.dumps(lesson_plan, indent=2, ensure_ascii=False),
316
+ # ensure_ascii=False for proper Unicode handling
317
+ file_name=f"lesson_plan_{language}.json",
318
+ mime="application/json"
319
+ )
320
 
321
  except Exception as e:
322
  st.error(f"An error occurred: {str(e)}")
323
+ st.error("Please check if your OpenAI API key is valid and has sufficient credits.")
324
 
325
  # Footer
326
  st.markdown("---")
327
+ st.markdown("© WizLab Lesson Plan Generator | powered by AI")