joshuarauh commited on
Commit
4a59421
·
verified ·
1 Parent(s): c6edca8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -2
app.py CHANGED
@@ -288,7 +288,7 @@ STRICT REQUIREMENTS:
288
  logger.debug("Successfully received response from Anthropic API")
289
 
290
  # Execute SymPy code and append results
291
- sympy_output = extract_and_run_sympy_code(response_text)
292
 
293
  if sympy_output:
294
  # Check if SymPy ran successfully
@@ -313,8 +313,42 @@ STRICT REQUIREMENTS:
313
 
314
  except Exception as e:
315
  logger.error(f"Error generating question: {str(e)}")
316
- return f"Error: {str(e)}", None, None
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  def extract_and_run_sympy_code(response_text):
319
  """
320
  Extract SymPy code from the response and execute it.
 
288
  logger.debug("Successfully received response from Anthropic API")
289
 
290
  # Execute SymPy code and append results
291
+ sympy_output = extract_and_run_sympy_code_simple(response_text)
292
 
293
  if sympy_output:
294
  # Check if SymPy ran successfully
 
313
 
314
  except Exception as e:
315
  logger.error(f"Error generating question: {str(e)}")
316
+ return f"Error: {str(e)}", None, Non
317
 
318
+ def extract_and_run_sympy_code_simple(response_text):
319
+ """
320
+ Extract SymPy code from the response and execute it.
321
+ Returns the output exactly as SymPy would produce it.
322
+ """
323
+ try:
324
+ # Extract code
325
+ sympy_start = response_text.find('```python')
326
+ if sympy_start == -1:
327
+ return "No SymPy code found in the response."
328
+
329
+ code_start = response_text.find('\n', sympy_start) + 1
330
+ code_end = response_text.find('```', code_start)
331
+ if code_end == -1:
332
+ return "Malformed SymPy code block."
333
+
334
+ sympy_code = response_text[code_start:code_end].strip()
335
+
336
+ # Set up basic SymPy environment
337
+ import io
338
+ import sympy
339
+ from contextlib import redirect_stdout
340
+
341
+ # Capture output
342
+ output_buffer = io.StringIO()
343
+ with redirect_stdout(output_buffer):
344
+ exec(sympy_code, {"sympy": sympy, "print": print})
345
+
346
+ return output_buffer.getvalue().strip() or "No output produced"
347
+
348
+ except Exception as e:
349
+ return f"Error executing SymPy code: {str(e)}"
350
+
351
+
352
  def extract_and_run_sympy_code(response_text):
353
  """
354
  Extract SymPy code from the response and execute it.