Spaces:
Running
Running
capradeepgujaran
commited on
Commit
•
5be175f
1
Parent(s):
ee5354f
Update app.py
Browse files
app.py
CHANGED
@@ -70,29 +70,39 @@ class QuizGenerator:
|
|
70 |
raise QuizGenerationError(f"Failed to generate questions: {str(e)}")
|
71 |
|
72 |
def _create_prompt(self, text: str, num_questions: int) -> str:
|
73 |
-
"""Create a simple, clear prompt optimized for
|
74 |
-
return f"""Create {num_questions} multiple choice questions
|
75 |
-
[
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
def _parse_response(self, response_text: str) -> List[Dict]:
|
98 |
"""Parse response with improved error handling"""
|
@@ -100,7 +110,7 @@ Text to use:
|
|
100 |
# Clean up the response text
|
101 |
cleaned = response_text.strip()
|
102 |
|
103 |
-
# Remove
|
104 |
cleaned = cleaned.replace('```json', '').replace('```', '').strip()
|
105 |
|
106 |
# Find the JSON array
|
@@ -112,35 +122,40 @@ Text to use:
|
|
112 |
|
113 |
json_str = cleaned[start:end]
|
114 |
|
115 |
-
#
|
116 |
-
json_str = re.sub(r',(\s*})', r'\1', json_str)
|
117 |
-
json_str = re.sub(r',(\s*])', r'\1', json_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
-
# Try to parse the cleaned JSON
|
120 |
try:
|
121 |
return json.loads(json_str)
|
122 |
except json.JSONDecodeError:
|
123 |
-
#
|
124 |
-
|
125 |
-
|
|
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
except Exception as e:
|
128 |
print(f"Parse error details: {str(e)}")
|
129 |
print(f"Attempted to parse: {response_text}")
|
130 |
-
|
131 |
-
# Last resort: try to fix the JSON manually
|
132 |
-
try:
|
133 |
-
# Remove any trailing commas and fix newlines
|
134 |
-
fixed = re.sub(r',(\s*[}\]])', r'\1', response_text)
|
135 |
-
fixed = fixed.replace('}\n{', '},{')
|
136 |
-
fixed = fixed.strip()
|
137 |
-
if not fixed.startswith('['):
|
138 |
-
fixed = '[' + fixed
|
139 |
-
if not fixed.endswith(']'):
|
140 |
-
fixed = fixed + ']'
|
141 |
-
return json.loads(fixed)
|
142 |
-
except:
|
143 |
-
raise ValueError(f"Failed to parse response: {str(e)}")
|
144 |
|
145 |
def _validate_questions(self, questions: List[Dict], num_questions: int) -> List[Question]:
|
146 |
"""Validate questions with strict checking"""
|
|
|
70 |
raise QuizGenerationError(f"Failed to generate questions: {str(e)}")
|
71 |
|
72 |
def _create_prompt(self, text: str, num_questions: int) -> str:
|
73 |
+
"""Create a simple, clear prompt optimized for gemma2-9b-it"""
|
74 |
+
return f"""Create exactly {num_questions} multiple choice questions based on this text. Return your response in this exact JSON format with no additional text or formatting:
|
75 |
+
[{{
|
76 |
+
"question": "Write the first question here?",
|
77 |
+
"options": [
|
78 |
+
"First option",
|
79 |
+
"Second option",
|
80 |
+
"Third option",
|
81 |
+
"Fourth option"
|
82 |
+
],
|
83 |
+
"correct_answer": 0
|
84 |
+
}},{{
|
85 |
+
"question": "Write the second question here?",
|
86 |
+
"options": [
|
87 |
+
"First option",
|
88 |
+
"Second option",
|
89 |
+
"Third option",
|
90 |
+
"Fourth option"
|
91 |
+
],
|
92 |
+
"correct_answer": 0
|
93 |
+
}}]
|
94 |
+
|
95 |
+
Important formatting rules:
|
96 |
+
1. The JSON must start with [ and end with ]
|
97 |
+
2. Each question object must be wrapped in {{ }}
|
98 |
+
3. Questions must be separated by commas
|
99 |
+
4. No trailing commas after the last question
|
100 |
+
5. Each question must have exactly 4 options
|
101 |
+
6. correct_answer must be a number (0, 1, 2, or 3)
|
102 |
+
7. Return only the JSON with no other text
|
103 |
+
|
104 |
+
Text to use:
|
105 |
+
{text.strip()}"""
|
106 |
|
107 |
def _parse_response(self, response_text: str) -> List[Dict]:
|
108 |
"""Parse response with improved error handling"""
|
|
|
110 |
# Clean up the response text
|
111 |
cleaned = response_text.strip()
|
112 |
|
113 |
+
# Remove markdown formatting
|
114 |
cleaned = cleaned.replace('```json', '').replace('```', '').strip()
|
115 |
|
116 |
# Find the JSON array
|
|
|
122 |
|
123 |
json_str = cleaned[start:end]
|
124 |
|
125 |
+
# Fix common JSON formatting issues
|
126 |
+
json_str = re.sub(r',(\s*})', r'\1', json_str) # Remove trailing commas in objects
|
127 |
+
json_str = re.sub(r',(\s*])', r'\1', json_str) # Remove trailing commas in arrays
|
128 |
+
json_str = re.sub(r'}\s*{', '},{', json_str) # Fix spacing between objects
|
129 |
+
json_str = re.sub(r'\n\s*\n', '\n', json_str) # Remove extra newlines
|
130 |
+
|
131 |
+
# Fix missing commas between objects
|
132 |
+
json_str = re.sub(r'}\s*{', '},{', json_str)
|
133 |
+
|
134 |
+
# Ensure array brackets
|
135 |
+
if not json_str.startswith('['):
|
136 |
+
json_str = '[' + json_str
|
137 |
+
if not json_str.endswith(']'):
|
138 |
+
json_str = json_str + ']'
|
139 |
|
|
|
140 |
try:
|
141 |
return json.loads(json_str)
|
142 |
except json.JSONDecodeError:
|
143 |
+
# Try fixing any remaining issues
|
144 |
+
fixed_str = re.sub(r'(["\d\w])\s*}', r'\1}', json_str) # Fix spacing before closing braces
|
145 |
+
fixed_str = re.sub(r'\[\s*{', '[{', fixed_str) # Fix spacing after opening bracket
|
146 |
+
fixed_str = re.sub(r'}\s*]', '}]', fixed_str) # Fix spacing before closing bracket
|
147 |
|
148 |
+
try:
|
149 |
+
return json.loads(fixed_str)
|
150 |
+
except:
|
151 |
+
# Last resort: Try using ast.literal_eval
|
152 |
+
import ast
|
153 |
+
return ast.literal_eval(fixed_str)
|
154 |
+
|
155 |
except Exception as e:
|
156 |
print(f"Parse error details: {str(e)}")
|
157 |
print(f"Attempted to parse: {response_text}")
|
158 |
+
raise ValueError(f"Failed to parse response: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
def _validate_questions(self, questions: List[Dict], num_questions: int) -> List[Question]:
|
161 |
"""Validate questions with strict checking"""
|