diff --git a/config.py b/config.py index 0ad723b..9cf06e7 100644 --- a/config.py +++ b/config.py @@ -60,3 +60,6 @@ AUTHENTICATION = [] # 重新URL重新定向,实现更换API_URL的作用(常规情况下,不要修改!!) # 格式 {"https://api.openai.com/v1/chat/completions": "重定向的URL"} API_URL_REDIRECT = {} + +# 如果你需要把网址放在二级地址下(常规情况下,不要修改!!)(需要配合修改main.py才能生效) +CUSTOM_PATH = "/" diff --git a/main.py b/main.py index 833720b..f57ac1a 100644 --- a/main.py +++ b/main.py @@ -188,5 +188,13 @@ def main(): auto_opentab_delay() demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png") + # 如果需要在二级路径下运行gradio + # CUSTOM_PATH, = get_conf('CUSTOM_PATH') + # if CUSTOM_PATH != "/": + # from toolbox import run_gradio_in_subpath + # run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH) + # else: + # demo.launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png") + if __name__ == "__main__": main() diff --git a/toolbox.py b/toolbox.py index d2c9e6e..febf6bc 100644 --- a/toolbox.py +++ b/toolbox.py @@ -520,3 +520,34 @@ class DummyWith(): def __exit__(self, exc_type, exc_value, traceback): return + +def run_gradio_in_subpath(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 + app = FastAPI() + 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