Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -546,195 +546,7 @@ def check_tokens():
|
|
546 |
|
547 |
return jsonify(results)
|
548 |
|
549 |
-
|
550 |
-
import time
|
551 |
-
import logging
|
552 |
-
import requests
|
553 |
-
import json
|
554 |
-
import uuid
|
555 |
-
from flask import Flask, request, jsonify
|
556 |
-
|
557 |
-
API_ENDPOINT = "https://api.siliconflow.cn/v1/user/info"
|
558 |
-
TEST_MODEL_ENDPOINT = "https://api.siliconflow.cn/v1/chat/completions"
|
559 |
-
EMBEDDINGS_ENDPOINT = "https://api.siliconflow.cn/v1/embeddings"
|
560 |
-
|
561 |
-
text_models = []
|
562 |
-
free_text_models = []
|
563 |
-
embedding_models = []
|
564 |
-
free_embedding_models = []
|
565 |
-
image_models = []
|
566 |
-
free_image_models = []
|
567 |
-
|
568 |
-
invalid_keys_global = []
|
569 |
-
free_keys_global = []
|
570 |
-
unverified_keys_global = []
|
571 |
-
valid_keys_global = []
|
572 |
-
model_key_indices = {}
|
573 |
-
|
574 |
-
request_timestamps = []
|
575 |
-
token_counts = []
|
576 |
-
data_lock = None
|
577 |
-
|
578 |
-
FREE_MODEL_TEST_KEY = (
|
579 |
-
"sk-bmjbjzleaqfgtqfzmcnsbagxrlohriadnxqrzfocbizaxukw"
|
580 |
-
)
|
581 |
-
|
582 |
-
FREE_IMAGE_LIST = [
|
583 |
-
"stabilityai/stable-diffusion-3-5-large",
|
584 |
-
"black-forest-labs/FLUX.1-schnell",
|
585 |
-
"stabilityai/stable-diffusion-3-medium",
|
586 |
-
"stabilityai/stable-diffusion-xl-base-1.0",
|
587 |
-
"stabilityai/stable-diffusion-2-1"
|
588 |
-
]
|
589 |
-
|
590 |
-
def get_credit_summary(api_key):
|
591 |
-
"""
|
592 |
-
使用 API 密钥获取额度信息。
|
593 |
-
"""
|
594 |
-
headers = {
|
595 |
-
"Authorization": f"Bearer {api_key}",
|
596 |
-
"Content-Type": "application/json"
|
597 |
-
}
|
598 |
-
try:
|
599 |
-
response = requests.get(API_ENDPOINT, headers=headers)
|
600 |
-
response.raise_for_status()
|
601 |
-
data = response.json().get("data", {})
|
602 |
-
total_balance = data.get("totalBalance", 0)
|
603 |
-
return {"total_balance": float(total_balance)}
|
604 |
-
except requests.exceptions.RequestException as e:
|
605 |
-
logging.error(f"获取额度信息失败,API Key:{api_key},错误信息:{e}")
|
606 |
-
return None
|
607 |
-
|
608 |
-
def test_model_availability(api_key, model_name):
|
609 |
-
"""
|
610 |
-
测试指定的模型是否可用。
|
611 |
-
"""
|
612 |
-
headers = {
|
613 |
-
"Authorization": f"Bearer {api_key}",
|
614 |
-
"Content-Type": "application/json"
|
615 |
-
}
|
616 |
-
try:
|
617 |
-
response = requests.post(
|
618 |
-
TEST_MODEL_ENDPOINT,
|
619 |
-
headers=headers,
|
620 |
-
json={
|
621 |
-
"model": model_name,
|
622 |
-
"messages": [{"role": "user", "content": "hi"}],
|
623 |
-
"max_tokens": 5,
|
624 |
-
"stream": False
|
625 |
-
},
|
626 |
-
timeout=5
|
627 |
-
)
|
628 |
-
if response.status_code == 429 or response.status_code == 200:
|
629 |
-
return True
|
630 |
-
else:
|
631 |
-
return False
|
632 |
-
except requests.exceptions.RequestException as e:
|
633 |
-
logging.error(
|
634 |
-
f"测试模型 {model_name} 可用性失败,"
|
635 |
-
f"API Key:{api_key},错误信息:{e}"
|
636 |
-
)
|
637 |
-
return False
|
638 |
-
|
639 |
-
def test_image_model_availability(api_key, model_name):
|
640 |
-
"""
|
641 |
-
测试指定的图像模型是否在 FREE_IMAGE_LIST 中。
|
642 |
-
如果在列表中,返回 True,否则返回 False。
|
643 |
-
"""
|
644 |
-
return model_name in FREE_IMAGE_LIST
|
645 |
-
|
646 |
-
def determine_request_type(model_name, model_list, free_model_list):
|
647 |
-
"""
|
648 |
-
根据用户请求的模型判断请求类型。
|
649 |
-
"""
|
650 |
-
if model_name in free_model_list:
|
651 |
-
return "free"
|
652 |
-
elif model_name in model_list:
|
653 |
-
return "paid"
|
654 |
-
else:
|
655 |
-
return "unknown"
|
656 |
-
|
657 |
-
def select_key(request_type, model_name):
|
658 |
-
"""
|
659 |
-
根据请求类型和模型名称选择合适的 KEY,
|
660 |
-
并实现轮询和重试机制。
|
661 |
-
"""
|
662 |
-
if request_type == "free":
|
663 |
-
available_keys = (
|
664 |
-
free_keys_global +
|
665 |
-
unverified_keys_global +
|
666 |
-
valid_keys_global
|
667 |
-
)
|
668 |
-
elif request_type == "paid":
|
669 |
-
available_keys = unverified_keys_global + valid_keys_global
|
670 |
-
else:
|
671 |
-
available_keys = (
|
672 |
-
free_keys_global +
|
673 |
-
unverified_keys_global +
|
674 |
-
valid_keys_global
|
675 |
-
)
|
676 |
-
|
677 |
-
if not available_keys:
|
678 |
-
return None
|
679 |
-
|
680 |
-
current_index = model_key_indices.get(model_name, 0)
|
681 |
-
|
682 |
-
for _ in range(len(available_keys)):
|
683 |
-
key = available_keys[current_index % len(available_keys)]
|
684 |
-
current_index += 1
|
685 |
-
|
686 |
-
if key_is_valid(key, request_type):
|
687 |
-
model_key_indices[model_name] = current_index
|
688 |
-
return key
|
689 |
-
else:
|
690 |
-
logging.warning(
|
691 |
-
f"KEY {key} 无效或达到限制,尝试下一个 KEY"
|
692 |
-
)
|
693 |
-
|
694 |
-
model_key_indices[model_name] = 0
|
695 |
-
return None
|
696 |
-
|
697 |
-
def key_is_valid(key, request_type):
|
698 |
-
"""
|
699 |
-
检查 KEY 是否有效,
|
700 |
-
根据不同的请求类型进行不同的检查。
|
701 |
-
"""
|
702 |
-
if request_type == "invalid":
|
703 |
-
return False
|
704 |
-
|
705 |
-
credit_summary = get_credit_summary(key)
|
706 |
-
if credit_summary is None:
|
707 |
-
return False
|
708 |
-
|
709 |
-
total_balance = credit_summary.get("total_balance", 0)
|
710 |
-
|
711 |
-
if request_type == "free":
|
712 |
-
return True
|
713 |
-
elif request_type == "paid" or request_type == "unverified":
|
714 |
-
return total_balance > 0
|
715 |
-
else:
|
716 |
-
return False
|
717 |
-
|
718 |
-
def check_authorization(request, authorization_key):
|
719 |
-
"""
|
720 |
-
检查请求头中的 Authorization 字段
|
721 |
-
是否匹配环境变量 AUTHORIZATION_KEY。
|
722 |
-
"""
|
723 |
-
if not authorization_key:
|
724 |
-
logging.warning("环境变量 AUTHORIZATION_KEY 未设置,请设置后重试。")
|
725 |
-
return False
|
726 |
-
|
727 |
-
auth_header = request.headers.get('Authorization')
|
728 |
-
if not auth_header:
|
729 |
-
logging.warning("请求头中缺少 Authorization 字段。")
|
730 |
-
return False
|
731 |
-
|
732 |
-
if auth_header != f"Bearer {authorization_key}":
|
733 |
-
logging.warning(f"无效的 Authorization 密钥:{auth_header}")
|
734 |
-
return False
|
735 |
-
|
736 |
-
return True
|
737 |
-
|
738 |
def handsome_chat_completions(request, authorization_key):
|
739 |
if not check_authorization(request, authorization_key):
|
740 |
return jsonify({"error": "Unauthorized"}), 401
|
|
|
546 |
|
547 |
return jsonify(results)
|
548 |
|
549 |
+
@app.route('/handsome/v1/chat/completions', methods=['POST'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
550 |
def handsome_chat_completions(request, authorization_key):
|
551 |
if not check_authorization(request, authorization_key):
|
552 |
return jsonify({"error": "Unauthorized"}), 401
|