File size: 951 Bytes
1a7acd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
import os
import requests
import logging
import sys

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

def ping_backend():
    url = os.environ.get("HF_BACKEND_URL")
    if not url:
        logging.error("HF_BACKEND_URL environment variable is not set.")
        sys.exit(1)

    try:
        logging.info(f"Pinging Hugging Face backend at: {url}")
        # Sending a simple GET request to wake up the Hugging Face Space
        # Even if the endpoint returns 404, the space will wake up.
        response = requests.get(url, timeout=15)
        
        # We don't raise_for_status() because a 404 on '/' still means the backend is awake
        logging.info(f"Ping completed. Status Code: {response.status_code}")
        
    except requests.exceptions.RequestException as e:
        logging.error(f"Failed to ping backend. Error: {e}")
        sys.exit(1)

if __name__ == "__main__":
    ping_backend()