mirror of
https://github.com/ParisNeo/lollms.git
synced 2025-02-20 17:33:03 +00:00
upgraded
This commit is contained in:
parent
dfcd3aa48b
commit
67abfab9c5
@ -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()
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user