Spaces:
Running
Running
| 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() | |