Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -126,6 +126,56 @@ def save_to_temp_file(content, filename):
|
|
126 |
logger.error(f"Error saving temporary file: {str(e)}")
|
127 |
raise
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
def generate_question(subject, difficulty, question_type):
|
130 |
"""Generate a single math question"""
|
131 |
try:
|
@@ -161,87 +211,10 @@ def generate_question(subject, difficulty, question_type):
|
|
161 |
logger.debug(f"Selected topic: {selected_topic}")
|
162 |
|
163 |
difficulty_params = get_difficulty_parameters(difficulty)
|
|
|
164 |
|
165 |
-
if
|
166 |
-
|
167 |
-
The application question MUST:
|
168 |
-
- Present a real-world scenario or practical problem
|
169 |
-
- Require modeling the situation mathematically
|
170 |
-
- Connect abstract mathematical concepts to concrete situations
|
171 |
-
- Include realistic context and data
|
172 |
-
- Require students to:
|
173 |
-
1. Identify relevant mathematical concepts
|
174 |
-
2. Translate the practical problem into mathematical terms
|
175 |
-
3. Solve using appropriate mathematical techniques
|
176 |
-
4. Interpret the results in the context of the original problem
|
177 |
-
|
178 |
-
Example contexts might include:
|
179 |
-
- Physics applications (motion, forces, work)
|
180 |
-
- Engineering scenarios (optimization, rates of change)
|
181 |
-
- Economics problems (cost optimization, growth models)
|
182 |
-
- Biological systems (population growth, reaction rates)
|
183 |
-
- Business applications (profit maximization, inventory management)
|
184 |
-
- Social science applications (demographic models, social network analysis)
|
185 |
-
- Data science applications (regression, statistical analysis)
|
186 |
-
"""
|
187 |
-
if difficulty == 5:
|
188 |
-
system_prompt = f"""You are an expert mathematics professor creating a graduate-level exam question.
|
189 |
-
STRICT REQUIREMENTS:
|
190 |
-
1. Write exactly 1 graduate-level application question on {subject} covering {selected_topic}.
|
191 |
-
2. Advanced Difficulty Requirements:
|
192 |
-
This question must be suitable for PhD qualifying exams or advanced competitions.
|
193 |
-
MUST include:
|
194 |
-
- Novel applications of theoretical concepts to complex real-world scenarios
|
195 |
-
- Graduate-level mathematical reasoning in practical contexts
|
196 |
-
- Sophisticated modeling of real situations
|
197 |
-
- Creative problem-solving approaches
|
198 |
-
- Multiple stages of mathematical modeling and analysis
|
199 |
-
|
200 |
-
Follow these specific constraints:
|
201 |
-
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
|
202 |
-
{application_addition}
|
203 |
-
3. Style Reference:
|
204 |
-
Question should be {difficulty_params['example_style']}
|
205 |
-
4. The question MUST:
|
206 |
-
- Bridge multiple mathematical domains
|
207 |
-
- Require deep theoretical understanding
|
208 |
-
- Test mastery of advanced concepts
|
209 |
-
- Demand innovative solution approaches
|
210 |
-
5. For LaTeX formatting:
|
211 |
-
- Use $ for inline math
|
212 |
-
- Use $$ on separate lines for equations and solutions
|
213 |
-
- Put each solution step on its own line in $$ $$
|
214 |
-
- DO NOT use \\begin{{aligned}} or similar environments
|
215 |
-
6. Include a detailed solution with thorough explanations of:
|
216 |
-
- How to model the situation
|
217 |
-
- Why specific mathematical techniques were chosen
|
218 |
-
- How to interpret the results
|
219 |
-
7. Maintain clear, precise formatting"""
|
220 |
-
else:
|
221 |
-
system_prompt = f"""You are an expert mathematics professor creating a {difficulty_params['description']} exam question.
|
222 |
-
STRICT REQUIREMENTS:
|
223 |
-
1. Write exactly 1 application question on {subject} covering {selected_topic}.
|
224 |
-
2. Difficulty Level Guidelines:
|
225 |
-
{difficulty_params['description'].upper()}
|
226 |
-
Follow these specific constraints:
|
227 |
-
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
|
228 |
-
{application_addition}
|
229 |
-
3. Style Reference:
|
230 |
-
Question should be {difficulty_params['example_style']}
|
231 |
-
4. For LaTeX formatting:
|
232 |
-
- Use $ for inline math
|
233 |
-
- Use $$ on separate lines for equations and solutions
|
234 |
-
- Put each solution step on its own line in $$ $$
|
235 |
-
- DO NOT use \\begin{{aligned}} or similar environments
|
236 |
-
5. Include a detailed solution that explains:
|
237 |
-
- How to model the situation
|
238 |
-
- Why specific mathematical techniques were chosen
|
239 |
-
- How to interpret the results
|
240 |
-
6. Maintain clear formatting"""
|
241 |
-
else:
|
242 |
-
# Standard prompts for computation and proof questions
|
243 |
-
if difficulty == 5:
|
244 |
-
system_prompt = f"""You are an expert mathematics professor creating a graduate-level exam question.
|
245 |
STRICT REQUIREMENTS:
|
246 |
1. Write exactly 1 graduate-level {question_type} question on {subject} covering {selected_topic}.
|
247 |
2. Advanced Difficulty Requirements:
|
@@ -255,6 +228,9 @@ STRICT REQUIREMENTS:
|
|
255 |
|
256 |
Follow these specific constraints:
|
257 |
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
|
|
|
|
|
|
|
258 |
3. Style Reference:
|
259 |
Question should be {difficulty_params['example_style']}
|
260 |
4. The question MUST:
|
@@ -269,14 +245,17 @@ STRICT REQUIREMENTS:
|
|
269 |
- DO NOT use \\begin{{aligned}} or similar environments
|
270 |
6. Include a detailed solution with thorough explanations of advanced concepts used
|
271 |
7. Maintain clear, precise formatting"""
|
272 |
-
|
273 |
-
|
274 |
STRICT REQUIREMENTS:
|
275 |
1. Write exactly 1 {question_type} question on {subject} covering {selected_topic}.
|
276 |
2. Difficulty Level Guidelines:
|
277 |
{difficulty_params['description'].upper()}
|
278 |
Follow these specific constraints:
|
279 |
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
|
|
|
|
|
|
|
280 |
3. Style Reference:
|
281 |
Question should be {difficulty_params['example_style']}
|
282 |
4. For LaTeX formatting:
|
|
|
126 |
logger.error(f"Error saving temporary file: {str(e)}")
|
127 |
raise
|
128 |
|
129 |
+
def get_problem_type_addition(question_type):
|
130 |
+
"""Return specific requirements based on problem type"""
|
131 |
+
problem_type_additions = {
|
132 |
+
"application": """
|
133 |
+
The application question MUST:
|
134 |
+
- Present a real-world scenario or practical problem
|
135 |
+
- Require modeling the situation mathematically
|
136 |
+
- Connect abstract mathematical concepts to concrete situations
|
137 |
+
- Include realistic context and data
|
138 |
+
- Require students to:
|
139 |
+
1. Identify relevant mathematical concepts
|
140 |
+
2. Translate the practical problem into mathematical terms
|
141 |
+
3. Solve using appropriate mathematical techniques
|
142 |
+
4. Interpret the results in the context of the original problem
|
143 |
+
|
144 |
+
Example contexts might include:
|
145 |
+
- Physics applications (motion, forces, work)
|
146 |
+
- Engineering scenarios (optimization, rates of change)
|
147 |
+
- Economics problems (cost optimization, growth models)
|
148 |
+
- Biological systems (population growth, reaction rates)
|
149 |
+
- Business applications (profit maximization, inventory management)
|
150 |
+
- Social science applications (demographic models, social network analysis)
|
151 |
+
- Data science applications (regression, statistical analysis)
|
152 |
+
""",
|
153 |
+
"proof": """
|
154 |
+
The proof question MUST:
|
155 |
+
- Require a formal mathematical proof
|
156 |
+
- Focus on demonstrating logical reasoning
|
157 |
+
- Require justification for each step
|
158 |
+
- Emphasize theoretical understanding
|
159 |
+
|
160 |
+
The proof question MAY NOT:
|
161 |
+
- Include Real-world applications or scenarios
|
162 |
+
- Include Pure computation problems
|
163 |
+
- Ask only for numerical answers
|
164 |
+
""",
|
165 |
+
"computation": """
|
166 |
+
The computation question MUST:
|
167 |
+
- Require specific numerical or algebraic calculations
|
168 |
+
- Focus on mathematical techniques
|
169 |
+
- Have concrete answers in the form of numbers or algebraic expressions
|
170 |
+
- Test procedural knowledge
|
171 |
+
|
172 |
+
The computation question MAY NOT:
|
173 |
+
- Include extended real-world applications or scenarios
|
174 |
+
- Ask for a proof
|
175 |
+
"""
|
176 |
+
}
|
177 |
+
return problem_type_additions.get(question_type, "")
|
178 |
+
|
179 |
def generate_question(subject, difficulty, question_type):
|
180 |
"""Generate a single math question"""
|
181 |
try:
|
|
|
211 |
logger.debug(f"Selected topic: {selected_topic}")
|
212 |
|
213 |
difficulty_params = get_difficulty_parameters(difficulty)
|
214 |
+
problem_type_addition = get_problem_type_addition(question_type)
|
215 |
|
216 |
+
if difficulty == 5:
|
217 |
+
system_prompt = f"""You are an expert mathematics professor creating a graduate-level exam question.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
STRICT REQUIREMENTS:
|
219 |
1. Write exactly 1 graduate-level {question_type} question on {subject} covering {selected_topic}.
|
220 |
2. Advanced Difficulty Requirements:
|
|
|
228 |
|
229 |
Follow these specific constraints:
|
230 |
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
|
231 |
+
|
232 |
+
{problem_type_addition}
|
233 |
+
|
234 |
3. Style Reference:
|
235 |
Question should be {difficulty_params['example_style']}
|
236 |
4. The question MUST:
|
|
|
245 |
- DO NOT use \\begin{{aligned}} or similar environments
|
246 |
6. Include a detailed solution with thorough explanations of advanced concepts used
|
247 |
7. Maintain clear, precise formatting"""
|
248 |
+
else:
|
249 |
+
system_prompt = f"""You are an expert mathematics professor creating a {difficulty_params['description']} exam question.
|
250 |
STRICT REQUIREMENTS:
|
251 |
1. Write exactly 1 {question_type} question on {subject} covering {selected_topic}.
|
252 |
2. Difficulty Level Guidelines:
|
253 |
{difficulty_params['description'].upper()}
|
254 |
Follow these specific constraints:
|
255 |
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
|
256 |
+
|
257 |
+
{problem_type_addition}
|
258 |
+
|
259 |
3. Style Reference:
|
260 |
Question should be {difficulty_params['example_style']}
|
261 |
4. For LaTeX formatting:
|