Spaces:
Running
Running
File size: 1,549 Bytes
8a74c03 |
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 |
#!/usr/bin/env python3
"""
Simple API test to check Hugging Face connectivity
"""
import requests
import json
from PIL import Image
import base64
from io import BytesIO
# Load settings
def load_settings():
try:
with open('settings.json', 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}
def test_simple_api():
"""Test basic API connectivity"""
settings = load_settings()
api_token = settings.get('hugging_face_api_token')
if not api_token:
print("No API token found")
return
print(f"Testing API connectivity with token: {api_token[:10]}...")
# Test with a simple image captioning model
API_URL = "https://api-inference.huggingface.co/models/nlpconnect/vit-gpt2-image-captioning"
headers = {"Authorization": f"Bearer {api_token}"}
# Create a simple test image (solid color)
test_image = Image.new('RGB', (224, 224), color='blue')
# Convert to bytes
buffer = BytesIO()
test_image.save(buffer, format="JPEG")
print("Making API request...")
response = requests.post(
API_URL,
headers=headers,
files={"data": buffer.getvalue()}
)
print(f"Response status: {response.status_code}")
print(f"Response headers: {dict(response.headers)}")
if response.status_code == 200:
print("SUCCESS!")
print(f"Response: {response.json()}")
else:
print(f"ERROR: {response.text}")
if __name__ == "__main__":
test_simple_api() |