Update app.py
Browse files
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 |
-
#
|
207 |
-
|
208 |
topic = st.text_input("Enter the topic for your quiz:")
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
st.
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
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:
|