-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_huggingface.py
More file actions
67 lines (51 loc) · 2.33 KB
/
test_huggingface.py
File metadata and controls
67 lines (51 loc) · 2.33 KB
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
import requests
from config import Config
def test_huggingface_api():
"""Test if Hugging Face API is working correctly"""
# Your API details from config
API_URL = Config.HUGGING_FACE_API_URL
API_TOKEN = Config.HUGGING_FACE_TOKEN
headers = {"Authorization": f"Bearer {API_TOKEN}"}
# Simple test prompt
test_prompt = "Hello, how are you?"
try:
print(f"Testing Hugging Face API connection to: {API_URL}")
print(f"Using token: {API_TOKEN[:10]}...{API_TOKEN[-10:]}")
print("-" * 50)
payload = {
"inputs": test_prompt,
"parameters": {
"max_new_tokens": 50,
"temperature": 0.7
}
}
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
result = response.json()
print("✅ API Connection Successful!")
print(f"Response: {result}")
if isinstance(result, list) and len(result) > 0:
if 'generated_text' in result[0]:
print(f"Generated Text: {result[0]['generated_text']}")
else:
print(f"Full Response: {result[0]}")
else:
print(f"Full Response: {result}")
elif response.status_code == 503:
print("❌ Model is loading. Please wait and try again in a few minutes.")
print("This is normal for models that aren't frequently used.")
elif response.status_code == 401:
print("❌ Authentication failed. Please check your API token.")
elif response.status_code == 404:
print("❌ Model not found. Please check the model URL.")
else:
print(f"❌ Error: {response.status_code} - {response.text}")
except requests.exceptions.Timeout:
print("❌ Request timed out. The API might be overloaded.")
except requests.exceptions.ConnectionError:
print("❌ Connection error. Please check your internet connection.")
except Exception as e:
print(f"❌ Unexpected error: {str(e)}")
if __name__ == "__main__":
test_huggingface_api()