mitudrudutta commited on
Commit
34a93bb
·
1 Parent(s): bd4f36c

feat: add canonical demo URLs and update root endpoint response

Browse files
Files changed (2) hide show
  1. server/app.py +33 -2
  2. tests/test_api.py +2 -0
server/app.py CHANGED
@@ -3,6 +3,7 @@
3
  from __future__ import annotations
4
 
5
  import logging
 
6
 
7
  from fastapi import HTTPException
8
  from fastapi.responses import JSONResponse
@@ -59,10 +60,39 @@ try:
59
  except Exception:
60
  logging.getLogger(__name__).warning("Gradio demo unavailable", exc_info=True)
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  @app.get("/")
64
  def root() -> JSONResponse:
65
- """Return a lightweight root response for HF Space and validator pings."""
 
 
 
 
 
66
 
67
  return JSONResponse(
68
  {
@@ -71,7 +101,8 @@ def root() -> JSONResponse:
71
  "docs_url": "/docs",
72
  "health_url": "/health",
73
  "tasks_url": "/tasks",
74
- "demo_url": "/demo",
 
75
  }
76
  )
77
 
 
3
  from __future__ import annotations
4
 
5
  import logging
6
+ import os
7
 
8
  from fastapi import HTTPException
9
  from fastapi.responses import JSONResponse
 
60
  except Exception:
61
  logging.getLogger(__name__).warning("Gradio demo unavailable", exc_info=True)
62
 
63
+ # Canonical Space card URL for README / judges (not the relative /demo path).
64
+ _DEFAULT_DEMO_SPACE_URL = "https://huggingface.co/spaces/mitudrudutta/ChargeBackOps"
65
+
66
+
67
+ def _canonical_demo_space_url() -> str:
68
+ """Human-facing Hugging Face Space URL (Space card + embedded app)."""
69
+
70
+ space_id = (os.environ.get("SPACE_ID") or "").strip()
71
+ if space_id:
72
+ return f"https://huggingface.co/spaces/{space_id}"
73
+ override = (os.environ.get("DEMO_SPACE_URL") or "").strip()
74
+ if override:
75
+ return override
76
+ return _DEFAULT_DEMO_SPACE_URL
77
+
78
+
79
+ def _interactive_demo_url() -> str:
80
+ """Same-origin Gradio mount; absolute URL when Hugging Face sets SPACE_HOST."""
81
+
82
+ host = (os.environ.get("SPACE_HOST") or "").strip()
83
+ if host:
84
+ return f"https://{host.rstrip('/')}/demo/"
85
+ return "/demo"
86
+
87
 
88
  @app.get("/")
89
  def root() -> JSONResponse:
90
+ """Return a lightweight root response for HF Space and validator pings.
91
+
92
+ ``demo_url`` is always the canonical Hugging Face Space page (shareable,
93
+ stable, matches README badges). ``interactive_demo_url`` is the live
94
+ Gradio app on this deployment (relative locally, absolute on Spaces).
95
+ """
96
 
97
  return JSONResponse(
98
  {
 
101
  "docs_url": "/docs",
102
  "health_url": "/health",
103
  "tasks_url": "/tasks",
104
+ "demo_url": _canonical_demo_space_url(),
105
+ "interactive_demo_url": _interactive_demo_url(),
106
  }
107
  )
108
 
tests/test_api.py CHANGED
@@ -16,6 +16,8 @@ def test_root_endpoint_payload():
16
  assert b"ChargebackOps" in response.body
17
  assert b"tasks_url" in response.body
18
  assert b"demo_url" in response.body
 
 
19
 
20
 
21
  def test_baseline_endpoint_works_without_api_key(monkeypatch):
 
16
  assert b"ChargebackOps" in response.body
17
  assert b"tasks_url" in response.body
18
  assert b"demo_url" in response.body
19
+ assert b"huggingface.co/spaces" in response.body
20
+ assert b"interactive_demo_url" in response.body
21
 
22
 
23
  def test_baseline_endpoint_works_without_api_key(monkeypatch):