Update app.py
Browse files
app.py
CHANGED
@@ -69,7 +69,6 @@ def parse_quiz_content(quiz_content):
|
|
69 |
st.write(f"Debugging: Skipping very short or empty block: '{block}'") # Debugging skip message
|
70 |
continue
|
71 |
|
72 |
-
|
73 |
lines = block.split('\n')
|
74 |
question_text = ""
|
75 |
options = {}
|
@@ -86,15 +85,17 @@ def parse_quiz_content(quiz_content):
|
|
86 |
if parsing_question_text:
|
87 |
if re.match(r'^Question\s+\d+:', line, re.IGNORECASE): # Check for "Question N:"
|
88 |
question_label_found = True # Mark label as found
|
89 |
-
|
90 |
-
|
|
|
91 |
parsing_question_text = False # Stop parsing question text, move to options
|
92 |
block_has_question_content = True # Mark that we found question content
|
93 |
else:
|
94 |
st.warning(f"Warning: Question label '{line}' found, but no question text followed.")
|
95 |
question_text = "" # No question text found after label
|
96 |
parsing_question_text = False # Move to options parsing even if question text missing
|
97 |
-
|
|
|
98 |
# If no "Question N:" label found yet in this block, and we are still parsing question text,
|
99 |
# consider this line as question text (for the very first line if no label)
|
100 |
question_text = line
|
@@ -110,7 +111,6 @@ def parse_quiz_content(quiz_content):
|
|
110 |
options[option_letter] = option_text
|
111 |
block_has_question_content = True # Mark that we found option content
|
112 |
|
113 |
-
|
114 |
# --- DEBUGGING PRINT: Print question text and options for each block ---
|
115 |
st.write(f"#### Debugging: Block {question_number} - Parsed Data:")
|
116 |
st.write(f"- Question Text: '{question_text}'")
|
@@ -125,26 +125,15 @@ def parse_quiz_content(quiz_content):
|
|
125 |
question_number += 1 # Increment expected question number
|
126 |
|
127 |
|
128 |
-
# --- Answer Key Parsing (
|
129 |
-
|
130 |
-
st.code(answer_key_section) # Display raw answer key in code block
|
131 |
-
answer_lines = answer_key_section.strip().split('\n')
|
132 |
for line in answer_lines:
|
133 |
line = line.strip()
|
134 |
-
match = re.match(r'(\d+)\.\s*([A-D])
|
135 |
-
if not match: # If no match with parenthesis, try without
|
136 |
-
match = re.match(r'(\d+)\.\s*([A-D])', line)
|
137 |
if match:
|
138 |
question_num = int(match.group(1)) - 1
|
139 |
correct_answer = match.group(2)
|
140 |
answer_key_dict[question_num] = correct_answer
|
141 |
-
st.write(f"Debugging: Answer Key - Parsed Answer: Question {question_num+1}, Answer: {correct_answer}") # Print parsed answer
|
142 |
-
else:
|
143 |
-
st.warning(f"Warning: Could not parse answer key line: '{line}'") # Warning for unparsed answer key lines
|
144 |
-
|
145 |
-
st.write("### Debugging: Parsed Answer Key Dictionary:") # Print the answer key dictionary
|
146 |
-
st.write(answer_key_dict)
|
147 |
-
|
148 |
|
149 |
# Basic validation
|
150 |
if not questions or not answer_key_dict:
|
@@ -154,11 +143,10 @@ def parse_quiz_content(quiz_content):
|
|
154 |
st.warning(f"Number of questions parsed ({len(questions)}) does not match number of answers in answer key ({len(answer_key_dict)}). Parsing might be incomplete.")
|
155 |
|
156 |
|
157 |
-
# Combine parsed questions and answer key into quiz_data
|
158 |
quiz_data_list = []
|
159 |
for i, q_data in enumerate(questions):
|
160 |
correct_answer = answer_key_dict.get(i)
|
161 |
-
st.write(f"Debugging: Creating quiz_data_list - Question Index: {i}, Correct Answer from dict: '{correct_answer}'") # Print during quiz_data_list creation
|
162 |
if correct_answer:
|
163 |
quiz_data_list.append({
|
164 |
'question': q_data['question'],
|
@@ -166,12 +154,9 @@ def parse_quiz_content(quiz_content):
|
|
166 |
'correct_answer': correct_answer
|
167 |
})
|
168 |
else:
|
169 |
-
st.warning(f"
|
170 |
return None, None
|
171 |
|
172 |
-
st.write("### Debugging: Final Parsed Quiz Data:") # Print the final parsed quiz data
|
173 |
-
st.write(quiz_data_list)
|
174 |
-
|
175 |
return quiz_data_list, answer_key_dict
|
176 |
|
177 |
|
@@ -184,7 +169,7 @@ def display_question():
|
|
184 |
if st.session_state.current_question_index < len(st.session_state.quiz_data):
|
185 |
question_data = st.session_state.quiz_data[st.session_state.current_question_index]
|
186 |
question_number = st.session_state.current_question_index + 1
|
187 |
-
st.markdown(f"**Question {question_number}:** {question_data['question']}")
|
188 |
|
189 |
options_list = [f"{key}. {value}" for key, value in question_data['options'].items()]
|
190 |
user_choice = st.radio("Choose an answer:", options_list, key=f"q_{question_number}") # Unique key for radio buttons
|
@@ -266,10 +251,6 @@ if topic:
|
|
266 |
else:
|
267 |
st.error("Failed to generate quiz content. Please try again or check your API key.")
|
268 |
|
269 |
-
except Exception as e:
|
270 |
-
st.error(f"An error occurred: {e}")
|
271 |
-
st.error("Please check your API key and network connection. If the problem persists, try a different topic or try again later.")
|
272 |
-
|
273 |
# Quiz Display Logic
|
274 |
if st.session_state.quiz_data:
|
275 |
if not st.session_state.quiz_completed:
|
|
|
69 |
st.write(f"Debugging: Skipping very short or empty block: '{block}'") # Debugging skip message
|
70 |
continue
|
71 |
|
|
|
72 |
lines = block.split('\n')
|
73 |
question_text = ""
|
74 |
options = {}
|
|
|
85 |
if parsing_question_text:
|
86 |
if re.match(r'^Question\s+\d+:', line, re.IGNORECASE): # Check for "Question N:"
|
87 |
question_label_found = True # Mark label as found
|
88 |
+
# **MODIFIED QUESTION TEXT EXTRACTION:**
|
89 |
+
if len(lines) > line_index + 1: # Check if there's a line AFTER "Question N:"
|
90 |
+
question_text = lines[line_index + 1].strip() # Take the *NEXT* line as question text
|
91 |
parsing_question_text = False # Stop parsing question text, move to options
|
92 |
block_has_question_content = True # Mark that we found question content
|
93 |
else:
|
94 |
st.warning(f"Warning: Question label '{line}' found, but no question text followed.")
|
95 |
question_text = "" # No question text found after label
|
96 |
parsing_question_text = False # Move to options parsing even if question text missing
|
97 |
+
# **MODIFIED - Moved this condition to be ELSE if question_label_found is False**
|
98 |
+
elif not question_label_found and not question_text: # Check if question_label NOT found AND question_text is still empty
|
99 |
# If no "Question N:" label found yet in this block, and we are still parsing question text,
|
100 |
# consider this line as question text (for the very first line if no label)
|
101 |
question_text = line
|
|
|
111 |
options[option_letter] = option_text
|
112 |
block_has_question_content = True # Mark that we found option content
|
113 |
|
|
|
114 |
# --- DEBUGGING PRINT: Print question text and options for each block ---
|
115 |
st.write(f"#### Debugging: Block {question_number} - Parsed Data:")
|
116 |
st.write(f"- Question Text: '{question_text}'")
|
|
|
125 |
question_number += 1 # Increment expected question number
|
126 |
|
127 |
|
128 |
+
# --- Answer Key Parsing (No change needed) ---
|
129 |
+
answer_lines = answer_key_section.strip('\n')
|
|
|
|
|
130 |
for line in answer_lines:
|
131 |
line = line.strip()
|
132 |
+
match = re.match(r'(\d+)\.\s*([A-D])', line)
|
|
|
|
|
133 |
if match:
|
134 |
question_num = int(match.group(1)) - 1
|
135 |
correct_answer = match.group(2)
|
136 |
answer_key_dict[question_num] = correct_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
# Basic validation
|
139 |
if not questions or not answer_key_dict:
|
|
|
143 |
st.warning(f"Number of questions parsed ({len(questions)}) does not match number of answers in answer key ({len(answer_key_dict)}). Parsing might be incomplete.")
|
144 |
|
145 |
|
146 |
+
# Combine parsed questions and answer key into quiz_data
|
147 |
quiz_data_list = []
|
148 |
for i, q_data in enumerate(questions):
|
149 |
correct_answer = answer_key_dict.get(i)
|
|
|
150 |
if correct_answer:
|
151 |
quiz_data_list.append({
|
152 |
'question': q_data['question'],
|
|
|
154 |
'correct_answer': correct_answer
|
155 |
})
|
156 |
else:
|
157 |
+
st.warning(f"Could not find correct answer for question {i+1} in the answer key.")
|
158 |
return None, None
|
159 |
|
|
|
|
|
|
|
160 |
return quiz_data_list, answer_key_dict
|
161 |
|
162 |
|
|
|
169 |
if st.session_state.current_question_index < len(st.session_state.quiz_data):
|
170 |
question_data = st.session_state.quiz_data[st.session_state.current_question_index]
|
171 |
question_number = st.session_state.current_question_index + 1
|
172 |
+
st.markdown(f"**Question {question_number}:** {question_data['question']}") # Display question from parsed data
|
173 |
|
174 |
options_list = [f"{key}. {value}" for key, value in question_data['options'].items()]
|
175 |
user_choice = st.radio("Choose an answer:", options_list, key=f"q_{question_number}") # Unique key for radio buttons
|
|
|
251 |
else:
|
252 |
st.error("Failed to generate quiz content. Please try again or check your API key.")
|
253 |
|
|
|
|
|
|
|
|
|
254 |
# Quiz Display Logic
|
255 |
if st.session_state.quiz_data:
|
256 |
if not st.session_state.quiz_completed:
|