This commit is contained in:
Saifeddine ALOUI 2023-08-20 03:09:17 +02:00
parent dfcd3aa48b
commit 67abfab9c5
4 changed files with 80 additions and 7 deletions

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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",