EdBoy2202 commited on
Commit
1661880
·
verified ·
1 Parent(s): 17b329c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -82
app.py CHANGED
@@ -203,87 +203,87 @@ def quiz_app():
203
  st.session_state.score = correct_count # Store score in session state
204
 
205
 
206
- # User input for topic
207
- topic = st.text_input("Enter the topic for your quiz:")
208
-
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:
@@ -292,7 +292,7 @@ def quiz_app():
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:
 
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:
 
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: