Spaces:
Running
Running
CassiopeiaCode
commited on
Commit
·
e2e9f4b
1
Parent(s):
2c96fd3
移除401/403重试,添加后台定时刷新token线程
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import time
|
|
| 5 |
import sqlite3
|
| 6 |
import importlib.util
|
| 7 |
import random
|
|
|
|
| 8 |
from pathlib import Path
|
| 9 |
from typing import Dict, Optional, List, Any, Generator, Tuple
|
| 10 |
|
|
@@ -108,6 +109,31 @@ def _row_to_dict(r: sqlite3.Row) -> Dict[str, Any]:
|
|
| 108 |
|
| 109 |
_ensure_db()
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
# ------------------------------------------------------------------------------
|
| 112 |
# Env and API Key authorization (keys are independent of AWS accounts)
|
| 113 |
# ------------------------------------------------------------------------------
|
|
@@ -341,17 +367,7 @@ def chat_completions(req: ChatCompletionRequest, account: Dict[str, Any] = Depen
|
|
| 341 |
access = refreshed.get("accessToken")
|
| 342 |
if not access:
|
| 343 |
raise HTTPException(status_code=502, detail="Access token unavailable after refresh")
|
| 344 |
-
|
| 345 |
-
return send_chat_request(access, [m.model_dump() for m in req.messages], model=model, stream=stream)
|
| 346 |
-
except requests.HTTPError as e:
|
| 347 |
-
status = getattr(e.response, "status_code", None)
|
| 348 |
-
if status in (401, 403):
|
| 349 |
-
refreshed = refresh_access_token_in_db(account["id"])
|
| 350 |
-
access2 = refreshed.get("accessToken")
|
| 351 |
-
if not access2:
|
| 352 |
-
raise HTTPException(status_code=502, detail="Token refresh failed")
|
| 353 |
-
return send_chat_request(access2, [m.model_dump() for m in req.messages], model=model, stream=stream)
|
| 354 |
-
raise
|
| 355 |
|
| 356 |
if not do_stream:
|
| 357 |
try:
|
|
|
|
| 5 |
import sqlite3
|
| 6 |
import importlib.util
|
| 7 |
import random
|
| 8 |
+
import threading
|
| 9 |
from pathlib import Path
|
| 10 |
from typing import Dict, Optional, List, Any, Generator, Tuple
|
| 11 |
|
|
|
|
| 109 |
|
| 110 |
_ensure_db()
|
| 111 |
|
| 112 |
+
# ------------------------------------------------------------------------------
|
| 113 |
+
# Background token refresh thread
|
| 114 |
+
# ------------------------------------------------------------------------------
|
| 115 |
+
|
| 116 |
+
def _refresh_stale_tokens():
|
| 117 |
+
while True:
|
| 118 |
+
try:
|
| 119 |
+
time.sleep(300) # 5 minutes
|
| 120 |
+
now = time.time()
|
| 121 |
+
with _conn() as conn:
|
| 122 |
+
rows = conn.execute("SELECT id, last_refresh_time FROM accounts WHERE enabled=1").fetchall()
|
| 123 |
+
for row in rows:
|
| 124 |
+
acc_id, last_refresh = row[0], row[1]
|
| 125 |
+
if last_refresh:
|
| 126 |
+
try:
|
| 127 |
+
last_time = time.mktime(time.strptime(last_refresh, "%Y-%m-%dT%H:%M:%S"))
|
| 128 |
+
if now - last_time > 1500: # 25 minutes
|
| 129 |
+
refresh_access_token_in_db(acc_id)
|
| 130 |
+
except Exception:
|
| 131 |
+
pass
|
| 132 |
+
except Exception:
|
| 133 |
+
pass
|
| 134 |
+
|
| 135 |
+
threading.Thread(target=_refresh_stale_tokens, daemon=True).start()
|
| 136 |
+
|
| 137 |
# ------------------------------------------------------------------------------
|
| 138 |
# Env and API Key authorization (keys are independent of AWS accounts)
|
| 139 |
# ------------------------------------------------------------------------------
|
|
|
|
| 367 |
access = refreshed.get("accessToken")
|
| 368 |
if not access:
|
| 369 |
raise HTTPException(status_code=502, detail="Access token unavailable after refresh")
|
| 370 |
+
return send_chat_request(access, [m.model_dump() for m in req.messages], model=model, stream=stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
if not do_stream:
|
| 373 |
try:
|