Merge pull request #37 from logspace-ai/live-port
This commit is contained in:
commit
b9207a36dc
4 changed files with 41 additions and 4 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import platform
|
import platform
|
||||||
|
import re
|
||||||
|
|
||||||
from langflow.main import create_app
|
from langflow.main import create_app
|
||||||
|
|
||||||
|
|
@ -14,7 +15,38 @@ def get_number_of_workers(workers=None):
|
||||||
return workers
|
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()
|
app = create_app()
|
||||||
# get the directory of the current file
|
# get the directory of the current file
|
||||||
path = Path(__file__).parent
|
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),
|
StaticFiles(directory=static_files_dir, html=True),
|
||||||
name="static",
|
name="static",
|
||||||
)
|
)
|
||||||
port = 7860
|
|
||||||
options = {
|
options = {
|
||||||
"bind": f"{host}:{port}",
|
"bind": f"{host}:{port}",
|
||||||
"workers": get_number_of_workers(workers),
|
"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,
|
"timeout": timeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Replace the port in index.html
|
||||||
|
replace_port(static_files_dir, host, port)
|
||||||
|
|
||||||
if platform.system() in ["Darwin", "Windows"]:
|
if platform.system() in ["Darwin", "Windows"]:
|
||||||
# Run using uvicorn on MacOS and Windows
|
# Run using uvicorn on MacOS and Windows
|
||||||
# Windows doesn't support gunicorn
|
# Windows doesn't support gunicorn
|
||||||
|
|
|
||||||
|
|
@ -276,7 +276,7 @@ def format_dict(d, name: Optional[str] = None):
|
||||||
"prefix",
|
"prefix",
|
||||||
"examples",
|
"examples",
|
||||||
"temperature",
|
"temperature",
|
||||||
# "model_name",
|
"model_name",
|
||||||
]
|
]
|
||||||
or "api_key" in key
|
or "api_key" in key
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,9 @@
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>LangFLow</title>
|
<title>LangFLow</title>
|
||||||
|
<script>
|
||||||
|
window.sessionStorage.setItem("port","http://localhost:7860")
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body id='body' style="width: 100%; height:100%">
|
<body id='body' style="width: 100%; height:100%">
|
||||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { APIObjectType, sendAllProps } from '../../types/api/index';
|
import { APIObjectType, sendAllProps } from '../../types/api/index';
|
||||||
import axios, { AxiosResponse } from "axios";
|
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<AxiosResponse<APIObjectType>> {
|
export async function getAll():Promise<AxiosResponse<APIObjectType>> {
|
||||||
return await axios.get(`${backendUrl}/all`);
|
return await axios.get(`${backendUrl}/all`);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue