File size: 2,641 Bytes
647f69c b2e88b8 647f69c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
#!/usr/bin/env python3
"""
Quick API test for SAM3 endpoint
Usage: python test_api.py
"""
import requests
import base64
import sys
ENDPOINT_URL = "https://p6irm2x7y9mwp4l4.us-east-1.aws.endpoints.huggingface.cloud"
def test_health():
"""Test health endpoint"""
print("Testing /health endpoint...")
response = requests.get(f"{ENDPOINT_URL}/health")
if response.status_code == 200:
data = response.json()
print(f"β
Health check passed")
print(f" Model: {data['model']}")
print(f" GPU: {'Available' if data['gpu_available'] else 'Not available'}")
print(f" VRAM: {data['vram']['free_gb']:.1f}GB free / {data['vram']['total_gb']:.1f}GB total")
return True
else:
print(f"β Health check failed: {response.status_code}")
return False
def test_inference():
"""Test inference with sample image"""
print("\nTesting inference endpoint...")
# Load test image
import os
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(script_dir))
test_image_path = os.path.join(project_root, "assets", "test_images", "test.jpg")
try:
with open(test_image_path, "rb") as f:
image_b64 = base64.b64encode(f.read()).decode()
except FileNotFoundError:
print(f"β Test image not found at: {test_image_path}")
return False
# Make request
response = requests.post(
ENDPOINT_URL,
json={
"inputs": image_b64,
"parameters": {
"classes": ["pothole", "asphalt"]
}
},
timeout=30
)
if response.status_code == 200:
results = response.json()
print(f"β
Inference successful ({response.elapsed.total_seconds():.2f}s)")
print(f" Generated {len(results)} masks:")
for result in results:
mask_size = len(base64.b64decode(result['mask']))
print(f" - {result['label']}: {mask_size:,} bytes (score: {result['score']:.2f})")
return True
else:
print(f"β Inference failed: {response.status_code}")
print(f" Response: {response.text}")
return False
def main():
print("=" * 60)
print("SAM3 API Test")
print("=" * 60)
print(f"Endpoint: {ENDPOINT_URL}\n")
health_ok = test_health()
inference_ok = test_inference()
print("\n" + "=" * 60)
if health_ok and inference_ok:
print("β
All tests passed!")
sys.exit(0)
else:
print("β Some tests failed")
sys.exit(1)
if __name__ == "__main__":
main()
|