Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -19,6 +19,12 @@ app = Flask(__name__)
|
|
19 |
all_models = []
|
20 |
free_models = []
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def get_credit_summary(api_key):
|
23 |
"""
|
24 |
使用 API 密钥获取额度信息。
|
@@ -220,6 +226,17 @@ def check_authorization(request):
|
|
220 |
|
221 |
return True
|
222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
# 创建一个后台调度器
|
224 |
scheduler = BackgroundScheduler()
|
225 |
|
@@ -306,6 +323,27 @@ def handsome_chat_completions():
|
|
306 |
except requests.exceptions.RequestException as e:
|
307 |
return jsonify({"error": str(e)}), 500
|
308 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
309 |
if __name__ == '__main__':
|
310 |
# 打印所有环境变量,方便调试
|
311 |
logging.info(f"环境变量:{os.environ}")
|
|
|
19 |
all_models = []
|
20 |
free_models = []
|
21 |
|
22 |
+
# 全局的 key 列表
|
23 |
+
invalid_keys_global = []
|
24 |
+
free_keys_global = []
|
25 |
+
unverified_keys_global = []
|
26 |
+
valid_keys_global = []
|
27 |
+
|
28 |
def get_credit_summary(api_key):
|
29 |
"""
|
30 |
使用 API 密钥获取额度信息。
|
|
|
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 |
|
|
|
323 |
except requests.exceptions.RequestException as e:
|
324 |
return jsonify({"error": str(e)}), 500
|
325 |
|
326 |
+
@app.route('/handsome/v1/models', methods=['GET'])
|
327 |
+
def handsome_models():
|
328 |
+
"""
|
329 |
+
处理 /handsome/v1/models 路由的请求,添加鉴权,返回所有模型列表。
|
330 |
+
"""
|
331 |
+
if not check_authorization(request):
|
332 |
+
return jsonify({"error": "Unauthorized"}), 401
|
333 |
+
|
334 |
+
return jsonify({"data": [{"id": model} for model in all_models]})
|
335 |
+
|
336 |
+
@app.route('/handsome/v1/balance', methods=['GET'])
|
337 |
+
def handsome_balance():
|
338 |
+
"""
|
339 |
+
处理 /handsome/v1/balance 路由的请求,添加鉴权,返回所有可用KEY的额度总和。
|
340 |
+
"""
|
341 |
+
if not check_authorization(request):
|
342 |
+
return jsonify({"error": "Unauthorized"}), 401
|
343 |
+
|
344 |
+
total_balance = get_total_balance()
|
345 |
+
return jsonify({"total_balance": total_balance})
|
346 |
+
|
347 |
if __name__ == '__main__':
|
348 |
# 打印所有环境变量,方便调试
|
349 |
logging.info(f"环境变量:{os.environ}")
|