Update app.py
Browse files103 unmodified lines104105106107108109110111333 unmodified lines44544644744844945045161 unmodified lines513514515513516517518519207 unmodified lines727728729727730731732733243 unmodified lines97797897998098198298313 unmodified lines9979989999961000100110021003100010011004100510061007100810091007100810101011101010111012101210131014101510161017101810191020102110221019102310241025102610271028102910301031103227 unmodified lines1060106110621058106310641065106623 unmodified lines10901091109210881093109410951096103 unmodified lines "PROXY": os.environ.get("PROXY") or None }, "SERVER": { "COOKIE": None, "CF_CLEARANCE":os.environ.get("CF_CLEARANCE") or None, "PORT": int(os.environ.get("PORT", 5200)) }, "RETRY": {333 unmodified lines
if proxy: logger.info(f"使用代理: {proxy}", "Server") if proxy.startswith("socks5://"): proxy_options["proxy"] = proxy 61 unmodified lines url, headers={ **DEFAULT_HEADERS, "Cookie": CONFIG["API"]["SIGNATURE_COOKIE"] "Cookie":CONFIG["SERVER"]['COOKIE'] }, json=upload_data, impersonate="chrome133a",207 unmodified lines f"https://assets.grok.com/{image_url}", headers={ **DEFAULT_HEADERS, "Cookie": CONFIG["API"]["SIGNATURE_COOKIE"] "Cookie":CONFIG["SERVER"]['COOKIE'] }, impersonate="chrome120", **proxy_options243 unmodified lines
@app.route('/v1/chat/completions', methods=['POST'])def chat_completions(): response_status_code = 500 try: auth_token = request.headers.get('Authorization', '').replace('Bearer ', '')13 unmodified lines retry_count = 0 grok_client = GrokApiClient(model) request_payload = grok_client.prepare_chat_request(data) response_status_code = 500
while retry_count < CONFIG["RETRY"]["MAX_ATTEMPTS"]: retry_count += 1 CONFIG["API"]["SIGNATURE_COOKIE"] = Utils.create_auth_headers( model) CONFIG["API"]["SIGNATURE_COOKIE"] = Utils.create_auth_headers(model)
if not CONFIG["API"]["SIGNATURE_COOKIE"]: raise ValueError('该模型无可用令牌')
logger.info( f"当前令牌: {json.dumps(CONFIG['API']['SIGNATURE_COOKIE'], indent=2)}", "Server") f"当前令牌: {json.dumps(CONFIG['API']['SIGNATURE_COOKIE'], indent=2)}","Server") logger.info( f"当前可用模型的全部可用数量: {json.dumps(token_manager.get_remaining_token_request_capacity(), indent=2)}", "Server")
f"当前可用模型的全部可用数量: {json.dumps(token_manager.get_remaining_token_request_capacity(), indent=2)}","Server") if CONFIG['SERVER']['CF_CLEARANCE']: CONFIG["SERVER"]['COOKIE'] = f"{CONFIG['API']['SIGNATURE_COOKIE']};cf_clearance={CONFIG['SERVER']['CF_CLEARANCE']}" else: CONFIG["SERVER"]['COOKIE'] = CONFIG['API']['SIGNATURE_COOKIE'] try: proxy_options = Utils.get_proxy_options() response = curl_requests.post( f"{CONFIG['API']['BASE_URL']}/rest/app-chat/conversations/new", headers={ **DEFAULT_HEADERS, "Cookie":f"{CONFIG['API']['SIGNATURE_COOKIE']}; cf_clearance=ndxDqSPPQQQicT0vsP6JSSudjOUBQ_G7fufmUuOnsm4-1741270561-1.2.1.1-dlueDEYSJbFuyAUcbUdn0xFpXSXqRppz8euI.sQhX0ZQyKgjLQBm2kL32e6.FpA8pgcPyLxfXYYkHJg_.5iS4C3T7lwDJDNjE7AIEAXOYxyhas6jvb71l7FBPpnDnCF97kVPxlAOLMHMUZegxCm7Ln8EGftiMb9qCDSVxyqO14eEVQyRJK6ziLnEdg3Vk9_aAaf6E3Dbk6nM7X1eRa2SBGehoLy8FNR7Jw.Uz2TA1VxRIPoR8tyvcl19PxEtPwr9snvquFuGLUfF8QxrOU.bi2JdSOYVD9P28oaL4xy_OgN9EL.CY2R4TARzlcho7vJUA4HHRcWVDB0X3OM96ZRCvocqXqf7_r3GZFzypNTphkLK3TvNC6NGH2HbDMlJ_BHhlBFL2Hy3ap5h4rGXfpmRnfo01XDoQCGq9enjYw6QQujNzNc.2KN7u5oNyUyUdRp9rHPhGDY.BNcFOKV33bFG2sMwmNarEEUQ2Z5hx3nIkfI;" "Cookie":CONFIG["SERVER"]['COOKIE'] }, data=json.dumps(request_payload), impersonate="chrome133a", stream=True, **proxy_options) print(CONFIG["SERVER"]['COOKIE']) if response.status_code == 200: response_status_code = 200 logger.info("请求成功", "Server")27 unmodified lines token_manager.reduce_token_request_count(model,1)#重置去除当前因为错误未成功请求的次数,确保不会因为错误未成功请求的次数导致次数上限 if token_manager.get_token_count_for_model(model) == 0: raise ValueError(f"{model} 次数已达上限,请切换其他模型或者重新对话") raise ValueError(f"IP暂时被封黑无法破盾,请稍后重试或者更换ip") raise ValueError(f"IP暂时被封无法破盾,请稍后重试或者更换ip") elif response.status_code == 429: response_status_code = 429 token_manager.reduce_token_request_count(model,1)23 unmodified lines raise continue if response_status_code == 403: raise ValueError('IP暂时被封黑无法破盾,请稍后重试或者更换ip') raise ValueError('IP暂时被封无法破盾,请稍后重试或者更换ip') elif response_status_code == 500: raise ValueError('当前模型所有令牌暂无可用,请稍后重试')
app.py+18-13