EdBoy2202 commited on
Commit
56f04a7
·
verified ·
1 Parent(s): 1661880

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -82
app.py CHANGED
@@ -56,6 +56,8 @@ def quiz_app():
56
  st.session_state.quiz_completed = False
57
  if 'score' not in st.session_state:
58
  st.session_state.score = 0
 
 
59
 
60
  # API Configuration based on selection - Moved inside quiz_app after login
61
  if st.session_state.selected_api == "Gemini API":
@@ -203,87 +205,93 @@ def quiz_app():
203
  st.session_state.score = correct_count # Store score in session state
204
 
205
 
206
- # Sidebar for topic input and quiz generation
207
- with st.sidebar:
208
  topic = st.text_input("Enter the topic for your quiz:")
209
- if topic:
210
- if st.button("Generate Quiz"):
211
- with st.spinner(f"Generating quiz on '{topic}'..."):
212
- try:
213
- prompt = f"""
214
- Generate a multiple-choice quiz on the topic of "{topic}".
215
- The quiz should have 5 questions.
216
-
217
- **Formatting Instructions:**
218
-
219
- 1. **Question Numbering:** Start each question with a number followed by a period (e.g., "1.").
220
- 2. **Question Text:** Immediately after the question number, write the question text.
221
- 3. **Options:**
222
- * List four options for each question, labeled A, B, C, and D.
223
- * Use the format: "A) Option text", "B) Option text", etc. (letter followed by a parenthesis, then a space, then the option text).
224
- * Place each option on a new line directly below the question.
225
- 4. **Answer Key:**
226
- * After all questions, create a separate section titled "**Answer Key:**".
227
- * In the Answer Key, list the correct answer for each question in the format: "1. Correct option letter", "2. Correct option letter", etc. (question number, period, space, correct option letter - A, B, C, or D).
228
-
229
- **Example of Desired Format:**
230
-
231
- **Quiz Title (Optional, but good to have)**
232
-
233
- 1. Question 1 text?
234
- A) Option A
235
- B) Option B
236
- C) Option C
237
- D) Option D
238
-
239
- 2. Question 2 text?
240
- A) Option A
241
- B) Option B
242
- C) Option C
243
- D) Option D
244
-
245
- ... (and so on for 5 questions) ...
246
-
247
- **Answer Key:**
248
- 1. C
249
- 2. A
250
- 3. B
251
- 4. D
252
- 5. A
253
-
254
- Please generate the quiz in this exact format.
255
- """
256
- if st.session_state.selected_api == "Gemini API":
257
- response = model.generate_content(prompt)
258
- quiz_content = response.text
259
- elif st.session_state.selected_api == "OpenAI API":
260
- response = openai.ChatCompletion.create(
261
- model=st.session_state.openai_model,
262
- messages=[{"role": "user", "content": prompt}]
263
- )
264
- quiz_content = response.choices[0].message.content
265
-
266
-
267
- parsed_quiz_data, answer_key = parse_quiz_content(quiz_content)
268
-
269
-
270
- if quiz_content: # Check if quiz_content was generated successfully (outer if)
271
- if parsed_quiz_data: # Check if parsing was successful (inner if)
272
- st.session_state.quiz_data = parsed_quiz_data
273
- st.session_state.current_question_index = 0
274
- st.session_state.user_answers = []
275
- st.session_state.quiz_completed = False
276
- st.session_state.score = 0
277
- st.success(f"Quiz on '{topic}' generated successfully! Let's begin.")
278
- else: # else associated with inner if parsed_quiz_data
279
- st.error("Failed to parse quiz content. Please try generating again.")
280
- st.session_state.quiz_data = None
281
- else: # else associated with outer if quiz_content
282
- st.error("Failed to generate quiz content. Please try again or check your API key.")
283
-
284
- except Exception as e:
285
- st.error(f"An error occurred: {e}")
286
- st.error("Please check your API key and network connection. If the problem persists, try a different topic or try again later.")
 
 
 
 
 
 
287
 
288
  # Quiz Display Logic
289
  if st.session_state.quiz_data:
@@ -291,8 +299,8 @@ def quiz_app():
291
  display_question()
292
  else:
293
  display_results()
294
- elif topic and not st.session_state.quiz_data and not st.session_state.quiz_completed: # Message if topic entered but quiz not generated yet
295
- st.info("Click 'Generate Quiz' to start the quiz in the sidebar.")
296
 
297
  # --- Main App Flow Control ---
298
  if 'logged_in' not in st.session_state:
 
56
  st.session_state.quiz_completed = False
57
  if 'score' not in st.session_state:
58
  st.session_state.score = 0
59
+ if 'quiz_started' not in st.session_state: # Track if quiz generation has started
60
+ st.session_state.quiz_started = False
61
 
62
  # API Configuration based on selection - Moved inside quiz_app after login
63
  if st.session_state.selected_api == "Gemini API":
 
205
  st.session_state.score = correct_count # Store score in session state
206
 
207
 
208
+ # User input for topic - Conditional placement
209
+ if not st.session_state.quiz_started:
210
  topic = st.text_input("Enter the topic for your quiz:")
211
+ else:
212
+ topic = st.sidebar.text_input("Quiz Topic", value=st.session_state.get('quiz_topic', '')) # Move to sidebar after quiz starts, keep topic value
213
+
214
+ if topic and not st.session_state.quiz_started: # Only process topic and button if quiz hasn't started yet for this topic
215
+ if st.button("Generate Quiz"):
216
+ st.session_state.quiz_started = True # Set quiz started to true when generate button is clicked
217
+ st.session_state.quiz_topic = topic # Store topic in session state to keep it in sidebar input
218
+ with st.spinner(f"Generating quiz on '{topic}'..."):
219
+ try:
220
+ prompt = f"""
221
+ Generate a multiple-choice quiz on the topic of "{topic}".
222
+ The quiz should have 5 questions.
223
+
224
+ **Formatting Instructions:**
225
+
226
+ 1. **Question Numbering:** Start each question with a number followed by a period (e.g., "1.").
227
+ 2. **Question Text:** Immediately after the question number, write the question text.
228
+ 3. **Options:**
229
+ * List four options for each question, labeled A, B, C, and D.
230
+ * Use the format: "A) Option text", "B) Option text", etc. (letter followed by a parenthesis, then a space, then the option text).
231
+ * Place each option on a new line directly below the question.
232
+ 4. **Answer Key:**
233
+ * After all questions, create a separate section titled "**Answer Key:**".
234
+ * In the Answer Key, list the correct answer for each question in the format: "1. Correct option letter", "2. Correct option letter", etc. (question number, period, space, correct option letter - A, B, C, or D).
235
+
236
+ **Example of Desired Format:**
237
+
238
+ **Quiz Title (Optional, but good to have)**
239
+
240
+ 1. Question 1 text?
241
+ A) Option A
242
+ B) Option B
243
+ C) Option C
244
+ D) Option D
245
+
246
+ 2. Question 2 text?
247
+ A) Option A
248
+ B) Option B
249
+ C) Option C
250
+ D) Option D
251
+
252
+ ... (and so on for 5 questions) ...
253
+
254
+ **Answer Key:**
255
+ 1. C
256
+ 2. A
257
+ 3. B
258
+ 4. D
259
+ 5. A
260
+
261
+ Please generate the quiz in this exact format.
262
+ """
263
+ if st.session_state.selected_api == "Gemini API":
264
+ response = model.generate_content(prompt)
265
+ quiz_content = response.text
266
+ elif st.session_state.selected_api == "OpenAI API":
267
+ response = openai.ChatCompletion.create(
268
+ model=st.session_state.openai_model,
269
+ messages=[{"role": "user", "content": prompt}]
270
+ )
271
+ quiz_content = response.choices[0].message.content
272
+
273
+
274
+ parsed_quiz_data, answer_key = parse_quiz_content(quiz_content)
275
+
276
+
277
+ if quiz_content: # Check if quiz_content was generated successfully (outer if)
278
+ if parsed_quiz_data: # Check if parsing was successful (inner if)
279
+ st.session_state.quiz_data = parsed_quiz_data
280
+ st.session_state.current_question_index = 0
281
+ st.session_state.user_answers = []
282
+ st.session_state.quiz_completed = False
283
+ st.session_state.score = 0
284
+ st.success(f"Quiz on '{topic}' generated successfully! Let's begin.")
285
+ else: # else associated with inner if parsed_quiz_data
286
+ st.error("Failed to parse quiz content. Please try generating again.")
287
+ st.session_state.quiz_data = None
288
+ else: # else associated with outer if quiz_content
289
+ st.error("Failed to generate quiz content. Please try again or check your API key.")
290
+
291
+ except Exception as e:
292
+ st.error(f"An error occurred: {e}")
293
+ st.error("Please check your API key and network connection. If the problem persists, try a different topic or try again later.")
294
+ st.rerun() # Rerun to move topic input to sidebar
295
 
296
  # Quiz Display Logic
297
  if st.session_state.quiz_data:
 
299
  display_question()
300
  else:
301
  display_results()
302
+ elif topic and not st.session_state.quiz_data and not st.session_state.quiz_completed and not st.session_state.quiz_started: # Message if topic entered but quiz not generated yet and quiz not started
303
+ st.info("Click 'Generate Quiz' to start the quiz.")
304
 
305
  # --- Main App Flow Control ---
306
  if 'logged_in' not in st.session_state: