codedebugger / test_apis.py
psdhanushkumar's picture
Upload folder using huggingface_hub
f361447 verified
Raw
History Blame Contribute Delete
2.25 kB
import os
from dotenv import load_dotenv
load_dotenv()
GREEN = '\033[92m'
RED = '\033[91m'
RESET = '\033[0m'
def test_gemini():
try:
from google import genai
client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="Say hello in one word"
)
print(f"{GREEN}[OK] Gemini: {response.text.strip()}{RESET}")
return True
except Exception as e:
print(f"{RED}[FAIL] Gemini failed: {e}{RESET}")
return False
def test_groq():
try:
import groq
client = groq.Groq(api_key=os.environ.get("GROQ_API_KEY"))
r = client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[{"role":"user","content":"Say hello in one word"}],
max_tokens=10
)
print(f"{GREEN}[OK] Groq: {r.choices[0].message.content.strip()}{RESET}")
return True
except Exception as e:
print(f"{RED}[FAIL] Groq failed: {e}{RESET}")
return False
def test_openrouter():
try:
import requests
r = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {os.environ.get('OPENROUTER_API_KEY')}",
"Content-Type": "application/json"
},
json={
"model": "mistralai/mistral-7b-instruct:free",
"messages": [{"role":"user","content":"Say hello in one word"}]
},
timeout=30
)
text = r.json()["choices"][0]["message"]["content"].strip()
print(f"{GREEN}[OK] OpenRouter: {text}{RESET}")
return True
except Exception as e:
print(f"{RED}[FAIL] OpenRouter failed: {e}{RESET}")
return False
print("Testing all APIs...\n")
g = test_gemini()
gr = test_groq()
op = test_openrouter()
print("\n--- SUMMARY ---")
if gr or op:
print(f"{GREEN}[OK] Enough APIs working to proceed!{RESET}")
if not g:
print(f"WARN: Gemini hit daily limit — Groq/OpenRouter will be primary")
else:
print(f"{RED}[FAIL] No APIs working — check your .env keys{RESET}")