Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
-
from
|
2 |
-
from
|
3 |
-
from fastapi import FastAPI, Request, HTTPException, status
|
4 |
-
from fastapi.responses import StreamingResponse, HTMLResponse, JSONResponse
|
5 |
from fastapi.middleware.cors import CORSMiddleware
|
6 |
import httpx
|
7 |
import json
|
@@ -9,16 +7,22 @@ import os
|
|
9 |
import random
|
10 |
from datetime import datetime
|
11 |
import pytz
|
12 |
-
import
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
22 |
"qwen-72b": "Qwen/Qwen2.5-72B-Instruct",
|
23 |
"qwen-32b": "Qwen/Qwen2.5-32B-Instruct",
|
24 |
"qwen-14b": "Qwen/Qwen2.5-14B-Instruct",
|
@@ -33,25 +37,31 @@ class ApiConfig:
|
|
33 |
"bce": "netease-youdao/bce-embedding-base_v1",
|
34 |
"bge-m3": "BAAI/bge-m3",
|
35 |
"bge-zh": "BAAI/bge-large-zh-v1.5",
|
36 |
-
"sd":"stabilityai/stable-diffusion-3-5-large",
|
37 |
-
"sd-turbo":"stabilityai/stable-diffusion-3-5-large-turbo",
|
38 |
-
"flux-s":"black-forest-labs/FLUX.1-schnell",
|
39 |
-
"flux-d":"black-forest-labs/FLUX.1-dev"
|
40 |
}
|
41 |
-
api_key_str: Optional[str] = os.environ.get("SI_KEY")
|
42 |
-
timezone: str = "Asia/Shanghai"
|
43 |
-
|
44 |
-
def __post_init__(self):
|
45 |
-
if not self.api_key_str :
|
46 |
-
logging.error("SI_KEY not found in env")
|
47 |
-
raise EnvironmentError("SI_KEY not found in env")
|
48 |
-
self.api_keys:list[str] = self.api_key_str.split(",")
|
49 |
-
if os.environ.get("MODEL_MAP"):
|
50 |
-
self.model_map = json.loads(os.environ.get("MODEL_MAP"))
|
51 |
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
|
|
54 |
app = FastAPI()
|
|
|
55 |
app.add_middleware(
|
56 |
CORSMiddleware,
|
57 |
allow_origins=["*"],
|
@@ -60,171 +70,115 @@ app.add_middleware(
|
|
60 |
allow_headers=["*"],
|
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 |
-
key_to_remove.append(key)
|
95 |
-
else:
|
96 |
-
balance_info = f"<h2>{key.strip()[0:4]}****{key.strip()[-4:]}————{balance}</h2>"
|
97 |
-
self.key_balance[key.strip()] = balance
|
98 |
-
self.key_balance_notes += balance_info
|
99 |
-
except HTTPException as e:
|
100 |
-
logging.error(f"Key {key} check balance failed, detail:{e.detail}")
|
101 |
-
key_to_remove.append(key)
|
102 |
-
except Exception as e:
|
103 |
-
logging.error(f"Key {key} check balance failed, unexcept error:{e}")
|
104 |
-
key_to_remove.append(key)
|
105 |
-
for remove_key in key_to_remove:
|
106 |
-
self.remove_key(remove_key)
|
107 |
-
self.last_update_time = datetime.now(pytz.timezone(config.timezone))
|
108 |
-
|
109 |
-
async def _fetch_balance(self, key: str) -> float:
|
110 |
-
"""发送 API 请求,获取 API Key 的余额"""
|
111 |
-
url = "https://api.siliconflow.cn/v1/user/info"
|
112 |
-
headers = {"Authorization": f"Bearer {key.strip()}"}
|
113 |
async with httpx.AsyncClient() as client:
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
except httpx.HTTPError as exc:
|
120 |
-
logging.error("httpx request error, detail:" + str(exc))
|
121 |
-
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Check balance failed with status:{exc.response.status_code},url:{exc.request.url}")
|
122 |
-
key_manager = ApiKeyManager(config.api_keys)
|
123 |
-
|
124 |
-
|
125 |
-
async def get_model_info() -> str:
|
126 |
-
models = ""
|
127 |
-
for key, value in config.model_map.items():
|
128 |
-
models += f"<h2>{key}————{value}</h2>"
|
129 |
-
return models
|
130 |
|
|
|
131 |
@app.get("/", response_class=HTMLResponse)
|
132 |
-
async def root()
|
133 |
-
"""
|
134 |
-
models_info =
|
135 |
-
|
|
|
|
|
|
|
136 |
<html>
|
137 |
<head>
|
138 |
-
<title
|
139 |
</head>
|
140 |
<body>
|
141 |
-
<h1>有效
|
142 |
{models_info}
|
143 |
-
<h1>最后更新时间:{
|
144 |
-
{
|
145 |
</body>
|
146 |
</html>
|
147 |
-
"""
|
148 |
|
149 |
|
150 |
@app.get("/check")
|
151 |
-
async def check()
|
152 |
-
"""
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
try:
|
170 |
-
key = key_manager.get_key()
|
171 |
-
except HTTPException as e:
|
172 |
-
return e
|
173 |
-
logging.info(f"using key {key[0:4]}***{key[-4:]} to {api_endpoint}")
|
174 |
-
headers = {"Authorization": f"Bearer {key}"}
|
175 |
-
url=f"{config.base_url}{api_endpoint}"
|
176 |
-
if api_endpoint == "/embeddings":
|
177 |
-
body=await request.json()
|
178 |
-
if "model" in body and body["model"] in config.model_map:
|
179 |
-
body["model"] = config.model_map[body["model"]]
|
180 |
-
async with httpx.AsyncClient() as client:
|
181 |
-
try:
|
182 |
-
response = await client.post(url,headers=headers, json=body)
|
183 |
-
response.raise_for_status()
|
184 |
-
return response.json()
|
185 |
-
except httpx.HTTPError as exc :
|
186 |
-
logging.error(f"httpx request embedding error :{exc}")
|
187 |
-
raise HTTPException(
|
188 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
189 |
-
detail=f"Request failed with status: {exc.response.status_code}, url:{exc.request.url}, detail:{exc.response.text}"
|
190 |
-
)
|
191 |
-
elif is_stream:
|
192 |
-
body = await request.json()
|
193 |
-
async def generate_response():
|
194 |
-
async with httpx.AsyncClient() as client:
|
195 |
-
async with client.stream("POST",url, headers=headers, json=body) as response:
|
196 |
-
response.raise_for_status() # 检查响应状态码
|
197 |
-
async for chunk in response.aiter_bytes():
|
198 |
-
if chunk:
|
199 |
-
yield chunk
|
200 |
-
return StreamingResponse(generate_response(), media_type="text/event-stream")
|
201 |
-
|
202 |
-
else :
|
203 |
-
body = await request.json()
|
204 |
-
async with httpx.AsyncClient() as client:
|
205 |
-
try:
|
206 |
-
response = await client.post(url, headers=headers, json=body)
|
207 |
-
response.raise_for_status()
|
208 |
-
return response.json()
|
209 |
-
except httpx.HTTPError as exc :
|
210 |
-
logging.error(f"httpx request error:{exc}")
|
211 |
-
raise HTTPException(
|
212 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
213 |
-
detail=f"Request failed with status: {exc.response.status_code},url:{exc.request.url},detail:{exc.response.text}"
|
214 |
-
)
|
215 |
@app.post("/hf/v1/chat/completions")
|
216 |
-
async def chat_completions(request: Request)
|
217 |
-
|
218 |
-
|
219 |
|
220 |
@app.post("/hf/v1/embeddings")
|
221 |
-
async def embeddings(request: Request)
|
222 |
-
"""转发
|
223 |
-
return await
|
224 |
-
|
225 |
-
@app.post("/hf/v1/images/generations")
|
226 |
-
async def image_generations(request:Request) -> Any:
|
227 |
-
"""转发图片生成请求"""
|
228 |
-
return await _forward_request(request, "/images/generations")
|
229 |
|
230 |
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request, HTTPException
|
2 |
+
from fastapi.responses import StreamingResponse, HTMLResponse
|
|
|
|
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
import httpx
|
5 |
import json
|
|
|
7 |
import random
|
8 |
from datetime import datetime
|
9 |
import pytz
|
10 |
+
from typing import Dict, List, Optional, Any, Callable
|
11 |
|
12 |
+
# --------------------------- 1. 环境变量和配置加载 ---------------------------
|
13 |
+
# 提取环境变量,并设置默认值(如果不存在)
|
14 |
+
SI_KEYS = os.environ.get("SI_KEY", "").split(",")
|
15 |
+
MODEL_MAP_JSON = os.environ.get("MODEL_MAP")
|
16 |
+
API_BASE_URL = os.environ.get("API_BASE_URL", "https://api.siliconflow.cn/v1")
|
17 |
|
18 |
+
# 定义模型映射的类型
|
19 |
+
ModelMap = Dict[str, str]
|
20 |
+
# 定义 Key 余额的类型
|
21 |
+
KeyBalance = Dict[str, float]
|
22 |
+
|
23 |
+
|
24 |
+
# 加载模型映射,如果环境变量 MODEL_MAP 存在,则使用它,否则使用默认值
|
25 |
+
DEFAULT_MODEL_MAP: ModelMap = {
|
26 |
"qwen-72b": "Qwen/Qwen2.5-72B-Instruct",
|
27 |
"qwen-32b": "Qwen/Qwen2.5-32B-Instruct",
|
28 |
"qwen-14b": "Qwen/Qwen2.5-14B-Instruct",
|
|
|
37 |
"bce": "netease-youdao/bce-embedding-base_v1",
|
38 |
"bge-m3": "BAAI/bge-m3",
|
39 |
"bge-zh": "BAAI/bge-large-zh-v1.5",
|
40 |
+
"sd": "stabilityai/stable-diffusion-3-5-large",
|
41 |
+
"sd-turbo": "stabilityai/stable-diffusion-3-5-large-turbo",
|
42 |
+
"flux-s": "black-forest-labs/FLUX.1-schnell",
|
43 |
+
"flux-d": "black-forest-labs/FLUX.1-dev",
|
44 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
model_map: ModelMap = json.loads(MODEL_MAP_JSON) if MODEL_MAP_JSON else DEFAULT_MODEL_MAP
|
47 |
+
|
48 |
+
# --------------------------- 2. 全局变量初始化 ---------------------------
|
49 |
+
keys: List[str] = [key.strip() for key in SI_KEYS if key.strip()] # 删除空白字符、空值
|
50 |
+
key_balance: KeyBalance = {}
|
51 |
+
key_balance_notes: str = ""
|
52 |
+
tz = pytz.timezone("Asia/Shanghai")
|
53 |
+
last_updated_time: str = ""
|
54 |
+
|
55 |
+
# --------------------------- 3. 密钥选择策略 ---------------------------
|
56 |
+
def get_api_key() -> str:
|
57 |
+
"""随机返回一个API密钥."""
|
58 |
+
random.shuffle(keys)
|
59 |
+
return keys[0] if keys else "" # 添加空列表保护
|
60 |
+
|
61 |
|
62 |
+
# --------------------------- 4. FastAPI 应用初始化 ---------------------------
|
63 |
app = FastAPI()
|
64 |
+
|
65 |
app.add_middleware(
|
66 |
CORSMiddleware,
|
67 |
allow_origins=["*"],
|
|
|
70 |
allow_headers=["*"],
|
71 |
)
|
72 |
|
73 |
+
|
74 |
+
# --------------------------- 5. 辅助函数 ---------------------------
|
75 |
+
def format_key_balance_note(key: str, balance: float) -> str:
|
76 |
+
"""将 key 和 balance 信息格式化为 HTML 片段."""
|
77 |
+
safe_key = f"{key[0:4]}****{key[-4:]}"
|
78 |
+
return f"<h2>{safe_key}————{balance}</h2>"
|
79 |
+
|
80 |
+
async def check_key(client: httpx.AsyncClient, key:str) -> Optional[float]:
|
81 |
+
"""检查单个密钥是否有效,并返回余额."""
|
82 |
+
url = f"{API_BASE_URL}/user/info"
|
83 |
+
headers = {"Authorization": f"Bearer {key}"}
|
84 |
+
try:
|
85 |
+
res = await client.get(url, headers=headers)
|
86 |
+
res.raise_for_status() # 确保状态码为200
|
87 |
+
balance = res.json()["data"]["balance"]
|
88 |
+
return float(balance)
|
89 |
+
except httpx.HTTPError as e:
|
90 |
+
print(f"Error checking key {key}: {e}")
|
91 |
+
return None
|
92 |
+
|
93 |
+
async def forward_request(
|
94 |
+
request: Request,
|
95 |
+
url_path: str,
|
96 |
+
is_stream: bool = False,
|
97 |
+
) -> Any:
|
98 |
+
"""通用的请求转发函数."""
|
99 |
+
body = await request.json()
|
100 |
+
key = get_api_key()
|
101 |
+
if not key:
|
102 |
+
raise HTTPException(status_code=400, detail="No valid API key available.")
|
103 |
+
headers = {"Authorization": f"Bearer {key}"}
|
104 |
|
105 |
+
if "model" in body and body["model"] in model_map:
|
106 |
+
body["model"] = model_map[body["model"]]
|
107 |
+
if not "stream" in body or not body['stream']
|
108 |
+
is_stream=False
|
109 |
+
if is_stream:
|
110 |
+
async def generate_response():
|
111 |
+
async with httpx.AsyncClient() as client:
|
112 |
+
async with client.stream(
|
113 |
+
"POST", f"{API_BASE_URL}{url_path}", headers=headers, json=body
|
114 |
+
) as response:
|
115 |
+
response.raise_for_status()
|
116 |
+
async for chunk in response.aiter_bytes():
|
117 |
+
if chunk:
|
118 |
+
yield chunk
|
119 |
+
return StreamingResponse(generate_response(), media_type="text/event-stream")
|
120 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
async with httpx.AsyncClient() as client:
|
122 |
+
response = await client.post(
|
123 |
+
f"{API_BASE_URL}{url_path}", headers=headers, json=body
|
124 |
+
)
|
125 |
+
response.raise_for_status()
|
126 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
# --------------------------- 6. API 路由处理 ---------------------------
|
129 |
@app.get("/", response_class=HTMLResponse)
|
130 |
+
async def root():
|
131 |
+
"""返回 HTML 格式的页面,显示模型和密钥信息."""
|
132 |
+
models_info = ""
|
133 |
+
for key, value in model_map.items():
|
134 |
+
models_info += f"<h2>{key}————{value}</h2>"
|
135 |
+
|
136 |
+
return f"""
|
137 |
<html>
|
138 |
<head>
|
139 |
+
<title>API 状态</title>
|
140 |
</head>
|
141 |
<body>
|
142 |
+
<h1>有效Key数量: {len(keys)}</h1>
|
143 |
{models_info}
|
144 |
+
<h1>最后更新时间:{last_updated_time}</h1>
|
145 |
+
{key_balance_notes}
|
146 |
</body>
|
147 |
</html>
|
148 |
+
"""
|
149 |
|
150 |
|
151 |
@app.get("/check")
|
152 |
+
async def check():
|
153 |
+
"""检查 API 密钥的余额,并更新 key_balance、key_balance_notes 和 last_updated_time."""
|
154 |
+
global key_balance, key_balance_notes, last_updated_time, keys
|
155 |
+
key_balance_notes = ""
|
156 |
+
new_keys = []
|
157 |
+
key_balance = {}
|
158 |
+
async with httpx.AsyncClient() as client:
|
159 |
+
for key in keys:
|
160 |
+
balance = await check_key(client, key)
|
161 |
+
if balance is not None and balance >= 0.1:
|
162 |
+
key_balance[key] = balance
|
163 |
+
key_balance_notes += format_key_balance_note(key, balance)
|
164 |
+
new_keys.append(key)
|
165 |
+
keys=new_keys
|
166 |
+
last_updated_time = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
167 |
+
return f"更新成功:{last_updated_time}"
|
168 |
+
|
169 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
@app.post("/hf/v1/chat/completions")
|
171 |
+
async def chat_completions(request: Request):
|
172 |
+
"""转发聊天补全请求,处理流式和非流式响应."""
|
173 |
+
return await forward_request(request, "/chat/completions", is_stream=True)
|
174 |
|
175 |
@app.post("/hf/v1/embeddings")
|
176 |
+
async def embeddings(request: Request):
|
177 |
+
"""转发 embedding 请求."""
|
178 |
+
return await forward_request(request, "/embeddings")
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
|
181 |
+
@app.post("/hf/v1/images/generations")
|
182 |
+
async def images_generations(request: Request):
|
183 |
+
"""转发图像生成请求."""
|
184 |
+
return await forward_request(request,"/images/generations",)
|