Spaces:
Sleeping
Sleeping
| import time | |
| import subprocess | |
| import datetime | |
| # --------------------------------------------------------------------------- | |
| # OPTIONAL: Keep-Alive Script for Inference Servers | |
| # --------------------------------------------------------------------------- | |
| # This script sends periodic requests to keep inference servers active. | |
| # Configure your own endpoint and credentials below if needed. | |
| # | |
| # Note: This is optional and only useful if you're hosting your own | |
| # inference server that goes to sleep after inactivity. | |
| # --------------------------------------------------------------------------- | |
| # Replace with your own inference endpoint and credentials | |
| CURL_COMMAND = """ | |
| curl --location 'YOUR_INFERENCE_URL_HERE' \ | |
| --header 'Content-Type: application/json' \ | |
| --header 'Authorization: YOUR_AUTH_HEADER_HERE' \ | |
| --data '{ | |
| "model": "your-model-name", | |
| "messages": [ | |
| { | |
| "role": "user", | |
| "content": "Hello, this is a keep-alive ping." | |
| } | |
| ] | |
| }' | |
| """ | |
| # How often to send requests (in seconds) | |
| INTERVAL_SECONDS = 60 | |
| # --------------------------------------------------------------------------- | |
| def run_periodically(): | |
| print(f"Keep-alive script started: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") | |
| print(f"Interval: {INTERVAL_SECONDS} seconds") | |
| print("-" * 50) | |
| if "YOUR_INFERENCE_URL_HERE" in CURL_COMMAND: | |
| print("⚠️ WARNING: Please configure CURL_COMMAND with your actual endpoint!") | |
| print("⚠️ Edit this file and replace the placeholder values.") | |
| return | |
| while True: | |
| try: | |
| current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
| print(f"[{current_time}] Sending keep-alive request...") | |
| result = subprocess.run(CURL_COMMAND, shell=True, capture_output=True, text=True) | |
| if result.returncode == 0: | |
| print(f"✓ Success! Response (first 100 chars): {result.stdout[:100]}...") | |
| else: | |
| print(f"✗ Error code: {result.returncode}") | |
| print(f"Error output: {result.stderr}") | |
| except Exception as e: | |
| print(f"Unexpected error: {e}") | |
| print(f"Waiting {INTERVAL_SECONDS} seconds...") | |
| print("-" * 50) | |
| time.sleep(INTERVAL_SECONDS) | |
| if __name__ == "__main__": | |
| run_periodically() | |