Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,7 @@ app = Flask(__name__)
|
|
19 |
all_models = []
|
20 |
free_models = []
|
21 |
|
22 |
-
#
|
23 |
invalid_keys_global = []
|
24 |
free_keys_global = []
|
25 |
unverified_keys_global = []
|
@@ -70,7 +70,7 @@ def test_model_availability(api_key, model_name):
|
|
70 |
# 检查是否是429错误
|
71 |
if response.status_code == 429:
|
72 |
return True
|
73 |
-
|
74 |
response.json() # 尝试解析 JSON 响应
|
75 |
return True
|
76 |
except requests.exceptions.RequestException as e:
|
@@ -114,7 +114,7 @@ def load_keys():
|
|
114 |
logging.info(f"免费 KEY:{free_keys}")
|
115 |
logging.info(f"未实名 KEY:{unverified_keys}")
|
116 |
logging.info(f"有效 KEY:{valid_keys}")
|
117 |
-
|
118 |
# 更新全局的 key 列表
|
119 |
global invalid_keys_global, free_keys_global, unverified_keys_global, valid_keys_global
|
120 |
invalid_keys_global = invalid_keys
|
@@ -155,7 +155,7 @@ def refresh_models():
|
|
155 |
刷新模型列表和免费模型列表。
|
156 |
"""
|
157 |
global all_models, free_models
|
158 |
-
|
159 |
# 使用 valid_keys_global 中的第一个 key 获取完整模型列表
|
160 |
if valid_keys_global:
|
161 |
all_models = get_all_models(valid_keys_global[0])
|
@@ -226,17 +226,6 @@ def check_authorization(request):
|
|
226 |
|
227 |
return True
|
228 |
|
229 |
-
def get_total_balance():
|
230 |
-
"""
|
231 |
-
获取所有大于等于0的key的额度的和。
|
232 |
-
"""
|
233 |
-
total_balance = 0
|
234 |
-
for key in free_keys_global + unverified_keys_global + valid_keys_global:
|
235 |
-
credit_summary = get_credit_summary(key)
|
236 |
-
if credit_summary is not None:
|
237 |
-
total_balance += credit_summary.get("total_balance", 0)
|
238 |
-
return total_balance
|
239 |
-
|
240 |
# 创建一个后台调度器
|
241 |
scheduler = BackgroundScheduler()
|
242 |
|
@@ -310,7 +299,7 @@ def handsome_chat_completions():
|
|
310 |
stream=data.get("stream", False),
|
311 |
timeout=60
|
312 |
)
|
313 |
-
|
314 |
# 检查是否是429错误
|
315 |
if response.status_code == 429:
|
316 |
return jsonify(response.json()), 429
|
@@ -324,25 +313,41 @@ def handsome_chat_completions():
|
|
324 |
return jsonify({"error": str(e)}), 500
|
325 |
|
326 |
@app.route('/handsome/v1/models', methods=['GET'])
|
327 |
-
def
|
328 |
"""
|
329 |
-
处理 /handsome/v1/models
|
330 |
"""
|
331 |
if not check_authorization(request):
|
332 |
return jsonify({"error": "Unauthorized"}), 401
|
333 |
|
334 |
-
|
|
|
|
|
|
|
|
|
335 |
|
336 |
-
@app.route('/handsome/v1/dashboard/billing
|
337 |
-
def
|
338 |
"""
|
339 |
-
处理 /handsome/v1/
|
340 |
"""
|
341 |
if not check_authorization(request):
|
342 |
return jsonify({"error": "Unauthorized"}), 401
|
343 |
|
344 |
-
total_balance =
|
345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
|
347 |
if __name__ == '__main__':
|
348 |
# 打印所有环境变量,方便调试
|
|
|
19 |
all_models = []
|
20 |
free_models = []
|
21 |
|
22 |
+
# 全局变量,用于存储各种类型的 KEY 列表
|
23 |
invalid_keys_global = []
|
24 |
free_keys_global = []
|
25 |
unverified_keys_global = []
|
|
|
70 |
# 检查是否是429错误
|
71 |
if response.status_code == 429:
|
72 |
return True
|
73 |
+
|
74 |
response.json() # 尝试解析 JSON 响应
|
75 |
return True
|
76 |
except requests.exceptions.RequestException as e:
|
|
|
114 |
logging.info(f"免费 KEY:{free_keys}")
|
115 |
logging.info(f"未实名 KEY:{unverified_keys}")
|
116 |
logging.info(f"有效 KEY:{valid_keys}")
|
117 |
+
|
118 |
# 更新全局的 key 列表
|
119 |
global invalid_keys_global, free_keys_global, unverified_keys_global, valid_keys_global
|
120 |
invalid_keys_global = invalid_keys
|
|
|
155 |
刷新模型列表和免费模型列表。
|
156 |
"""
|
157 |
global all_models, free_models
|
158 |
+
|
159 |
# 使用 valid_keys_global 中的第一个 key 获取完整模型列表
|
160 |
if valid_keys_global:
|
161 |
all_models = get_all_models(valid_keys_global[0])
|
|
|
226 |
|
227 |
return True
|
228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
# 创建一个后台调度器
|
230 |
scheduler = BackgroundScheduler()
|
231 |
|
|
|
299 |
stream=data.get("stream", False),
|
300 |
timeout=60
|
301 |
)
|
302 |
+
|
303 |
# 检查是否是429错误
|
304 |
if response.status_code == 429:
|
305 |
return jsonify(response.json()), 429
|
|
|
313 |
return jsonify({"error": str(e)}), 500
|
314 |
|
315 |
@app.route('/handsome/v1/models', methods=['GET'])
|
316 |
+
def list_models():
|
317 |
"""
|
318 |
+
处理 /handsome/v1/models 路由的请求,返回模型列表。
|
319 |
"""
|
320 |
if not check_authorization(request):
|
321 |
return jsonify({"error": "Unauthorized"}), 401
|
322 |
|
323 |
+
# 返回模型列表和免费模型列表
|
324 |
+
return jsonify({
|
325 |
+
"data": [{"id": model, "object": "model"} for model in all_models],
|
326 |
+
"free_models": free_models
|
327 |
+
})
|
328 |
|
329 |
+
@app.route('/handsome/v1/dashboard/billing', methods=['GET'])
|
330 |
+
def billing_info():
|
331 |
"""
|
332 |
+
处理 /handsome/v1/dashboard/billing 路由的请求,返回所有余额大于0的key的总和。
|
333 |
"""
|
334 |
if not check_authorization(request):
|
335 |
return jsonify({"error": "Unauthorized"}), 401
|
336 |
|
337 |
+
total_balance = 0
|
338 |
+
# 遍历 valid_keys_global 和 unverified_keys_global 获取总余额
|
339 |
+
for key in valid_keys_global + unverified_keys_global:
|
340 |
+
credit_summary = get_credit_summary(key)
|
341 |
+
if credit_summary and credit_summary.get("total_balance", 0) > 0:
|
342 |
+
total_balance += credit_summary["total_balance"]
|
343 |
+
|
344 |
+
return jsonify({
|
345 |
+
"total_granted": total_balance, # 使用 total_granted 字段
|
346 |
+
"total_usage": 0, # 暂时设置为0,因为没有使用量数据
|
347 |
+
"total_available": total_balance,
|
348 |
+
"has_payment_method": False,
|
349 |
+
"object": "billing_summary"
|
350 |
+
})
|
351 |
|
352 |
if __name__ == '__main__':
|
353 |
# 打印所有环境变量,方便调试
|