2022-08-17 00:07:20 +08:00
|
|
|
import importlib
|
|
|
|
import gradio as gr
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import copy
|
|
|
|
import pathlib
|
2023-12-06 23:53:16 +08:00
|
|
|
from fastapi import FastAPI, Request
|
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
import uvicorn
|
|
|
|
from gradio.utils import get_space
|
2022-08-17 00:07:20 +08:00
|
|
|
|
2022-08-26 04:23:28 +08:00
|
|
|
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
2022-08-17 00:07:20 +08:00
|
|
|
|
|
|
|
demo_dir = pathlib.Path(__file__).parent / "demos"
|
|
|
|
|
|
|
|
|
2023-12-06 23:53:16 +08:00
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
|
|
|
names = sorted(os.listdir("./demos"))
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
def index(request: Request):
|
|
|
|
names = [[p[0], p[2]] for p in all_demos]
|
|
|
|
return templates.TemplateResponse("index.html", {"request": request, "names": names,
|
|
|
|
"initial_demo": names[0][0], "is_space": get_space()})
|
|
|
|
|
|
|
|
|
2022-08-17 00:07:20 +08:00
|
|
|
all_demos = []
|
|
|
|
demo_module = None
|
2022-08-23 04:04:59 +08:00
|
|
|
for p in sorted(os.listdir("./demos")):
|
2022-08-17 00:07:20 +08:00
|
|
|
old_path = copy.deepcopy(sys.path)
|
|
|
|
sys.path = [os.path.join(demo_dir, p)] + sys.path
|
2022-08-27 07:35:47 +08:00
|
|
|
try: # Some demos may not be runnable because of 429 timeouts, etc.
|
|
|
|
if demo_module is None:
|
|
|
|
demo_module = importlib.import_module(f"run")
|
|
|
|
else:
|
|
|
|
demo_module = importlib.reload(demo_module)
|
2023-12-06 23:53:16 +08:00
|
|
|
all_demos.append((p, demo_module.demo.queue(), False))
|
2022-08-27 07:35:47 +08:00
|
|
|
except Exception as e:
|
|
|
|
with gr.Blocks() as demo:
|
|
|
|
gr.Markdown(f"Error loading demo: {e}")
|
2023-12-06 23:53:16 +08:00
|
|
|
all_demos.append((p, demo, True))
|
|
|
|
|
|
|
|
for demo_name, demo, _ in all_demos:
|
|
|
|
app = gr.mount_gradio_app(app, demo, f"/demo/{demo_name}")
|
2022-08-17 00:07:20 +08:00
|
|
|
|
|
|
|
|
2023-12-06 23:53:16 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
uvicorn.run(app, port=7860, host="0.0.0.0")
|