gradio/demo/all_demos/run.py
Freddy Boulton 1086542f17
Render each app in the PR's spaces preview in a separate page (#6657)
* Use template response

* minor fix

* Return type hint

* add changeset

* Remove return types

* response_class=None

* Use relative path

* SPA

* remove pydantic pin

* Revert

* delete changeset

* Overflow hidden on body

* text gray

* Collapsible sidebar

* max-height

* Use search params

* document.location.search

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
2023-12-06 10:53:16 -05:00

53 lines
1.5 KiB
Python

import importlib
import gradio as gr
import os
import sys
import copy
import pathlib
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
import uvicorn
from gradio.utils import get_space
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
demo_dir = pathlib.Path(__file__).parent / "demos"
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()})
all_demos = []
demo_module = None
for p in sorted(os.listdir("./demos")):
old_path = copy.deepcopy(sys.path)
sys.path = [os.path.join(demo_dir, p)] + sys.path
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)
all_demos.append((p, demo_module.demo.queue(), False))
except Exception as e:
with gr.Blocks() as demo:
gr.Markdown(f"Error loading demo: {e}")
all_demos.append((p, demo, True))
for demo_name, demo, _ in all_demos:
app = gr.mount_gradio_app(app, demo, f"/demo/{demo_name}")
if __name__ == "__main__":
uvicorn.run(app, port=7860, host="0.0.0.0")