Spaces:
Sleeping
Sleeping
Delete checks
Browse files- checks/endpoint_check.py +0 -85
- checks/failed_check.py +0 -46
- checks/health_check.py +0 -53
checks/endpoint_check.py
DELETED
|
@@ -1,85 +0,0 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import sys
|
| 3 |
-
|
| 4 |
-
'''
|
| 5 |
-
# Example usage:
|
| 6 |
-
endpoint = "https://api-inference.huggingface.co/models/gpt2"
|
| 7 |
-
status_info = check_endpoint(endpoint)
|
| 8 |
-
|
| 9 |
-
if status_info["status"]:
|
| 10 |
-
print("Success:", status_info["message"])
|
| 11 |
-
else:
|
| 12 |
-
print("Error:", status_info["message"])
|
| 13 |
-
print("Status Code:", status_info["status_code"])
|
| 14 |
-
print("Response Data:", status_info["response_data"])
|
| 15 |
-
'''
|
| 16 |
-
|
| 17 |
-
def check_public_endpoint(endpoint: str):
|
| 18 |
-
"""
|
| 19 |
-
Checks the given endpoint and provides a detailed status and message.
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
endpoint (str): The URL of the endpoint to check.
|
| 23 |
-
|
| 24 |
-
Returns:
|
| 25 |
-
dict: Contains status (True/False) and a message explaining the result.
|
| 26 |
-
"""
|
| 27 |
-
result = {
|
| 28 |
-
"status": False, # Default status is failure
|
| 29 |
-
"message": "Unknown error", # Default message
|
| 30 |
-
"status_code": None,
|
| 31 |
-
"response_data": None
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
try: # No Authorization header required for public models
|
| 35 |
-
response = requests.get(endpoint)
|
| 36 |
-
result["status_code"] = response.status_code
|
| 37 |
-
result["response_data"] = response.text
|
| 38 |
-
|
| 39 |
-
if response.status_code == 503:
|
| 40 |
-
result["status"] = True
|
| 41 |
-
result["message"] = "Endpoint is reachable and returned a service unavailable response."
|
| 42 |
-
|
| 43 |
-
if response.status_code == 200:
|
| 44 |
-
result["status"] = True
|
| 45 |
-
result["message"] = "Endpoint is reachable and returned a valid response."
|
| 46 |
-
else:
|
| 47 |
-
result["message"] = f"Request failed with status code {response.status_code}. Response: {response.text}"
|
| 48 |
-
|
| 49 |
-
except requests.exceptions.RequestException as e:
|
| 50 |
-
result["message"] = f"Request failed with exception: {e}"
|
| 51 |
-
|
| 52 |
-
return result
|
| 53 |
-
|
| 54 |
-
'''
|
| 55 |
-
# Check if the response status code is 200 and it returns inference data
|
| 56 |
-
if response.status_code == 200:
|
| 57 |
-
# Public models will return inference data without needing an API key
|
| 58 |
-
# Attempt to parse JSON response
|
| 59 |
-
response_json = response.json()
|
| 60 |
-
# Print the first few keys of the response JSON for debugging
|
| 61 |
-
print(f"Response JSON keys: {list(response_json.keys())[:5]}")
|
| 62 |
-
|
| 63 |
-
# Public models will return inference data without needing an API key
|
| 64 |
-
if "model" in response_json or "error" in response_json:
|
| 65 |
-
return True
|
| 66 |
-
else:
|
| 67 |
-
print("The response does not contain inference-related data.")
|
| 68 |
-
return False
|
| 69 |
-
|
| 70 |
-
'''
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def is_huggingface_endpoint(endpoint: str):
|
| 74 |
-
try:
|
| 75 |
-
headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_KEY"}
|
| 76 |
-
response = requests.get(endpoint, headers=headers)
|
| 77 |
-
|
| 78 |
-
if response.status_code == 200 and "model" in response.json():
|
| 79 |
-
return True
|
| 80 |
-
else:
|
| 81 |
-
print("This is NOT a Hugging Face Inference Endpoint.")
|
| 82 |
-
return False
|
| 83 |
-
except requests.exceptions.RequestException as e:
|
| 84 |
-
print(f"Request failed: {e}")
|
| 85 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
checks/failed_check.py
DELETED
|
@@ -1,46 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import time
|
| 3 |
-
import random
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Function to simulate the status of the app
|
| 8 |
-
def check_app_status():
|
| 9 |
-
# Simulate different app statuses
|
| 10 |
-
status_options = [
|
| 11 |
-
"The app is building. Please wait a few moments...",
|
| 12 |
-
"The app is restarting. Hold on...",
|
| 13 |
-
"The endpoint is starting up. It might take a few minutes...",
|
| 14 |
-
"Payment is needed for inferences. Please complete payment to continue.",
|
| 15 |
-
"The endpoint is scaled to zero due to inactivity. Starting it now...",
|
| 16 |
-
|
| 17 |
-
]
|
| 18 |
-
|
| 19 |
-
# Simulate a real condition check (for demonstration, we randomly select one status)
|
| 20 |
-
current_status = random.choice(status_options)
|
| 21 |
-
|
| 22 |
-
# If the endpoint is scaled to zero, simulate the time it takes to start
|
| 23 |
-
if current_status == "The endpoint is scaled to zero due to inactivity. Starting it now...":
|
| 24 |
-
time.sleep(5) # Simulate the time it takes to start the endpoint
|
| 25 |
-
|
| 26 |
-
# Simulate some delay for other operations (like checking the status)
|
| 27 |
-
time.sleep(2)
|
| 28 |
-
return "App start up failure, please check back in a day or two"
|
| 29 |
-
return current_status
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Function to simulate the button click event in Gradio UI
|
| 33 |
-
def get_status():
|
| 34 |
-
return check_app_status()
|
| 35 |
-
|
| 36 |
-
# Create the Gradio interface
|
| 37 |
-
def create_failed_gradio_ui(status_info):
|
| 38 |
-
with gr.Blocks() as interface:
|
| 39 |
-
gr.Markdown(f"## Inference Endpoint Status")
|
| 40 |
-
gr.Markdown(f"### Status Code: {status_info['status_code']}")
|
| 41 |
-
gr.Markdown(f"### Message: {status_info['message']}")
|
| 42 |
-
gr.JSON(status_info["response_data"], label="Response Data")
|
| 43 |
-
return interface
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
checks/health_check.py
DELETED
|
@@ -1,53 +0,0 @@
|
|
| 1 |
-
# checks/health_check.py
|
| 2 |
-
import os
|
| 3 |
-
import requests
|
| 4 |
-
from typing import Dict, Tuple
|
| 5 |
-
|
| 6 |
-
from checks.endpoint_check import check_public_endpoint
|
| 7 |
-
|
| 8 |
-
def check_with_prompt(endpoint_uri: str):
|
| 9 |
-
|
| 10 |
-
# Try a direct test of your endpoint
|
| 11 |
-
test_prompt = "Hello, how are you?"
|
| 12 |
-
response = requests.post(
|
| 13 |
-
endpoint_uri,
|
| 14 |
-
json={"inputs": test_prompt},
|
| 15 |
-
headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
| 16 |
-
)
|
| 17 |
-
print(response.json())
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def check_model_endpoint(endpoint_uri: str) -> Tuple[bool, Dict]:
|
| 22 |
-
"""Comprehensive check of model endpoint health"""
|
| 23 |
-
# Basic availability check
|
| 24 |
-
status_info = check_public_endpoint(endpoint_uri)
|
| 25 |
-
|
| 26 |
-
if not status_info["status"] or status_info["status_code"] in [503, 502]:
|
| 27 |
-
return False, status_info
|
| 28 |
-
|
| 29 |
-
# Test actual model response
|
| 30 |
-
try:
|
| 31 |
-
test_prompt = "Hello, how are you?"
|
| 32 |
-
response = requests.post(
|
| 33 |
-
endpoint_uri,
|
| 34 |
-
json={"inputs": test_prompt},
|
| 35 |
-
headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"},
|
| 36 |
-
timeout=10
|
| 37 |
-
)
|
| 38 |
-
response.raise_for_status()
|
| 39 |
-
return True, {
|
| 40 |
-
"status": True,
|
| 41 |
-
"status_code": response.status_code,
|
| 42 |
-
"response": response.json()
|
| 43 |
-
}
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return False, {
|
| 46 |
-
"status": False,
|
| 47 |
-
"status_code": 500,
|
| 48 |
-
"error": str(e)
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
def should_launch_ui(status_info: Dict) -> bool:
|
| 52 |
-
"""Determine if UI should be launched based on status"""
|
| 53 |
-
return status_info.get("status", False) and status_info.get("status_code", 500) == 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|