Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -355,115 +355,7 @@ def extract_and_run_sympy_code_simple(response_text):
|
|
355 |
|
356 |
except Exception as e:
|
357 |
return f"Error executing SymPy code: {str(e)}"
|
358 |
-
|
359 |
-
|
360 |
-
def extract_and_run_sympy_code(response_text):
|
361 |
-
"""
|
362 |
-
Extract SymPy code from the response and execute it.
|
363 |
-
Returns only the essential calculation results as a string.
|
364 |
-
"""
|
365 |
-
try:
|
366 |
-
# Find the SymPy code block
|
367 |
-
sympy_start = response_text.find('```python')
|
368 |
-
if sympy_start == -1:
|
369 |
-
return "No SymPy code found in the response."
|
370 |
-
|
371 |
-
# Extract the code (excluding the ```python and ``` markers)
|
372 |
-
code_start = response_text.find('\n', sympy_start) + 1
|
373 |
-
code_end = response_text.find('```', code_start)
|
374 |
-
if code_end == -1:
|
375 |
-
return "Malformed SymPy code block."
|
376 |
-
|
377 |
-
sympy_code = response_text[code_start:code_end].strip()
|
378 |
-
|
379 |
-
if "print" not in sympy_code and any(x in sympy_code for x in ['diff', 'integrate', 'solve']):
|
380 |
-
lines = sympy_code.split('\n')
|
381 |
-
modified_lines = []
|
382 |
-
for line in lines:
|
383 |
-
modified_lines.append(line)
|
384 |
-
# If line assigns a value and looks like it creates an expression
|
385 |
-
if ('=' in line and '#' not in line and 'import' not in line
|
386 |
-
and any(x in line for x in ['diff', 'integrate', 'solve', 'sqrt', 'log'])):
|
387 |
-
var_name = line.split('=')[0].strip()
|
388 |
-
modified_lines.append(f"print('{var_name} = ', {var_name})")
|
389 |
-
sympy_code = '\n'.join(modified_lines)
|
390 |
-
|
391 |
-
# Import necessary modules
|
392 |
-
import io
|
393 |
-
import sympy
|
394 |
-
from contextlib import redirect_stdout
|
395 |
-
|
396 |
-
# Create a string buffer to capture print output
|
397 |
-
output_buffer = io.StringIO()
|
398 |
-
|
399 |
-
# Create globals dict with minimal SymPy elements
|
400 |
-
sympy_globals = {
|
401 |
-
"Symbol": sympy.Symbol,
|
402 |
-
"symbols": sympy.symbols,
|
403 |
-
"solve": sympy.solve,
|
404 |
-
"integrate": sympy.integrate,
|
405 |
-
"diff": sympy.diff,
|
406 |
-
"limit": sympy.limit,
|
407 |
-
"sqrt": sympy.sqrt,
|
408 |
-
"exp": sympy.exp,
|
409 |
-
"simplify": sympy.simplify,
|
410 |
-
"expand": sympy.expand,
|
411 |
-
"log": sympy.log,
|
412 |
-
"print": print, # Allow print statements
|
413 |
-
"pi": sympy.pi,
|
414 |
-
"N": sympy.N,
|
415 |
-
"evalf": lambda x: x.evalf()
|
416 |
-
}
|
417 |
-
|
418 |
-
# Create locals dict to capture new variables
|
419 |
-
local_vars = {}
|
420 |
-
|
421 |
-
# Execute the code and capture output
|
422 |
-
with redirect_stdout(output_buffer):
|
423 |
-
exec(sympy_code, sympy_globals, local_vars)
|
424 |
-
|
425 |
-
# Get the printed output
|
426 |
-
printed_output = output_buffer.getvalue().strip()
|
427 |
-
|
428 |
-
# Initialize result string
|
429 |
-
result = []
|
430 |
-
|
431 |
-
# Add numerical and symbolic calculation results
|
432 |
-
for name, value in local_vars.items():
|
433 |
-
# Skip private variables and functions
|
434 |
-
if name.startswith('__') or callable(value) or isinstance(value, type):
|
435 |
-
continue
|
436 |
-
|
437 |
-
# Skip module references
|
438 |
-
if hasattr(value, '__module__'):
|
439 |
-
continue
|
440 |
|
441 |
-
try:
|
442 |
-
# If it's a sympy expression or numerical result
|
443 |
-
if isinstance(value, (sympy.Basic, int, float)) or hasattr(value, 'free_symbols'):
|
444 |
-
result.append(f"{name} = {value}")
|
445 |
-
# If it's a list of solutions from solve()
|
446 |
-
elif isinstance(value, list):
|
447 |
-
result.append(f"{name} = {value}")
|
448 |
-
# If it's a list or tuple of sympy expressions
|
449 |
-
elif isinstance(value, (list, tuple)) and all(isinstance(x, (sympy.Basic, int, float)) for x in value):
|
450 |
-
result.append(f"{name} = {value}")
|
451 |
-
except:
|
452 |
-
continue
|
453 |
-
|
454 |
-
# Add printed output if it exists and isn't just whitespace
|
455 |
-
if printed_output and not printed_output.isspace():
|
456 |
-
result.insert(0, printed_output)
|
457 |
-
|
458 |
-
# If we have no results, provide a message
|
459 |
-
if not result:
|
460 |
-
return "No calculation results produced."
|
461 |
-
|
462 |
-
return "\n".join(result)
|
463 |
-
|
464 |
-
except Exception as e:
|
465 |
-
return f"Error executing SymPy code: {str(e)}"
|
466 |
-
|
467 |
def check_and_resolve_discrepancy(initial_response, sympy_output):
|
468 |
"""
|
469 |
Compare the SymPy output with the initial response and resolve any discrepancies
|
|
|
355 |
|
356 |
except Exception as e:
|
357 |
return f"Error executing SymPy code: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
359 |
def check_and_resolve_discrepancy(initial_response, sympy_output):
|
360 |
"""
|
361 |
Compare the SymPy output with the initial response and resolve any discrepancies
|