enhanced code execution

This commit is contained in:
Saifeddine ALOUI
2024-01-08 00:22:23 +01:00
parent de8adeb8d4
commit 53dbdeb49e
9 changed files with 229 additions and 207 deletions

View File

@ -6,26 +6,14 @@ description:
This is a utility for executing python code
"""
from fastapi import APIRouter, Request, routing
from lollms_webui import LOLLMSWebUI
from pydantic import BaseModel
from starlette.responses import StreamingResponse
from lollms.types import MSG_TYPE
from lollms.main_config import BaseConfig
from lollms.utilities import detect_antiprompt, remove_text_from_string, trace_exception, get_trace_exception
from ascii_colors import ASCIIColors
from api.db import DiscussionsDB
from pathlib import Path
from safe_store.text_vectorizer import TextVectorizer, VectorizationMethod, VisualizationMethod
import tqdm
from fastapi import FastAPI, UploadFile, File
import shutil
from ascii_colors import get_trace_exception, trace_exception
import time
import subprocess
import json
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
def execute_bash(lollmsElfServer, code, discussion_id, message_id):
def execute_bash(code, discussion_id, message_id):
def spawn_process(code):
"""Executes Python code and returns the output as JSON."""
@ -51,7 +39,7 @@ def execute_bash(lollmsElfServer, code, discussion_id, message_id):
execution_time = time.time() - start_time
error_message = f"Error executing Python code: {ex}"
error_json = {"output": "<div class='text-red-500'>"+str(ex)+"\n"+get_trace_exception(ex)+"</div>", "execution_time": execution_time}
return json.dumps(error_json)
return error_json
# Stop the timer.
execution_time = time.time() - start_time
@ -61,9 +49,9 @@ def execute_bash(lollmsElfServer, code, discussion_id, message_id):
# The child process threw an exception.
error_message = f"Error executing Python code: {error.decode('utf8')}"
error_json = {"output": "<div class='text-red-500'>"+error_message+"</div>", "execution_time": execution_time}
return json.dumps(error_json)
return error_json
# The child process was successful.
output_json = {"output": output.decode("utf8"), "execution_time": execution_time}
return json.dumps(output_json)
return output_json
return spawn_process(code)