File size: 14,846 Bytes
ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b ac6c361 040f97b | 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | """
api_handler.py β AI Provider Integration + Smart Error Classification
Error types and handling strategy:
RateLimitError β exponential backoff, wait up to 60s, retry 6 times
TokenLimitError β caller should split chunk smaller (not retryable here)
QuotaError β not retryable, raise immediately with clear message
NetworkError β retry 4 times with backoff
AuthError β not retryable, raise immediately
Public API
----------
fetch_models(provider, api_key) -> list[str]
validate_api_key(provider, api_key) -> (bool, str)
call_ai(prompt, *, provider, model, api_key,
system_prompt, timeout) -> str
classify_error(exception) -> ErrorKind
"""
import logging
import time
import re
import requests
logger = logging.getLogger(__name__)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Error classification
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class RateLimitError(Exception):
"""429 β slow down, retry after backoff."""
def __init__(self, msg, retry_after: int = 0):
super().__init__(msg)
self.retry_after = retry_after # seconds hint from header
class TokenLimitError(Exception):
"""Output/context token limit exceeded β caller must split chunk."""
class QuotaError(Exception):
"""Daily/monthly quota exhausted β cannot retry today."""
class AuthError(Exception):
"""Invalid API key β not retryable."""
class NetworkError(Exception):
"""Transient network failure β retry."""
def classify_error(exc: Exception):
"""
Inspect an exception and return the most specific error subclass.
Works for both Gemini (google-generativeai) and OpenRouter (openai SDK).
"""
msg = str(exc).lower()
code = getattr(exc, "status_code", None) or getattr(exc, "code", None)
# Try to extract HTTP status from the message string if not on object
if code is None:
m = re.search(r'\b(4\d\d|5\d\d)\b', str(exc))
if m:
code = int(m.group(1))
# Rate limit
if code == 429 or "rate" in msg or "quota" in msg and "per" in msg:
# Try to parse retry-after from message
ra = 0
m = re.search(r'retry.{0,10}after.{0,10}(\d+)', msg)
if m:
ra = int(m.group(1))
return RateLimitError(str(exc), retry_after=ra)
# Quota exhausted (daily limit)
if code == 429 and ("daily" in msg or "exhausted" in msg or "exceeded" in msg):
return QuotaError(str(exc))
if "resource_exhausted" in msg or "quota_exceeded" in msg:
return QuotaError(str(exc))
# Token / context limit
if any(k in msg for k in [
"token", "context", "too long", "maximum context",
"max_tokens", "finish_reason: length", "content_filter"
]):
return TokenLimitError(str(exc))
# Auth
if code in (401, 403) or any(k in msg for k in ["api key", "unauthorized", "forbidden", "invalid key"]):
return AuthError(str(exc))
# Network
if isinstance(exc, (requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
ConnectionError, TimeoutError)):
return NetworkError(str(exc))
if code and code >= 500:
return NetworkError(str(exc))
return exc # unknown β return original
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Constants
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
OPENROUTER_BASE = "https://openrouter.ai/api/v1"
OPENROUTER_MODELS = f"{OPENROUTER_BASE}/models"
GEMINI_MODELS_URL = "https://generativelanguage.googleapis.com/v1beta/models"
DEFAULT_TIMEOUT = 60
GEMINI_FALLBACK = [
"gemini-2.0-flash", "gemini-2.0-flash-lite",
"gemini-1.5-pro", "gemini-1.5-flash", "gemini-1.5-flash-8b",
]
OPENROUTER_FALLBACK = [
"google/gemini-2.0-flash-exp:free",
"meta-llama/llama-3.3-70b-instruct:free",
"deepseek/deepseek-chat-v3-0324:free",
"mistralai/mistral-7b-instruct:free",
"qwen/qwen-2.5-72b-instruct:free",
]
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Model listing
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def fetch_gemini_models(api_key: str) -> list:
if not api_key or not api_key.strip():
return GEMINI_FALLBACK.copy()
try:
resp = requests.get(
GEMINI_MODELS_URL,
params={"key": api_key.strip(), "pageSize": 100},
timeout=15,
)
resp.raise_for_status()
models = []
for m in resp.json().get("models", []):
name = m.get("name", "").replace("models/", "")
if "generateContent" in m.get("supportedGenerationMethods", []):
models.append(name)
models.sort(key=lambda x: (0 if x.startswith("gemini-2") else
1 if x.startswith("gemini-1.5") else 2, x))
return models or GEMINI_FALLBACK.copy()
except Exception as exc:
logger.warning("Gemini model fetch: %s", exc)
return GEMINI_FALLBACK.copy()
def fetch_openrouter_models(api_key: str) -> list:
if not api_key or not api_key.strip():
return OPENROUTER_FALLBACK.copy()
try:
resp = requests.get(
OPENROUTER_MODELS,
headers={
"Authorization": f"Bearer {api_key.strip()}",
"HTTP-Referer": "https://huggingface.co/spaces",
"X-Title": "SubSync Myanmar Translator",
},
timeout=15,
)
resp.raise_for_status()
raw = resp.json().get("data", [])
raw.sort(key=lambda m: (-m.get("context_length", 0), m.get("id", "")))
ids = [m["id"] for m in raw if "id" in m]
return ids or OPENROUTER_FALLBACK.copy()
except Exception as exc:
logger.warning("OpenRouter model fetch: %s", exc)
return OPENROUTER_FALLBACK.copy()
def fetch_models(provider: str, api_key: str) -> list:
if provider == "gemini":
return fetch_gemini_models(api_key)
if provider == "openrouter":
return fetch_openrouter_models(api_key)
raise ValueError(f"Unknown provider: {provider!r}")
def validate_api_key(provider: str, api_key: str) -> tuple:
if not api_key or not api_key.strip():
return False, "API key ααα―α‘ααΊαααΊ"
try:
if provider == "gemini":
resp = requests.get(GEMINI_MODELS_URL,
params={"key": api_key.strip(), "pageSize": 5},
timeout=15)
if resp.status_code == 400: return False, "API key αααΎααΊαα« (400)"
if resp.status_code == 403: return False, "API key αα½αα·αΊαααΌα― (403)"
resp.raise_for_status()
return True, f"β Valid β {len(resp.json().get('models',[]))} models"
if provider == "openrouter":
resp = requests.get(OPENROUTER_MODELS,
headers={"Authorization": f"Bearer {api_key.strip()}",
"HTTP-Referer": "https://huggingface.co/spaces"},
timeout=15)
if resp.status_code == 401: return False, "API key αααΎααΊαα« (401)"
resp.raise_for_status()
return True, f"β Valid β {len(resp.json().get('data',[]))} models"
return False, f"Unknown provider: {provider}"
except requests.exceptions.ConnectionError:
return False, "Network error β internet α
α
αΊαα±αΈαα«"
except requests.exceptions.Timeout:
return False, "Request timeout"
except Exception as exc:
return False, f"Error: {exc}"
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Inference β with smart retry per error type
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _smart_retry_call(fn, max_attempts: int = 6):
"""
Call fn() with smart per-error-type retry strategy:
RateLimitError β wait retry_after (or exp backoff 5sβ120s), retry up to 6x
NetworkError β exp backoff 2sβ30s, retry up to 4x
TokenLimitError β raise immediately (caller splits chunk)
QuotaError β raise immediately (user must wait/switch key)
AuthError β raise immediately (wrong key)
"""
rate_delay = 5 # starting backoff for rate limits
net_delay = 2 # starting backoff for network errors
rate_tries = 0
net_tries = 0
for attempt in range(1, max_attempts + 1):
try:
return fn()
except Exception as raw_exc:
classified = classify_error(raw_exc)
if isinstance(classified, (TokenLimitError, QuotaError, AuthError)):
raise classified from raw_exc
if isinstance(classified, RateLimitError):
rate_tries += 1
wait = classified.retry_after if classified.retry_after > 0 else rate_delay
rate_delay = min(rate_delay * 2, 120)
if rate_tries >= max_attempts:
raise classified from raw_exc
logger.warning("Rate limit β waiting %ds (attempt %d/%d)",
wait, attempt, max_attempts)
time.sleep(wait)
continue
if isinstance(classified, NetworkError):
net_tries += 1
if net_tries >= 4:
raise classified from raw_exc
logger.warning("Network error β waiting %ds (attempt %d)",
net_delay, attempt)
time.sleep(net_delay)
net_delay = min(net_delay * 2, 30)
continue
# Unknown error β limited retry
if attempt >= 3:
raise
time.sleep(3)
raise RuntimeError(f"All {max_attempts} attempts failed")
def _call_gemini(prompt: str, *, model: str, api_key: str,
system_prompt: str = "", timeout: int = DEFAULT_TIMEOUT) -> str:
import google.generativeai as genai
from google.generativeai.types import HarmCategory, HarmBlockThreshold
genai.configure(api_key=api_key.strip())
gen_cfg = genai.GenerationConfig(temperature=0.3, max_output_tokens=8192)
safety = {
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
}
gemini_m = genai.GenerativeModel(
model_name=model, generation_config=gen_cfg,
safety_settings=safety,
system_instruction=system_prompt or None,
)
return _smart_retry_call(lambda: gemini_m.generate_content(prompt).text)
def _call_openrouter(prompt: str, *, model: str, api_key: str,
system_prompt: str = "", timeout: int = DEFAULT_TIMEOUT) -> str:
from openai import OpenAI
client = OpenAI(
base_url=OPENROUTER_BASE, api_key=api_key.strip(),
default_headers={
"HTTP-Referer": "https://huggingface.co/spaces",
"X-Title": "SubSync Myanmar Translator",
},
timeout=timeout,
)
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.append({"role": "user", "content": prompt})
def _call():
resp = client.chat.completions.create(
model=model, messages=messages, temperature=0.3, max_tokens=8192
)
return resp.choices[0].message.content or ""
return _smart_retry_call(_call)
def call_ai(prompt: str, *, provider: str, model: str, api_key: str,
system_prompt: str = "", timeout: int = DEFAULT_TIMEOUT) -> str:
"""
Unified AI call with smart error classification and retry.
Raises:
RateLimitError β hit rate limit after all retries
TokenLimitError β chunk too long for model (caller should split)
QuotaError β daily quota exhausted (cannot retry)
AuthError β invalid API key
RuntimeError β other failure
"""
if not api_key or not api_key.strip():
raise AuthError("API key ααα«αα« β Settings αα½ααΊ ααα·αΊαα«")
if not model:
raise ValueError("Model ααα½α±αΈααα±αΈαα« β Settings αα½ααΊ αα½α±αΈαα«")
try:
if provider == "gemini":
return _call_gemini(prompt, model=model, api_key=api_key,
system_prompt=system_prompt, timeout=timeout)
if provider == "openrouter":
return _call_openrouter(prompt, model=model, api_key=api_key,
system_prompt=system_prompt, timeout=timeout)
raise ValueError(f"Unknown provider: {provider!r}")
except (RateLimitError, TokenLimitError, QuotaError, AuthError, ValueError):
raise
except Exception as exc:
classified = classify_error(exc)
if isinstance(classified, Exception) and classified is not exc:
raise classified from exc
raise RuntimeError(f"AI call failed ({provider}): {exc}") from exc
|