From f0261fed806514a6f16e67462271ae237bf92cd3 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Fri, 17 Mar 2023 17:46:10 -0300 Subject: [PATCH 1/3] version with dinamic port --- src/frontend/public/index.html | 3 +++ src/frontend/src/controllers/NodesServices/index.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/frontend/public/index.html b/src/frontend/public/index.html index aa19f07de..c1953d6b8 100644 --- a/src/frontend/public/index.html +++ b/src/frontend/public/index.html @@ -5,6 +5,9 @@ LangFLow + diff --git a/src/frontend/src/controllers/NodesServices/index.ts b/src/frontend/src/controllers/NodesServices/index.ts index f3fc86e5d..40e98821c 100644 --- a/src/frontend/src/controllers/NodesServices/index.ts +++ b/src/frontend/src/controllers/NodesServices/index.ts @@ -1,7 +1,7 @@ import { APIObjectType, sendAllProps } from '../../types/api/index'; import axios, { AxiosResponse } from "axios"; -const backendUrl = process.env.BACKEND || "http://localhost:7860"; +const backendUrl = window.sessionStorage.getItem('port') || "http://localhost:7860"; export async function getAll():Promise> { return await axios.get(`${backendUrl}/all`); From c02e36d5ade55fee8cf8140ddc3e0ff45d5233d8 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Fri, 17 Mar 2023 17:48:55 -0300 Subject: [PATCH 2/3] model name added --- src/backend/langflow/utils/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/langflow/utils/util.py b/src/backend/langflow/utils/util.py index 3a097f6d5..d023fbc3e 100644 --- a/src/backend/langflow/utils/util.py +++ b/src/backend/langflow/utils/util.py @@ -276,7 +276,7 @@ def format_dict(d, name: Optional[str] = None): "prefix", "examples", "temperature", - # "model_name", + "model_name", ] or "api_key" in key ) From a4c6a6abd55586a71796df036844924a201f91b6 Mon Sep 17 00:00:00 2001 From: Gabriel Almeida Date: Fri, 17 Mar 2023 17:59:51 -0300 Subject: [PATCH 3/3] fix: custom ports and host now work --- src/backend/langflow/__main__.py | 38 ++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/backend/langflow/__main__.py b/src/backend/langflow/__main__.py index 55dd21445..c93de7667 100644 --- a/src/backend/langflow/__main__.py +++ b/src/backend/langflow/__main__.py @@ -1,5 +1,6 @@ import multiprocessing import platform +import re from langflow.main import create_app @@ -14,7 +15,38 @@ def get_number_of_workers(workers=None): return workers -def serve(host: str = "127.0.0.1", workers: int = 1, timeout: int = 60): +def replace_port(static_files_dir, host, port): + # Load index.html from frontend directory + # In it there is a script tag that sets the base url + # like so setItem("port", "http://localhost:7860") + # localhost could be anything so we need to verify for string + # we need to set the base url to the port that the server is running on + # so that the frontend can make requests to the backend + # This is a hacky way to do it, but it works + new_string = f'setItem("port","http://{host}:{port}")' + with open(static_files_dir / "index.html", "r") as f: + index_html = f.read() + # using regex to replace the port + index_html = re.sub( + r"setItem\(\"port\",.*\)", + new_string, + index_html, + ) + with open(static_files_dir / "index.html", "w") as f: + f.write(index_html) + # Verify that the port was replaced + with open(static_files_dir / "index.html", "r") as f: + index_html = f.read() + if new_string not in index_html: + raise ValueError( + "The port was not replaced in index.html. " + "Please check the regex in main.py" + ) + + +def serve( + host: str = "127.0.0.1", workers: int = 1, timeout: int = 60, port: int = 7860 +): app = create_app() # get the directory of the current file path = Path(__file__).parent @@ -24,7 +56,6 @@ def serve(host: str = "127.0.0.1", workers: int = 1, timeout: int = 60): StaticFiles(directory=static_files_dir, html=True), name="static", ) - port = 7860 options = { "bind": f"{host}:{port}", "workers": get_number_of_workers(workers), @@ -32,6 +63,9 @@ def serve(host: str = "127.0.0.1", workers: int = 1, timeout: int = 60): "timeout": timeout, } + # Replace the port in index.html + replace_port(static_files_dir, host, port) + if platform.system() in ["Darwin", "Windows"]: # Run using uvicorn on MacOS and Windows # Windows doesn't support gunicorn