File size: 853 Bytes
6bf9c98
36334a9
 
6bf9c98
 
26127a9
36334a9
26127a9
6bf9c98
26127a9
 
 
 
 
 
 
 
 
36334a9
6bf9c98
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# status_check.py

from typing import Optional, Tuple
import os
import requests
from urllib.parse import urlparse

    
def resolve_endpoint() -> Optional[str]:
    uri = (os.environ.get("HF_ENDPOINT_URI") or "").strip()
    if not uri:
        return None
    p = urlparse(uri)
    if p.scheme not in {"http", "https"} or not p.netloc:
        # treat bad values like “localhost:8000” (no scheme) as missing
        return None
    return uri
    

def is_endpoint_healthy(uri: str, timeout: float = 5.0) -> Tuple[bool, str]:
    if not uri:
        return False, "No endpoint URI configured."
    try:
        r = requests.post(uri, json={"inputs": "ping"}, timeout=timeout)
        return (True, "OK") if r.ok else (False, f"HTTP {r.status_code}")
    except requests.exceptions.RequestException as e:
        return False, f"{type(e).__name__}"