HFUsman commited on
Commit
171d39f
·
verified ·
1 Parent(s): 9cc493c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -82,17 +82,27 @@ def generate_mcqs(text, num_questions):
82
  raw_response = response.choices[0].message.content
83
  st.write(f"Raw Response: {raw_response}") # Display the raw response in Streamlit for debugging
84
 
85
- # Attempt to parse the response as JSON
 
 
 
86
  try:
87
- # The model should return a JSON-formatted string
88
- mcqs = json.loads(raw_response)
89
  if isinstance(mcqs, list):
90
- return mcqs
 
 
 
 
 
 
 
91
  else:
92
  st.error("The generated MCQs are not in the expected format.")
93
  return []
94
  except json.JSONDecodeError as e:
95
- st.error(f"Error decoding MCQs: {e}")
96
  return []
97
  except Exception as e:
98
  st.error(f"Unexpected error: {e}")
 
82
  raw_response = response.choices[0].message.content
83
  st.write(f"Raw Response: {raw_response}") # Display the raw response in Streamlit for debugging
84
 
85
+ # Clean any unwanted characters (e.g., extra spaces or newlines) that might cause parsing issues
86
+ cleaned_response = raw_response.strip()
87
+
88
+ # Attempt to parse the cleaned response as JSON
89
  try:
90
+ # Load the JSON response
91
+ mcqs = json.loads(cleaned_response) # Attempt to parse the cleaned response
92
  if isinstance(mcqs, list):
93
+ # Reformat the MCQs into the desired format
94
+ formatted_mcqs = []
95
+ for i, mcq in enumerate(mcqs, 1):
96
+ question = mcq['question']
97
+ options = "\n".join([f"{chr(97 + idx)}) {option}" for idx, option in enumerate(mcq['options'])])
98
+ answer = mcq['answer']
99
+ formatted_mcqs.append(f"**Q{i}. {question}**\n{options}\n\nAnswer: {answer}\n")
100
+ return formatted_mcqs
101
  else:
102
  st.error("The generated MCQs are not in the expected format.")
103
  return []
104
  except json.JSONDecodeError as e:
105
+ st.error(f"Error decoding MCQs: {e}. Raw response: {cleaned_response}")
106
  return []
107
  except Exception as e:
108
  st.error(f"Unexpected error: {e}")