Update app.py
Browse files
app.py
CHANGED
@@ -22,7 +22,7 @@ logging.basicConfig(level=logging.INFO,
|
|
22 |
|
23 |
API_ENDPOINT = "https://api.deepseek.com/user/balance"
|
24 |
TEST_MODEL_ENDPOINT = "https://api.deepseek.com/v1/chat/completions"
|
25 |
-
MODELS_ENDPOINT = "https://api.deepseek.com/
|
26 |
|
27 |
app = Flask(__name__)
|
28 |
|
@@ -103,86 +103,6 @@ def get_usd_to_cny_rate():
|
|
103 |
logging.error(f"获取美元兑人民币汇率失败,错误信息:{e}")
|
104 |
return None
|
105 |
|
106 |
-
def test_model_availability(api_key, model_name):
|
107 |
-
"""
|
108 |
-
测试指定的模型是否可用。
|
109 |
-
"""
|
110 |
-
headers = {
|
111 |
-
"Authorization": f"Bearer {api_key}",
|
112 |
-
"Content-Type": "application/json"
|
113 |
-
}
|
114 |
-
try:
|
115 |
-
response = requests.post(
|
116 |
-
TEST_MODEL_ENDPOINT,
|
117 |
-
headers=headers,
|
118 |
-
json={
|
119 |
-
"model": model_name,
|
120 |
-
"messages": [{"role": "user", "content": "hi"}],
|
121 |
-
"max_tokens": 5,
|
122 |
-
"stream": False
|
123 |
-
},
|
124 |
-
timeout=5
|
125 |
-
)
|
126 |
-
if response.status_code == 429 or response.status_code == 200:
|
127 |
-
return True
|
128 |
-
else:
|
129 |
-
return False
|
130 |
-
except requests.exceptions.RequestException as e:
|
131 |
-
logging.error(
|
132 |
-
f"测试模型 {model_name} 可用性失败,"
|
133 |
-
f"API Key:{api_key},错误信息:{e}"
|
134 |
-
)
|
135 |
-
return False
|
136 |
-
|
137 |
-
def refresh_models():
|
138 |
-
"""
|
139 |
-
刷新模型列表和免费模型列表。
|
140 |
-
"""
|
141 |
-
global text_models, free_text_models
|
142 |
-
|
143 |
-
text_models = get_all_models(FREE_MODEL_TEST_KEY, "chat")
|
144 |
-
free_text_models = []
|
145 |
-
|
146 |
-
ban_models_str = os.environ.get("BAN_MODELS")
|
147 |
-
ban_models = []
|
148 |
-
if ban_models_str:
|
149 |
-
try:
|
150 |
-
ban_models = json.loads(ban_models_str)
|
151 |
-
if not isinstance(ban_models, list):
|
152 |
-
logging.warning(
|
153 |
-
"环境变量 BAN_MODELS 格式不正确,应为 JSON 数组。"
|
154 |
-
)
|
155 |
-
ban_models = []
|
156 |
-
except json.JSONDecodeError:
|
157 |
-
logging.warning(
|
158 |
-
"环境变量 BAN_MODELS JSON 解析失败,请检查格式。"
|
159 |
-
)
|
160 |
-
ban_models = []
|
161 |
-
|
162 |
-
text_models = [model for model in text_models if model not in ban_models]
|
163 |
-
|
164 |
-
with concurrent.futures.ThreadPoolExecutor(
|
165 |
-
max_workers=1000
|
166 |
-
) as executor:
|
167 |
-
future_to_model = {
|
168 |
-
executor.submit(
|
169 |
-
test_model_availability,
|
170 |
-
FREE_MODEL_TEST_KEY,
|
171 |
-
model
|
172 |
-
): model for model in text_models
|
173 |
-
}
|
174 |
-
for future in concurrent.futures.as_completed(future_to_model):
|
175 |
-
model = future_to_model[future]
|
176 |
-
try:
|
177 |
-
is_free = future.result()
|
178 |
-
if is_free:
|
179 |
-
free_text_models.append(model)
|
180 |
-
except Exception as exc:
|
181 |
-
logging.error(f"模型 {model} 测试生成异常: {exc}")
|
182 |
-
|
183 |
-
logging.info(f"所有文本模型列表:{text_models}")
|
184 |
-
logging.info(f"免费文本模型列表:{free_text_models}")
|
185 |
-
|
186 |
def load_keys():
|
187 |
"""
|
188 |
从环境变量中加载 keys,进行去重,
|
@@ -190,12 +110,6 @@ def load_keys():
|
|
190 |
然后记录到日志中。
|
191 |
使用线程池并发处理每个 key。
|
192 |
"""
|
193 |
-
keys_str = os.environ.get("KEYS")
|
194 |
-
test_model = os.environ.get(
|
195 |
-
"TEST_MODEL",
|
196 |
-
"Pro/google/gemma-2-9b-it"
|
197 |
-
)
|
198 |
-
|
199 |
if keys_str:
|
200 |
keys = [key.strip() for key in keys_str.split(',')]
|
201 |
unique_keys = list(set(keys))
|
|
|
22 |
|
23 |
API_ENDPOINT = "https://api.deepseek.com/user/balance"
|
24 |
TEST_MODEL_ENDPOINT = "https://api.deepseek.com/v1/chat/completions"
|
25 |
+
MODELS_ENDPOINT = "https://api.deepseek.com/models"
|
26 |
|
27 |
app = Flask(__name__)
|
28 |
|
|
|
103 |
logging.error(f"获取美元兑人民币汇率失败,错误信息:{e}")
|
104 |
return None
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
def load_keys():
|
107 |
"""
|
108 |
从环境变量中加载 keys,进行去重,
|
|
|
110 |
然后记录到日志中。
|
111 |
使用线程池并发处理每个 key。
|
112 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
if keys_str:
|
114 |
keys = [key.strip() for key in keys_str.split(',')]
|
115 |
unique_keys = list(set(keys))
|