EdBoy2202 commited on
Commit
1bd6aae
·
verified ·
1 Parent(s): ed51c97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -72,6 +72,7 @@ def parse_quiz_content(quiz_content):
72
  question_text = ""
73
  options = {}
74
  parsing_question_text = True # Flag to indicate we are parsing question text
 
75
 
76
  for line_index, line in enumerate(lines):
77
  line = line.strip()
@@ -79,17 +80,19 @@ def parse_quiz_content(quiz_content):
79
  continue
80
 
81
  if parsing_question_text:
82
- if re.match(r'^Question\s+\d+:\s*(.+)', line, re.IGNORECASE): # Check for "Question N:" and capture question text on same line
83
- match = re.match(r'^Question\s+\d+:\s*(.+)', line, re.IGNORECASE)
84
- if match:
85
- question_text = match.group(1).strip() # Extract question text from the same line
86
  parsing_question_text = False # Stop parsing question text, move to options
87
- else: # Should not happen, but for robustness
88
- question_text = ""
89
- parsing_question_text = False
90
- else:
91
- # Fallback: If line doesn't start with "Question N:", consider it as question text
92
- question_text = line # Consider this line as question text
 
 
93
  parsing_question_text = False # Move to options parsing
94
 
95
 
@@ -240,10 +243,6 @@ if topic:
240
  else:
241
  st.error("Failed to generate quiz content. Please try again or check your API key.")
242
 
243
- except Exception as e:
244
- st.error(f"An error occurred: {e}")
245
- st.error("Please check your API key and network connection. If the problem persists, try a different topic or try again later.")
246
-
247
  # Quiz Display Logic
248
  if st.session_state.quiz_data:
249
  if not st.session_state.quiz_completed:
 
72
  question_text = ""
73
  options = {}
74
  parsing_question_text = True # Flag to indicate we are parsing question text
75
+ question_label_found = False # Flag to track if "Question N:" label was found
76
 
77
  for line_index, line in enumerate(lines):
78
  line = line.strip()
 
80
  continue
81
 
82
  if parsing_question_text:
83
+ if re.match(r'^Question\s+\d+:', line, re.IGNORECASE): # Check for "Question N:"
84
+ question_label_found = True # Mark label as found
85
+ if len(lines) > line_index + 1: # Check if there's a line after "Question N:"
86
+ question_text = lines[line_index + 1].strip() # Take the *next* line as question text
87
  parsing_question_text = False # Stop parsing question text, move to options
88
+ else:
89
+ st.warning(f"Warning: Question label '{line}' found, but no question text followed.")
90
+ question_text = "" # No question text found after label
91
+ parsing_question_text = False # Move to options parsing even if question text missing
92
+ elif not question_label_found:
93
+ # If no "Question N:" label found yet in this block, and we are still parsing question text,
94
+ # consider this line as question text (for the very first line if no label)
95
+ question_text = line
96
  parsing_question_text = False # Move to options parsing
97
 
98
 
 
243
  else:
244
  st.error("Failed to generate quiz content. Please try again or check your API key.")
245
 
 
 
 
 
246
  # Quiz Display Logic
247
  if st.session_state.quiz_data:
248
  if not st.session_state.quiz_completed: