From 367fa039a0fa0f7d3599437421057186549761e2 Mon Sep 17 00:00:00 2001 From: Jan-Timo Hesse Date: Thu, 8 Aug 2024 09:46:14 +0200 Subject: [PATCH] added install_frontmatter_requirements --- backend/apps/webui/utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backend/apps/webui/utils.py b/backend/apps/webui/utils.py index 96d2b29eb..34682920b 100644 --- a/backend/apps/webui/utils.py +++ b/backend/apps/webui/utils.py @@ -1,6 +1,8 @@ from importlib import util import os import re +import sys +import subprocess from config import TOOLS_DIR, FUNCTIONS_DIR @@ -52,6 +54,7 @@ def load_toolkit_module_by_id(toolkit_id): frontmatter = extract_frontmatter(toolkit_path) try: + install_frontmatter_requirements(frontmatter.get("requirements", "")) spec.loader.exec_module(module) print(f"Loaded module: {module.__name__}") if hasattr(module, "Tools"): @@ -73,6 +76,7 @@ def load_function_module_by_id(function_id): frontmatter = extract_frontmatter(function_path) try: + install_frontmatter_requirements(frontmatter.get("requirements", "")) spec.loader.exec_module(module) print(f"Loaded module: {module.__name__}") if hasattr(module, "Pipe"): @@ -88,3 +92,12 @@ def load_function_module_by_id(function_id): # Move the file to the error folder os.rename(function_path, f"{function_path}.error") raise e + +def install_frontmatter_requirements(requirements): + if requirements: + req_list = [req.strip() for req in requirements.split(',')] + for req in req_list: + print(f"Installing requirement: {req}") + subprocess.check_call([sys.executable, "-m", "pip", "install", req]) + else: + print("No requirements found in frontmatter.") \ No newline at end of file