Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -316,7 +316,8 @@ STRICT REQUIREMENTS: | |
| 316 | 
             
            def extract_and_run_sympy_code(response_text):
         | 
| 317 | 
             
                """
         | 
| 318 | 
             
                Extract SymPy code from the response and execute it.
         | 
| 319 | 
            -
                Returns the  | 
|  | |
| 320 | 
             
                """
         | 
| 321 | 
             
                try:
         | 
| 322 | 
             
                    # Find the SymPy code block
         | 
| @@ -340,29 +341,64 @@ def extract_and_run_sympy_code(response_text): | |
| 340 | 
             
                    # Create a string buffer to capture print output
         | 
| 341 | 
             
                    output_buffer = io.StringIO()
         | 
| 342 |  | 
| 343 | 
            -
                    # Create globals dict with SymPy  | 
| 344 | 
            -
                    sympy_globals = { | 
| 345 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 346 |  | 
| 347 | 
             
                    # Create locals dict to capture new variables
         | 
| 348 | 
             
                    local_vars = {}
         | 
| 349 |  | 
| 350 | 
            -
                    #  | 
| 351 | 
             
                    with redirect_stdout(output_buffer):
         | 
| 352 | 
             
                        exec(sympy_code, sympy_globals, local_vars)
         | 
| 353 |  | 
| 354 | 
            -
                    #  | 
| 355 | 
            -
                    output_buffer. | 
| 356 | 
            -
                     | 
| 357 | 
            -
                     | 
| 358 | 
            -
             | 
| 359 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 360 |  | 
| 361 | 
            -
                     | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 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")
         | 
|  | |
| 316 | 
             
            def extract_and_run_sympy_code(response_text):
         | 
| 317 | 
             
                """
         | 
| 318 | 
             
                Extract SymPy code from the response and execute it.
         | 
| 319 | 
            +
                Returns only the essential calculation results as a string.
         | 
| 320 | 
            +
                Filters out debugging info and module imports.
         | 
| 321 | 
             
                """
         | 
| 322 | 
             
                try:
         | 
| 323 | 
             
                    # Find the SymPy code block
         | 
|  | |
| 341 | 
             
                    # Create a string buffer to capture print output
         | 
| 342 | 
             
                    output_buffer = io.StringIO()
         | 
| 343 |  | 
| 344 | 
            +
                    # Create a clean globals dict with only essential SymPy components
         | 
| 345 | 
            +
                    sympy_globals = {
         | 
| 346 | 
            +
                        "Symbol": sympy.Symbol,
         | 
| 347 | 
            +
                        "symbols": sympy.symbols,
         | 
| 348 | 
            +
                        "solve": sympy.solve,
         | 
| 349 | 
            +
                        "integrate": sympy.integrate,
         | 
| 350 | 
            +
                        "diff": sympy.diff,
         | 
| 351 | 
            +
                        "limit": sympy.limit,
         | 
| 352 | 
            +
                        "sqrt": sympy.sqrt,
         | 
| 353 | 
            +
                        "simplify": sympy.simplify,
         | 
| 354 | 
            +
                        "expand": sympy.expand
         | 
| 355 | 
            +
                    }
         | 
| 356 |  | 
| 357 | 
             
                    # Create locals dict to capture new variables
         | 
| 358 | 
             
                    local_vars = {}
         | 
| 359 |  | 
| 360 | 
            +
                    # Execute the code and capture output
         | 
| 361 | 
             
                    with redirect_stdout(output_buffer):
         | 
| 362 | 
             
                        exec(sympy_code, sympy_globals, local_vars)
         | 
| 363 |  | 
| 364 | 
            +
                    # Get the printed output
         | 
| 365 | 
            +
                    printed_output = output_buffer.getvalue().strip()
         | 
| 366 | 
            +
                    
         | 
| 367 | 
            +
                    # Filter the local variables to only include calculation results
         | 
| 368 | 
            +
                    # Exclude built-in variables, modules, and functions
         | 
| 369 | 
            +
                    calculation_results = {
         | 
| 370 | 
            +
                        name: value for name, value in local_vars.items()
         | 
| 371 | 
            +
                        if (
         | 
| 372 | 
            +
                            not name.startswith('__') and
         | 
| 373 | 
            +
                            not hasattr(value, '__call__') and  # Exclude functions
         | 
| 374 | 
            +
                            not hasattr(value, '__module__')    # Exclude modules
         | 
| 375 | 
            +
                        )
         | 
| 376 | 
            +
                    }
         | 
| 377 | 
            +
                    
         | 
| 378 | 
            +
                    # Build the final output string
         | 
| 379 | 
            +
                    result_parts = []
         | 
| 380 |  | 
| 381 | 
            +
                    # Add any printed output first
         | 
| 382 | 
            +
                    if printed_output:
         | 
| 383 | 
            +
                        result_parts.append(printed_output)
         | 
| 384 | 
            +
                    
         | 
| 385 | 
            +
                    # Add calculation results if there are any
         | 
| 386 | 
            +
                    if calculation_results:
         | 
| 387 | 
            +
                        if printed_output:  # Add a separator if we already have printed output
         | 
| 388 | 
            +
                            result_parts.append("\nCalculation Results:")
         | 
| 389 | 
            +
                            result_parts.append("-" * 20)
         | 
| 390 | 
            +
                        for var_name, value in calculation_results.items():
         | 
| 391 | 
            +
                            result_parts.append(f"{var_name} = {value}")
         | 
| 392 | 
            +
                    
         | 
| 393 | 
            +
                    # If we have no results at all, provide a message
         | 
| 394 | 
            +
                    if not result_parts:
         | 
| 395 | 
            +
                        return "No calculation results produced."
         | 
| 396 | 
            +
                    
         | 
| 397 | 
            +
                    return "\n".join(result_parts)
         | 
| 398 |  | 
| 399 | 
             
                except Exception as e:
         | 
| 400 | 
             
                    return f"Error executing SymPy code: {str(e)}"
         | 
| 401 | 
            +
                    
         | 
| 402 | 
             
            # Create Gradio interface
         | 
| 403 | 
             
            with gr.Blocks() as interface:
         | 
| 404 | 
             
                gr.Markdown("# Advanced Mathematics Question Generator")
         |