Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -319,105 +319,50 @@ def extract_and_run_sympy_code(response_text):
|
|
319 |
Returns the execution output as a string.
|
320 |
"""
|
321 |
try:
|
322 |
-
logger.debug("Starting SymPy code extraction")
|
323 |
-
|
324 |
# Find the SymPy code block
|
325 |
sympy_start = response_text.find('```python')
|
326 |
if sympy_start == -1:
|
327 |
-
logger.debug("No SymPy code block found")
|
328 |
return "No SymPy code found in the response."
|
329 |
|
330 |
-
# Extract the code
|
331 |
code_start = response_text.find('\n', sympy_start) + 1
|
332 |
code_end = response_text.find('```', code_start)
|
333 |
if code_end == -1:
|
334 |
-
logger.debug("Malformed code block - couldn't find ending ```")
|
335 |
return "Malformed SymPy code block."
|
336 |
|
337 |
sympy_code = response_text[code_start:code_end].strip()
|
338 |
-
logger.debug(f"Extracted code:\n{sympy_code}")
|
339 |
-
|
340 |
-
# Remove sympy imports
|
341 |
-
original_lines = sympy_code.split('\n')
|
342 |
-
filtered_lines = [line for line in original_lines
|
343 |
-
if not line.strip().startswith('from sympy')
|
344 |
-
and not line.strip().startswith('import sympy')]
|
345 |
-
sympy_code = '\n'.join(filtered_lines)
|
346 |
-
logger.debug(f"Code after removing imports:\n{sympy_code}")
|
347 |
|
348 |
-
# Create
|
349 |
import io
|
350 |
import sys
|
351 |
from contextlib import redirect_stdout
|
352 |
-
output_buffer = io.StringIO()
|
353 |
|
354 |
-
#
|
355 |
-
|
356 |
-
|
357 |
-
# Create globals dict with SymPy module
|
358 |
-
globals_dict = {
|
359 |
-
'__builtins__': __builtins__,
|
360 |
-
'Symbol': sympy.Symbol,
|
361 |
-
'symbols': sympy.symbols,
|
362 |
-
'solve': sympy.solve,
|
363 |
-
'diff': sympy.diff,
|
364 |
-
'integrate': sympy.integrate,
|
365 |
-
'limit': sympy.limit,
|
366 |
-
'oo': sympy.oo,
|
367 |
-
'Sum': sympy.Sum,
|
368 |
-
'simplify': sympy.simplify,
|
369 |
-
'expand': sympy.expand,
|
370 |
-
'factor': sympy.factor,
|
371 |
-
'sin': sympy.sin,
|
372 |
-
'cos': sympy.cos,
|
373 |
-
'tan': sympy.tan,
|
374 |
-
'exp': sympy.exp,
|
375 |
-
'log': sympy.log,
|
376 |
-
'sqrt': sympy.sqrt,
|
377 |
-
'pi': sympy.pi
|
378 |
-
}
|
379 |
|
380 |
-
|
|
|
|
|
381 |
|
382 |
-
# Create locals dict to
|
383 |
local_vars = {}
|
384 |
|
385 |
-
# Execute the code
|
386 |
-
|
387 |
-
with redirect_stdout(output_buffer):
|
388 |
-
exec(sympy_code, globals_dict, local_vars)
|
389 |
-
stdout_output = output_buffer.getvalue()
|
390 |
-
logger.debug(f"Code execution stdout: {stdout_output}")
|
391 |
-
except Exception as e:
|
392 |
-
logger.error(f"Error during code execution: {str(e)}")
|
393 |
-
return f"Error executing code: {str(e)}"
|
394 |
|
395 |
-
#
|
396 |
-
|
|
|
|
|
|
|
|
|
|
|
397 |
|
398 |
-
|
399 |
-
if stdout_output.strip():
|
400 |
-
output_lines.extend(["", "Output:", stdout_output.strip()])
|
401 |
-
|
402 |
-
# Add variables
|
403 |
-
output_lines.extend(["", "Variables:"])
|
404 |
-
for name, value in local_vars.items():
|
405 |
-
if not name.startswith('__') and not hasattr(value, '__module__'):
|
406 |
-
output_lines.append(f"{name} = {value}")
|
407 |
-
|
408 |
-
# Join all lines
|
409 |
-
final_output = "\n".join(output_lines)
|
410 |
-
logger.debug(f"Final formatted output:\n{final_output}")
|
411 |
-
|
412 |
-
return final_output
|
413 |
|
414 |
except Exception as e:
|
415 |
-
logger.error(f"Top-level error in extract_and_run_sympy_code: {str(e)}")
|
416 |
-
import traceback
|
417 |
-
logger.error(f"Full traceback:\n{traceback.format_exc()}")
|
418 |
return f"Error executing SymPy code: {str(e)}"
|
419 |
-
|
420 |
-
|
421 |
# Create Gradio interface
|
422 |
with gr.Blocks() as interface:
|
423 |
gr.Markdown("# Advanced Mathematics Question Generator")
|
|
|
319 |
Returns the execution output as a string.
|
320 |
"""
|
321 |
try:
|
|
|
|
|
322 |
# Find the SymPy code block
|
323 |
sympy_start = response_text.find('```python')
|
324 |
if sympy_start == -1:
|
|
|
325 |
return "No SymPy code found in the response."
|
326 |
|
327 |
+
# Extract the code (excluding the ```python and ``` markers)
|
328 |
code_start = response_text.find('\n', sympy_start) + 1
|
329 |
code_end = response_text.find('```', code_start)
|
330 |
if code_end == -1:
|
|
|
331 |
return "Malformed SymPy code block."
|
332 |
|
333 |
sympy_code = response_text[code_start:code_end].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
334 |
|
335 |
+
# Create a string buffer to capture print output
|
336 |
import io
|
337 |
import sys
|
338 |
from contextlib import redirect_stdout
|
|
|
339 |
|
340 |
+
# Create string buffer for output
|
341 |
+
output_buffer = io.StringIO()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
|
343 |
+
# Create globals dict with all SymPy symbols
|
344 |
+
sympy_globals = {}
|
345 |
+
sympy_globals.update({name: getattr(sympy, name) for name in dir(sympy)})
|
346 |
|
347 |
+
# Create locals dict to capture new variables
|
348 |
local_vars = {}
|
349 |
|
350 |
+
# Execute the original code
|
351 |
+
exec(sympy_code, sympy_globals, local_vars)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
352 |
|
353 |
+
# Print all variables that were created (excluding built-ins and modules)
|
354 |
+
with redirect_stdout(output_buffer):
|
355 |
+
print("SymPy Calculation Results:")
|
356 |
+
print("-" * 25)
|
357 |
+
for var_name, value in local_vars.items():
|
358 |
+
if not var_name.startswith('__') and not hasattr(value, '__module__'):
|
359 |
+
print(f"{var_name}: {value}")
|
360 |
|
361 |
+
return output_buffer.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
|
363 |
except Exception as e:
|
|
|
|
|
|
|
364 |
return f"Error executing SymPy code: {str(e)}"
|
365 |
+
|
|
|
366 |
# Create Gradio interface
|
367 |
with gr.Blocks() as interface:
|
368 |
gr.Markdown("# Advanced Mathematics Question Generator")
|