Spaces:
Sleeping
Sleeping
File size: 1,658 Bytes
65a8bf3 | 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 | 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}")
|