Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,13 @@ import time
|
|
| 3 |
import logging
|
| 4 |
import requests
|
| 5 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 6 |
-
from flask import Flask
|
| 7 |
|
| 8 |
# 配置日志记录
|
| 9 |
logging.basicConfig(level=logging.INFO,
|
| 10 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 11 |
|
| 12 |
-
API_ENDPOINT = "https://api.siliconflow.cn/
|
| 13 |
|
| 14 |
app = Flask(__name__)
|
| 15 |
|
|
@@ -24,10 +24,15 @@ def get_credit_summary(api_key):
|
|
| 24 |
try:
|
| 25 |
response = requests.get(API_ENDPOINT, headers=headers)
|
| 26 |
response.raise_for_status()
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
except requests.exceptions.RequestException as e:
|
| 29 |
logging.error(f"获取额度信息失败,API Key:{api_key},错误信息:{e}")
|
| 30 |
return None
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def load_keys():
|
| 33 |
"""
|
|
@@ -58,6 +63,54 @@ def index():
|
|
| 58 |
"""
|
| 59 |
return "<h1>Welcome to SiliconFlow</h1>"
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
if __name__ == '__main__':
|
| 62 |
# 打印所有环境变量,方便调试
|
| 63 |
logging.info(f"环境变量:{os.environ}")
|
|
|
|
| 3 |
import logging
|
| 4 |
import requests
|
| 5 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 6 |
+
from flask import Flask, request, jsonify
|
| 7 |
|
| 8 |
# 配置日志记录
|
| 9 |
logging.basicConfig(level=logging.INFO,
|
| 10 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 11 |
|
| 12 |
+
API_ENDPOINT = "https://api.siliconflow.cn/v1/user/info" # 修改为获取用户信息的接口
|
| 13 |
|
| 14 |
app = Flask(__name__)
|
| 15 |
|
|
|
|
| 24 |
try:
|
| 25 |
response = requests.get(API_ENDPOINT, headers=headers)
|
| 26 |
response.raise_for_status()
|
| 27 |
+
data = response.json().get("data", {})
|
| 28 |
+
total_balance = data.get("totalBalance", 0) # 获取总余额
|
| 29 |
+
return {"total_balance": total_balance}
|
| 30 |
except requests.exceptions.RequestException as e:
|
| 31 |
logging.error(f"获取额度信息失败,API Key:{api_key},错误信息:{e}")
|
| 32 |
return None
|
| 33 |
+
except (KeyError, TypeError) as e:
|
| 34 |
+
logging.error(f"解析额度信息失败,API Key:{api_key},错误信息:{e}")
|
| 35 |
+
return None
|
| 36 |
|
| 37 |
def load_keys():
|
| 38 |
"""
|
|
|
|
| 63 |
"""
|
| 64 |
return "<h1>Welcome to SiliconFlow</h1>"
|
| 65 |
|
| 66 |
+
@app.route('/check_tokens', methods=['POST'])
|
| 67 |
+
def check_tokens():
|
| 68 |
+
"""
|
| 69 |
+
处理前端发送的 Token 检测请求。
|
| 70 |
+
"""
|
| 71 |
+
tokens = request.json.get('tokens', [])
|
| 72 |
+
results = []
|
| 73 |
+
for token in tokens:
|
| 74 |
+
is_valid = False
|
| 75 |
+
balance = 0
|
| 76 |
+
error_message = ""
|
| 77 |
+
|
| 78 |
+
# 先尝试进行一个简单的请求来验证 Token 的有效性
|
| 79 |
+
try:
|
| 80 |
+
headers = {
|
| 81 |
+
"Authorization": f"Bearer {token}",
|
| 82 |
+
"Content-Type": "application/json"
|
| 83 |
+
}
|
| 84 |
+
test_response = requests.post("https://api.siliconflow.cn/v1/chat/completions",
|
| 85 |
+
headers=headers,
|
| 86 |
+
json={
|
| 87 |
+
"model": "Qwen/Qwen2.5-72B-Instruct",
|
| 88 |
+
"messages": [{"role": "user", "content": "hi"}],
|
| 89 |
+
"max_tokens": 100,
|
| 90 |
+
"stream": False
|
| 91 |
+
},
|
| 92 |
+
timeout=5)
|
| 93 |
+
|
| 94 |
+
if test_response.status_code == 200:
|
| 95 |
+
is_valid = True
|
| 96 |
+
credit_summary = get_credit_summary(token)
|
| 97 |
+
if credit_summary:
|
| 98 |
+
balance = credit_summary.get("total_balance", 0)
|
| 99 |
+
else:
|
| 100 |
+
error_message = test_response.json().get("message", "未知错误")
|
| 101 |
+
|
| 102 |
+
except requests.exceptions.RequestException as e:
|
| 103 |
+
error_message = str(e)
|
| 104 |
+
|
| 105 |
+
results.append({
|
| 106 |
+
"token": token,
|
| 107 |
+
"isValid": is_valid,
|
| 108 |
+
"balance": balance,
|
| 109 |
+
"message": error_message
|
| 110 |
+
})
|
| 111 |
+
|
| 112 |
+
return jsonify(results)
|
| 113 |
+
|
| 114 |
if __name__ == '__main__':
|
| 115 |
# 打印所有环境变量,方便调试
|
| 116 |
logging.info(f"环境变量:{os.environ}")
|