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/4] 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/4] =?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/4] =?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/4] =?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