yangtb24 commited on
Commit
0de216a
·
verified ·
1 Parent(s): 095a61c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -47
app.py CHANGED
@@ -20,10 +20,10 @@ time.tzset()
20
  logging.basicConfig(level=logging.INFO,
21
  format='%(asctime)s - %(levelname)s - %(message)s')
22
 
23
- API_ENDPOINT = "https://api.siliconflow.cn/v1/user/info"
24
- TEST_MODEL_ENDPOINT = "https://api.siliconflow.cn/v1/chat/completions"
25
- MODELS_ENDPOINT = "https://api.siliconflow.cn/v1/models"
26
- EMBEDDINGS_ENDPOINT = "https://api.siliconflow.cn/v1/embeddings"
27
 
28
  app = Flask(__name__)
29
 
@@ -48,7 +48,7 @@ data_lock = threading.Lock()
48
 
49
  def get_credit_summary(api_key):
50
  """
51
- 使用 API 密钥获取额度信息。
52
  """
53
  headers = {
54
  "Authorization": f"Bearer {api_key}",
@@ -57,12 +57,55 @@ def get_credit_summary(api_key):
57
  try:
58
  response = requests.get(API_ENDPOINT, headers=headers)
59
  response.raise_for_status()
60
- data = response.json().get("data", {})
61
- total_balance = data.get("totalBalance", 0)
62
- return {"total_balance": float(total_balance)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  except requests.exceptions.RequestException as e:
64
  logging.error(f"获取额度信息失败,API Key:{api_key},错误信息:{e}")
65
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  FREE_MODEL_TEST_KEY = (
68
  "sk-bmjbjzleaqfgtqfzmcnsbagxrlohriadnxqrzfocbizaxukw"
@@ -552,38 +595,10 @@ def check_tokens():
552
  def list_models():
553
  if not check_authorization(request):
554
  return jsonify({"error": "Unauthorized"}), 401
555
-
556
- detailed_models = []
557
 
558
- for model in text_models:
559
- detailed_models.append({
560
- "id": model,
561
- "object": "model",
562
- "created": 1678888888,
563
- "owned_by": "openai",
564
- "permission": [
565
- {
566
- "id": f"modelperm-{uuid.uuid4().hex}",
567
- "object": "model_permission",
568
- "created": 1678888888,
569
- "allow_create_engine": False,
570
- "allow_sampling": True,
571
- "allow_logprobs": True,
572
- "allow_search_indices": False,
573
- "allow_view": True,
574
- "allow_fine_tuning": False,
575
- "organization": "*",
576
- "group": None,
577
- "is_blocking": False
578
- }
579
- ],
580
- "root": model,
581
- "parent": None
582
- })
583
-
584
- for model in embedding_models:
585
- detailed_models.append({
586
- "id": model,
587
  "object": "model",
588
  "created": 1678888888,
589
  "owned_by": "openai",
@@ -603,13 +618,11 @@ def list_models():
603
  "is_blocking": False
604
  }
605
  ],
606
- "root": model,
607
  "parent": None
608
- })
609
-
610
- for model in image_models:
611
- detailed_models.append({
612
- "id": model,
613
  "object": "model",
614
  "created": 1678888888,
615
  "owned_by": "openai",
@@ -629,9 +642,10 @@ def list_models():
629
  "is_blocking": False
630
  }
631
  ],
632
- "root": model,
633
  "parent": None
634
- })
 
635
 
636
  return jsonify({
637
  "success": True,
 
20
  logging.basicConfig(level=logging.INFO,
21
  format='%(asctime)s - %(levelname)s - %(message)s')
22
 
23
+ API_ENDPOINT = "https://api.deepseek.cn/user/balance"
24
+ TEST_MODEL_ENDPOINT = "https://api.deepseek.cn/v1/chat/completions"
25
+ MODELS_ENDPOINT = "https://api.deepseek.cn/v1/models"
26
+ EMBEDDINGS_ENDPOINT = "https://api.deepseek.cn/v1/embeddings"
27
 
28
  app = Flask(__name__)
29
 
 
48
 
49
  def get_credit_summary(api_key):
50
  """
51
+ 使用 API 密钥获取额度信息,并将美元余额转换为人民币。
52
  """
53
  headers = {
54
  "Authorization": f"Bearer {api_key}",
 
57
  try:
58
  response = requests.get(API_ENDPOINT, headers=headers)
59
  response.raise_for_status()
60
+ data = response.json()
61
+ if not data.get("is_available", False):
62
+ logging.warning(f"API Key: {api_key} is not available.")
63
+ return None
64
+
65
+ balance_infos = data.get("balance_infos", [])
66
+ total_balance_cny = 0.0
67
+ usd_balance = 0.0
68
+ for balance_info in balance_infos:
69
+ currency = balance_info.get("currency")
70
+ total_balance = float(balance_info.get("total_balance", 0))
71
+
72
+ if currency == "CNY":
73
+ total_balance_cny += total_balance
74
+ elif currency == "USD":
75
+ usd_balance = total_balance
76
+
77
+ try:
78
+ exchange_rate = get_usd_to_cny_rate()
79
+ if exchange_rate is not None:
80
+ total_balance_cny += usd_balance * exchange_rate
81
+ else:
82
+ logging.warning(f"获取美元兑人民币汇率失败,无法转换美元余额,API Key:{api_key}")
83
+ total_balance_cny += usd_balance * 7.2
84
+ except Exception as e:
85
+ logging.error(f"获取美元兑人民币汇率失败,API Key:{api_key},错误信息:{e}")
86
+ total_balance_cny += usd_balance * 7.2
87
+
88
+ return {"total_balance_cny": round(total_balance_cny, 2)}
89
  except requests.exceptions.RequestException as e:
90
  logging.error(f"获取额度信息失败,API Key:{api_key},错误信息:{e}")
91
  return None
92
+ except Exception as e:
93
+ logging.error(f"处理额度信息失败,API Key:{api_key},错误信息:{e}")
94
+ return None
95
+
96
+ def get_usd_to_cny_rate():
97
+ """
98
+ 获取美元兑人民币的汇率。
99
+ 这里使用一个公共的汇率 API,你可以替换成你自己的。
100
+ """
101
+ try:
102
+ response = requests.get("https://api.exchangerate-api.com/v4/latest/USD")
103
+ response.raise_for_status()
104
+ data = response.json()
105
+ return data.get("rates", {}).get("CNY")
106
+ except requests.exceptions.RequestException as e:
107
+ logging.error(f"获取美元兑人民币汇率失败,错误信息:{e}")
108
+ return None
109
 
110
  FREE_MODEL_TEST_KEY = (
111
  "sk-bmjbjzleaqfgtqfzmcnsbagxrlohriadnxqrzfocbizaxukw"
 
595
  def list_models():
596
  if not check_authorization(request):
597
  return jsonify({"error": "Unauthorized"}), 401
 
 
598
 
599
+ detailed_models = [
600
+ {
601
+ "id": "deepseek-chat",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
602
  "object": "model",
603
  "created": 1678888888,
604
  "owned_by": "openai",
 
618
  "is_blocking": False
619
  }
620
  ],
621
+ "root": "deepseek-chat",
622
  "parent": None
623
+ },
624
+ {
625
+ "id": "deepseek-code",
 
 
626
  "object": "model",
627
  "created": 1678888888,
628
  "owned_by": "openai",
 
642
  "is_blocking": False
643
  }
644
  ],
645
+ "root": "deepseek-code",
646
  "parent": None
647
+ }
648
+ ]
649
 
650
  return jsonify({
651
  "success": True,