Merge pull request #544 from yuxiaoyuan0406/suburl
fix apache2 sub-path deploy issue
This commit is contained in:
		
						commit
						206f4138b6
					
				@ -60,3 +60,6 @@ AUTHENTICATION = []
 | 
				
			|||||||
# 重新URL重新定向,实现更换API_URL的作用(常规情况下,不要修改!!)
 | 
					# 重新URL重新定向,实现更换API_URL的作用(常规情况下,不要修改!!)
 | 
				
			||||||
# 格式 {"https://api.openai.com/v1/chat/completions": "重定向的URL"}
 | 
					# 格式 {"https://api.openai.com/v1/chat/completions": "重定向的URL"}
 | 
				
			||||||
API_URL_REDIRECT = {}
 | 
					API_URL_REDIRECT = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 如果你需要把网址放在二级地址下(常规情况下,不要修改!!)
 | 
				
			||||||
 | 
					CUSTOM_PATH = "/"
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										17
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								main.py
									
									
									
									
									
								
							@ -3,10 +3,10 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
 | 
				
			|||||||
def main():
 | 
					def main():
 | 
				
			||||||
    import gradio as gr
 | 
					    import gradio as gr
 | 
				
			||||||
    from request_llm.bridge_all import predict
 | 
					    from request_llm.bridge_all 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, run_gradio, DummyWith
 | 
				
			||||||
    # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
 | 
					    # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
 | 
				
			||||||
    proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY, AVAIL_LLM_MODELS = \
 | 
					    proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY, AVAIL_LLM_MODELS, CUSTOM_PATH = \
 | 
				
			||||||
        get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY', 'AVAIL_LLM_MODELS')
 | 
					        get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY', 'AVAIL_LLM_MODELS', 'CUSTOM_PATH')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # 如果WEB_PORT是-1, 则随机选取WEB端口
 | 
					    # 如果WEB_PORT是-1, 则随机选取WEB端口
 | 
				
			||||||
    PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
 | 
					    PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
 | 
				
			||||||
@ -176,17 +176,20 @@ def main():
 | 
				
			|||||||
    def auto_opentab_delay():
 | 
					    def auto_opentab_delay():
 | 
				
			||||||
        import threading, webbrowser, time
 | 
					        import threading, webbrowser, time
 | 
				
			||||||
        print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
 | 
					        print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
 | 
				
			||||||
        print(f"\t(亮色主题): http://localhost:{PORT}")
 | 
					        print(f"\t(亮色主题): http://localhost:{PORT}" + f"{CUSTOM_PATH}".replace('//','/'))
 | 
				
			||||||
        print(f"\t(暗色主题): http://localhost:{PORT}/?__dark-theme=true")
 | 
					        print(f"\t(暗色主题): http://localhost:{PORT}" + f"{CUSTOM_PATH}/?__dark-theme=true".replace('//','/'))
 | 
				
			||||||
        def open(): 
 | 
					        def open(): 
 | 
				
			||||||
            time.sleep(2)       # 打开浏览器
 | 
					            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=open, name="open-browser", daemon=True).start()
 | 
				
			||||||
        threading.Thread(target=auto_update, name="self-upgrade", 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()
 | 
					        threading.Thread(target=warm_up_modules, name="warm-up", daemon=True).start()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    auto_opentab_delay()
 | 
					    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")
 | 
					    demo.queue(concurrency_count=CONCURRENT_COUNT)
 | 
				
			||||||
 | 
					    run_gradio(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    main()
 | 
					    main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										31
									
								
								toolbox.py
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								toolbox.py
									
									
									
									
									
								
							@ -520,3 +520,34 @@ class DummyWith():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def __exit__(self, exc_type, exc_value, traceback):
 | 
					    def __exit__(self, exc_type, exc_value, traceback):
 | 
				
			||||||
        return
 | 
					        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
 | 
				
			||||||
 | 
					    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
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user