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