Fix: restore 5 verified Gemini models + fast-fail on invalid model errors instead of hanging
9a9d2d0 Download utils/key_manager.py from Basel0/qshub: direct link, hf CLI and curl.
- Browser
- Download file 10.4 kB
-
https://huggingface.co/spaces/Basel0/qshub/resolve/main/utils/key_manager.py
- Command line
-
hf download hf://spaces/Basel0/qshub/utils/key_manager.py
-
curl -L -o key_manager.py https://huggingface.co/spaces/Basel0/qshub/resolve/main/utils/key_manager.py
10.4 kB
| """ | |
| ู ุฏูุฑ ู ูุงุชูุญ ุงูู API โ ูุฏูุฑ ุจูู ุงูู ูุงุชูุญ ูุงููู ุงุฐุฌ ุชููุงุฆูุงู (2D Rotation) | |
| ======================================================= | |
| ูุฏูุฑ ุจูู 3 ูู ุงุฐุฌ ู ุฎุชููุฉ (Flash, Flash-Lite 2.5, Flash-Lite 3.1) | |
| ู ุถุฑูุจุฉ ูู ุนุฏุฏ ุงูู ูุงุชูุญ ูุชุฌุงูุฒ ุญุฏูุฏ ุงูู RPM ูุงูู RPD ุชู ุงู ุงู. | |
| ======================================================= | |
| """ | |
| import os | |
| import time | |
| import threading | |
| from typing import List, Optional, Tuple | |
| from datetime import datetime, date | |
| # โโ Monkeypatch google.genai.Client to disable HTTP/2 and prevent timeouts on Windows / proxy networks โโ | |
| try: | |
| from google import genai | |
| from google.genai import types | |
| import httpx | |
| _orig_init = genai.Client.__init__ | |
| def patched_init(self, *args, **kwargs): | |
| if 'http_options' not in kwargs: | |
| kwargs['http_options'] = {} | |
| http_opts = kwargs['http_options'] | |
| if isinstance(http_opts, dict): | |
| timeout_val = http_opts.get('timeout', 300.0) | |
| http_opts['httpx_client'] = httpx.Client(timeout=timeout_val, http2=False) | |
| http_opts['httpx_async_client'] = httpx.AsyncClient(timeout=timeout_val, http2=False) | |
| clean_opts = {k: v for k, v in http_opts.items() if k != 'timeout'} | |
| kwargs['http_options'] = types.HttpOptions(**clean_opts) | |
| elif isinstance(http_opts, types.HttpOptions): | |
| if not http_opts.httpx_client: | |
| http_opts.httpx_client = httpx.Client(timeout=60.0, http2=False) | |
| if not http_opts.httpx_async_client: | |
| http_opts.httpx_async_client = httpx.AsyncClient(timeout=60.0, http2=False) | |
| _orig_init(self, *args, **kwargs) | |
| genai.Client.__init__ = patched_init | |
| except Exception: | |
| pass | |
| # ุงููู ุงุฐุฌ ุงูุชู ุณูุชู ุงูุชุฏููุฑ ุจูููุง ู ุน ูู ู ูุชุงุญ | |
| ROTATION_MODELS = [ | |
| "gemini-2.5-flash", | |
| "gemini-3.1-flash-lite", | |
| "gemini-3.6-flash", | |
| "gemini-3.5-flash", | |
| "gemini-3.5-flash-lite" | |
| ] | |
| # โโ ุญุฏูุฏ ุงููู ุงุฐุฌ ุงูู ุฎุชููุฉ (RPD) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| MODEL_DAILY_LIMITS = { | |
| "gemini-2.5-flash": 1500, | |
| "gemini-3.1-flash-lite": 500, | |
| "gemini-3.6-flash": 1500, | |
| "gemini-3.5-flash": 1500, | |
| "gemini-3.5-flash-lite": 1500, | |
| "default": 1500, | |
| } | |
| class KeyManager: | |
| """ | |
| ูุฏูุฑ ู ุตูููุฉ ุซูุงุฆูุฉ ุงูุฃุจุนุงุฏ (ู ูุชุงุญ ร ูู ูุฐุฌ) ููุฏูุฑ ุจูููุง. | |
| - ูุชุชุจุน ุนุฏุฏ ุงูุงุณุชุฎุฏุงู ุงุช ููู (ู ูุชุงุญุ ูู ูุฐุฌ) | |
| - ูุชุฎุทู ุงูุชุฑููุจุฉ ุงูุชู ุชุตู ุญุฏูุง (429) | |
| """ | |
| def __init__(self, keys: List[str]): | |
| self._keys = [k.strip() for k in keys if k and k.strip()] | |
| self._lock = threading.Lock() | |
| # Rotate keys first, then models โ spreads RPM load across keys before hitting same model twice | |
| self._combinations = [] | |
| for k in self._keys: | |
| for m in ROTATION_MODELS: | |
| self._combinations.append((k, m)) | |
| self._index = 0 | |
| self._usage: dict = {} | |
| for k, m in self._combinations: | |
| self._usage[(k, m)] = {"count": 0, "date": date.today(), "exhausted": False, "exhausted_at": 0} | |
| # โโ Public API โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def get_key_and_model(self) -> Tuple[Optional[str], str]: | |
| """ูุฑุฌุน ุงูุชุฑููุจุฉ ุงูุญุงููุฉ ุงููุดุทุฉ (ุงูู ูุชุงุญ ูุงููู ูุฐุฌ) ููุฒูุฏ ุนุฏุงุฏูุง.""" | |
| with self._lock: | |
| self._reset_daily_counts() | |
| combo = self._active_combo() | |
| if combo: | |
| self._usage[combo]["count"] += 1 | |
| self._advance() | |
| return combo | |
| return None, "gemini-3.1-flash-lite" # fallback if all exhausted | |
| def mark_rate_limited(self, key: str, model: str): | |
| """ุงุณุชุฏุนู ูุฐุง ุนูุฏ ุงุณุชูุจุงู 429 โ ูุชุฎุทู ูุฐู ุงูุชุฑููุจุฉ ูู ุฏุฉ 60 ุซุงููุฉ ููุท.""" | |
| with self._lock: | |
| combo = (key, model) | |
| if combo in self._usage: | |
| self._usage[combo]["exhausted"] = True | |
| self._usage[combo]["exhausted_at"] = time.time() | |
| def mark_exhausted(self, key: str, model: str): | |
| """ุนููู ุชุฑููุจุฉ ุจุงุนุชุจุงุฑูุง ู ูุชููุฉ ูุฏููุงู.""" | |
| self.mark_rate_limited(key, model) | |
| def status(self) -> List[dict]: | |
| """ู ูุฎุต ุญุงูุฉ ูู ุงูุชุฑููุจุงุช.""" | |
| with self._lock: | |
| self._reset_daily_counts() | |
| active_combo = self._active_combo() | |
| res = [] | |
| for combo in self._combinations: | |
| k, m = combo | |
| u = self._usage[combo] | |
| limit = MODEL_DAILY_LIMITS.get(m, MODEL_DAILY_LIMITS["default"]) | |
| res.append({ | |
| "key_hint": f"...{k[-8:]}", | |
| "model": m, | |
| "requests": u["count"], | |
| "limit": limit, | |
| "remaining": max(limit - u["count"], 0), | |
| "exhausted": u["exhausted"], | |
| "active": (combo == active_combo), | |
| }) | |
| return res | |
| def total_remaining(self) -> int: | |
| """ู ุฌู ูุน ุงูุทูุจุงุช ุงูู ุชุจููุฉ ููู ุงูุชุฑููุจุงุช.""" | |
| with self._lock: | |
| self._reset_daily_counts() | |
| total = 0 | |
| for combo in self._combinations: | |
| k, m = combo | |
| if not self._usage[combo]["exhausted"]: | |
| limit = MODEL_DAILY_LIMITS.get(m, MODEL_DAILY_LIMITS["default"]) | |
| total += max(limit - self._usage[combo]["count"], 0) | |
| return total | |
| def key_count(self) -> int: | |
| return len(self._keys) | |
| # โโ Internal โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def _active_combo(self) -> Optional[Tuple[str, str]]: | |
| """ูุจุญุซ ุนู ุฃูู ุชุฑููุจุฉ ุบูุฑ ู ูุชููุฉ. ุงูู ูุงุชูุญ ุงูู ุนูููุฉ ุชูุชูู ุจุนุฏ 60 ุซุงููุฉ ุชููุงุฆูุงู.""" | |
| if not self._combinations: | |
| return None | |
| now = time.time() | |
| for _ in range(len(self._combinations)): | |
| combo = self._combinations[self._index % len(self._combinations)] | |
| u = self._usage[combo] | |
| # Auto-recover after 60 seconds | |
| if u["exhausted"] and (now - u.get("exhausted_at", 0)) > 60: | |
| u["exhausted"] = False | |
| if not u["exhausted"]: | |
| return combo | |
| self._index += 1 | |
| return None | |
| def _advance(self): | |
| if self._combinations: | |
| self._index = (self._index + 1) % len(self._combinations) | |
| def _reset_daily_counts(self): | |
| """ูุตููุฑ ุงูุนุฏุงุฏ ูู ููู ุฌุฏูุฏ ูููุดุบูู auto-exhaustion ุนูุฏ ุจููุบ ุงูุญุฏ.""" | |
| today = date.today() | |
| for combo in self._combinations: | |
| k, m = combo | |
| u = self._usage[combo] | |
| if u["date"] != today: | |
| self._usage[combo] = {"count": 0, "date": today, "exhausted": False, "exhausted_at": 0} | |
| else: | |
| # Auto-mark exhausted when daily limit reached | |
| limit = MODEL_DAILY_LIMITS.get(m, MODEL_DAILY_LIMITS["default"]) | |
| if u["count"] >= limit and not u["exhausted"]: | |
| u["exhausted"] = True | |
| u["exhausted_at"] = time.time() | |
| # โโ ุชุญู ูู ุงูู ูุงุชูุญ ู ู .env ุฃู ู ุชุบูุฑุงุช ุงูุจูุฆุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def _load_keys_from_env() -> List[str]: | |
| try: | |
| from dotenv import load_dotenv | |
| import pathlib | |
| env_path = pathlib.Path(__file__).resolve().parent.parent / ".env" | |
| load_dotenv(dotenv_path=env_path, override=False) | |
| except ImportError: | |
| pass | |
| multi = os.environ.get("AI_API_KEYS", "") | |
| if multi: | |
| return [k.strip() for k in multi.split(",") if k.strip()] | |
| numbered = [] | |
| for i in range(1, 21): | |
| k = os.environ.get(f"AI_API_KEY_{i}", "") | |
| if k.strip(): | |
| numbered.append(k.strip()) | |
| if numbered: | |
| return numbered | |
| single = (os.environ.get("GOOGLE_API_KEY") or | |
| os.environ.get("AI_API_KEY") or "") | |
| return [single] if single else [] | |
| # โโ Singleton โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| _manager: Optional[KeyManager] = None | |
| _manager_lock = threading.Lock() | |
| def get_key_manager() -> KeyManager: | |
| global _manager | |
| with _manager_lock: | |
| if _manager is None: | |
| keys = _load_keys_from_env() | |
| # If no keys are provided, create a dummy key so the system doesn't crash | |
| if not keys: | |
| keys = ["NO_API_KEY_FOUND"] | |
| _manager = KeyManager(keys) | |
| return _manager | |
| def reload_manager(keys: List[str]): | |
| global _manager | |
| with _manager_lock: | |
| if not keys: | |
| keys = ["NO_API_KEY_FOUND"] | |
| _manager = KeyManager(keys) | |
| def print_status(): | |
| mgr = get_key_manager() | |
| print(f"\n{'โ'*80}") | |
| print(f" ู ุฏูุฑ ุงูู ูุงุชูุญ ูุงููู ุงุฐุฌ โ {mgr.key_count()} ู ูุชุงุญ | ู ุชุจูู: {mgr.total_remaining()} ุทูุจ") | |
| print(f"{'โ'*80}") | |
| for s in mgr.status(): | |
| icon = "โ " if s["active"] else ("๐ด" if s["exhausted"] else "โช") | |
| bar = "โ" * int(s["requests"] / max(s["limit"], 1) * 20) | |
| bar = bar.ljust(20, "โ") | |
| rem = "(ู ูุชูู)" if s["exhausted"] else f"ู ุชุจูู: {s['remaining']}" | |
| m_name = s['model'][:25].ljust(25) | |
| print(f" {icon} {s['key_hint']} | {m_name} | [{bar}] {s['requests']}/{s['limit']} {rem}") | |
| print(f"{'โ'*80}\n") | |