Update app.py
Browse files
app.py
CHANGED
@@ -46,56 +46,71 @@ def parse_quiz_content(quiz_content):
|
|
46 |
question_section = quiz_parts[0].strip()
|
47 |
answer_key_section = quiz_parts[1].strip()
|
48 |
|
49 |
-
# --- Split into blocks by double newline ---
|
50 |
-
blocks = re.split(r'\n\n+', question_section)
|
51 |
|
52 |
-
# --- Handle Potential Title (First Block) ---
|
53 |
if blocks:
|
54 |
first_block = blocks[0].strip()
|
55 |
-
if not re.
|
56 |
-
#
|
57 |
-
blocks = blocks[1:] # Slice to remove the first block (title)
|
58 |
-
# else: # No warning needed if it starts with "Question", it's likely question 1
|
59 |
|
60 |
|
61 |
# --- Process Question Blocks ---
|
|
|
|
|
62 |
for block in blocks:
|
63 |
block = block.strip()
|
64 |
-
if not block:
|
65 |
continue
|
66 |
|
67 |
lines = block.split('\n')
|
68 |
-
|
69 |
-
question_text = lines[0].strip() # Initially take the first line
|
70 |
-
if re.match(r'^Question \d+:', question_text, re.IGNORECASE): # If it's "Question N:"
|
71 |
-
if len(lines) > 1:
|
72 |
-
question_text = lines[1].strip() # Take the second line as the actual question
|
73 |
-
else:
|
74 |
-
question_text = "" # Handle case where "Question N:" line is there but no question text follows
|
75 |
-
|
76 |
-
|
77 |
options = {}
|
78 |
-
|
|
|
|
|
79 |
line = line.strip()
|
80 |
-
if
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
if match:
|
83 |
option_letter = match.group(1)
|
84 |
option_text = match.group(2).strip()
|
85 |
options[option_letter] = option_text
|
86 |
|
87 |
|
88 |
-
if question_text
|
89 |
questions.append({'question': question_text, 'options': options})
|
|
|
|
|
|
|
|
|
90 |
|
91 |
|
92 |
-
# --- Answer Key Parsing (
|
93 |
answer_lines = answer_key_section.strip().split('\n')
|
94 |
for line in answer_lines:
|
95 |
line = line.strip()
|
96 |
-
match = re.match(r'(\d+)\.\s*([A-D])
|
97 |
-
if not match: # If the above doesn't match, try to match without the closing parenthesis
|
98 |
-
match = re.match(r'(\d+)\.\s*([A-D])', line) # Match "1. B"
|
99 |
if match:
|
100 |
question_num = int(match.group(1)) - 1
|
101 |
correct_answer = match.group(2)
|
@@ -183,10 +198,10 @@ if topic:
|
|
183 |
prompt = f"""
|
184 |
Generate a multiple-choice quiz on the topic of "{topic}".
|
185 |
The quiz should have 5 questions.
|
186 |
-
|
187 |
-
|
188 |
Clearly indicate the correct answer for each question at the end in a section called "Answer Key:".
|
189 |
-
Format the quiz
|
190 |
"""
|
191 |
response = model.generate_content(prompt)
|
192 |
quiz_content = response.text
|
@@ -197,8 +212,8 @@ if topic:
|
|
197 |
|
198 |
parsed_quiz_data, answer_key = parse_quiz_content(quiz_content)
|
199 |
|
200 |
-
|
201 |
-
|
202 |
|
203 |
# --- END DEBUGGING PRINTS UNCOMMENTED ---
|
204 |
|
|
|
46 |
question_section = quiz_parts[0].strip()
|
47 |
answer_key_section = quiz_parts[1].strip()
|
48 |
|
49 |
+
# --- Split into blocks by double newline, more robust splitting ---
|
50 |
+
blocks = re.split(r'\n\n+', question_section)
|
51 |
|
52 |
+
# --- Handle Potential Title (First Block) - More robust title check ---
|
53 |
if blocks:
|
54 |
first_block = blocks[0].strip()
|
55 |
+
if not re.search(r'Question\s+\d+:', first_block, re.IGNORECASE): # Use re.search for more flexible title detection
|
56 |
+
blocks = blocks[1:] # Remove title block if it doesn't look like "Question 1:", etc.
|
|
|
|
|
57 |
|
58 |
|
59 |
# --- Process Question Blocks ---
|
60 |
+
question_number = 1 # Keep track of expected question number for better parsing
|
61 |
+
|
62 |
for block in blocks:
|
63 |
block = block.strip()
|
64 |
+
if not block: # Skip completely empty blocks
|
65 |
continue
|
66 |
|
67 |
lines = block.split('\n')
|
68 |
+
question_text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
options = {}
|
70 |
+
parsing_question_text = True # Flag to indicate we are parsing question text
|
71 |
+
|
72 |
+
for line_index, line in enumerate(lines):
|
73 |
line = line.strip()
|
74 |
+
if not line: # Skip empty lines within a question block
|
75 |
+
continue
|
76 |
+
|
77 |
+
if parsing_question_text:
|
78 |
+
if re.match(r'^Question\s+\d+:', line, re.IGNORECASE): # Check for "Question 1:", "Question 2:", etc.
|
79 |
+
if len(lines) > line_index + 1: # Check if there's a line after "Question N:"
|
80 |
+
question_text = lines[line_index + 1].strip() # Take the next line as question text
|
81 |
+
parsing_question_text = False # Stop parsing question text, move to options
|
82 |
+
else:
|
83 |
+
st.warning(f"Warning: Question label '{line}' found, but no question text followed.")
|
84 |
+
question_text = "" # No question text found after label
|
85 |
+
parsing_question_text = False # Move to options parsing even if question text missing
|
86 |
+
else:
|
87 |
+
# If the line is not "Question N:", and we are still parsing question text,
|
88 |
+
# it might be the question text itself (in case of format variation)
|
89 |
+
question_text = line # Consider this line as question text
|
90 |
+
parsing_question_text = False # Move to options parsing
|
91 |
+
|
92 |
+
|
93 |
+
elif re.match(r'^[A-D]\)\s(.+)', line): # Parse options
|
94 |
+
match = re.match(r'^([A-D])\)\s(.+)', line)
|
95 |
if match:
|
96 |
option_letter = match.group(1)
|
97 |
option_text = match.group(2).strip()
|
98 |
options[option_letter] = option_text
|
99 |
|
100 |
|
101 |
+
if question_text: # Only add question if we successfully extracted question text
|
102 |
questions.append({'question': question_text, 'options': options})
|
103 |
+
else:
|
104 |
+
st.warning(f"Warning: Could not parse question text for block (potential issue in question {question_number}). Block content:\n{block}")
|
105 |
+
|
106 |
+
question_number += 1 # Increment expected question number
|
107 |
|
108 |
|
109 |
+
# --- Answer Key Parsing (No change needed) ---
|
110 |
answer_lines = answer_key_section.strip().split('\n')
|
111 |
for line in answer_lines:
|
112 |
line = line.strip()
|
113 |
+
match = re.match(r'(\d+)\.\s*([A-D])', line)
|
|
|
|
|
114 |
if match:
|
115 |
question_num = int(match.group(1)) - 1
|
116 |
correct_answer = match.group(2)
|
|
|
198 |
prompt = f"""
|
199 |
Generate a multiple-choice quiz on the topic of "{topic}".
|
200 |
The quiz should have 5 questions.
|
201 |
+
Label each question as "Question 1:", "Question 2:", etc.
|
202 |
+
Provide four plausible options for each question, labeled A), B), C), and D).
|
203 |
Clearly indicate the correct answer for each question at the end in a section called "Answer Key:".
|
204 |
+
Format the quiz for easy reading.
|
205 |
"""
|
206 |
response = model.generate_content(prompt)
|
207 |
quiz_content = response.text
|
|
|
212 |
|
213 |
parsed_quiz_data, answer_key = parse_quiz_content(quiz_content)
|
214 |
|
215 |
+
st.write("### Parsed Quiz Data:") # Added Parsed Data debug print back
|
216 |
+
st.write(parsed_quiz_data) # Display the parsed data structure
|
217 |
|
218 |
# --- END DEBUGGING PRINTS UNCOMMENTED ---
|
219 |
|