diff --git a/lollms/apps/playground2/__init__.py b/lollms/apps/playground2/__init__.py index b900cdf..c57675f 100644 --- a/lollms/apps/playground2/__init__.py +++ b/lollms/apps/playground2/__init__.py @@ -1,5 +1,7 @@ -from flask import Flask, send_from_directory, request +from flask import Flask, send_from_directory, request, jsonify +from lollms.helpers import get_trace_exception import json +import shutil import os app = Flask(__name__, static_folder='dist/') @@ -33,6 +35,65 @@ def save_preset(): return "Preset saved successfully!" +@app.route("/execute_python_code", methods=["POST"]) +def execute_python_code(): + """Executes Python code and returns the output.""" + data = request.get_json() + code = data["code"] + # Import the necessary modules. + import io + import sys + import time + + # Create a Python interpreter. + interpreter = io.StringIO() + sys.stdout = interpreter + + # Execute the code. + start_time = time.time() + try: + exec(code) + # Get the output. + output = interpreter.getvalue() + except Exception as ex: + output = str(ex)+"\n"+get_trace_exception(ex) + end_time = time.time() + + return jsonify({"output":output,"execution_time":end_time - start_time}) + + +@app.route("/get_presets", methods=["GET"]) +def get_presets(self): + presets_file = self.lollms_paths.personal_databases_path/"presets.json" + if not presets_file.exists(): + shutil.copy("presets/presets.json",presets_file) + with open(presets_file) as f: + data = json.loads(f.read()) + return jsonify(data) + + +@app.route("/save_presets", methods=["POST"]) +def save_presets(self): + """Saves a preset to a file. + + Args: + None. + + Returns: + None. + """ + + # Get the JSON data from the POST request. + preset_data = request.get_json() + + presets_file = self.lollms_paths.personal_databases_path/"presets.json" + # Save the JSON data to a file. + with open(presets_file, "w") as f: + json.dump(preset_data, f, indent=4) + + return "Preset saved successfully!" + + def main(): app.run() diff --git a/lollms/apps/server/__init__.py b/lollms/apps/server/__init__.py index 017e545..1314ec8 100644 --- a/lollms/apps/server/__init__.py +++ b/lollms/apps/server/__init__.py @@ -23,6 +23,9 @@ import yaml import copy import gc import json +import shutil + + def reset_all_installs(lollms_paths:LollmsPaths): ASCIIColors.info("Removeing all configuration files to force reinstall") ASCIIColors.info(f"Searching files from {lollms_paths.personal_configuration_path}") @@ -84,13 +87,15 @@ class LoLLMsServer(LollmsApplication): self.app = Flask("LoLLMsServer") #self.app.config['SECRET_KEY'] = 'lollmssecret' CORS(self.app) # Enable CORS for all routes - def get_config(self): + + def get_config(): ASCIIColors.yellow("Requested configuration") return jsonify(self.config.to_dict()) self.app.add_url_rule( "/get_config", "get_config", get_config, methods=["GET"] - ) + ) + self.socketio = SocketIO(self.app, cors_allowed_origins='*', ping_timeout=1200, ping_interval=4000) # Set log level to warning diff --git a/lollms/helpers.py b/lollms/helpers.py index 3b046f9..b8181d4 100644 --- a/lollms/helpers.py +++ b/lollms/helpers.py @@ -1,15 +1,22 @@ import traceback -def trace_exception(ex): +def get_trace_exception(ex): """ - Traces an exception (useful for debug) + Traces an exception (useful for debug) and returns the full trace of the exception """ # Catch the exception and get the traceback as a list of strings traceback_lines = traceback.format_exception(type(ex), ex, ex.__traceback__) # Join the traceback lines into a single string traceback_text = ''.join(traceback_lines) - ASCIIColors.error(traceback_text) + return traceback_text + +def trace_exception(ex): + """ + Traces an exception (useful for debug) + """ + ASCIIColors.error(get_trace_exception(ex)) + class ASCIIColors: # Reset diff --git a/setup.py b/setup.py index 0341c13..8641200 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ def get_all_files(path): setuptools.setup( name="lollms", - version="3.1.0", + version="3.2.0", author="Saifeddine ALOUI", author_email="aloui.saifeddine@gmail.com", description="A python library for AI personality definition",