File size: 1,506 Bytes
05e4d44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
import time
import requests
import datetime

# Thay bằng đường dẫn (URL) public của Hugging Face Space của bạn
# Ví dụ: "https://neshaki-arq-rag-turboquant.hf.space"
SPACE_URL = "https://neshaki-arq-rag-turboquant.hf.space"

# Thời gian chờ giữa mỗi lần ping (giây). HF Space thường sleep sau 48h, 
# ping mỗi 10-15 phút (600-900 giây) là đẹp nhất.
PING_INTERVAL = 600 

def keep_alive():
    print(f"[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 🚀 Bắt đầu chương trình chống Sleep cho Hugging Face Space!")
    print(f"🔗 Target URL: {SPACE_URL}")
    print(f"⏱️ Interval: {PING_INTERVAL} seconds ({PING_INTERVAL/60:.1f} minutes)\n")
    
    while True:
        try:
            # Gửi một GET request đơn giản tới endpoint gốc "/"
            response = requests.get(SPACE_URL, timeout=10)
            
            if response.status_code == 200:
                print(f"[{datetime.datetime.now().strftime('%H:%M:%S')}] ✅ Ping thành công! Space đang thức.")
            else:
                print(f"[{datetime.datetime.now().strftime('%H:%M:%S')}] ⚠️ Ping trả về HTTP {response.status_code}.")
        
        except requests.exceptions.RequestException as e:
            print(f"[{datetime.datetime.now().strftime('%H:%M:%S')}] ❌ Lỗi kết nối: {e}")
            
        # Nghỉ trước khi ping lần tiếp theo
        time.sleep(PING_INTERVAL)

if __name__ == "__main__":
    keep_alive()