HFUsman commited on
Commit
88d5467
·
verified ·
1 Parent(s): d9e829f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -24
app.py CHANGED
@@ -124,31 +124,32 @@ if uploaded_files or manual_input:
124
  mcqs = []
125
  for chunk in text_chunks:
126
  response = process_with_groq([
127
- {"role": "system", "content": "Generate multiple-choice questions for a lesson."},
128
- {"role": "user", "content": f"Context: {chunk}\n\nGenerate {num_questions} MCQs as a JSON array."}
129
  ])
130
- try:
131
- # Try to parse the response as JSON
132
- mcqs_data = json.loads(response)
133
- if isinstance(mcqs_data, list): # Ensure it's a valid JSON array
134
- mcqs.extend(mcqs_data)
135
- else:
136
- st.error(f"Invalid MCQs response: {response}")
137
- except json.JSONDecodeError:
138
- # If the response is not valid JSON, treat it as plain text
139
- st.warning(f"Error decoding JSON response. Attempting to extract MCQs from text.")
140
-
141
- # Extract questions manually from the response (basic heuristic approach)
142
- lines = response.split('\n')
143
- for line in lines:
144
- if line.startswith('Q'):
145
- mcqs.append({'question': line.strip(), 'options': []})
146
- elif line.startswith('-'):
147
- if mcqs:
148
- mcqs[-1]['options'].append(line.strip())
149
-
150
- if not mcqs:
151
- st.error(f"Failed to extract MCQs from the response: {response}")
 
152
 
153
  st.write("### Multiple Choice Questions")
154
  for idx, mcq in enumerate(mcqs):
 
124
  mcqs = []
125
  for chunk in text_chunks:
126
  response = process_with_groq([
127
+ {"role": "system", "content": "You are an AI assistant generating multiple-choice questions. Please provide each MCQ in a clearly structured format with the following fields: question, options (list of options), and answer. Separate each question with a newline."},
128
+ {"role": "user", "content": f"Context: {chunk}\n\nGenerate {num_questions} MCQs in a structured format."}
129
  ])
130
+
131
+ # Check if the response contains a valid structure (e.g., 'Q1:', 'Options:')
132
+ if "Q" in response and "Options:" in response:
133
+ # Split the response into individual MCQs using a delimiter like 'Q' or a newline
134
+ mcq_blocks = response.split("\n")
135
+ for block in mcq_blocks:
136
+ if block.strip().startswith("Q"):
137
+ question = block.strip()
138
+ options = []
139
+ answer = ""
140
+ # Extract options and answer
141
+ for option_line in mcq_blocks:
142
+ if option_line.startswith("Options:"):
143
+ options = option_line[len("Options:"):].split(" ")
144
+ if option_line.startswith("Answer:"):
145
+ answer = option_line[len("Answer:"):].strip()
146
+ mcqs.append({
147
+ "question": question,
148
+ "options": options,
149
+ "answer": answer
150
+ })
151
+ else:
152
+ st.error(f"Failed to parse structured MCQs from the response: {response}")
153
 
154
  st.write("### Multiple Choice Questions")
155
  for idx, mcq in enumerate(mcqs):