fixed macos

This commit is contained in:
Saifeddine ALOUI 2024-04-21 16:19:35 +02:00
parent c9f0381535
commit ad4359a1f5
3 changed files with 178 additions and 39 deletions

View File

@ -25,7 +25,7 @@ miniconda_folder="./installer_files"
TMP="./installer_files/temp" TMP="./installer_files/temp"
if [ -e "$miniconda_folder" ]; then if [ -e "$miniconda_folder" ]; then
INSTALL_ENV_DIR="./installer_files/lollms_env" INSTALL_ENV_DIR="./installer_files/miniconda3/envs/lollms"
MINICONDA_DIR="./installer_files/miniconda3" MINICONDA_DIR="./installer_files/miniconda3"
MINICONDA_CMD="$MINICONDA_DIR/bin/activate" MINICONDA_CMD="$MINICONDA_DIR/bin/activate"
if [ ! -f "$MINICONDA_DIR/bin/activate" ]; then if [ ! -f "$MINICONDA_DIR/bin/activate" ]; then

View File

@ -17,7 +17,7 @@ from lollms.utilities import discussion_path_2_url
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance() lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
def build_graphviz_output(code, ifram_name="unnamed"): def build_graphviz_output(code, ifram_name=None):
""" """
This function creates an HTML5 iframe with the given HTML content and iframe name. This function creates an HTML5 iframe with the given HTML content and iframe name.
@ -29,43 +29,75 @@ def build_graphviz_output(code, ifram_name="unnamed"):
str: The HTML string for the iframe. str: The HTML string for the iframe.
""" """
# Start the timer. # Start the timer.
start_time = time.time() start_time = time.time()
rendered = "\n".join([ if ifram_name!=None:
'<div style="width: 100%; margin: 0 auto;">', rendered = "\n".join([
f'<iframe id="{ifram_name}" srcdoc=\'', '<div style="width: 100%; margin: 0 auto;">',
'<style>', f'<iframe id="{ifram_name}" srcdoc=\'',
'iframe {', '<style>',
'width: 100%;', 'iframe {',
'height: 100%;', 'width: 100%;',
'border: none;', 'height: 100%;',
'}', 'border: none;',
'.graph {', '}',
'background-color: transparent;', '.graph {',
'padding: 20px;', 'background-color: transparent;',
'border-radius: 10px;', 'padding: 20px;',
'display: flex;', 'border-radius: 10px;',
'justify-content: center;', 'display: flex;',
'align-items: center;', 'justify-content: center;',
'height: 100%;', 'align-items: center;',
'}', 'height: 100%;',
'</style>', '}',
'<div id="graph" class="graph"></div>', '</style>',
'<script src="https://github.com/mdaines/viz-js/releases/download/release-viz-3.2.4/viz-standalone.js"></script>', '<div id="graph" class="graph"></div>',
'<script>', '<script src="https://github.com/mdaines/viz-js/releases/download/release-viz-3.2.4/viz-standalone.js"></script>',
'// Initialize the mermaid library and render our diagram', '<script>',
'Viz.instance().then(function(viz) {', '// Initialize the mermaid library and render our diagram',
'var svg = viz.renderSVGElement(`', 'Viz.instance().then(function(viz) {',
"\n".join([c.replace("'","\"") for c in code.split("\n") if c.strip()!=""]), 'var svg = viz.renderSVGElement(`',
'`);', "\n".join([c.replace("'","\"") for c in code.split("\n") if c.strip()!=""]),
'document.getElementById("graph").appendChild(svg);', '`);',
'});', 'document.getElementById("graph").appendChild(svg);',
'</script>', '});',
'<div style="text-align: center;">', '</script>',
'</div>', '<div style="text-align: center;">',
'\' style="width: 100%; height: 600px; border: none;"></iframe>', '</div>',
'</div>' '\' style="width: 100%; height: 600px; border: none;"></iframe>',
] '</div>'
) ]
)
else:
rendered = "\n".join([
'<div style="width: 100%; margin: 0 auto;">',
'<style>',
'.graph {',
'background-color: transparent;',
'padding: 20px;',
'border-radius: 10px;',
'display: flex;',
'justify-content: center;',
'align-items: center;',
'height: 100%;',
'}',
'</style>',
'<div id="graph" class="graph"></div>',
'<script src="https://github.com/mdaines/viz-js/releases/download/release-viz-3.2.4/viz-standalone.js"></script>',
'<script>',
'// Initialize the mermaid library and render our diagram',
'Viz.instance().then(function(viz) {',
'var svg = viz.renderSVGElement(`',
"\n".join([c.replace("'","\"") for c in code.split("\n") if c.strip()!=""]),
'`);',
'document.getElementById("graph").appendChild(svg);',
'});',
'</script>',
'<div style="text-align: center;">',
'</div>',
'</div>'
]
)
execution_time = time.time() - start_time execution_time = time.time() - start_time
return {"output": rendered, "execution_time": execution_time} return {"output": rendered, "execution_time": execution_time}

View File

@ -0,0 +1,107 @@
"""
project: lollms_webui
file: shell_execution_engine.py
author: ParisNeo
description:
This is a utility for executing python code
"""
from lollms_webui import LOLLMSWebUI
from ascii_colors import get_trace_exception, trace_exception
import time
import subprocess
import json
from lollms.client_session import Client
from lollms.utilities import discussion_path_2_url
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
def build_svg_output(code, ifram_name="unnamed"):
"""
This function creates an HTML5 iframe with the given HTML content and iframe name.
Args:
code (str): The svg code
ifram_name (str, optional): The name of the iframe. Defaults to "unnamed".
Returns:
str: The HTML string for the iframe.
"""
# Start the timer.
start_time = time.time()
rendered = "\n".join([
'<div style="width: 100%; margin: 0 auto;">',
f'<iframe id="{ifram_name}" style="width: 100%" srcdoc="',
'<style>',
'iframe {',
'width: 100%;',
'height: 100%;',
'border: none;',
'}',
'.svg {',
'background-color: transparent;',
'padding: 20px;',
'border-radius: 10px;',
'display: flex;',
'justify-content: center;',
'align-items: center;',
'height: 100%;',
'}',
'</style>',
'<div class=\'svg\'>',
"\n".join([c for c in code.split("\n") if c.strip()!=""]),
'</div>',
'<script src=\'https://cdn.jsdelivr.net/npm/svg/dist/svg.min.js\'></script>',
'<script>',
'// Initialize the svg library and render our diagram',
'svg.initialize({ startOnLoad: true });',
'// Function to save SVG content to a file',
'function saveSVG() {',
'var svg = document.querySelector(".svg > svg");',
'var serializer = new XMLSerializer();',
'var source = serializer.serializeToString(svg);',
'var blob = new Blob([source], {type: "image/svg+xml;charset=utf-8"});',
'var url = URL.createObjectURL(blob);',
'var a = document.createElement("a");',
'a.href = url;',
'a.download = "diagram.svg";',
'a.click();',
'}',
'</script>',
'<div style=\'text-align: center;\'>',
'</div>',
'<button onclick="saveSVG()">Save SVG</button>',
'" style="width: 100%; height: 600px; border: none;"></iframe>',
'</div>'
]
)
execution_time = time.time() - start_time
return {"output": rendered, "execution_time": execution_time}
def execute_svg(code, client:Client, message_id, build_file=False):
if build_file:
# Start the timer.
start_time = time.time()
if not "http" in lollmsElfServer.config.host and not "https" in lollmsElfServer.config.host:
host = "http://"+lollmsElfServer.config.host
else:
host = lollmsElfServer.config.host
# Create a temporary file.
root_folder = client.discussion.discussion_folder
root_folder.mkdir(parents=True,exist_ok=True)
tmp_file = root_folder/f"ai_code_{message_id}.html"
with open(tmp_file,"w",encoding="utf8") as f:
f.write(build_svg_output(code)["output"])
link = f"{host}:{lollmsElfServer.config.port}/{discussion_path_2_url(tmp_file)}"
# Stop the timer.
execution_time = time.time() - start_time
output_json = {"output": f'<b>Page built successfully</b><br><a href="{link}" target="_blank">Press here to view the page</a>', "execution_time": execution_time}
return output_json
else:
return build_svg_output(code)