Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import time
|
| 3 |
import logging
|
| 4 |
import requests
|
|
|
|
| 5 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 6 |
from flask import Flask, request, jsonify, Response, stream_with_context
|
| 7 |
|
|
@@ -326,27 +327,79 @@ def list_models():
|
|
| 326 |
"free_models": free_models
|
| 327 |
})
|
| 328 |
|
| 329 |
-
|
| 330 |
-
def billing_info():
|
| 331 |
"""
|
| 332 |
-
|
| 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
|
| 342 |
-
total_balance += credit_summary
|
| 343 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
return jsonify({
|
| 345 |
-
"
|
| 346 |
-
"total_usage": 0, # 暂时设置为0,因为没有使用量数据
|
| 347 |
-
"total_available": total_balance,
|
| 348 |
"has_payment_method": False,
|
| 349 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
})
|
| 351 |
|
| 352 |
if __name__ == '__main__':
|
|
|
|
| 2 |
import time
|
| 3 |
import logging
|
| 4 |
import requests
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 7 |
from flask import Flask, request, jsonify, Response, stream_with_context
|
| 8 |
|
|
|
|
| 327 |
"free_models": free_models
|
| 328 |
})
|
| 329 |
|
| 330 |
+
def get_billing_info():
|
|
|
|
| 331 |
"""
|
| 332 |
+
获取所有KEY的额度信息。
|
| 333 |
"""
|
|
|
|
|
|
|
|
|
|
| 334 |
total_balance = 0
|
|
|
|
| 335 |
for key in valid_keys_global + unverified_keys_global:
|
| 336 |
credit_summary = get_credit_summary(key)
|
| 337 |
+
if credit_summary:
|
| 338 |
+
total_balance += credit_summary.get("total_balance", 0)
|
| 339 |
+
return total_balance
|
| 340 |
+
|
| 341 |
+
@app.route('/handsome/v1/dashboard/billing/usage', methods=['GET'])
|
| 342 |
+
def billing_usage():
|
| 343 |
+
"""
|
| 344 |
+
处理 /handsome/v1/dashboard/billing/usage 路由的请求,返回用量信息(模拟)。
|
| 345 |
+
"""
|
| 346 |
+
if not check_authorization(request):
|
| 347 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 348 |
+
|
| 349 |
+
# 获取当前时间和30天前的时间
|
| 350 |
+
end_date = datetime.now()
|
| 351 |
+
start_date = end_date - timedelta(days=30)
|
| 352 |
+
|
| 353 |
+
# 构造用量数据(模拟)
|
| 354 |
+
daily_usage = []
|
| 355 |
+
current_date = start_date
|
| 356 |
+
while current_date <= end_date:
|
| 357 |
+
# 假设每天的用量是总额度的 1/1000
|
| 358 |
+
daily_usage.append({
|
| 359 |
+
"timestamp": int(current_date.timestamp()),
|
| 360 |
+
"daily_usage": get_billing_info() / 1000
|
| 361 |
+
})
|
| 362 |
+
current_date += timedelta(days=1)
|
| 363 |
+
|
| 364 |
+
return jsonify({
|
| 365 |
+
"object": "list",
|
| 366 |
+
"data": daily_usage,
|
| 367 |
+
"total_usage": sum(day["daily_usage"] for day in daily_usage)
|
| 368 |
+
})
|
| 369 |
+
|
| 370 |
+
@app.route('/handsome/v1/dashboard/billing/subscription', methods=['GET'])
|
| 371 |
+
def billing_subscription():
|
| 372 |
+
"""
|
| 373 |
+
处理 /handsome/v1/dashboard/billing/subscription 路由的请求,返回订阅信息。
|
| 374 |
+
"""
|
| 375 |
+
if not check_authorization(request):
|
| 376 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 377 |
+
|
| 378 |
+
total_balance = get_billing_info()
|
| 379 |
+
|
| 380 |
return jsonify({
|
| 381 |
+
"object": "billing_subscription",
|
|
|
|
|
|
|
| 382 |
"has_payment_method": False,
|
| 383 |
+
"canceled": False,
|
| 384 |
+
"canceled_at": None,
|
| 385 |
+
"delinquent": None,
|
| 386 |
+
"access_until": int(datetime(9999, 12, 31).timestamp()), # 设置一个较远的未来时间
|
| 387 |
+
"soft_limit": 0,
|
| 388 |
+
"hard_limit": total_balance,
|
| 389 |
+
"system_hard_limit": total_balance,
|
| 390 |
+
"soft_limit_usd": 0,
|
| 391 |
+
"hard_limit_usd": total_balance,
|
| 392 |
+
"system_hard_limit_usd": total_balance,
|
| 393 |
+
"plan": {
|
| 394 |
+
"name": "SiliconFlow API",
|
| 395 |
+
"id": "siliconflow-api"
|
| 396 |
+
},
|
| 397 |
+
"account_name": "SiliconFlow User",
|
| 398 |
+
"po_number": None,
|
| 399 |
+
"billing_email": None,
|
| 400 |
+
"tax_ids": [],
|
| 401 |
+
"billing_address": None,
|
| 402 |
+
"business_address": None
|
| 403 |
})
|
| 404 |
|
| 405 |
if __name__ == '__main__':
|