Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 55 additions & 57 deletions test_api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,133 +13,131 @@

def test_groq_api():
"""Test Groq API connection and basic transcription."""
print("\n" + "=" * 80)
print("Testing Groq API")
print("=" * 80)
logger.info("\n%s", "=" * 80)
logger.info("Testing Groq API")
logger.info("%s", "=" * 80)

if not Config.GROQ_API_KEY:
print("❌ GROQ_API_KEY not found in environment")
print(" Set it in your .env file or via Settings & Tools in the UI")
logger.error("GROQ_API_KEY not found in environment")
logger.info(" Set it in your .env file or via Settings & Tools in the UI")
return False

print(f"✓ API Key found: {Config.GROQ_API_KEY[:10]}...")
logger.info("API Key found: %s...", Config.GROQ_API_KEY[:10])

try:
from groq import Groq
client = Groq(api_key=Config.GROQ_API_KEY)
print("✓ Groq client initialized")
logger.info("Groq client initialized")

# Test with a simple completion (cheaper than transcription)
print("\nTesting API connection with a simple request...")
logger.info("\nTesting API connection with a simple request...")
response = client.chat.completions.create(
messages=[{"role": "user", "content": "Say 'API test successful' and nothing else."}],
model="llama-3.3-70b-versatile", # Updated to current model
max_tokens=10,
)

result = response.choices[0].message.content
print(f"✓ API Response: {result}")
logger.info("API Response: %s", result)

print("\n Groq API is working correctly!")
print(" You can use 'groq' for transcription and classification backends")
logger.info("\n[SUCCESS] Groq API is working correctly!")
logger.info(" You can use 'groq' for transcription and classification backends")
return True

except ImportError:
print("❌ Groq package not installed")
print(" Install with: pip install groq")
logger.error("Groq package not installed")
logger.info(" Install with: pip install groq")
return False
except Exception as e:
print(f"❌ Error testing Groq API: {e}")
print(" Check that your API key is valid")
logger.exception("Error testing Groq API: %s", e)
logger.info(" Check that your API key is valid")
return False


def test_huggingface_api():
"""Test Hugging Face API connection."""
print("\n" + "=" * 80)
print("Testing Hugging Face API")
print("=" * 80)
logger.info("\n%s", "=" * 80)
logger.info("Testing Hugging Face API")
logger.info("%s", "=" * 80)

if not Config.HUGGING_FACE_API_KEY:
print("❌ HUGGING_FACE_API_KEY not found in environment")
print(" Set it in your .env file or via Settings & Tools in the UI")
logger.error("HUGGING_FACE_API_KEY not found in environment")
logger.info(" Set it in your .env file or via Settings & Tools in the UI")
return False

print(f"✓ API Key found: {Config.HUGGING_FACE_API_KEY[:10]}...")
logger.info("API Key found: %s...", Config.HUGGING_FACE_API_KEY[:10])

try:
# Use the huggingface_hub library for authentication test
from huggingface_hub import HfApi

print("\nTesting API authentication...")
logger.info("\nTesting API authentication...")
api = HfApi(token=Config.HUGGING_FACE_API_KEY)

# Try to get user info - this validates the token
user_info = api.whoami()
username = user_info.get("name", "unknown")

print(f"✓ Authenticated as: {username}")
print("\n Hugging Face API is working correctly!")
print(" You can use 'hf_api' for the diarization backend")
logger.info("Authenticated as: %s", username)
logger.info("\n[SUCCESS] Hugging Face API is working correctly!")
logger.info(" You can use 'hf_api' for the diarization backend")
return True

except ImportError:
print("❌ 'huggingface_hub' package not installed")
print(" Install with: pip install huggingface-hub")
logger.error("'huggingface_hub' package not installed")
logger.info(" Install with: pip install huggingface-hub")
return False
except Exception as e:
error_msg = str(e)
if "401" in error_msg or "Invalid token" in error_msg:
print(f"❌ Authentication failed: Invalid API key")
print(" Check that your HF token is correct")
logger.error("Authentication failed: Invalid API key")
logger.info(" Check that your HF token is correct")
elif "403" in error_msg:
print(f"❌ Authorization failed: Token lacks required permissions")
print(" Ensure your token has 'Make calls to Inference Providers' permission")
logger.error("Authorization failed: Token lacks required permissions")
logger.info(" Ensure your token has 'Make calls to Inference Providers' permission")
else:
print(f"❌ Error testing Hugging Face API: {error_msg}")
print(" Check your internet connection and API key")
logger.exception("Error testing Hugging Face API: %s", error_msg)
logger.info(" Check your internet connection and API key")
return False


def main():
"""Run all API tests."""
print("=" * 80)
print("API Key Validation Test Suite")
print("=" * 80)
print("\nThis script tests your cloud API configurations.")
print("Make sure you've set your API keys in .env or via the UI.")
logger.info("%s", "=" * 80)
logger.info("API Key Validation Test Suite")
logger.info("%s", "=" * 80)
logger.info("\nThis script tests your cloud API configurations.")
logger.info("Make sure you've set your API keys in .env or via the UI.")

groq_ok = test_groq_api()
hf_ok = test_huggingface_api()

print("\n" + "=" * 80)
print("Test Summary")
print("=" * 80)
print(f"Groq API: {'✅ PASS' if groq_ok else '❌ FAIL'}")
print(f"Hugging Face API: {'✅ PASS' if hf_ok else '❌ FAIL'}")
logger.info("\n%s", "=" * 80)
logger.info("Test Summary")
logger.info("%s", "=" * 80)
logger.info("Groq API: %s", "[PASS]" if groq_ok else "[FAIL]")
logger.info("Hugging Face API: %s", "[PASS]" if hf_ok else "[FAIL]")

if groq_ok and hf_ok:
print("\n🎉 All APIs are configured correctly!")
print("\nYou can now use cloud backends in the UI:")
print(" - Transcription: Select 'groq' backend")
print(" - Diarization: Select 'hf_api' backend")
print(" - Classification: Select 'groq' backend")
logger.info("\n[SUCCESS] All APIs are configured correctly!")
logger.info("\nYou can now use cloud backends in the UI:")
logger.info(" - Transcription: Select 'groq' backend")
logger.info(" - Diarization: Select 'hf_api' backend")
logger.info(" - Classification: Select 'groq' backend")
elif groq_ok or hf_ok:
print("\n Some APIs are working, but not all")
print(" Set the missing API keys to enable all cloud features")
logger.warning("\n[WARNING] Some APIs are working, but not all")
logger.info(" Set the missing API keys to enable all cloud features")
else:
print("\n No APIs are configured")
print(" Add your API keys to .env or via Settings & Tools in the UI")
logger.error("\n[ERROR] No APIs are configured")
logger.info(" Add your API keys to .env or via Settings & Tools in the UI")

print("=" * 80)
logger.info("%s", "=" * 80)


if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\nTest interrupted by user")
logger.warning("\n\nTest interrupted by user")
except Exception as e:
print(f"\n\n❌ Unexpected error: {e}")
import traceback
traceback.print_exc()
logger.exception("\n\nUnexpected error: %s", e)