Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -514,36 +514,58 @@ def extract_and_run_sympy_code_simple(response_text):
|
|
514 |
from contextlib import redirect_stdout
|
515 |
|
516 |
output_buffer = io.StringIO()
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
521 |
|
522 |
except Exception as e:
|
523 |
-
return f"Error
|
524 |
|
525 |
def check_and_resolve_discrepancy(initial_response, sympy_output):
|
526 |
"""
|
527 |
-
Compare the SymPy output with the initial response and resolve any discrepancies
|
528 |
-
|
529 |
"""
|
530 |
try:
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
Original solution:
|
543 |
{initial_response}
|
544 |
-
|
545 |
-
|
546 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
547 |
|
548 |
# Make API call for resolution
|
549 |
message = anthropic.messages.create(
|
@@ -560,13 +582,13 @@ Please maintain the same LaTeX formatting as the original solution."""
|
|
560 |
|
561 |
# Check if resolution contains new SymPy code
|
562 |
if "```python" in resolution_text:
|
563 |
-
new_sympy_output =
|
564 |
resolution_text += "\n\nNew SymPy Verification Results:\n```\n" + new_sympy_output + "\n```"
|
565 |
|
566 |
return resolution_text
|
567 |
except Exception as e:
|
568 |
logger.error(f"Error in discrepancy resolution: {str(e)}")
|
569 |
-
return
|
570 |
|
571 |
# Create Gradio interface
|
572 |
with gr.Blocks() as interface:
|
|
|
514 |
from contextlib import redirect_stdout
|
515 |
|
516 |
output_buffer = io.StringIO()
|
517 |
+
try:
|
518 |
+
with redirect_stdout(output_buffer):
|
519 |
+
exec(modified_code, globals_dict)
|
520 |
+
return output_buffer.getvalue().strip() or "No output produced"
|
521 |
+
except NonInvertibleMatrixError:
|
522 |
+
return "ERROR_NONINVERTIBLE_MATRIX: The system of equations has either no solution or infinitely many solutions. Matrix is singular (determinant = 0)."
|
523 |
+
except Exception as e:
|
524 |
+
if "det == 0" in str(e) or "not invertible" in str(e):
|
525 |
+
return "ERROR_NONINVERTIBLE_MATRIX: The system of equations has either no solution or infinitely many solutions. Matrix is singular (determinant = 0)."
|
526 |
+
return f"Error executing SymPy code: {str(e)}"
|
527 |
|
528 |
except Exception as e:
|
529 |
+
return f"Error in code extraction: {str(e)}"
|
530 |
|
531 |
def check_and_resolve_discrepancy(initial_response, sympy_output):
|
532 |
"""
|
533 |
+
Compare the SymPy output with the initial response and resolve any discrepancies.
|
534 |
+
Now handles singular matrix cases specifically.
|
535 |
"""
|
536 |
try:
|
537 |
+
if "ERROR_NONINVERTIBLE_MATRIX" in sympy_output:
|
538 |
+
resolution_prompt = f"""The original solution claims to solve a system of linear equations, but SymPy indicates the coefficient matrix is not invertible (singular). This means:
|
539 |
+
1. The system has either no solution or infinitely many solutions
|
540 |
+
2. The vectors are linearly dependent
|
541 |
+
3. The solution given cannot be unique
|
542 |
+
|
543 |
+
Please:
|
544 |
+
1. Verify if the system has no solutions or infinitely many solutions
|
545 |
+
2. If solutions exist, express them in parametric form
|
546 |
+
3. Provide a complete revised solution that correctly addresses this case
|
547 |
+
|
548 |
Original solution:
|
549 |
{initial_response}
|
550 |
+
|
551 |
+
Please maintain the same LaTeX formatting as the original solution and include correct SymPy code that uses row reduction instead of matrix inversion."""
|
552 |
+
else:
|
553 |
+
resolution_prompt = f"""Here is a mathematics question with two answers.
|
554 |
+
The first, called Original solution, is a complete solution.
|
555 |
+
The second, called SymPy Verification, will only provide the final answer.
|
556 |
+
|
557 |
+
If the SymPy Verification answer is consistent with the final answer Original solution,
|
558 |
+
then please say that they are consistent and briefly explain why.
|
559 |
+
|
560 |
+
If the two answers are inconsistent with each other then please:
|
561 |
+
1. Identify which solution is correct
|
562 |
+
2. Explain the error in the incorrect solution
|
563 |
+
3. Provide a revised complete solution that fixes any errors
|
564 |
+
Original solution:
|
565 |
+
{initial_response}
|
566 |
+
SymPy Verification Results:
|
567 |
+
{sympy_output}
|
568 |
+
Please maintain the same LaTeX formatting as the original solution."""
|
569 |
|
570 |
# Make API call for resolution
|
571 |
message = anthropic.messages.create(
|
|
|
582 |
|
583 |
# Check if resolution contains new SymPy code
|
584 |
if "```python" in resolution_text:
|
585 |
+
new_sympy_output = extract_and_run_sympy_code_simple(resolution_text)
|
586 |
resolution_text += "\n\nNew SymPy Verification Results:\n```\n" + new_sympy_output + "\n```"
|
587 |
|
588 |
return resolution_text
|
589 |
except Exception as e:
|
590 |
logger.error(f"Error in discrepancy resolution: {str(e)}")
|
591 |
+
return f"Error in resolution: {str(e)}"
|
592 |
|
593 |
# Create Gradio interface
|
594 |
with gr.Blocks() as interface:
|