yetessam commited on
Commit
074f0cf
·
verified ·
1 Parent(s): 8aa8219

Update status_check.py

Browse files
Files changed (1) hide show
  1. status_check.py +34 -6
status_check.py CHANGED
@@ -5,6 +5,9 @@ import os
5
  import requests
6
  from urllib.parse import urlparse
7
 
 
 
 
8
 
9
  def resolve_endpoint() -> Optional[str]:
10
  uri = (os.environ.get("HF_ENDPOINT_URI") or "").strip()
@@ -17,11 +20,36 @@ def resolve_endpoint() -> Optional[str]:
17
  return uri
18
 
19
 
20
- def is_endpoint_healthy(uri: str, timeout: float = 5.0) -> Tuple[bool, str]:
 
21
  if not uri:
22
- return False, "No endpoint URI configured."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  try:
24
- r = requests.post(uri, json={"inputs": "ping"}, timeout=timeout)
25
- return (True, "OK") if r.ok else (False, f"HTTP {r.status_code}")
26
- except requests.exceptions.RequestException as e:
27
- return False, f"{type(e).__name__}"
 
 
 
 
 
 
 
5
  import requests
6
  from urllib.parse import urlparse
7
 
8
+ from auth_helpers import hf_headers # or same module where you defined it
9
+
10
+
11
 
12
  def resolve_endpoint() -> Optional[str]:
13
  uri = (os.environ.get("HF_ENDPOINT_URI") or "").strip()
 
20
  return uri
21
 
22
 
23
+
24
+ def is_endpoint_healthy(uri: Optional[str], timeout: float = 5.0) -> Tuple[bool, str]:
25
  if not uri:
26
+ return False, "no URI configured"
27
+
28
+ headers = hf_headers()
29
+
30
+ # Try /health first (some endpoints support it)
31
+ try:
32
+ r = requests.get(f"{uri.rstrip('/')}/health", headers=headers, timeout=timeout)
33
+ if r.ok:
34
+ return True, "OK"
35
+ try:
36
+ msg = (r.json().get("error") or r.json().get("message")) # type: ignore
37
+ except Exception:
38
+ msg = (r.text or "").strip()
39
+ if r.status_code in (401, 403):
40
+ return False, f"HTTP {r.status_code} – Unauthorized (check HF_TOKEN). {msg}"
41
+ except requests.RequestException:
42
+ pass
43
+
44
+ # Fallback: POST /
45
  try:
46
+ r = requests.post(uri, headers=headers, json={"inputs": "ping"}, timeout=timeout)
47
+ if r.ok:
48
+ return True, "OK"
49
+ try:
50
+ msg = (r.json().get("error") or r.json().get("message")) # type: ignore
51
+ except Exception:
52
+ msg = (r.text or "").strip()
53
+ return False, f"HTTP {r.status_code}" + (f" – {msg}" if msg else "")
54
+ except requests.RequestException as e:
55
+ return False, type(e).__name__