Spaces:
Running
Running
File size: 2,839 Bytes
37cadfb |
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 |
#!/usr/bin/env python3
"""
Simple API key testing script to verify your Hugging Face Space API keys are working.
Run this in your Space console to check if your API keys are configured correctly.
"""
import os
from dotenv import load_dotenv
import sys
# Load environment variables
load_dotenv()
def test_api_keys():
"""Test API keys loaded from environment variables"""
print("π Testing API Keys...\n")
# Check Gemini API Key
gemini_key = os.getenv("GEMINI_API_KEY")
print(f"GEMINI_API_KEY: {'β
Found' if gemini_key else 'β Not found or empty'}")
# Check HuggingFace Token
hf_token = os.getenv("HUGGINGFACE_TOKEN")
print(f"HUGGINGFACE_TOKEN: {'β
Found' if hf_token else 'β Not found or empty'}")
# Check Kluster API Key (optional)
kluster_key = os.getenv("KLUSTER_API_KEY")
print(f"KLUSTER_API_KEY: {'β
Found' if kluster_key else 'β Not found (optional)'}")
# Check SerpAPI Key (optional)
serpapi_key = os.getenv("SERPAPI_API_KEY")
print(f"SERPAPI_API_KEY: {'β
Found' if serpapi_key else 'β Not found (optional)'}")
print("\nπ Testing API Key Validity...\n")
# Test Gemini key if available
if gemini_key:
try:
import litellm
os.environ["GEMINI_API_KEY"] = gemini_key
response = litellm.completion(
model="gemini/gemini-2.0-flash",
messages=[{"role": "user", "content": "Hello, this is a test."}],
max_tokens=10
)
print(f"β
Gemini API key is valid! Response: {response.choices[0].message.content}")
except Exception as e:
print(f"β Gemini API key validation failed: {str(e)}")
# Test HuggingFace token if available
if hf_token:
try:
import requests
headers = {"Authorization": f"Bearer {hf_token}"}
response = requests.get(
"https://huggingface.co/api/whoami",
headers=headers
)
if response.status_code == 200:
print(f"β
HuggingFace token is valid! User: {response.json().get('name', 'Unknown')}")
else:
print(f"β HuggingFace token validation failed: Status {response.status_code}")
except Exception as e:
print(f"β HuggingFace token validation failed: {str(e)}")
print("\nπ§ Environment Summary")
print(f"Python version: {sys.version}")
print(f"Platform: {sys.platform}")
# Final message
if gemini_key or hf_token:
print("\nβ
At least one required API key is available. The application should work.")
else:
print("\nβ No required API keys found. The application will fail to initialize.")
if __name__ == "__main__":
test_api_keys()
|