cryogenic22 commited on
Commit
9cf27dd
·
verified ·
1 Parent(s): 1661332

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -377
app.py CHANGED
@@ -1,382 +1,5 @@
1
  import streamlit as st
2
 
3
- # Must be the first Streamlit command
4
- st.set_page_config(
5
- page_title="MicroGuru",
6
- page_icon="🎓",
7
- layout="wide"
8
- )
9
-
10
- import os
11
- from learning_platform import LearningPlatform
12
- import asyncio
13
- import nest_asyncio
14
- import logging
15
- from openai import OpenAI
16
- from datetime import datetime
17
-
18
- nest_asyncio.apply()
19
-
20
- # Setup logging
21
- logging.basicConfig(level=logging.INFO)
22
- logger = logging.getLogger(__name__)
23
-
24
- # Initialize session state
25
- if 'learning_path' not in st.session_state:
26
- st.session_state.learning_path = None
27
- if 'current_module' not in st.session_state:
28
- st.session_state.current_module = 0
29
- if 'current_question' not in st.session_state:
30
- st.session_state.current_question = 0
31
- if 'quiz_mode' not in st.session_state:
32
- st.session_state.quiz_mode = False
33
- if 'quiz_score' not in st.session_state:
34
- st.session_state.quiz_score = 0
35
- if 'courses' not in st.session_state:
36
- st.session_state.courses = {
37
- 'in_progress': {}, # {course_name: {data}}
38
- 'completed': {} # {course_name: {data}}
39
- }
40
- if 'current_view' not in st.session_state:
41
- st.session_state.current_view = 'main' # 'main', 'my_courses'
42
-
43
- # Initialize the platform with API key
44
- api_key = os.environ.get('OPENAI_API_KEY')
45
-
46
- if not api_key:
47
- st.error("⚠️ OpenAI API key not found in environment variables.")
48
- st.stop()
49
-
50
- platform = LearningPlatform(api_key)
51
-
52
- # Custom CSS
53
- st.markdown("""
54
- <style>
55
- .main-title {
56
- font-size: 3em !important;
57
- color: #1E88E5;
58
- margin-bottom: 0.5em;
59
- }
60
- .tagline {
61
- font-size: 1.2em;
62
- color: #666;
63
- font-style: italic;
64
- margin-bottom: 1em;
65
- }
66
- .disclaimer {
67
- padding: 10px;
68
- background-color: #FFF3E0;
69
- border-left: 5px solid #FFB74D;
70
- margin: 10px 0;
71
- }
72
- .module-navigator {
73
- padding: 1rem;
74
- background-color: #f8f9fa;
75
- border-radius: 10px;
76
- margin-bottom: 2rem;
77
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
78
- }
79
- .progress-section {
80
- background: linear-gradient(135deg, #1e88e5, #1565c0);
81
- color: white !important;
82
- padding: 20px;
83
- border-radius: 10px;
84
- margin-bottom: 20px;
85
- }
86
- .progress-section h3 {
87
- color: white !important;
88
- }
89
- .topics-section {
90
- background: white;
91
- padding: 20px;
92
- border-radius: 10px;
93
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
94
- }
95
- .module-card {
96
- background: white;
97
- padding: 20px;
98
- border-radius: 10px;
99
- margin-bottom: 20px;
100
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
101
- }
102
- .quiz-button {
103
- background-color: #4CAF50;
104
- color: white;
105
- padding: 15px 25px;
106
- border-radius: 5px;
107
- text-align: center;
108
- margin: 20px 0;
109
- cursor: pointer;
110
- font-weight: bold;
111
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
112
- transition: all 0.3s ease;
113
- }
114
- .quiz-button:hover {
115
- background-color: #45a049;
116
- box-shadow: 0 4px 8px rgba(0,0,0,0.2);
117
- }
118
- .stButton button {
119
- width: 100%;
120
- }
121
- .quiz-card {
122
- background-color: white;
123
- padding: 20px;
124
- border-radius: 10px;
125
- margin: 20px 0;
126
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
127
- }
128
- </style>
129
- """, unsafe_allow_html=True)
130
-
131
- # Course tracking functions
132
- def add_course_to_progress(course):
133
- course_data = {
134
- 'title': course.topic,
135
- 'difficulty': course.difficulty_level,
136
- 'started_date': datetime.now().strftime("%Y-%m-%d"),
137
- 'progress': 0,
138
- 'modules_completed': [],
139
- 'quizzes_completed': []
140
- }
141
- st.session_state.courses['in_progress'][course.topic] = course_data
142
-
143
- def update_course_progress():
144
- if st.session_state.learning_path:
145
- course = st.session_state.learning_path
146
- total_modules = len(course.modules)
147
- completed_modules = len(st.session_state.courses['in_progress'][course.topic]['modules_completed'])
148
- progress = (completed_modules / total_modules) * 100
149
- st.session_state.courses['in_progress'][course.topic]['progress'] = progress
150
-
151
- if progress == 100:
152
- course_data = st.session_state.courses['in_progress'].pop(course.topic)
153
- course_data['completed_date'] = datetime.now().strftime("%Y-%m-%d")
154
- st.session_state.courses['completed'][course.topic] = course_data
155
-
156
- def mark_module_complete(module_index):
157
- if st.session_state.learning_path:
158
- course = st.session_state.learning_path
159
- if course.topic in st.session_state.courses['in_progress']:
160
- if module_index not in st.session_state.courses['in_progress'][course.topic]['modules_completed']:
161
- st.session_state.courses['in_progress'][course.topic]['modules_completed'].append(module_index)
162
- update_course_progress()
163
-
164
- # Main navigation
165
- st.markdown('<h1 class="main-title">🎓 MicroGuru</h1>', unsafe_allow_html=True)
166
- st.markdown('<p class="tagline">Learn at your pace with your AI assistant</p>', unsafe_allow_html=True)
167
-
168
- # Navigation tabs
169
- tab1, tab2 = st.tabs(["Learn", "My Courses"])
170
-
171
- with tab1:
172
- st.markdown('<div class="disclaimer">⚠️ <b>Disclaimer:</b> This course is AI-generated - some content may require verification.</div>', unsafe_allow_html=True)
173
-
174
- # Course Generation Section
175
- if not st.session_state.learning_path:
176
- st.markdown("### 🚀 Start Your Learning Journey")
177
- with st.form("course_generator"):
178
- col1, col2 = st.columns([2, 1])
179
- with col1:
180
- topic = st.text_input("What would you like to learn about?",
181
- placeholder="e.g., Python Programming")
182
- with col2:
183
- difficulty = st.selectbox(
184
- "Choose your level",
185
- ["beginner", "intermediate", "advanced"],
186
- index=0
187
- )
188
- submit_button = st.form_submit_button("Create My Learning Path 🎯")
189
-
190
- if submit_button and topic:
191
- with st.spinner("🤖 Crafting your personalized learning experience..."):
192
- try:
193
- st.session_state.learning_path = asyncio.run(platform.create_course(topic, difficulty))
194
- add_course_to_progress(st.session_state.learning_path)
195
- st.success("✨ Your learning path is ready!")
196
- st.rerun()
197
- except Exception as e:
198
- st.error(f"Error: {str(e)}")
199
-
200
- # Learning Path and Quiz Logic
201
- if st.session_state.learning_path:
202
- learning_path = st.session_state.learning_path
203
-
204
- if not st.session_state.quiz_mode:
205
- # Module Content Display
206
- current_module = learning_path.modules[st.session_state.current_module]
207
-
208
- # Display module content in a card
209
- st.markdown('<div class="module-card">', unsafe_allow_html=True)
210
- st.markdown(f"### 📚 {current_module.title}")
211
- st.markdown(current_module.content)
212
-
213
- # Quiz Button
214
- if hasattr(current_module, 'quiz_questions') and len(current_module.quiz_questions) > 0:
215
- quiz_col1, quiz_col2, quiz_col3 = st.columns([1,2,1])
216
- with quiz_col2:
217
- if st.button("📝 Start Module Quiz", type="primary"):
218
- st.session_state.quiz_mode = True
219
- st.session_state.current_question = 0
220
- st.session_state.quiz_score = 0
221
- st.rerun()
222
- st.markdown('</div>', unsafe_allow_html=True)
223
-
224
- # Module Navigation Buttons
225
- col1, col2, col3 = st.columns([1, 2, 1])
226
- with col1:
227
- if st.session_state.current_module > 0:
228
- if st.button("⬅️ Previous Module"):
229
- st.session_state.current_module -= 1
230
- st.rerun()
231
- with col3:
232
- if st.session_state.current_module < len(st.session_state.learning_path.modules) - 1:
233
- if st.button("Next Module ➡️"):
234
- st.session_state.current_module += 1
235
- st.rerun()
236
-
237
- else:
238
- # Quiz Mode
239
- current_module = learning_path.modules[st.session_state.current_module]
240
-
241
- if not hasattr(current_module, 'quiz_questions') or not current_module.quiz_questions:
242
- st.warning("No quiz available for this module.")
243
- st.session_state.quiz_mode = False
244
- st.rerun()
245
-
246
- questions = current_module.quiz_questions
247
-
248
- st.markdown('<div class="quiz-card">', unsafe_allow_html=True)
249
- st.markdown("### 📝 Module Quiz")
250
-
251
- if st.session_state.current_question < len(questions):
252
- question = questions[st.session_state.current_question]
253
- total_questions = len(questions)
254
-
255
- # Progress bar for quiz
256
- st.progress(st.session_state.current_question / total_questions)
257
- st.markdown(f"Question {st.session_state.current_question + 1} of {total_questions}")
258
-
259
- # Display question
260
- st.markdown(f"**{question['question']}**")
261
-
262
- # Display options
263
- if 'options' in question and question['options']:
264
- answer = st.radio(
265
- "Select your answer:",
266
- question['options'],
267
- key=f"quiz_{st.session_state.current_module}_{st.session_state.current_question}"
268
- )
269
-
270
- if st.button("Submit Answer", type="primary"):
271
- if answer == question.get('correct_answer'):
272
- st.success("✅ Correct!")
273
- st.session_state.quiz_score += 1
274
- else:
275
- st.error("❌ Incorrect")
276
- st.markdown(f"The correct answer is: **{question.get('correct_answer')}**")
277
-
278
- if 'explanation' in question:
279
- st.info(f"**Explanation:** {question['explanation']}")
280
-
281
- # Add continue button after showing result
282
- if st.button("Continue to Next Question ➡️"):
283
- st.session_state.current_question += 1
284
- st.rerun()
285
-
286
- # Show current score
287
- st.markdown(f"**Current Score:** {st.session_state.quiz_score}/{total_questions}")
288
-
289
- else:
290
- # Quiz Complete
291
- score_percentage = (st.session_state.quiz_score / len(questions)) * 100
292
- st.markdown(f"### 🎉 Quiz Complete!")
293
- st.markdown(f"### Final Score: {score_percentage:.1f}%")
294
-
295
- # Award badge based on score
296
- if score_percentage >= 90:
297
- st.balloons()
298
- st.markdown("### 🏆 Expert Achievement Unlocked!")
299
- elif score_percentage >= 75:
300
- st.markdown("### 🌟 Advanced Learner Badge!")
301
- elif score_percentage >= 60:
302
- st.markdown("### 📚 Knowledge Seeker Award!")
303
-
304
- col1, col2 = st.columns(2)
305
- with col1:
306
- if st.button("📚 Review Module", type="secondary"):
307
- st.session_state.quiz_mode = False
308
- mark_module_complete(st.session_state.current_module)
309
- st.rerun()
310
- with col2:
311
- if st.session_state.current_module < len(learning_path.modules) - 1:
312
- if st.button("Next Module ➡️", type="primary"):
313
- st.session_state.current_module += 1
314
- st.session_state.quiz_mode = False
315
- mark_module_complete(st.session_state.current_module - 1)
316
- st.rerun()
317
-
318
- st.markdown('</div>', unsafe_allow_html=True)
319
-
320
- with tab2:
321
- # In Progress Courses
322
- st.markdown("#### 🚀 In Progress")
323
- if st.session_state.courses['in_progress']:
324
- for course_name, data in st.session_state.courses['in_progress'].items():
325
- with st.expander(f"{course_name} ({data['progress']}% complete)"):
326
- st.write(f"Started: {data['started_date']}")
327
- st.write(f"Difficulty: {data['difficulty']}")
328
- st.progress(data['progress'] / 100)
329
- if st.button(f"Continue {course_name}", key=f"continue_{course_name}"):
330
- st.session_state.learning_path = asyncio.run(platform.create_course(course_name, data['difficulty']))
331
- st.rerun()
332
- else:
333
- st.info("No courses in progress")
334
-
335
- # Completed Courses
336
- st.markdown("#### 🏆 Completed")
337
- if st.session_state.courses['completed']:
338
- for course_name, data in st.session_state.courses['completed'].items():
339
- with st.expander(f"{course_name} (Completed)"):
340
- st.write(f"Completed: {data['completed_date']}")
341
- st.write(f"Difficulty: {data['difficulty']}")
342
- if st.button(f"Review {course_name}", key=f"review_{course_name}"):
343
- st.session_state.learning_path = asyncio.run(platform.create_course(course_name, data['difficulty']))
344
- st.rerun()
345
- else:
346
- st.info("No completed courses yet")
347
-
348
- # Sidebar
349
- with st.sidebar:
350
- if st.session_state.learning_path:
351
- st.markdown('<div class="progress-section">', unsafe_allow_html=True)
352
- st.markdown("### 📊 Your Progress")
353
- st.write(f"Current Module: {st.session_state.current_module + 1}/{len(learning_path.modules)}")
354
- st.write(f"Course: {learning_path.topic}")
355
- st.write(f"Level: {learning_path.difficulty_level}")
356
- st.markdown('</div>', unsafe_allow_html=True)
357
-
358
- st.markdown("### 🧭 Module Navigation")
359
- module_titles = [module.title for module in st.session_state.learning_path.modules]
360
- selected_module_index = st.selectbox("Go to Module:", options=module_titles, index=st.session_state.current_module)
361
- st.session_state.current_module = module_titles.index(selected_module_index)
362
-
363
- st.markdown('<div class="topics-section">', unsafe_allow_html=True)
364
- st.markdown("### 💡 Popular Topics")
365
- topics = {
366
- "Python Programming": "🐍",
367
- "Machine Learning": "🤖",
368
- "Web Development": "🌐",
369
- "Data Science": "📊",
370
- "Digital Marketing": "��"
371
- }
372
- for topic, icon in topics.items():import streamlit as st
373
-
374
- # Must be the first Streamlit command
375
- st.set_page_config(
376
- page_title="MicroGuru",
377
- page_icon="🎓",
378
- layout="wide"
379
- )
380
 
381
  import os
382
  from learning_platform import LearningPlatform
 
1
  import streamlit as st
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  import os
5
  from learning_platform import LearningPlatform