demo / test_api.py
samiee2213's picture
Upload 55 files
65a8bf3 verified
Raw
History Blame Contribute Delete
1.66 kB
import os
import json
import urllib.request
def load_keys():
keys = []
# backend/.env
b_env = "/Users/sam22ridhi/Downloads/earlycareers/backend/.env"
if os.path.exists(b_env):
with open(b_env, "r") as f:
for line in f:
if "GEMINI_API_KEY" in line:
keys.append(("backend/.env", line.split("=")[1].strip().strip('"').strip("'")))
# root .env.local
r_env = "/Users/sam22ridhi/Downloads/earlycareers/.env.local"
if os.path.exists(r_env):
with open(r_env, "r") as f:
for line in f:
if "GEMINI_API_KEY" in line:
keys.append(("root/.env.local", line.split("=")[1].strip().strip('"').strip("'")))
return keys
for name, key in load_keys():
print(f"Testing key from {name}: {key[:8]}...")
for model in ["gemini-2.0-flash", "gemini-1.5-flash", "gemini-1.5-pro"]:
url = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={key}"
payload = {
"contents": [{"parts": [{"text": "Say 'OK'"}]}]
}
try:
req = urllib.request.Request(
url,
data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json"}
)
with urllib.request.urlopen(req, timeout=5) as res:
data = json.loads(res.read().decode("utf-8"))
text = data["candidates"][0]["content"]["parts"][0]["text"]
print(f" {model}: SUCCESS -> {text.strip()}")
except Exception as e:
print(f" {model}: FAIL -> {e}")