greenai / check_deployed_api.py
Surajkumaar's picture
d5
850e269
#!/usr/bin/env python3
"""
Check if the deployed Hugging Face Space API has the updated code
"""
import requests
import sys
def check_deployed_api(api_url):
"""Check if the deployed API has the health endpoint and shows correct version"""
print("=" * 60)
print("CHECKING DEPLOYED API")
print("=" * 60)
print(f"API URL: {api_url}")
print()
try:
# Check health endpoint
print("1. Checking /health endpoint...")
response = requests.get(f"{api_url}/health", timeout=10)
if response.status_code == 200:
data = response.json()
print(" βœ… API is online!")
print(f" Status: {data.get('status')}")
print(f" Storage: {data.get('storage')}")
print(f" Supabase: {data.get('apis', {}).get('supabase')}")
print()
else:
print(f" ❌ Health check failed: HTTP {response.status_code}")
return False
# Check root endpoint for version info
print("2. Checking / endpoint for version...")
response = requests.get(f"{api_url}/", timeout=10)
if response.status_code == 200:
data = response.json()
print(" βœ… Root endpoint accessible!")
print(f" Message: {data.get('message')}")
print(f" Version: {data.get('version')}")
print(f" Data Storage: {data.get('data_storage')}")
print()
else:
print(f" ❌ Root endpoint failed: HTTP {response.status_code}")
# Try a test submission to see error handling
print("3. Testing error handling (this should fail gracefully)...")
print(" Sending invalid submission to check error messages...")
# This should fail with a proper error message
test_data = {
'species_name': 'TEST_SPECIES',
'location': 'INVALID_LOCATION', # This should fail validation
'species_type': 'plant'
}
response = requests.post(
f"{api_url}/submit-new-species",
data=test_data,
timeout=10
)
if response.status_code == 400:
error_data = response.json()
print(" βœ… Error handling works!")
print(f" Error message: {error_data.get('detail')}")
# Check if it's the NEW error message format
if "Invalid location" in error_data.get('detail', ''):
print(" βœ… NEW CODE DETECTED - Error messages are specific!")
else:
print(" ⚠️ OLD CODE - Error messages are generic")
else:
print(f" Status: {response.status_code}")
print(f" Response: {response.text[:200]}")
print()
print("=" * 60)
print("βœ… API CHECK COMPLETE")
print("=" * 60)
print()
print("Next steps:")
print("1. If API is online but using old code:")
print(" β†’ Restart the Hugging Face Space")
print("2. If API is offline:")
print(" β†’ Check Hugging Face Space logs for errors")
print("3. Test with a real species submission from the frontend")
print()
return True
except requests.exceptions.ConnectionError:
print(" ❌ Connection Error - Cannot reach the API")
print(" β†’ Check if the Hugging Face Space is running")
print(" β†’ Verify the API URL is correct")
return False
except requests.exceptions.Timeout:
print(" ❌ Timeout - API is not responding")
print(" β†’ The Space might be starting up (wait 2-3 minutes)")
return False
except Exception as e:
print(f" ❌ Error: {e}")
return False
if __name__ == "__main__":
# Default API URL - update this with your actual Hugging Face Space URL
api_url = input("Enter your Hugging Face Space API URL (e.g., https://YOUR-USERNAME-YOUR-SPACE.hf.space): ").strip()
if not api_url:
print("No URL provided. Using example...")
api_url = "http://localhost:7860" # Fallback to local
# Remove trailing slash if present
api_url = api_url.rstrip('/')
check_deployed_api(api_url)