yetessam commited on
Commit
8aa8219
·
verified ·
1 Parent(s): 884bc8f

Update endpoint_utils.py

Browse files
Files changed (1) hide show
  1. endpoint_utils.py +36 -29
endpoint_utils.py CHANGED
@@ -4,7 +4,10 @@ from typing import Optional, Tuple, Callable, Dict, Any
4
  from urllib.parse import urlparse
5
  import os, time, requests
6
 
7
-
 
 
 
8
  def _valid_uri(uri: Optional[str]) -> bool:
9
  if not uri:
10
  return False
@@ -12,6 +15,13 @@ def _valid_uri(uri: Optional[str]) -> bool:
12
  return p.scheme in {"http", "https"} and bool(p.netloc)
13
 
14
 
 
 
 
 
 
 
 
15
  def wake_endpoint(
16
  uri: Optional[str],
17
  *,
@@ -28,35 +38,34 @@ def wake_endpoint(
28
  if not _valid_uri(uri):
29
  return False, "invalid or missing URI (expect http(s)://...)"
30
 
31
- headers: Dict[str, str] = {}
32
- tok = token or os.environ.get("HF_TOKEN")
33
- if tok:
34
- headers["Authorization"] = f"Bearer {tok}"
35
 
36
- # 0) Try a quick health check first (cheap)
37
- last_detail = "no response"
 
38
  try:
39
  hr = requests.get(f"{uri.rstrip('/')}/health", headers=headers, timeout=5)
40
  if hr.ok:
41
  log("✅ /health reports ready.")
42
  return True, None
43
- try:
44
- last_detail = (hr.json().get("error") or hr.json().get("message")) # type: ignore
45
- except Exception:
46
- last_detail = (hr.text or "").strip()
47
- log(f"[health] HTTP {hr.status_code} – {last_detail or 'warming?'}")
48
  except requests.RequestException as e:
49
- last_detail = type(e).__name__
50
- log(f"[health] {last_detail}")
51
 
52
- # 1) Initial nudge (ignore errors)
53
  payload = warm_payload if warm_payload is not None else {"inputs": "wake"}
54
  try:
55
  requests.post(uri, headers=headers, json=payload, timeout=5)
56
  except requests.RequestException:
57
  pass
58
 
59
- # 2) Poll until healthy or timeout
60
  deadline = time.time() + max_wait
61
  while time.time() < deadline:
62
  try:
@@ -65,24 +74,22 @@ def wake_endpoint(
65
  log("✅ Endpoint is awake and responsive.")
66
  return True, None
67
 
68
- # extract any helpful server message
69
- detail = ""
70
- try:
71
- data = r.json()
72
- detail = data.get("error") or data.get("message") or ""
73
- except ValueError:
74
- detail = (r.text or "").strip()
75
 
76
- last_detail = f"HTTP {r.status_code}" + (f" – {detail}" if detail else "")
77
  if r.status_code in (429, 503, 504):
78
- log(f"[server] {detail or 'warming up'} (HTTP {r.status_code}); retrying in {int(poll_every)}s…")
79
  else:
80
- log(f"[server] {detail or 'unexpected response'} (HTTP {r.status_code}); retrying in {int(poll_every)}s…")
81
 
82
  except requests.RequestException as e:
83
- last_detail = type(e).__name__
84
- log(f"[client] {last_detail}; retrying in {int(poll_every)}s…")
85
 
86
  time.sleep(poll_every)
87
 
88
- return False, f"Timed out after {max_wait}s — last status: {last_detail}"
 
 
4
  from urllib.parse import urlparse
5
  import os, time, requests
6
 
7
+ def hf_headers():
8
+ tok = os.environ.get("HF_TOKEN", "").strip()
9
+ return {"Authorization": f"Bearer {tok}"} if tok else {}
10
+
11
  def _valid_uri(uri: Optional[str]) -> bool:
12
  if not uri:
13
  return False
 
15
  return p.scheme in {"http", "https"} and bool(p.netloc)
16
 
17
 
18
+ def _detail(resp: requests.Response) -> str:
19
+ try:
20
+ j = resp.json()
21
+ return (j.get("error") or j.get("message") or "").strip()
22
+ except Exception:
23
+ return (resp.text or "").strip()
24
+
25
  def wake_endpoint(
26
  uri: Optional[str],
27
  *,
 
38
  if not _valid_uri(uri):
39
  return False, "invalid or missing URI (expect http(s)://...)"
40
 
41
+ headers = hf_headers()
42
+ if not headers:
43
+ log("⚠️ HF_TOKEN not set — POST / will likely return 401/403.")
 
44
 
45
+ last = "no response"
46
+
47
+ # /health probe (auth included if required)
48
  try:
49
  hr = requests.get(f"{uri.rstrip('/')}/health", headers=headers, timeout=5)
50
  if hr.ok:
51
  log("✅ /health reports ready.")
52
  return True, None
53
+ last = f"HTTP {hr.status_code} – {_detail(hr) or 'warming?'}"
54
+ log(f"[health] {last}")
55
+ if hr.status_code in (401, 403):
56
+ return False, f"Unauthorized (check HF_TOKEN). {last}"
 
57
  except requests.RequestException as e:
58
+ last = type(e).__name__
59
+ log(f"[health] {last}")
60
 
61
+ # warmup nudge
62
  payload = warm_payload if warm_payload is not None else {"inputs": "wake"}
63
  try:
64
  requests.post(uri, headers=headers, json=payload, timeout=5)
65
  except requests.RequestException:
66
  pass
67
 
68
+ # poll
69
  deadline = time.time() + max_wait
70
  while time.time() < deadline:
71
  try:
 
74
  log("✅ Endpoint is awake and responsive.")
75
  return True, None
76
 
77
+ d = _detail(r)
78
+ last = f"HTTP {r.status_code}" + (f" – {d}" if d else "")
79
+
80
+ if r.status_code in (401, 403):
81
+ return False, f"Unauthorized (check HF_TOKEN, org access). {last}"
 
 
82
 
 
83
  if r.status_code in (429, 503, 504):
84
+ log(f"[server] {d or 'warming up'} (HTTP {r.status_code}); retrying in {int(poll_every)}s…")
85
  else:
86
+ log(f"[server] {d or 'unexpected response'} (HTTP {r.status_code}); retrying in {int(poll_every)}s…")
87
 
88
  except requests.RequestException as e:
89
+ last = type(e).__name__
90
+ log(f"[client] {last}; retrying in {int(poll_every)}s…")
91
 
92
  time.sleep(poll_every)
93
 
94
+ return False, f"Timed out after {max_wait}s — last status: {last}"
95
+