3v324v23 commited on
Commit
33ea739
2 Parent(s): 7d44210 e90eee2

Merge branch 'subpath'

Browse files
Files changed (3) hide show
  1. config.py +3 -0
  2. main.py +8 -0
  3. toolbox.py +31 -0
config.py CHANGED
@@ -60,3 +60,6 @@ AUTHENTICATION = []
60
  # 重新URL重新定向,实现更换API_URL的作用(常规情况下,不要修改!!)
61
  # 格式 {"https://api.openai.com/v1/chat/completions": "重定向的URL"}
62
  API_URL_REDIRECT = {}
 
 
 
 
60
  # 重新URL重新定向,实现更换API_URL的作用(常规情况下,不要修改!!)
61
  # 格式 {"https://api.openai.com/v1/chat/completions": "重定向的URL"}
62
  API_URL_REDIRECT = {}
63
+
64
+ # 如果你需要把网址放在二级地址下(常规情况下,不要修改!!)(需要配合修改main.py才能生效)
65
+ CUSTOM_PATH = "/"
main.py CHANGED
@@ -188,5 +188,13 @@ def main():
188
  auto_opentab_delay()
189
  demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png")
190
 
 
 
 
 
 
 
 
 
191
  if __name__ == "__main__":
192
  main()
 
188
  auto_opentab_delay()
189
  demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png")
190
 
191
+ # 如果需要在二级路径下运行gradio
192
+ # CUSTOM_PATH, = get_conf('CUSTOM_PATH')
193
+ # if CUSTOM_PATH != "/":
194
+ # from toolbox import run_gradio_in_subpath
195
+ # run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH)
196
+ # else:
197
+ # demo.launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png")
198
+
199
  if __name__ == "__main__":
200
  main()
toolbox.py CHANGED
@@ -520,3 +520,34 @@ class DummyWith():
520
 
521
  def __exit__(self, exc_type, exc_value, traceback):
522
  return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520
 
521
  def __exit__(self, exc_type, exc_value, traceback):
522
  return
523
+
524
+ def run_gradio_in_subpath(demo, auth, port, custom_path):
525
+ def is_path_legal(path: str)->bool:
526
+ '''
527
+ check path for sub url
528
+ path: path to check
529
+ return value: do sub url wrap
530
+ '''
531
+ if path == "/": return True
532
+ if len(path) == 0:
533
+ print("ilegal custom path: {}\npath must not be empty\ndeploy on root url".format(path))
534
+ return False
535
+ if path[0] == '/':
536
+ if path[1] != '/':
537
+ print("deploy on sub-path {}".format(path))
538
+ return True
539
+ return False
540
+ print("ilegal custom path: {}\npath should begin with \'/\'\ndeploy on root url".format(path))
541
+ return False
542
+
543
+ if not is_path_legal(custom_path): raise RuntimeError('Ilegal custom path')
544
+ import uvicorn
545
+ import gradio as gr
546
+ from fastapi import FastAPI
547
+ app = FastAPI()
548
+ if custom_path != "/":
549
+ @app.get("/")
550
+ def read_main():
551
+ return {"message": f"Gradio is running at: {custom_path}"}
552
+ app = gr.mount_gradio_app(app, demo, path=custom_path)
553
+ uvicorn.run(app, host="0.0.0.0", port=port) # , auth=auth