From 4cfbacdb26bd495e60d7efadc054e0aef89c3769 Mon Sep 17 00:00:00 2001 From: yuxiaoyuan0406 <973971990@qq.com> Date: Thu, 20 Apr 2023 17:21:47 +0800 Subject: [PATCH 1/6] fix sub-path deploy --- main.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 2e4e275..06719ab 100644 --- a/main.py +++ b/main.py @@ -171,4 +171,21 @@ def auto_opentab_delay(): threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start() auto_opentab_delay() -demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION) +# demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION) +demo.queue(concurrency_count=CONCURRENT_COUNT) + +CUSTOM_PATH = '/chatgpt' + +import uvicorn +from fastapi import FastAPI + +app = FastAPI() + +@app.get("/") +def read_main(): + return {"message": "NULL"} + +app = gr.mount_gradio_app(app, demo, path=CUSTOM_PATH) + +if __name__ == '__main__': + uvicorn.run(app, host="0.0.0.0", port=PORT) From 7dd73e1330d610c16f5c6ebb113e35c3176682d8 Mon Sep 17 00:00:00 2001 From: yuxiaoyuan0406 <973971990@qq.com> Date: Thu, 20 Apr 2023 18:20:25 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=A3=80=E6=9F=A5path=E7=9A=84=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- toolbox.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/toolbox.py b/toolbox.py index 5bbe954..4921e6e 100644 --- a/toolbox.py +++ b/toolbox.py @@ -527,3 +527,22 @@ class DummyWith(): def __exit__(self, exc_type, exc_value, traceback): return + +def custom_path_check(path: str)->bool: + ''' + check path for sub url + + path: path to check + + return value: do sub url wrap + ''' + if len(path) == 0: + print("ilegal custom path: {}\npath must not be empty\ndeploy on root url".format(path)) + return False + if path[0] == '/': + if path[1] != '/': + print("deploy on sub-path {}".format(path)) + return True + return False + print("ilegal custom path: {}\npath should begin with \'/\'\ndeploy on root url".format(path)) + return False From f0ff1f2c64378e3266a8e1a7c348c5fcb3d57cac Mon Sep 17 00:00:00 2001 From: yuxiaoyuan0406 <973971990@qq.com> Date: Thu, 20 Apr 2023 18:22:58 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0CUSTOM=5FPATH=E6=9D=A5?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E5=88=B0=E5=AD=90=E7=BA=A7=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 2 ++ main.py | 29 +++++++++++++++-------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/config.py b/config.py index 658de99..ce65e06 100644 --- a/config.py +++ b/config.py @@ -56,3 +56,5 @@ CONCURRENT_COUNT = 100 # 设置用户名和密码(相关功能不稳定,与gradio版本和网络都相关,如果本地使用不建议加这个) # [("username", "password"), ("username2", "password2"), ...] AUTHENTICATION = [] + +CUSTOM_PATH = "/" diff --git a/main.py b/main.py index 06719ab..09e67cc 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,11 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 import gradio as gr from request_llm.bridge_chatgpt import predict -from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith +from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, custom_path_check, DummyWith # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 -proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY = \ - get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY') +proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY, CUSTOM_PATH = \ + get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY', 'CUSTOM_PATH') # 如果WEB_PORT是-1, 则随机选取WEB端口 PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT @@ -171,21 +171,22 @@ def auto_opentab_delay(): threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start() auto_opentab_delay() -# demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION) demo.queue(concurrency_count=CONCURRENT_COUNT) -CUSTOM_PATH = '/chatgpt' +if custom_path_check(CUSTOM_PATH): + import uvicorn + from fastapi import FastAPI -import uvicorn -from fastapi import FastAPI + app = FastAPI() -app = FastAPI() + @app.get("/") + def read_main(): + return {"message": "NULL"} -@app.get("/") -def read_main(): - return {"message": "NULL"} + app = gr.mount_gradio_app(app, demo, path=CUSTOM_PATH) -app = gr.mount_gradio_app(app, demo, path=CUSTOM_PATH) - -if __name__ == '__main__': uvicorn.run(app, host="0.0.0.0", port=PORT) +else: + demo.launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION) + + From 9945d5048a60b1d48d9829999a98dd80d3d8a7ac Mon Sep 17 00:00:00 2001 From: yuxiaoyuan0406 <973971990@qq.com> Date: Thu, 20 Apr 2023 18:31:26 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=9B=B4=E5=A5=BD=E7=9A=84=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E5=AD=90=E8=B7=AF=E5=BE=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- toolbox.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/toolbox.py b/toolbox.py index 4921e6e..b521268 100644 --- a/toolbox.py +++ b/toolbox.py @@ -540,6 +540,9 @@ def custom_path_check(path: str)->bool: print("ilegal custom path: {}\npath must not be empty\ndeploy on root url".format(path)) return False if path[0] == '/': + if len(path) == 1: + print("deploy on root url") + return False if path[1] != '/': print("deploy on sub-path {}".format(path)) return True From 7beea951c667415a24f502e38f09e6df2fac2bdb Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 22 Apr 2023 17:24:22 +0800 Subject: [PATCH 5/6] unifying code --- config.py | 2 +- main.py | 8 ++++---- toolbox.py | 13 +++++++------ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/config.py b/config.py index a78af65..f4a59de 100644 --- a/config.py +++ b/config.py @@ -62,4 +62,4 @@ AUTHENTICATION = [] API_URL_REDIRECT = {} # 如果你需要把网址放在二级地址下(常规情况下,不要修改!!) -CUSTOM_PATH = "/" +CUSTOM_PATH = "/gra" diff --git a/main.py b/main.py index 11f92f4..ce2c566 100644 --- a/main.py +++ b/main.py @@ -176,18 +176,18 @@ def main(): def auto_opentab_delay(): import threading, webbrowser, time print(f"如果浏览器没有自动打开,请复制并转到以下URL:") - print(f"\t(亮色主题): http://localhost:{PORT}") - print(f"\t(暗色主题): http://localhost:{PORT}/?__dark-theme=true") + print(f"\t(亮色主题): http://localhost:{PORT}" + f"{CUSTOM_PATH}".replace('//','/')) + print(f"\t(暗色主题): http://localhost:{PORT}" + f"{CUSTOM_PATH}/?__dark-theme=true".replace('//','/')) def open(): time.sleep(2) # 打开浏览器 - webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true") + webbrowser.open_new_tab(f"http://localhost:{PORT}" + f"{CUSTOM_PATH}/?__dark-theme=true".replace('//','/')) threading.Thread(target=open, name="open-browser", daemon=True).start() threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start() threading.Thread(target=warm_up_modules, name="warm-up", daemon=True).start() auto_opentab_delay() demo.queue(concurrency_count=CONCURRENT_COUNT) - run_gradio(demo, auth=AUTHENTICATION, favicon_path="docs/logo.png", port=PORT, custom_path=CUSTOM_PATH) + run_gradio(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH) if __name__ == "__main__": main() diff --git a/toolbox.py b/toolbox.py index bf32ccd..cd6c61e 100644 --- a/toolbox.py +++ b/toolbox.py @@ -521,13 +521,14 @@ class DummyWith(): def __exit__(self, exc_type, exc_value, traceback): return -def run_gradio(demo, auth, favicon_path, port, custom_path): +def run_gradio(demo, auth, port, custom_path): import uvicorn import gradio as gr from fastapi import FastAPI app = FastAPI() - @app.get("/") - def read_main(): - return {"message": "NULL"} - app = gr.mount_gradio_app(app, demo, path=custom_path, auth=auth, favicon_path=favicon_path) - uvicorn.run(app, host="0.0.0.0", port=port) + if custom_path != "/": + @app.get("/") + def read_main(): + return {"message": f"Gradio is running at: {custom_path}"} + app = gr.mount_gradio_app(app, demo, path=custom_path) + uvicorn.run(app, host="0.0.0.0", port=port) # , auth=auth From f1234937c68f5b74391454d337aae082210769d2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 22 Apr 2023 17:30:21 +0800 Subject: [PATCH 6/6] add check path back --- config.py | 2 +- toolbox.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/config.py b/config.py index f4a59de..a78af65 100644 --- a/config.py +++ b/config.py @@ -62,4 +62,4 @@ AUTHENTICATION = [] API_URL_REDIRECT = {} # 如果你需要把网址放在二级地址下(常规情况下,不要修改!!) -CUSTOM_PATH = "/gra" +CUSTOM_PATH = "/" diff --git a/toolbox.py b/toolbox.py index cd6c61e..2bf5451 100644 --- a/toolbox.py +++ b/toolbox.py @@ -522,6 +522,25 @@ class DummyWith(): return def run_gradio(demo, auth, port, custom_path): + def is_path_legal(path: str)->bool: + ''' + check path for sub url + path: path to check + return value: do sub url wrap + ''' + if path == "/": return True + if len(path) == 0: + print("ilegal custom path: {}\npath must not be empty\ndeploy on root url".format(path)) + return False + if path[0] == '/': + if path[1] != '/': + print("deploy on sub-path {}".format(path)) + return True + return False + print("ilegal custom path: {}\npath should begin with \'/\'\ndeploy on root url".format(path)) + return False + + if not is_path_legal(custom_path): raise RuntimeError('Ilegal custom path') import uvicorn import gradio as gr from fastapi import FastAPI