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

Fix HF Space unstyled UI: stop overriding Gradio proxy settings

Browse files

Forcing server_name=0.0.0.0 made Gradio serve CSS from http://0.0.0.0,
which breaks behind the HF HTTPS proxy (plain black-and-white HTML).
Use default demo.launch() on Spaces; keep custom bind only for local runs.

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

Files changed (2) hide show
  1. app.py +13 -17
  2. setup.sh +4 -6
app.py CHANGED
@@ -14,14 +14,14 @@ def _is_hf_space() -> bool:
14
 
15
 
16
  def _configure_runtime() -> None:
17
- os.environ.setdefault("GRADIO_SERVER_PORT", os.environ.get("PORT", "7860"))
18
  os.environ.setdefault(
19
  "PREBACKBONE_ONLY_WEIGHTS",
20
  str(_ROOT / "weights" / "prebackbone_a11_ca.pt"),
21
  )
22
  if _is_hf_space():
23
  os.environ.setdefault("PRELOAD_MODEL", "1")
24
- os.environ.setdefault("GRADIO_SERVER_NAME", "0.0.0.0")
 
25
 
26
 
27
  _configure_runtime()
@@ -65,7 +65,6 @@ def run_demo(defect_path: str | None, reference_path: str | None):
65
 
66
 
67
  def _example_pairs() -> list[list[str]]:
68
- """Curated examples (Interface layout; keep short for HF iframe width)."""
69
  pairs: list[list[str]] = []
70
  ex_dir = _ROOT / "examples"
71
  if not ex_dir.is_dir():
@@ -101,7 +100,6 @@ def _preload_model() -> None:
101
 
102
 
103
  def build_demo() -> gr.Interface:
104
- """gr.Interface gives the same polished card layout locally and on HF Spaces."""
105
  return gr.Interface(
106
  fn=run_demo,
107
  inputs=[
@@ -125,13 +123,7 @@ def build_demo() -> gr.Interface:
125
  demo = build_demo()
126
 
127
 
128
- def _launch_kwargs() -> dict:
129
- if _is_hf_space():
130
- return {
131
- "server_name": "0.0.0.0",
132
- "server_port": int(os.environ.get("PORT", "7860")),
133
- "share": False,
134
- }
135
  server_name = os.environ.get("GRADIO_SERVER_NAME", "127.0.0.1")
136
  preferred_port = int(os.environ.get("PORT", os.environ.get("GRADIO_SERVER_PORT", "7860")))
137
  bind_host = "127.0.0.1" if server_name in ("127.0.0.1", "localhost") else server_name
@@ -159,9 +151,13 @@ if __name__ == "__main__":
159
  if os.environ.get("PRELOAD_MODEL", "0").lower() in ("1", "true", "yes"):
160
  _preload_model()
161
 
162
- demo.launch(
163
- **(_launch_kwargs()),
164
- show_error=True,
165
- inbrowser=False,
166
- prevent_thread_lock=False,
167
- )
 
 
 
 
 
14
 
15
 
16
  def _configure_runtime() -> None:
 
17
  os.environ.setdefault(
18
  "PREBACKBONE_ONLY_WEIGHTS",
19
  str(_ROOT / "weights" / "prebackbone_a11_ca.pt"),
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()
 
65
 
66
 
67
  def _example_pairs() -> list[list[str]]:
 
68
  pairs: list[list[str]] = []
69
  ex_dir = _ROOT / "examples"
70
  if not ex_dir.is_dir():
 
100
 
101
 
102
  def build_demo() -> gr.Interface:
 
103
  return gr.Interface(
104
  fn=run_demo,
105
  inputs=[
 
123
  demo = build_demo()
124
 
125
 
126
+ def _local_launch_kwargs() -> dict:
 
 
 
 
 
 
127
  server_name = os.environ.get("GRADIO_SERVER_NAME", "127.0.0.1")
128
  preferred_port = int(os.environ.get("PORT", os.environ.get("GRADIO_SERVER_PORT", "7860")))
129
  bind_host = "127.0.0.1" if server_name in ("127.0.0.1", "localhost") else server_name
 
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
+ )
setup.sh CHANGED
@@ -1,12 +1,10 @@
1
  #!/usr/bin/env bash
2
- # Hugging Face Spaces: install deps before app starts (no ultralytics).
3
  set -euo pipefail
4
  cd "$(dirname "$0")"
5
  pip install -q -r requirements.txt
6
- pip install -q "numpy>=1.23.0,<2" "gradio-client==1.7.0"
7
- python -c "import gradio_patch; gradio_patch.apply(); print('gradio_patch OK')"
8
- export GRADIO_SERVER_NAME="${GRADIO_SERVER_NAME:-0.0.0.0}"
9
- export GRADIO_SERVER_PORT="${GRADIO_SERVER_PORT:-${PORT:-7860}}"
10
  export PRELOAD_MODEL="${PRELOAD_MODEL:-1}"
11
  export PREBACKBONE_ONLY_WEIGHTS="${PREBACKBONE_ONLY_WEIGHTS:-$(pwd)/weights/prebackbone_a11_ca.pt}"
12
- echo "RefDiffNet: standalone A11_CA (GRADIO_SERVER_NAME=${GRADIO_SERVER_NAME})"
 
1
  #!/usr/bin/env bash
2
+ # Hugging Face Spaces: install ML deps only (Gradio version comes from README sdk_version).
3
  set -euo pipefail
4
  cd "$(dirname "$0")"
5
  pip install -q -r requirements.txt
6
+ pip install -q "numpy>=1.23.0,<2"
7
+ # Do not reinstall gradio-client here — mismatched client breaks HF Space CSS/JS loading.
 
 
8
  export PRELOAD_MODEL="${PRELOAD_MODEL:-1}"
9
  export PREBACKBONE_ONLY_WEIGHTS="${PREBACKBONE_ONLY_WEIGHTS:-$(pwd)/weights/prebackbone_a11_ca.pt}"
10
+ echo "RefDiffNet: deps ready (gradio from sdk_version in README)"