joshuarauh commited on
Commit
56c0514
·
verified ·
1 Parent(s): b3ff73e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py CHANGED
@@ -7,6 +7,7 @@ import random
7
  import logging
8
  import tempfile
9
  from pathlib import Path
 
10
 
11
  # Set up logging
12
  logging.basicConfig(
@@ -302,6 +303,58 @@ STRICT REQUIREMENTS:
302
  logger.error(f"Error generating question: {str(e)}")
303
  return f"Error: {str(e)}", None, None
304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  # Create Gradio interface
306
  with gr.Blocks() as interface:
307
  gr.Markdown("# Advanced Mathematics Question Generator")
 
7
  import logging
8
  import tempfile
9
  from pathlib import Path
10
+ from sympy import *
11
 
12
  # Set up logging
13
  logging.basicConfig(
 
303
  logger.error(f"Error generating question: {str(e)}")
304
  return f"Error: {str(e)}", None, None
305
 
306
+ from sympy import * # Import all SymPy functionality
307
+
308
+ def extract_and_run_sympy_code(response_text):
309
+ """
310
+ Extract SymPy code from the response and execute it.
311
+ Returns the execution output as a string.
312
+ """
313
+ try:
314
+ # Find the SymPy code block
315
+ sympy_start = response_text.find('```python')
316
+ if sympy_start == -1:
317
+ return "No SymPy code found in the response."
318
+
319
+ # Extract the code (excluding the ```python and ``` markers)
320
+ code_start = response_text.find('\n', sympy_start) + 1
321
+ code_end = response_text.find('```', code_start)
322
+ if code_end == -1:
323
+ return "Malformed SymPy code block."
324
+
325
+ sympy_code = response_text[code_start:code_end].strip()
326
+
327
+ # Create a string buffer to capture print output
328
+ import io
329
+ import sys
330
+ from contextlib import redirect_stdout
331
+
332
+ # Create string buffer for output
333
+ output_buffer = io.StringIO()
334
+
335
+ # Create globals dict with all SymPy symbols
336
+ sympy_globals = {}
337
+ sympy_globals.update({name: getattr(sympy, name) for name in dir(sympy)})
338
+
339
+ # Create locals dict to capture new variables
340
+ local_vars = {}
341
+
342
+ # Execute the original code
343
+ exec(sympy_code, sympy_globals, local_vars)
344
+
345
+ # Print all variables that were created (excluding built-ins and modules)
346
+ with redirect_stdout(output_buffer):
347
+ print("SymPy Calculation Results:")
348
+ print("-" * 25)
349
+ for var_name, value in local_vars.items():
350
+ if not var_name.startswith('__') and not hasattr(value, '__module__'):
351
+ print(f"{var_name}: {value}")
352
+
353
+ return output_buffer.getvalue()
354
+
355
+ except Exception as e:
356
+ return f"Error executing SymPy code: {str(e)}"
357
+
358
  # Create Gradio interface
359
  with gr.Blocks() as interface:
360
  gr.Markdown("# Advanced Mathematics Question Generator")