Spaces:
Sleeping
Sleeping
File size: 4,070 Bytes
4588d9f |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
#!/usr/bin/env python3
"""
Test script to validate API keys for TheoremExplainAgent
Usage: python test_api_keys.py
"""
import os
import sys
from dotenv import load_dotenv
def test_gemini_api():
"""Test Gemini API key(s)"""
print("Testing Gemini API...")
try:
import google.generativeai as genai
import random
# Load environment variables
load_dotenv()
# Get API key with fallback support
gemini_key_env = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
if not gemini_key_env:
print("β No GEMINI_API_KEY found in environment")
print(" Please set GEMINI_API_KEY in your .env file")
print(" Get your API key from: https://aistudio.google.com/app/apikey")
return False
# Handle multiple keys
if ',' in gemini_key_env:
keys = [key.strip() for key in gemini_key_env.split(',') if key.strip()]
print(f" Found {len(keys)} API keys to test")
api_key = random.choice(keys)
print(f" Testing random key: {api_key[:20]}...")
else:
api_key = gemini_key_env
print(f" Testing key: {api_key[:20]}...")
# Configure and test
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-1.5-pro')
# Simple test
response = model.generate_content("Say hello in one word")
print(f"β
Gemini API works! Response: {response.text.strip()}")
return True
except Exception as e:
print(f"β Gemini API test failed: {e}")
return False
def test_elevenlabs_api():
"""Test ElevenLabs API key"""
print("\nTesting ElevenLabs API...")
try:
import requests
# Load environment variables
load_dotenv()
api_key = os.getenv("ELEVENLABS_API_KEY")
if not api_key:
print("β No ELEVENLABS_API_KEY found in environment")
print(" Please set ELEVENLABS_API_KEY in your .env file")
print(" Get your API key from: https://elevenlabs.io/app/settings/api-keys")
return False
print(f" Testing key: {api_key[:20]}...")
# Test API with a simple request
headers = {
"Accept": "application/json",
"xi-api-key": api_key
}
response = requests.get("https://api.elevenlabs.io/v1/user", headers=headers)
if response.status_code == 200:
user_data = response.json()
print(f"β
ElevenLabs API works! User: {user_data.get('email', 'Unknown')}")
return True
else:
print(f"β ElevenLabs API test failed: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"β ElevenLabs API test failed: {e}")
return False
def main():
"""Main test function"""
print("π Testing API Keys for TheoremExplainAgent\n")
# Check if .env file exists
if not os.path.exists('.env'):
print("β No .env file found!")
print(" Please create a .env file based on .env.template")
print(" Run: cp .env.template .env")
return
gemini_ok = test_gemini_api()
elevenlabs_ok = test_elevenlabs_api()
print("\n" + "="*50)
if gemini_ok and elevenlabs_ok:
print("β
All API keys are working correctly!")
print(" You can now run generate_video.py")
elif gemini_ok:
print("β οΈ Gemini API works, but ElevenLabs API failed")
print(" Video generation will work but TTS might fail")
elif elevenlabs_ok:
print("β οΈ ElevenLabs API works, but Gemini API failed")
print(" TTS will work but video generation will fail")
else:
print("β Both API keys failed")
print(" Please check your .env file and API keys")
if __name__ == "__main__":
main() |