working playground

This commit is contained in:
Saifeddine ALOUI 2023-08-20 03:09:30 +02:00
parent 68fed9326b
commit 603d38255e
4 changed files with 43 additions and 18 deletions

55
app.py
View File

@ -19,6 +19,11 @@ __version__ ="5.0"
main_repo = "https://github.com/ParisNeo/lollms-webui.git"
import os
import sys
from flask import request, jsonify
import io
import sys
import time
import traceback
def run_update_script(args=None):
update_script = "update_script.py"
@ -423,30 +428,50 @@ class LoLLMsWebUI(LoLLMsAPPI):
)
def execute_python_code(self):
"""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
def execute_code_internal():
# Create a Python interpreter.
interpreter = io.StringIO()
sys.stdout = interpreter
globals_dict = {'__name__': '__main__', '__file__': 'ai_code.py'}
# Execute the code.
try:
exec(code, globals_dict)
# Get the output.
output = interpreter.getvalue()
except Exception as ex:
# Get the traceback information
tb_str = traceback.format_exc().replace("<string>","ai_code.py")
substring = 'File "ai_code.py"'
try:
position = tb_str.index(substring)
tb_str = tb_str[position:]
except ValueError:
pass
output = (
f"<div class='text-red-500'>{ex}\n"
f"Traceback:\n{tb_str}</div>"
)
finally:
sys.stdout = sys.__stdout__ # Restore the standard output
return output
# Execute the code and capture the output
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)
output = execute_code_internal()
end_time = time.time()
return jsonify({"output":output,"execution_time":end_time - start_time})
return jsonify({"output": output, "execution_time": end_time - start_time})
def get_presets(self):
presets_file = self.lollms_paths.personal_databases_path/"presets.json"

View File

@ -72,7 +72,7 @@ https://github.com/highlightjs/highlight.js/issues/2277`),_e=T,we=q),G===void 0&
// Now you can work with the JSON data
console.log(jsonData);
preExecElement.classList.remove('hidden');
codeExecElement.innerText=jsonData.output
codeExecElement.innerHTML = jsonData.output
})
.catch(error => {
// Handle any errors that occurred during the fetch process

2
web/dist/index.html vendored
View File

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LoLLMS WebUI - Welcome</title>
<script type="module" crossorigin src="/assets/index-19fca3d7.js"></script>
<script type="module" crossorigin src="/assets/index-43dbf6bb.js"></script>
<link rel="stylesheet" href="/assets/index-90cbdba9.css">
</head>
<body>

View File

@ -229,7 +229,7 @@ export default {
// Now you can work with the JSON data
console.log(jsonData);
preExecElement.classList.remove('hidden');
codeExecElement.innerText=jsonData.output
codeExecElement.innerHTML = jsonData.output
})
.catch(error => {
// Handle any errors that occurred during the fetch process