Spaces:
Running
Running
#!/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() | |