|
|
|
|
|
"""Test HF token with modern InferenceClient approach.""" |
|
|
|
|
|
import os |
|
|
|
|
|
from dotenv import load_dotenv |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
def test_hf_token(): |
|
|
"""Test HF token with different approaches.""" |
|
|
token = os.getenv("HF_TOKEN") |
|
|
print(f"Token loaded: {token[:20]}..." if token else "No token found") |
|
|
|
|
|
if not token: |
|
|
print("β No HF_TOKEN found in environment") |
|
|
return None |
|
|
|
|
|
|
|
|
print("\nπ Testing with InferenceClient (recommended)...") |
|
|
try: |
|
|
from huggingface_hub import InferenceClient |
|
|
client = InferenceClient(token=token) |
|
|
result = client.text_classification( |
|
|
"I love this product!", |
|
|
model="cardiffnlp/twitter-roberta-base-sentiment-latest" |
|
|
) |
|
|
print("β
Success with InferenceClient!") |
|
|
print("Result:", result) |
|
|
return True |
|
|
except Exception as e: |
|
|
print("β InferenceClient error:", str(e)) |
|
|
|
|
|
|
|
|
print("\nπ Testing with direct API call...") |
|
|
try: |
|
|
import requests |
|
|
headers = {"Authorization": f"Bearer {token}"} |
|
|
payload = {"inputs": "I love this product!"} |
|
|
response = requests.post( |
|
|
"https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment-latest", |
|
|
headers=headers, |
|
|
json=payload, |
|
|
timeout=30 |
|
|
) |
|
|
print(f"Status code: {response.status_code}") |
|
|
if response.status_code == 200: |
|
|
print("β
Success with direct API!") |
|
|
print("Result:", response.json()) |
|
|
return True |
|
|
print("β API error:", response.text) |
|
|
except Exception as e: |
|
|
print("β Direct API error:", str(e)) |
|
|
|
|
|
|
|
|
print("\nπ Testing token validation...") |
|
|
try: |
|
|
import requests |
|
|
headers = {"Authorization": f"Bearer {token}"} |
|
|
response = requests.get("https://huggingface.co/api/whoami", headers=headers) |
|
|
if response.status_code == 200: |
|
|
print("β
Token is valid!") |
|
|
print("User info:", response.json()) |
|
|
else: |
|
|
print("β Token validation failed:", response.text) |
|
|
except Exception as e: |
|
|
print("β Token validation error:", str(e)) |
|
|
|
|
|
return False |
|
|
|
|
|
if __name__ == "__main__": |
|
|
test_hf_token() |
|
|
|