Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -104,6 +104,8 @@ CONFIG = {
|
|
104 |
"PROXY": os.environ.get("PROXY") or None
|
105 |
},
|
106 |
"SERVER": {
|
|
|
|
|
107 |
"PORT": int(os.environ.get("PORT", 5200))
|
108 |
},
|
109 |
"RETRY": {
|
@@ -443,6 +445,7 @@ class Utils:
|
|
443 |
|
444 |
if proxy:
|
445 |
logger.info(f"使用代理: {proxy}", "Server")
|
|
|
446 |
if proxy.startswith("socks5://"):
|
447 |
proxy_options["proxy"] = proxy
|
448 |
|
@@ -510,7 +513,7 @@ class GrokApiClient:
|
|
510 |
url,
|
511 |
headers={
|
512 |
**DEFAULT_HEADERS,
|
513 |
-
"Cookie":
|
514 |
},
|
515 |
json=upload_data,
|
516 |
impersonate="chrome133a",
|
@@ -724,7 +727,7 @@ def handle_image_response(image_url):
|
|
724 |
f"https://assets.grok.com/{image_url}",
|
725 |
headers={
|
726 |
**DEFAULT_HEADERS,
|
727 |
-
"Cookie":
|
728 |
},
|
729 |
impersonate="chrome120",
|
730 |
**proxy_options
|
@@ -974,6 +977,7 @@ def get_models():
|
|
974 |
|
975 |
@app.route('/v1/chat/completions', methods=['POST'])
|
976 |
def chat_completions():
|
|
|
977 |
try:
|
978 |
auth_token = request.headers.get('Authorization',
|
979 |
'').replace('Bearer ', '')
|
@@ -993,35 +997,36 @@ def chat_completions():
|
|
993 |
retry_count = 0
|
994 |
grok_client = GrokApiClient(model)
|
995 |
request_payload = grok_client.prepare_chat_request(data)
|
996 |
-
|
997 |
|
998 |
while retry_count < CONFIG["RETRY"]["MAX_ATTEMPTS"]:
|
999 |
retry_count += 1
|
1000 |
-
CONFIG["API"]["SIGNATURE_COOKIE"] = Utils.create_auth_headers(
|
1001 |
-
model)
|
1002 |
|
1003 |
if not CONFIG["API"]["SIGNATURE_COOKIE"]:
|
1004 |
raise ValueError('该模型无可用令牌')
|
1005 |
|
1006 |
logger.info(
|
1007 |
-
f"当前令牌: {json.dumps(CONFIG['API']['SIGNATURE_COOKIE'], indent=2)}",
|
1008 |
-
"Server")
|
1009 |
logger.info(
|
1010 |
-
f"当前可用模型的全部可用数量: {json.dumps(token_manager.get_remaining_token_request_capacity(), indent=2)}",
|
1011 |
-
|
1012 |
-
|
|
|
|
|
1013 |
try:
|
1014 |
proxy_options = Utils.get_proxy_options()
|
1015 |
response = curl_requests.post(
|
1016 |
f"{CONFIG['API']['BASE_URL']}/rest/app-chat/conversations/new",
|
1017 |
headers={
|
1018 |
**DEFAULT_HEADERS,
|
1019 |
-
"Cookie":
|
1020 |
},
|
1021 |
data=json.dumps(request_payload),
|
1022 |
impersonate="chrome133a",
|
1023 |
stream=True,
|
1024 |
**proxy_options)
|
|
|
1025 |
if response.status_code == 200:
|
1026 |
response_status_code = 200
|
1027 |
logger.info("请求成功", "Server")
|
@@ -1055,7 +1060,7 @@ def chat_completions():
|
|
1055 |
token_manager.reduce_token_request_count(model,1)#重置去除当前因为错误未成功请求的次数,确保不会因为错误未成功请求的次数导致次数上限
|
1056 |
if token_manager.get_token_count_for_model(model) == 0:
|
1057 |
raise ValueError(f"{model} 次数已达上限,请切换其他模型或者重新对话")
|
1058 |
-
raise ValueError(f"IP
|
1059 |
elif response.status_code == 429:
|
1060 |
response_status_code = 429
|
1061 |
token_manager.reduce_token_request_count(model,1)
|
@@ -1085,7 +1090,7 @@ def chat_completions():
|
|
1085 |
raise
|
1086 |
continue
|
1087 |
if response_status_code == 403:
|
1088 |
-
raise ValueError('IP
|
1089 |
elif response_status_code == 500:
|
1090 |
raise ValueError('当前模型所有令牌暂无可用,请稍后重试')
|
1091 |
|
|
|
104 |
"PROXY": os.environ.get("PROXY") or None
|
105 |
},
|
106 |
"SERVER": {
|
107 |
+
"COOKIE": None,
|
108 |
+
"CF_CLEARANCE":os.environ.get("CF_CLEARANCE") or None,
|
109 |
"PORT": int(os.environ.get("PORT", 5200))
|
110 |
},
|
111 |
"RETRY": {
|
|
|
445 |
|
446 |
if proxy:
|
447 |
logger.info(f"使用代理: {proxy}", "Server")
|
448 |
+
|
449 |
if proxy.startswith("socks5://"):
|
450 |
proxy_options["proxy"] = proxy
|
451 |
|
|
|
513 |
url,
|
514 |
headers={
|
515 |
**DEFAULT_HEADERS,
|
516 |
+
"Cookie":CONFIG["SERVER"]['COOKIE']
|
517 |
},
|
518 |
json=upload_data,
|
519 |
impersonate="chrome133a",
|
|
|
727 |
f"https://assets.grok.com/{image_url}",
|
728 |
headers={
|
729 |
**DEFAULT_HEADERS,
|
730 |
+
"Cookie":CONFIG["SERVER"]['COOKIE']
|
731 |
},
|
732 |
impersonate="chrome120",
|
733 |
**proxy_options
|
|
|
977 |
|
978 |
@app.route('/v1/chat/completions', methods=['POST'])
|
979 |
def chat_completions():
|
980 |
+
response_status_code = 500
|
981 |
try:
|
982 |
auth_token = request.headers.get('Authorization',
|
983 |
'').replace('Bearer ', '')
|
|
|
997 |
retry_count = 0
|
998 |
grok_client = GrokApiClient(model)
|
999 |
request_payload = grok_client.prepare_chat_request(data)
|
1000 |
+
|
1001 |
|
1002 |
while retry_count < CONFIG["RETRY"]["MAX_ATTEMPTS"]:
|
1003 |
retry_count += 1
|
1004 |
+
CONFIG["API"]["SIGNATURE_COOKIE"] = Utils.create_auth_headers(model)
|
|
|
1005 |
|
1006 |
if not CONFIG["API"]["SIGNATURE_COOKIE"]:
|
1007 |
raise ValueError('该模型无可用令牌')
|
1008 |
|
1009 |
logger.info(
|
1010 |
+
f"当前令牌: {json.dumps(CONFIG['API']['SIGNATURE_COOKIE'], indent=2)}","Server")
|
|
|
1011 |
logger.info(
|
1012 |
+
f"当前可用模型的全部可用数量: {json.dumps(token_manager.get_remaining_token_request_capacity(), indent=2)}","Server")
|
1013 |
+
if CONFIG['SERVER']['CF_CLEARANCE']:
|
1014 |
+
CONFIG["SERVER"]['COOKIE'] = f"{CONFIG['API']['SIGNATURE_COOKIE']};cf_clearance={CONFIG['SERVER']['CF_CLEARANCE']}"
|
1015 |
+
else:
|
1016 |
+
CONFIG["SERVER"]['COOKIE'] = CONFIG['API']['SIGNATURE_COOKIE']
|
1017 |
try:
|
1018 |
proxy_options = Utils.get_proxy_options()
|
1019 |
response = curl_requests.post(
|
1020 |
f"{CONFIG['API']['BASE_URL']}/rest/app-chat/conversations/new",
|
1021 |
headers={
|
1022 |
**DEFAULT_HEADERS,
|
1023 |
+
"Cookie":CONFIG["SERVER"]['COOKIE']
|
1024 |
},
|
1025 |
data=json.dumps(request_payload),
|
1026 |
impersonate="chrome133a",
|
1027 |
stream=True,
|
1028 |
**proxy_options)
|
1029 |
+
print(CONFIG["SERVER"]['COOKIE'])
|
1030 |
if response.status_code == 200:
|
1031 |
response_status_code = 200
|
1032 |
logger.info("请求成功", "Server")
|
|
|
1060 |
token_manager.reduce_token_request_count(model,1)#重置去除当前因为错误未成功请求的次数,确保不会因为错误未成功请求的次数导致次数上限
|
1061 |
if token_manager.get_token_count_for_model(model) == 0:
|
1062 |
raise ValueError(f"{model} 次数已达上限,请切换其他模型或者重新对话")
|
1063 |
+
raise ValueError(f"IP暂时被封无法破盾,请稍后重试或者更换ip")
|
1064 |
elif response.status_code == 429:
|
1065 |
response_status_code = 429
|
1066 |
token_manager.reduce_token_request_count(model,1)
|
|
|
1090 |
raise
|
1091 |
continue
|
1092 |
if response_status_code == 403:
|
1093 |
+
raise ValueError('IP暂时被封无法破盾,请稍后重试或者更换ip')
|
1094 |
elif response_status_code == 500:
|
1095 |
raise ValueError('当前模型所有令牌暂无可用,请稍后重试')
|
1096 |
|