Spaces:
Sleeping
Sleeping
ping and try to wake up agent
Browse files
app.py
CHANGED
|
@@ -43,12 +43,42 @@ def initialize_agent(endpoint_uri: str):
|
|
| 43 |
additional_authorized_imports=["pytz"]
|
| 44 |
)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def main():
|
| 48 |
# Load endpoint and check health
|
| 49 |
|
| 50 |
# Prechecks
|
| 51 |
endpoint_uri = load_huggingface_model() # Get the URI for the endpoint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
is_healthy, status_info = check_model_endpoint(endpoint_uri) # Test the endpoint
|
| 53 |
|
| 54 |
if not is_healthy:
|
|
|
|
| 43 |
additional_authorized_imports=["pytz"]
|
| 44 |
)
|
| 45 |
|
| 46 |
+
def wake_up_endpoint(endpoint_uri, max_wait=30):
|
| 47 |
+
"""Poll the endpoint until it responds OK or timeout."""
|
| 48 |
+
payload = {"inputs": "ping"} # Adjust to minimal valid input
|
| 49 |
+
start = time.time()
|
| 50 |
+
|
| 51 |
+
while time.time() - start < max_wait:
|
| 52 |
+
try:
|
| 53 |
+
print("Pinging endpoint...")
|
| 54 |
+
response = requests.post(endpoint_uri, json=payload, timeout=5)
|
| 55 |
+
|
| 56 |
+
if response.ok:
|
| 57 |
+
print("✅ Endpoint is awake.")
|
| 58 |
+
return True
|
| 59 |
+
|
| 60 |
+
# If it's a 503 or 504, it's likely still warming up
|
| 61 |
+
print(f"Response: {response.status_code}, retrying...")
|
| 62 |
+
except requests.exceptions.RequestException as e:
|
| 63 |
+
print(f"Exception: {e}, retrying...")
|
| 64 |
+
|
| 65 |
+
time.sleep(3) # fixed short backoff; can be exponential if needed
|
| 66 |
+
|
| 67 |
+
print("❌ Timed out waiting for endpoint to wake up.")
|
| 68 |
+
return False
|
| 69 |
|
| 70 |
def main():
|
| 71 |
# Load endpoint and check health
|
| 72 |
|
| 73 |
# Prechecks
|
| 74 |
endpoint_uri = load_huggingface_model() # Get the URI for the endpoint
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# Wake it up before health check
|
| 78 |
+
wake_up_successful = wake_up_endpoint(endpoint_uri)
|
| 79 |
+
if not wake_up_successful:
|
| 80 |
+
print("Warning: Could not wake up the endpoint. Continuing anyway...")
|
| 81 |
+
|
| 82 |
is_healthy, status_info = check_model_endpoint(endpoint_uri) # Test the endpoint
|
| 83 |
|
| 84 |
if not is_healthy:
|