Spaces:
Sleeping
Sleeping
Commit ·
d4b8661
1
Parent(s): 3a68c3f
hf adaption
Browse files- freeplay2api.py +28 -28
freeplay2api.py
CHANGED
|
@@ -7,6 +7,7 @@ import logging
|
|
| 7 |
import asyncio
|
| 8 |
import concurrent.futures
|
| 9 |
from typing import Any, List, Optional, Dict, Generator, Union
|
|
|
|
| 10 |
|
| 11 |
from proxy_pool import ProxyPool
|
| 12 |
from fastapi import FastAPI, HTTPException, Depends, Response, Request
|
|
@@ -274,10 +275,9 @@ class AccountManager:
|
|
| 274 |
logging.info(f"Loaded {len(self.accounts)} accounts from {self.filepath}")
|
| 275 |
|
| 276 |
def save_accounts(self):
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
f.write(json.dumps(account) + "\n")
|
| 281 |
|
| 282 |
def add_account(self, account: Dict):
|
| 283 |
with self.lock:
|
|
@@ -367,7 +367,12 @@ class KeyMaintainer(threading.Thread):
|
|
| 367 |
|
| 368 |
|
| 369 |
# --- FastAPI应用 ---
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
security = HTTPBearer()
|
| 372 |
|
| 373 |
|
|
@@ -379,30 +384,28 @@ def initialize_app():
|
|
| 379 |
return
|
| 380 |
|
| 381 |
# 1. 加载配置
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
"
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
"check_interval": 180,
|
| 396 |
-
}
|
| 397 |
}
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
config = default_config
|
| 401 |
-
logging.info("Created default config.json")
|
| 402 |
-
else:
|
| 403 |
with open("config.json", "r") as f:
|
| 404 |
config = json.load(f)
|
| 405 |
logging.info("Loaded config from config.json")
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
# 2. 加载客户端密钥
|
| 408 |
api_key = os.environ.get("API_KEY", "sk-123456")
|
|
@@ -434,9 +437,6 @@ async def authenticate_client(auth: HTTPAuthorizationCredentials = Depends(secur
|
|
| 434 |
raise HTTPException(status_code=403, detail="Invalid client API key.")
|
| 435 |
|
| 436 |
|
| 437 |
-
@app.on_event("startup")
|
| 438 |
-
async def startup_event():
|
| 439 |
-
initialize_app()
|
| 440 |
|
| 441 |
|
| 442 |
@app.get("/v1/models", response_model=ModelList)
|
|
|
|
| 7 |
import asyncio
|
| 8 |
import concurrent.futures
|
| 9 |
from typing import Any, List, Optional, Dict, Generator, Union
|
| 10 |
+
from contextlib import asynccontextmanager
|
| 11 |
|
| 12 |
from proxy_pool import ProxyPool
|
| 13 |
from fastapi import FastAPI, HTTPException, Depends, Response, Request
|
|
|
|
| 275 |
logging.info(f"Loaded {len(self.accounts)} accounts from {self.filepath}")
|
| 276 |
|
| 277 |
def save_accounts(self):
|
| 278 |
+
# This operation is disabled to ensure the application is stateless.
|
| 279 |
+
# Account data is now only managed in memory for the duration of the process.
|
| 280 |
+
pass
|
|
|
|
| 281 |
|
| 282 |
def add_account(self, account: Dict):
|
| 283 |
with self.lock:
|
|
|
|
| 367 |
|
| 368 |
|
| 369 |
# --- FastAPI应用 ---
|
| 370 |
+
@asynccontextmanager
|
| 371 |
+
async def lifespan(app: FastAPI):
|
| 372 |
+
initialize_app()
|
| 373 |
+
yield
|
| 374 |
+
|
| 375 |
+
app = FastAPI(title="Freeplay.ai to OpenAI API Adapter", lifespan=lifespan)
|
| 376 |
security = HTTPBearer()
|
| 377 |
|
| 378 |
|
|
|
|
| 384 |
return
|
| 385 |
|
| 386 |
# 1. 加载配置
|
| 387 |
+
default_config = {
|
| 388 |
+
"HOST": "0.0.0.0",
|
| 389 |
+
"PORT": 8057,
|
| 390 |
+
"ACCOUNTS_FILE": "accounts.json",
|
| 391 |
+
"LOW_BALANCE_THRESHOLD": 2.0,
|
| 392 |
+
"ACTIVE_KEY_THRESHOLD": 5,
|
| 393 |
+
"CHECK_INTERVAL_SECONDS": 300,
|
| 394 |
+
"REGISTRATION_CONCURRENCY": 2,
|
| 395 |
+
"USE_PROXY_POOL": True,
|
| 396 |
+
"PROXY_POOL_CONFIG": {
|
| 397 |
+
"target_count": 20,
|
| 398 |
+
"min_threshold": 5,
|
| 399 |
+
"check_interval": 180,
|
|
|
|
|
|
|
| 400 |
}
|
| 401 |
+
}
|
| 402 |
+
try:
|
|
|
|
|
|
|
|
|
|
| 403 |
with open("config.json", "r") as f:
|
| 404 |
config = json.load(f)
|
| 405 |
logging.info("Loaded config from config.json")
|
| 406 |
+
except (FileNotFoundError, json.JSONDecodeError):
|
| 407 |
+
config = default_config
|
| 408 |
+
logging.info("Using default config as config.json was not found or invalid.")
|
| 409 |
|
| 410 |
# 2. 加载客户端密钥
|
| 411 |
api_key = os.environ.get("API_KEY", "sk-123456")
|
|
|
|
| 437 |
raise HTTPException(status_code=403, detail="Invalid client API key.")
|
| 438 |
|
| 439 |
|
|
|
|
|
|
|
|
|
|
| 440 |
|
| 441 |
|
| 442 |
@app.get("/v1/models", response_model=ModelList)
|