Update app.py
Browse files
app.py
CHANGED
@@ -60,6 +60,8 @@ def quiz_app():
|
|
60 |
st.session_state.quiz_started = False
|
61 |
if 'quiz_topic' not in st.session_state:
|
62 |
st.session_state.quiz_topic = "" # Initialize quiz topic
|
|
|
|
|
63 |
|
64 |
|
65 |
# API Configuration based on selection - Moved inside quiz_app after login
|
@@ -233,24 +235,48 @@ def quiz_app():
|
|
233 |
st.session_state.score = correct_count # Store score in session state
|
234 |
|
235 |
|
236 |
-
#
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
st.session_state.
|
241 |
-
st.session_state.
|
242 |
-
# Reset quiz state
|
243 |
st.session_state.quiz_data = None
|
244 |
st.session_state.current_question_index = 0
|
245 |
st.session_state.user_answers = []
|
246 |
st.session_state.quiz_completed = False
|
247 |
st.session_state.score = 0
|
248 |
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
The quiz should have 5 questions.
|
255 |
|
256 |
**Formatting Instructions:**
|
@@ -292,35 +318,33 @@ def quiz_app():
|
|
292 |
|
293 |
Please generate the quiz in this exact format.
|
294 |
"""
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
else: # else associated with outer if quiz_content
|
317 |
st.error("Failed to generate quiz content. Please try again or check your API key.")
|
318 |
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
else:
|
323 |
-
st.warning("Please enter a topic to generate a quiz.")
|
324 |
st.rerun() # Rerun to update the quiz display based on new topic/generation
|
325 |
|
326 |
|
@@ -330,10 +354,8 @@ def quiz_app():
|
|
330 |
display_question()
|
331 |
else:
|
332 |
display_results()
|
333 |
-
elif
|
334 |
-
st.info("Enter a topic and click 'Generate Quiz'
|
335 |
-
elif not topic and not st.session_state.quiz_data and not st.session_state.quiz_completed and st.session_state.quiz_started == False:
|
336 |
-
st.info("Enter a topic in the sidebar to generate a quiz.")
|
337 |
|
338 |
|
339 |
# --- Main App Flow Control ---
|
|
|
60 |
st.session_state.quiz_started = False
|
61 |
if 'quiz_topic' not in st.session_state:
|
62 |
st.session_state.quiz_topic = "" # Initialize quiz topic
|
63 |
+
if 'topic_from_sidebar' not in st.session_state:
|
64 |
+
st.session_state.topic_from_sidebar = False # Flag to track if topic is from sidebar
|
65 |
|
66 |
|
67 |
# API Configuration based on selection - Moved inside quiz_app after login
|
|
|
235 |
st.session_state.score = correct_count # Store score in session state
|
236 |
|
237 |
|
238 |
+
# Sidebar topic input - Always visible
|
239 |
+
sidebar_topic = st.sidebar.text_input("New Quiz Topic", value=st.session_state.get('quiz_topic', ''))
|
240 |
+
if st.sidebar.button("Generate New Quiz"): # Button in sidebar to generate quiz, always resets
|
241 |
+
st.session_state.quiz_started = True
|
242 |
+
st.session_state.quiz_topic = sidebar_topic
|
243 |
+
st.session_state.topic_from_sidebar = True # Flag to indicate topic from sidebar
|
244 |
+
# Reset quiz state for new quiz
|
245 |
st.session_state.quiz_data = None
|
246 |
st.session_state.current_question_index = 0
|
247 |
st.session_state.user_answers = []
|
248 |
st.session_state.quiz_completed = False
|
249 |
st.session_state.score = 0
|
250 |
|
251 |
+
|
252 |
+
# Initial topic input in main area - Shown only if quiz not started or topic was from sidebar (new quiz from sidebar)
|
253 |
+
if not st.session_state.quiz_started or st.session_state.topic_from_sidebar:
|
254 |
+
initial_topic = st.text_input("Enter the topic for your quiz:", value=st.session_state.get('quiz_topic', '') if not st.session_state.topic_from_sidebar else "") # Keep value if not from sidebar, clear if from sidebar
|
255 |
+
if st.button("Generate Quiz"): # Button to generate quiz from initial input
|
256 |
+
if initial_topic:
|
257 |
+
st.session_state.quiz_started = True
|
258 |
+
st.session_state.quiz_topic = initial_topic
|
259 |
+
st.session_state.topic_from_sidebar = False # Reset sidebar topic flag
|
260 |
+
# Reset quiz state - although should be already reset if coming from sidebar, or first time
|
261 |
+
st.session_state.quiz_data = None
|
262 |
+
st.session_state.current_question_index = 0
|
263 |
+
st.session_state.user_answers = []
|
264 |
+
st.session_state.quiz_completed = False
|
265 |
+
st.session_state.score = 0
|
266 |
+
else:
|
267 |
+
st.warning("Please enter a topic to generate a quiz.")
|
268 |
+
|
269 |
+
|
270 |
+
# Quiz Generation Logic - Moved outside input blocks, runs after button presses
|
271 |
+
topic_to_generate = None
|
272 |
+
if st.session_state.quiz_started and st.session_state.quiz_data is None: # Generate only if quiz started and no data yet
|
273 |
+
topic_to_generate = st.session_state.quiz_topic
|
274 |
+
|
275 |
+
if topic_to_generate:
|
276 |
+
with st.spinner(f"Generating quiz on '{topic_to_generate}'..."):
|
277 |
+
try:
|
278 |
+
prompt = f"""
|
279 |
+
Generate a multiple-choice quiz on the topic of "{topic_to_generate}".
|
280 |
The quiz should have 5 questions.
|
281 |
|
282 |
**Formatting Instructions:**
|
|
|
318 |
|
319 |
Please generate the quiz in this exact format.
|
320 |
"""
|
321 |
+
if st.session_state.selected_api == "Gemini API":
|
322 |
+
response = model.generate_content(prompt)
|
323 |
+
quiz_content = response.text
|
324 |
+
elif st.session_state.selected_api == "OpenAI API":
|
325 |
+
response = openai.ChatCompletion.create(
|
326 |
+
model=st.session_state.openai_model,
|
327 |
+
messages=[{"role": "user", "content": prompt}]
|
328 |
+
)
|
329 |
+
quiz_content = response.choices[0].message.content
|
330 |
+
|
331 |
+
|
332 |
+
parsed_quiz_data, answer_key = parse_quiz_content(quiz_content)
|
333 |
+
|
334 |
+
|
335 |
+
if quiz_content: # Check if quiz_content was generated successfully (outer if)
|
336 |
+
if parsed_quiz_data: # Check if parsing was successful (inner if)
|
337 |
+
st.session_state.quiz_data = parsed_quiz_data
|
338 |
+
st.success(f"Quiz on '{topic_to_generate}' generated successfully! Let's begin.")
|
339 |
+
else: # else associated with inner if parsed_quiz_data
|
340 |
+
st.error("Failed to parse quiz content. Please try generating again.")
|
341 |
+
st.session_state.quiz_data = None
|
342 |
else: # else associated with outer if quiz_content
|
343 |
st.error("Failed to generate quiz content. Please try again or check your API key.")
|
344 |
|
345 |
+
except Exception as e:
|
346 |
+
st.error(f"An error occurred: {e}")
|
347 |
+
st.error("Please check your API key and network connection. If the problem persists, try a different topic or try again later.")
|
|
|
|
|
348 |
st.rerun() # Rerun to update the quiz display based on new topic/generation
|
349 |
|
350 |
|
|
|
354 |
display_question()
|
355 |
else:
|
356 |
display_results()
|
357 |
+
elif not st.session_state.quiz_started and not st.session_state.quiz_data and not st.session_state.quiz_completed:
|
358 |
+
st.info("Enter a topic in the main area and click 'Generate Quiz' to start, or use the sidebar to start a new quiz anytime.")
|
|
|
|
|
359 |
|
360 |
|
361 |
# --- Main App Flow Control ---
|