Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI,Request,HTTPException
|
2 |
+
from fastapi.responses import StreamingResponse
|
3 |
+
from fastapi.responses import HTMLResponse
|
4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
5 |
+
import httpx
|
6 |
+
import json,os,random
|
7 |
+
from datetime import datetime
|
8 |
+
import pytz
|
9 |
+
def get_si_key():
|
10 |
+
random.shuffle(keys)
|
11 |
+
return keys[0]
|
12 |
+
app = FastAPI()
|
13 |
+
app.add_middleware(
|
14 |
+
CORSMiddleware,
|
15 |
+
allow_origins=["*"], # 允许的源列表
|
16 |
+
allow_credentials=True, # 允许发送 cookies
|
17 |
+
allow_methods=["*"], # 允许所有方法
|
18 |
+
allow_headers=["*"], # 允许所有头部
|
19 |
+
)
|
20 |
+
base_url = "https://api.siliconflow.cn/v1/chat/completions"
|
21 |
+
if os.environ.get("MODEL_MAP"):
|
22 |
+
model_map=json.loads(os.environ.get("MODEL_MAP"))
|
23 |
+
else:
|
24 |
+
model_map={
|
25 |
+
"qwen-72b":"Qwen/Qwen2.5-72B-Instruct",
|
26 |
+
"qwen-32b":"Qwen/Qwen2.5-32B-Instruct",
|
27 |
+
"qwen-14b":"Qwen/Qwen2.5-14B-Instruct",
|
28 |
+
"qwen-7b":"Qwen/Qwen2.5-7B-Instruct",
|
29 |
+
"qwen-vl":"Qwen/Qwen2-VL-72B-Instruct",
|
30 |
+
"qwen-coder":"Qwen/Qwen2.5-Coder-32B-Instruct",
|
31 |
+
"qwq":"Qwen/QwQ-32B-Preview",
|
32 |
+
"o1":"AIDC-AI/Marco-o1",
|
33 |
+
"deepseek":"deepseek-ai/DeepSeek-V2.5",
|
34 |
+
"deepseek-vl":"deepseek-ai/deepseek-vl2",
|
35 |
+
"glm-9b":"THUDM/glm-4-9b-chat",
|
36 |
+
"bce":"netease-youdao/bce-embedding-base_v1",
|
37 |
+
"bge-m3":"BAAI/bge-m3",
|
38 |
+
"bge-zh":"BAAI/bge-large-zh-v1.5"
|
39 |
+
}
|
40 |
+
keys=os.environ.get("SI_KEY").split(",")
|
41 |
+
key_balacnce={}
|
42 |
+
key_balacnce_notes=""
|
43 |
+
# 创建一个东八区的时区对象
|
44 |
+
tz = pytz.timezone('Asia/Shanghai')
|
45 |
+
|
46 |
+
now=""
|
47 |
+
@app.get("/",response_class=HTMLResponse)
|
48 |
+
async def root():
|
49 |
+
# 获取当前时间,并设置时区为东八区
|
50 |
+
models=""
|
51 |
+
for key in model_map:
|
52 |
+
models+=f"<h2>{key}————{model_map[key]}</h2>"
|
53 |
+
global now
|
54 |
+
return f"""
|
55 |
+
<html>
|
56 |
+
<head>
|
57 |
+
<title>富文本示例</title>
|
58 |
+
</head>
|
59 |
+
<body>
|
60 |
+
<h1>有效key数量:{len(keys)}</h1>
|
61 |
+
{models}
|
62 |
+
<h1>最后更新时间:{now}</h1>
|
63 |
+
{key_balacnce_notes}
|
64 |
+
</body>
|
65 |
+
</html>
|
66 |
+
"""
|
67 |
+
@app.get("/check")
|
68 |
+
async def check():
|
69 |
+
global key_balacnce,key_balacnce_notes,now,keys
|
70 |
+
key_balacnce_notes=""
|
71 |
+
for i,key in enumerate(keys):
|
72 |
+
url = "https://api.siliconflow.cn/v1/user/info"
|
73 |
+
headers={
|
74 |
+
"Authorization":f"Bearer {key.strip()}"
|
75 |
+
}
|
76 |
+
async with httpx.AsyncClient() as client:
|
77 |
+
res=await client.get(url,headers=headers)
|
78 |
+
if res.status_code==200:
|
79 |
+
balance=res.json()['data']['balance']
|
80 |
+
if float(balance)<0.1:
|
81 |
+
keys.pop(i)
|
82 |
+
continue
|
83 |
+
va=f'''<h2>{key.strip()[0:4]}****{key.strip()[-4:]}————{balance}</h2>'''
|
84 |
+
key_balacnce[key.strip()]=balance
|
85 |
+
key_balacnce_notes+=va
|
86 |
+
|
87 |
+
now = datetime.now(tz)
|
88 |
+
|
89 |
+
return f"更新成功:{now}"
|
90 |
+
@app.post("/hf/v1/chat/completions")
|
91 |
+
async def reforword(request:Request):
|
92 |
+
body = await request.json()
|
93 |
+
# 获取 API 密钥
|
94 |
+
key = get_si_key()
|
95 |
+
print(key)
|
96 |
+
headers = {
|
97 |
+
"Authorization": f"Bearer {key}"
|
98 |
+
}
|
99 |
+
body_map=model_map
|
100 |
+
if "model" in body.keys() and body['model'] in body_map.keys():
|
101 |
+
body['model']=body_map[body['model']]
|
102 |
+
if "stream" in body.keys() and body['stream']:
|
103 |
+
# 使用 httpx 发送流式请求
|
104 |
+
async def generate_response():
|
105 |
+
async with httpx.AsyncClient() as client:
|
106 |
+
async with client.stream("POST", base_url, headers=headers, json=body) as response:
|
107 |
+
response.raise_for_status() # 检查响应状态码
|
108 |
+
async for chunk in response.aiter_bytes():
|
109 |
+
if chunk:
|
110 |
+
yield chunk
|
111 |
+
|
112 |
+
return StreamingResponse(generate_response(), media_type="text/event-stream")
|
113 |
+
else:
|
114 |
+
# 发送 POST 请求
|
115 |
+
async with httpx.AsyncClient() as client:
|
116 |
+
response = await client.post(base_url, headers=headers, json=body)
|
117 |
+
response.raise_for_status()
|
118 |
+
return response.json()
|
119 |
+
@app.post("/hf/v1/embeddings")
|
120 |
+
async def embedding(request:Request):
|
121 |
+
body=await request.json()
|
122 |
+
body_map=model_map
|
123 |
+
if "model" in body.keys() and body['model'] in body_map.keys():
|
124 |
+
body['model']=body_map[body['model']]
|
125 |
+
# 获取 API 密钥
|
126 |
+
key = get_si_key()
|
127 |
+
print(key)
|
128 |
+
headers = {
|
129 |
+
"Authorization": f"Bearer {key}"
|
130 |
+
}
|
131 |
+
async with httpx.AsyncClient() as client:
|
132 |
+
response = await client.post(base_url, json=body, headers=headers)
|
133 |
+
response.raise_for_status() # 检查请求是否成功
|
134 |
+
return response.json()
|