EdBoy2202 commited on
Commit
2be75a5
·
verified ·
1 Parent(s): 82b0b01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -30
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) # Split by one or more double newlines
51
 
52
- # --- Handle Potential Title (First Block) ---
53
  if blocks:
54
  first_block = blocks[0].strip()
55
- if not re.match(r'^Question \d+:', first_block, re.IGNORECASE): # Check if the first block DOES NOT start with "Question 1:", etc.
56
- # Assume the first block is the title and remove it
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
- # --- First line might be "Question N:", extract question text from the next line ---
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
- for line in lines[1:]: # Start from the second line to parse options (or from line 2 if "Question N:" was on line 1)
 
 
79
  line = line.strip()
80
- if re.match(r'^[A-D]\)\s(.+)', line): # Options are like "A) 4" - Capture text after "A) "
81
- match = re.match(r'^([A-D])\)\s(.+)', line) # Capture letter and option text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 and not question_text.lower().endswith("quiz"): # Avoid title as question, case-insensitive check
89
  questions.append({'question': question_text, 'options': options})
 
 
 
 
90
 
91
 
92
- # --- Answer Key Parsing (Adjusted to only capture letter) ---
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])\)', line) # Match "1. B)" or "1. B" - capture letter only
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
- For each question, label questions as "Question 1:", "Question 2:", etc.
187
- For each question, provide four plausible options labeled A), B), C), and D).
188
  Clearly indicate the correct answer for each question at the end in a section called "Answer Key:".
189
- Format the quiz clearly for easy reading.
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
- # st.write("### Parsed Quiz Data:")
201
- # st.write(parsed_quiz_data) # Display the parsed data structure
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