yangtb24 commited on
Commit
c2bbdf7
·
verified ·
1 Parent(s): 984906e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -113
app.py CHANGED
@@ -103,18 +103,6 @@ def get_usd_to_cny_rate():
103
  logging.error(f"获取美元兑人民币汇率失败,错误信息:{e}")
104
  return None
105
 
106
- FREE_MODEL_TEST_KEY = (
107
- "sk-bmjbjzleaqfgtqfzmcnsbagxrlohriadnxqrzfocbizaxukw"
108
- )
109
-
110
- FREE_IMAGE_LIST = [
111
- "stabilityai/stable-diffusion-3-5-large",
112
- "black-forest-labs/FLUX.1-schnell",
113
- "stabilityai/stable-diffusion-3-medium",
114
- "stabilityai/stable-diffusion-xl-base-1.0",
115
- "stabilityai/stable-diffusion-2-1"
116
- ]
117
-
118
  def test_model_availability(api_key, model_name):
119
  """
120
  测试指定的模型是否可用。
@@ -151,15 +139,9 @@ def refresh_models():
151
  刷新模型列表和免费模型列表。
152
  """
153
  global text_models, free_text_models
154
- global embedding_models, free_embedding_models
155
- global image_models, free_image_models
156
 
157
  text_models = get_all_models(FREE_MODEL_TEST_KEY, "chat")
158
- embedding_models = get_all_models(FREE_MODEL_TEST_KEY, "embedding")
159
- image_models = get_all_models(FREE_MODEL_TEST_KEY, "text-to-image")
160
  free_text_models = []
161
- free_embedding_models = []
162
- free_image_models = []
163
 
164
  ban_models_str = os.environ.get("BAN_MODELS")
165
  ban_models = []
@@ -178,8 +160,6 @@ def refresh_models():
178
  ban_models = []
179
 
180
  text_models = [model for model in text_models if model not in ban_models]
181
- embedding_models = [model for model in embedding_models if model not in ban_models]
182
- image_models = [model for model in image_models if model not in ban_models]
183
 
184
  with concurrent.futures.ThreadPoolExecutor(
185
  max_workers=1000
@@ -200,84 +180,8 @@ def refresh_models():
200
  except Exception as exc:
201
  logging.error(f"模型 {model} 测试生成异常: {exc}")
202
 
203
- with concurrent.futures.ThreadPoolExecutor(
204
- max_workers=1000
205
- ) as executor:
206
- future_to_model = {
207
- executor.submit(
208
- test_embedding_model_availability,
209
- FREE_MODEL_TEST_KEY, model
210
- ): model for model in embedding_models
211
- }
212
- for future in concurrent.futures.as_completed(future_to_model):
213
- model = future_to_model[future]
214
- try:
215
- is_free = future.result()
216
- if is_free:
217
- free_embedding_models.append(model)
218
- except Exception as exc:
219
- logging.error(f"模型 {model} 测试生成异常: {exc}")
220
-
221
- with concurrent.futures.ThreadPoolExecutor(
222
- max_workers=1000
223
- ) as executor:
224
- future_to_model = {
225
- executor.submit(
226
- test_image_model_availability,
227
- FREE_MODEL_TEST_KEY, model
228
- ): model for model in image_models
229
- }
230
- for future in concurrent.futures.as_completed(future_to_model):
231
- model = future_to_model[future]
232
- try:
233
- is_free = future.result()
234
- if is_free:
235
- free_image_models.append(model)
236
- except Exception as exc:
237
- logging.error(f"模型 {model} 测试生成异常: {exc}")
238
-
239
  logging.info(f"所有文本模型列表:{text_models}")
240
  logging.info(f"免费文本模型列表:{free_text_models}")
241
- logging.info(f"所有向量模型列表:{embedding_models}")
242
- logging.info(f"免费向量模型列表:{free_embedding_models}")
243
- logging.info(f"所有生图模型列表:{image_models}")
244
- logging.info(f"免费生图模型列表:{free_image_models}")
245
-
246
- def test_embedding_model_availability(api_key, model_name):
247
- """
248
- 测试指定的向量模型是否可用。
249
- """
250
- headers = {
251
- "Authorization": f"Bearer {api_key}",
252
- "Content-Type": "application/json"
253
- }
254
- try:
255
- response = requests.post(
256
- EMBEDDINGS_ENDPOINT,
257
- headers=headers,
258
- json={
259
- "model": model_name,
260
- "input": ["hi"],
261
- },
262
- timeout=10
263
- )
264
- if response.status_code == 429 or response.status_code == 200:
265
- return True
266
- else:
267
- return False
268
- except requests.exceptions.RequestException as e:
269
- logging.error(
270
- f"测试向量模型 {model_name} 可用性失败,"
271
- f"API Key:{api_key},错误信息:{e}"
272
- )
273
- return False
274
-
275
- def test_image_model_availability(api_key, model_name):
276
- """
277
- 测试指定的图像模型是否在 FREE_IMAGE_LIST 中。
278
- 如果在列表中,返回 True,否则返回 False。
279
- """
280
- return model_name in FREE_IMAGE_LIST
281
 
282
  def load_keys():
283
  """
@@ -310,8 +214,6 @@ def load_keys():
310
  }
311
 
312
  invalid_keys = []
313
- free_keys = []
314
- unverified_keys = []
315
  valid_keys = []
316
 
317
  for future in concurrent.futures.as_completed(
@@ -322,25 +224,16 @@ def load_keys():
322
  key_type = future.result()
323
  if key_type == "invalid":
324
  invalid_keys.append(key)
325
- elif key_type == "free":
326
- free_keys.append(key)
327
- elif key_type == "unverified":
328
- unverified_keys.append(key)
329
  elif key_type == "valid":
330
  valid_keys.append(key)
331
  except Exception as exc:
332
  logging.error(f"处理 KEY {key} 生成异常: {exc}")
333
 
334
  logging.info(f"无效 KEY:{invalid_keys}")
335
- logging.info(f"免费 KEY:{free_keys}")
336
- logging.info(f"未实名 KEY:{unverified_keys}")
337
  logging.info(f"有效 KEY:{valid_keys}")
338
 
339
- global invalid_keys_global, free_keys_global
340
- global unverified_keys_global, valid_keys_global
341
  invalid_keys_global = invalid_keys
342
- free_keys_global = free_keys
343
- unverified_keys_global = unverified_keys
344
  valid_keys_global = valid_keys
345
 
346
  else:
@@ -356,12 +249,9 @@ def process_key(key, test_model):
356
  else:
357
  total_balance = credit_summary.get("total_balance", 0)
358
  if total_balance <= 0:
359
- return "free"
360
  else:
361
- if test_model_availability(key, test_model):
362
- return "valid"
363
- else:
364
- return "unverified"
365
 
366
  def get_all_models(api_key, sub_type):
367
  """
 
103
  logging.error(f"获取美元兑人民币汇率失败,错误信息:{e}")
104
  return None
105
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  def test_model_availability(api_key, model_name):
107
  """
108
  测试指定的模型是否可用。
 
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 = []
 
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
 
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
  """
 
214
  }
215
 
216
  invalid_keys = []
 
 
217
  valid_keys = []
218
 
219
  for future in concurrent.futures.as_completed(
 
224
  key_type = future.result()
225
  if key_type == "invalid":
226
  invalid_keys.append(key)
 
 
 
 
227
  elif key_type == "valid":
228
  valid_keys.append(key)
229
  except Exception as exc:
230
  logging.error(f"处理 KEY {key} 生成异常: {exc}")
231
 
232
  logging.info(f"无效 KEY:{invalid_keys}")
 
 
233
  logging.info(f"有效 KEY:{valid_keys}")
234
 
235
+ global invalid_keys_global, valid_keys_global
 
236
  invalid_keys_global = invalid_keys
 
 
237
  valid_keys_global = valid_keys
238
 
239
  else:
 
249
  else:
250
  total_balance = credit_summary.get("total_balance", 0)
251
  if total_balance <= 0:
252
+ return "invalid"
253
  else:
254
+ return "valid"
 
 
 
255
 
256
  def get_all_models(api_key, sub_type):
257
  """