""" project: lollms_webui file: shell_execution_engine.py author: ParisNeo description: This is a utility for executing python code """ from lollms_webui import LOLLMSWebUI from ascii_colors import get_trace_exception, trace_exception import time import subprocess import json from lollms.client_session import Client lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance() def execute_bash(code, client:Client, message_id, build_file=False): def spawn_process(code): """Executes Python code and returns the output as JSON.""" # Start the timer. start_time = time.time() # Create a temporary file. root_folder = client.discussion.discussion_folder root_folder.mkdir(parents=True,exist_ok=True) try: # Execute the Python code in a temporary file. process = subprocess.Popen( code, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=client.discussion.discussion_folder ) # Get the output and error from the process. output, error = process.communicate() except Exception as ex: # Stop the timer. execution_time = time.time() - start_time error_message = f"Error executing Python code: {ex}" error_json = {"output": "