""" project: lollms_webui file: latex_execution_engine.py author: ParisNeo description: This is a utility for executing latex code """ from fastapi import APIRouter, Request, routing from lollms_webui import LOLLMSWebUI from pydantic import BaseModel from starlette.responses import StreamingResponse from lollms.types import MSG_OPERATION_TYPE from lollms.main_config import BaseConfig from ascii_colors import get_trace_exception, trace_exception from ascii_colors import ASCIIColors from lollms.databases.discussions_database import DiscussionsDB from pathlib import Path import tqdm from fastapi import FastAPI, UploadFile, File import shutil import time import subprocess import json from lollms.client_session import Client from lollms.utilities import discussion_path_to_url lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance() def execute_latex(code, client:Client, message_id): # Start the timer. start_time = time.time() # Create a temporary file. root_folder = client.discussion.discussion_folder root_folder.mkdir(parents=True,exist_ok=True) tmp_file = root_folder/f"latex_file_{message_id}.tex" with open(tmp_file,"w", encoding="utf-8") as f: f.write(code) try: # Determine the pdflatex command based on the provided or default path if lollmsElfServer.config.pdf_latex_path: pdflatex_command = lollmsElfServer.config.pdf_latex_path else: pdflatex_command = 'pdflatex' # Set the execution path to the folder containing the tmp_file execution_path = tmp_file.parent # Execute the Python code in a temporary file. process = subprocess.Popen( [pdflatex_command, "-interaction=nonstopmode", str(tmp_file)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=execution_path ) # Get the output and error from the process. output, error = process.communicate() except Exception as ex: # Stop the timer. execution_time = time.time() - start_time error_message = f"Error executing Python code: {ex}" error_json = {"output": "