ShashwatGupta23 Cursor commited on
Commit
fbb9697
·
1 Parent(s): 3fd90d5

Fix HF unstyled UI: disable SSR, stop pip-overriding Gradio

Browse files

Gradio 5 SSR on Spaces often serves plain HTML without CSS when Node
is unavailable. Pin sdk_version 5.23.0 and remove gradio from
requirements.txt so HF's bundled frontend matches the server.

Co-authored-by: Cursor <cursoragent@cursor.com>

Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +15 -10
  3. requirements.txt +1 -3
  4. run.sh +59 -0
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🔬
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: "5.16.1"
8
  python_version: "3.10"
9
  app_file: app.py
10
  pinned: false
 
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: "5.23.0"
8
  python_version: "3.10"
9
  app_file: app.py
10
  pinned: false
app.py CHANGED
@@ -6,6 +6,9 @@ import os
6
  import socket
7
  from pathlib import Path
8
 
 
 
 
9
  _ROOT = Path(__file__).resolve().parent
10
 
11
 
@@ -20,8 +23,6 @@ def _configure_runtime() -> None:
20
  )
21
  if _is_hf_space():
22
  os.environ.setdefault("PRELOAD_MODEL", "1")
23
- # Do NOT set GRADIO_SERVER_NAME here — HF injects GRADIO_ROOT_PATH; forcing
24
- # 0.0.0.0 breaks CSS/JS (unstyled black-and-white page behind the proxy).
25
 
26
 
27
  _configure_runtime()
@@ -146,18 +147,22 @@ def _local_launch_kwargs() -> dict:
146
  }
147
 
148
 
149
- if __name__ == "__main__":
150
- print(f"[RefDiffNet] gradio {getattr(gr, '__version__', 'unknown')}")
151
- if os.environ.get("PRELOAD_MODEL", "0").lower() in ("1", "true", "yes"):
152
- _preload_model()
153
-
154
  if _is_hf_space():
155
- # HF Spaces proxy sets PORT + GRADIO_ROOT_PATH — plain launch keeps themed UI.
156
- demo.launch(show_error=True)
157
  else:
158
  demo.launch(
159
  **_local_launch_kwargs(),
160
- show_error=True,
161
  inbrowser=False,
162
  prevent_thread_lock=False,
 
163
  )
 
 
 
 
 
 
 
 
6
  import socket
7
  from pathlib import Path
8
 
9
+ # HF Spaces: SSR needs Node and often serves unstyled HTML when it fails.
10
+ os.environ.setdefault("GRADIO_SSR_MODE", "false")
11
+
12
  _ROOT = Path(__file__).resolve().parent
13
 
14
 
 
23
  )
24
  if _is_hf_space():
25
  os.environ.setdefault("PRELOAD_MODEL", "1")
 
 
26
 
27
 
28
  _configure_runtime()
 
147
  }
148
 
149
 
150
+ def _launch() -> None:
151
+ """Launch with SSR off (required for themed UI on HF Spaces)."""
152
+ kwargs: dict = {"show_error": True, "ssr_mode": False}
 
 
153
  if _is_hf_space():
154
+ demo.launch(**kwargs)
 
155
  else:
156
  demo.launch(
157
  **_local_launch_kwargs(),
 
158
  inbrowser=False,
159
  prevent_thread_lock=False,
160
+ **kwargs,
161
  )
162
+
163
+
164
+ if __name__ == "__main__":
165
+ print(f"[RefDiffNet] gradio {getattr(gr, '__version__', 'unknown')} ssr_mode=False")
166
+ if os.environ.get("PRELOAD_MODEL", "0").lower() in ("1", "true", "yes"):
167
+ _preload_model()
168
+ _launch()
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- # Gradio version matches README sdk_version (Hugging Face Spaces)
2
  # Python 3.13+ removed stdlib audioop; pydub (gradio dep) needs this backport
3
  audioop-lts>=0.2.1; python_version >= "3.13"
4
  torch>=2.0.0
@@ -6,6 +6,4 @@ torchvision>=0.15.0
6
  pillow>=10.0.0,<11
7
  # torch 2.2.x wheels are built against NumPy 1.x
8
  numpy>=1.23.0,<2
9
- gradio==5.16.1
10
- gradio-client==1.7.0
11
  huggingface_hub>=0.23.0,<1.0
 
1
+ # ML deps only — Gradio version is installed by HF from README sdk_version (do not pin here).
2
  # Python 3.13+ removed stdlib audioop; pydub (gradio dep) needs this backport
3
  audioop-lts>=0.2.1; python_version >= "3.13"
4
  torch>=2.0.0
 
6
  pillow>=10.0.0,<11
7
  # torch 2.2.x wheels are built against NumPy 1.x
8
  numpy>=1.23.0,<2
 
 
9
  huggingface_hub>=0.23.0,<1.0
run.sh ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Launch RefDiffNet demo (lightweight: skip reinstall if deps OK)
3
+ set -euo pipefail
4
+ cd "$(dirname "$0")"
5
+
6
+ if [[ -f "${HOME}/anaconda3/etc/profile.d/conda.sh" ]]; then
7
+ # shellcheck source=/dev/null
8
+ source "${HOME}/anaconda3/etc/profile.d/conda.sh"
9
+ conda activate pcb
10
+ fi
11
+
12
+ PY="${CONDA_PREFIX:-}/bin/python"
13
+ PIP="${CONDA_PREFIX:-}/bin/pip"
14
+ SITE="${CONDA_PREFIX:-}/lib/python3.10/site-packages"
15
+
16
+ echo "==> Python: $(which python)"
17
+
18
+ need_numpy_fix() {
19
+ ! "${PY}" -c "import numpy; assert numpy.__version__.startswith('1.')" 2>/dev/null
20
+ }
21
+
22
+ fix_numpy() {
23
+ echo "==> numpy 1.x (torch 2.2)"
24
+ if ! "${PIP}" install --no-cache-dir --ignore-installed "numpy>=1.23.0,<2" 2>/dev/null; then
25
+ rm -rf "${SITE}"/numpy "${SITE}"/numpy-*.dist-info 2>/dev/null || true
26
+ "${PIP}" install --no-cache-dir "numpy>=1.23.0,<2"
27
+ fi
28
+ }
29
+
30
+ need_gradio_fix() {
31
+ ! "${PY}" -c "import gradio_patch; gradio_patch.apply(); import gradio" 2>/dev/null
32
+ }
33
+
34
+ fix_gradio() {
35
+ echo "==> gradio 5.23.0 (match HF sdk_version)"
36
+ "${PIP}" install --no-cache-dir "gradio==5.23.0"
37
+ }
38
+
39
+ free_port() {
40
+ local p="$1"
41
+ if lsof -i:"${p}" -sTCP:LISTEN -t >/dev/null 2>&1; then
42
+ echo " Stopping process on port ${p}..."
43
+ kill "$(lsof -t -i:"${p}" -sTCP:LISTEN 2>/dev/null)" 2>/dev/null || true
44
+ sleep 1
45
+ fi
46
+ }
47
+
48
+ need_numpy_fix && fix_numpy
49
+ need_gradio_fix && fix_gradio
50
+ free_port 7860
51
+
52
+ export PYTHONNOUSERSITE=1
53
+ export PREBACKBONE_ONLY_WEIGHTS="${PREBACKBONE_ONLY_WEIGHTS:-weights/prebackbone_a11_ca.pt}"
54
+ export PRELOAD_MODEL="${PRELOAD_MODEL:-0}"
55
+ export GRADIO_SHARE="${GRADIO_SHARE:-0}"
56
+ # Low RAM: PREBACKBONE_DEVICE=cpu bash run.sh
57
+
58
+ echo "==> Starting app (PRELOAD_MODEL=${PRELOAD_MODEL} GRADIO_SHARE=${GRADIO_SHARE})"
59
+ exec "${PY}" app.py