File size: 2,237 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
"""
Test with known working Hugging Face models
"""
import requests
import json
from PIL import Image
from io import BytesIO

def load_settings():
    try:
        with open('settings.json', 'r') as f:
            return json.load(f)
    except FileNotFoundError:
        return {}

def test_working_models():
    """Test with models that are known to work"""
    settings = load_settings()
    api_token = settings.get('hugging_face_api_token')
    
    if not api_token:
        print("No API token found")
        return
    
    print(f"Testing with token: {api_token[:10]}...")
    
    # Create a simple test image
    test_image = Image.new('RGB', (224, 224), color='red')
    buffer = BytesIO()
    test_image.save(buffer, format="JPEG")
    image_bytes = buffer.getvalue()
    
    # Test different API approaches
    models_to_test = [
        "Salesforce/blip-image-captioning-base-large",
        "microsoft/DialoGPT-medium",
        "google/vit-base-patch16-224"
    ]
    
    for model_name in models_to_test:
        print(f"\nTesting {model_name}...")
        
        API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
        headers = {"Authorization": f"Bearer {api_token}"}
        
        # Try different payload formats
        response = requests.post(
            API_URL,
            headers=headers,
            data=image_bytes
        )
        
        print(f"Status: {response.status_code}")
        
        if response.status_code == 200:
            print(f"SUCCESS! Response: {response.json()}")
            break
        elif response.status_code == 503:
            print("Model is loading, please wait...")
        else:
            print(f"Error: {response.text}")

    # Also test token validity
    print("\nTesting token validity...")
    headers = {"Authorization": f"Bearer {api_token}"}
    response = requests.get("https://huggingface.co/api/whoami", headers=headers)
    print(f"Token check status: {response.status_code}")
    if response.status_code == 200:
        print(f"Token is valid. User info: {response.json()}")
    else:
        print(f"Token validation failed: {response.text}")

if __name__ == "__main__":
    test_working_models()