Spaces:
Paused
Paused
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -190,10 +190,10 @@ class APIKeyManager:
|
|
| 190 |
def show_all_keys(self):
|
| 191 |
logger.info(f"当前可用API key个数: {len(self.api_keys)} ")
|
| 192 |
for i, api_key in enumerate(self.api_keys):
|
| 193 |
-
logger.info(f"API Key{i}: {api_key[:
|
| 194 |
|
| 195 |
def blacklist_key(self, key):
|
| 196 |
-
logger.warning(f"{key[:
|
| 197 |
api_key_blacklist.add(key)
|
| 198 |
|
| 199 |
scheduler.add_job(lambda: api_key_blacklist.discard(key), 'date', run_date=datetime.now() + timedelta(seconds=api_key_blacklist_duration))
|
|
@@ -207,11 +207,11 @@ def switch_api_key():
|
|
| 207 |
key = key_manager.get_available_key()
|
| 208 |
if key:
|
| 209 |
current_api_key = key
|
| 210 |
-
logger.info(f"API key 替换为 → {current_api_key[:
|
| 211 |
else:
|
| 212 |
logger.error("API key 替换失败,所有API key都已耗尽或被暂时禁用,请重新配置或稍后重试")
|
| 213 |
|
| 214 |
-
logger.info(f"当前 API key: {current_api_key[:
|
| 215 |
|
| 216 |
GEMINI_MODELS = [
|
| 217 |
{"id": "text-embedding-004"},
|
|
@@ -302,14 +302,14 @@ def handle_api_error(error, attempt):
|
|
| 302 |
})
|
| 303 |
|
| 304 |
if isinstance(error, InvalidArgument):
|
| 305 |
-
logger.error(f"{current_api_key[:
|
| 306 |
key_manager.blacklist_key(current_api_key)
|
| 307 |
switch_api_key()
|
| 308 |
return 0, None
|
| 309 |
|
| 310 |
elif isinstance(error, ResourceExhausted):
|
| 311 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
| 312 |
-
logger.warning(f"{current_api_key[:
|
| 313 |
key_manager.blacklist_key(current_api_key)
|
| 314 |
switch_api_key()
|
| 315 |
time.sleep(delay)
|
|
@@ -317,27 +317,27 @@ def handle_api_error(error, attempt):
|
|
| 317 |
|
| 318 |
elif isinstance(error, Aborted):
|
| 319 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
| 320 |
-
logger.warning(f"{current_api_key[:
|
| 321 |
switch_api_key()
|
| 322 |
time.sleep(delay)
|
| 323 |
return 0, None
|
| 324 |
|
| 325 |
elif isinstance(error, InternalServerError):
|
| 326 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
| 327 |
-
logger.warning(f"{current_api_key[:
|
| 328 |
switch_api_key()
|
| 329 |
time.sleep(delay)
|
| 330 |
return 0, None
|
| 331 |
|
| 332 |
elif isinstance(error, ServiceUnavailable):
|
| 333 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
| 334 |
-
logger.warning(f"{current_api_key[:
|
| 335 |
switch_api_key()
|
| 336 |
time.sleep(delay)
|
| 337 |
return 0, None
|
| 338 |
|
| 339 |
elif isinstance(error, PermissionDenied):
|
| 340 |
-
logger.error(f"{current_api_key[:
|
| 341 |
key_manager.blacklist_key(current_api_key)
|
| 342 |
switch_api_key()
|
| 343 |
return 0, None
|
|
@@ -398,7 +398,7 @@ def chat_completions():
|
|
| 398 |
show_thoughts = request_data.get('show_thoughts', False)
|
| 399 |
stream = request_data.get('stream', False)
|
| 400 |
hint = "流式" if stream else "非流"
|
| 401 |
-
logger.info(f"\n{model} [{hint}] → {current_api_key[:
|
| 402 |
is_thinking = 'thinking' in model
|
| 403 |
api_version = 'v1alpha' if is_thinking else 'v1beta'
|
| 404 |
response_type = 'streamGenerateContent' if stream else 'generateContent'
|
|
|
|
| 190 |
def show_all_keys(self):
|
| 191 |
logger.info(f"当前可用API key个数: {len(self.api_keys)} ")
|
| 192 |
for i, api_key in enumerate(self.api_keys):
|
| 193 |
+
logger.info(f"API Key{i}: {api_key[:8]}...{api_key[-3:]}")
|
| 194 |
|
| 195 |
def blacklist_key(self, key):
|
| 196 |
+
logger.warning(f"{key[:8]} → 暂时禁用 {api_key_blacklist_duration} 秒")
|
| 197 |
api_key_blacklist.add(key)
|
| 198 |
|
| 199 |
scheduler.add_job(lambda: api_key_blacklist.discard(key), 'date', run_date=datetime.now() + timedelta(seconds=api_key_blacklist_duration))
|
|
|
|
| 207 |
key = key_manager.get_available_key()
|
| 208 |
if key:
|
| 209 |
current_api_key = key
|
| 210 |
+
logger.info(f"API key 替换为 → {current_api_key[:8]}...{current_api_key[-3:]}")
|
| 211 |
else:
|
| 212 |
logger.error("API key 替换失败,所有API key都已耗尽或被暂时禁用,请重新配置或稍后重试")
|
| 213 |
|
| 214 |
+
logger.info(f"当前 API key: {current_api_key[:8]}...{current_api_key[-3:]}")
|
| 215 |
|
| 216 |
GEMINI_MODELS = [
|
| 217 |
{"id": "text-embedding-004"},
|
|
|
|
| 302 |
})
|
| 303 |
|
| 304 |
if isinstance(error, InvalidArgument):
|
| 305 |
+
logger.error(f"{current_api_key[:8]} → 无效,可能已过期或被删除")
|
| 306 |
key_manager.blacklist_key(current_api_key)
|
| 307 |
switch_api_key()
|
| 308 |
return 0, None
|
| 309 |
|
| 310 |
elif isinstance(error, ResourceExhausted):
|
| 311 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
| 312 |
+
logger.warning(f"{current_api_key[:8]} ... {current_api_key[-3:]} → 429 官方资源耗尽 → {delay} 秒后重试...")
|
| 313 |
key_manager.blacklist_key(current_api_key)
|
| 314 |
switch_api_key()
|
| 315 |
time.sleep(delay)
|
|
|
|
| 317 |
|
| 318 |
elif isinstance(error, Aborted):
|
| 319 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
| 320 |
+
logger.warning(f"{current_api_key[:8]} ... {current_api_key[-3:]} → 操作被中止 → {delay} 秒后重试...")
|
| 321 |
switch_api_key()
|
| 322 |
time.sleep(delay)
|
| 323 |
return 0, None
|
| 324 |
|
| 325 |
elif isinstance(error, InternalServerError):
|
| 326 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
| 327 |
+
logger.warning(f"{current_api_key[:8]} ... {current_api_key[-3:]} → 500 服务器内部错误 → {delay} 秒后重试...")
|
| 328 |
switch_api_key()
|
| 329 |
time.sleep(delay)
|
| 330 |
return 0, None
|
| 331 |
|
| 332 |
elif isinstance(error, ServiceUnavailable):
|
| 333 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
| 334 |
+
logger.warning(f"{current_api_key[:8]} ... {current_api_key[-3:]} → 503 服务不可用 → {delay} 秒后重试...")
|
| 335 |
switch_api_key()
|
| 336 |
time.sleep(delay)
|
| 337 |
return 0, None
|
| 338 |
|
| 339 |
elif isinstance(error, PermissionDenied):
|
| 340 |
+
logger.error(f"{current_api_key[:8]} ... {current_api_key[-3:]} → 403 权限被拒绝,该 API KEY 可能已经被官方封禁")
|
| 341 |
key_manager.blacklist_key(current_api_key)
|
| 342 |
switch_api_key()
|
| 343 |
return 0, None
|
|
|
|
| 398 |
show_thoughts = request_data.get('show_thoughts', False)
|
| 399 |
stream = request_data.get('stream', False)
|
| 400 |
hint = "流式" if stream else "非流"
|
| 401 |
+
logger.info(f"\n{model} [{hint}] → {current_api_key[:8]}...{current_api_key[-3:]}")
|
| 402 |
is_thinking = 'thinking' in model
|
| 403 |
api_version = 'v1alpha' if is_thinking else 'v1beta'
|
| 404 |
response_type = 'streamGenerateContent' if stream else 'generateContent'
|