Self-host @gradio/client: download at startup, serve same-origin at /gradio-client.js
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import av
|
|
| 7 |
import gradio as gr
|
| 8 |
import numpy as np
|
| 9 |
import torch
|
| 10 |
-
from fastapi.responses import HTMLResponse
|
| 11 |
from gradio import Server
|
| 12 |
from gradio.data_classes import FileData
|
| 13 |
from PIL import Image
|
|
@@ -41,6 +41,25 @@ model.fov.load_state_dict(
|
|
| 41 |
)
|
| 42 |
model.eval().to("cuda")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def _resize(img: np.ndarray, f_px: float | None, max_side: int):
|
| 46 |
"""Downscale so the longer side is at most ``max_side``, scaling ``f_px`` to match."""
|
|
@@ -168,6 +187,14 @@ def predict_depth_video(video: FileData, request: gr.Request) -> FileData:
|
|
| 168 |
return FileData(path=_infer_video(video["path"]))
|
| 169 |
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
@app.get("/", response_class=HTMLResponse)
|
| 172 |
async def homepage():
|
| 173 |
return (Path(__file__).resolve().parent / "index.html").read_text(encoding="utf-8")
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
import numpy as np
|
| 9 |
import torch
|
| 10 |
+
from fastapi.responses import FileResponse, HTMLResponse
|
| 11 |
from gradio import Server
|
| 12 |
from gradio.data_classes import FileData
|
| 13 |
from PIL import Image
|
|
|
|
| 41 |
)
|
| 42 |
model.eval().to("cuda")
|
| 43 |
|
| 44 |
+
# Self-host the @gradio/client bundle: browsers on some networks cannot reach public CDNs, and
|
| 45 |
+
# the page's dynamic import is what failed for them. Download it once at startup (server-side
|
| 46 |
+
# package installs work here) and serve it from this Space's own origin; the page falls back to
|
| 47 |
+
# the CDNs only if this file is missing.
|
| 48 |
+
_CLIENT_SOURCES = [
|
| 49 |
+
"https://cdn.jsdelivr.net/npm/@gradio/client@6.32.0/dist/index.min.js",
|
| 50 |
+
"https://unpkg.com/@gradio/client@6.32.0/dist/index.min.js",
|
| 51 |
+
]
|
| 52 |
+
_client_path = Path(__file__).resolve().parent / "gradio_client.min.js"
|
| 53 |
+
if not _client_path.exists():
|
| 54 |
+
import urllib.request
|
| 55 |
+
|
| 56 |
+
for _src in _CLIENT_SOURCES:
|
| 57 |
+
try:
|
| 58 |
+
urllib.request.urlretrieve(_src, _client_path)
|
| 59 |
+
break
|
| 60 |
+
except OSError:
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
|
| 64 |
def _resize(img: np.ndarray, f_px: float | None, max_side: int):
|
| 65 |
"""Downscale so the longer side is at most ``max_side``, scaling ``f_px`` to match."""
|
|
|
|
| 187 |
return FileData(path=_infer_video(video["path"]))
|
| 188 |
|
| 189 |
|
| 190 |
+
@app.get("/gradio-client.js")
|
| 191 |
+
def gradio_client_js():
|
| 192 |
+
"""The @gradio/client bundle, served same-origin so no CDN is needed in the browser."""
|
| 193 |
+
if _client_path.exists():
|
| 194 |
+
return FileResponse(_client_path, media_type="text/javascript")
|
| 195 |
+
return HTMLResponse("// client bundle not available", status_code=404)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
@app.get("/", response_class=HTMLResponse)
|
| 199 |
async def homepage():
|
| 200 |
return (Path(__file__).resolve().parent / "index.html").read_text(encoding="utf-8")
|