Spaces:
Paused
Paused
Upload 4 files
Browse files176 unmodified lines1771781791801811821831841851864 unmodified lines1911921931941951961974 unmodified lines20220320420520620720820921021121221321421521621721 unmodified lines23924024124224324424516 unmodified lines26226326426526626726852 unmodified lines321322323324325326327328329330331332333334335176 unmodified linesrequest_counts[api_key] = deque()request_counts[api_key].append(now)def handle_api_error(error, attempt, stream=False):if attempt > MAX_RETRIES:logger.error(f"{MAX_RETRIES} 次尝试后仍然失败,请修改预设或输入")return False, jsonify({'error': {'message': f"{MAX_RETRIES} 次尝试后仍然失败,请修改预设或输入",'type': 'max_retries_exceeded'4 unmodified lineslogger.error(f"{current_api_key[:11]} → 无效,可能已过期或被删除")key_manager.blacklist_key(current_api_key)switch_api_key()return False, Noneelif isinstance(error, (ResourceExhausted, Aborted, InternalServerError, ServiceUnavailable)):delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)4 unmodified lineskey_manager.blacklist_key(current_api_key)switch_api_key()time.sleep(delay)return False, Noneelif isinstance(error, generation_types.StopCandidateException):logger.warning(f"AI输出内容被Gemini官方阻挡,代理没有得到有效回复")switch_api_key()return False, Noneelse:logger.error(f"未知错误↙\n {error}")return False, None@app.route('/hf/v1/chat/completions', methods=['POST'])def chat_completions():21 unmodified linesif not isok:logger.warning(f"{current_api_key[:11]} → 暂时超过限额,该API key将在 {time} 秒后启用...")switch_api_key()return False, Noneincrement_request_count(current_api_key)16 unmodified linesresponse = chat_session.send_message(user_message, stream=stream)else:response = gen_model.generate_content(user_message, stream=stream)return True, responseexcept Exception as e:return handle_api_error(e, attempt, stream)52 unmodified linesyield f"data: {json.dumps(data)}\n\n"attempt = 0success = Falseresponse = Nonewhile attempt < MAX_RETRIES and not success:attempt += 1logger.info(f"第 {attempt}/{MAX_RETRIES} 次尝试 ...")success, response = do_request(current_api_key, attempt)if not success:logger.error(f"{MAX_RETRIES} 次尝试均失败,请调整配置或向Moonfanz反馈")response = {'error': {176 unmodified lines1771781791801811821831841851864 unmodified lines1911921931941951961974 unmodified lines20220320420520620720820921021121221321421521621721 unmodified lines23924024124224324424516 unmodified lines26226326426526626726852 unmodified lines321322323324325326327328329330331332333334335336337338339340341342343344345346347176 unmodified linesrequest_counts[api_key] = deque()request_counts[api_key].append(now)def handle_api_error(error, attempt):if attempt > MAX_RETRIES:logger.error(f"{MAX_RETRIES} 次尝试后仍然失败,请修改预设或输入")return 0, jsonify({'error': {'message': f"{MAX_RETRIES} 次尝试后仍然失败,请修改预设或输入",'type': 'max_retries_exceeded'4 unmodified lineslogger.error(f"{current_api_key[:11]} → 无效,可能已过期或被删除")key_manager.blacklist_key(current_api_key)switch_api_key()return 0, Noneelif isinstance(error, (ResourceExhausted, Aborted, InternalServerError, ServiceUnavailable)):delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)4 unmodified lineskey_manager.blacklist_key(current_api_key)switch_api_key()time.sleep(delay)return 0, Noneelif isinstance(error, generation_types.StopCandidateException):logger.warning(f"AI输出内容被Gemini官方阻挡,代理没有得到有效回复")switch_api_key()return 0, Noneelse:logger.error(f"该模型暂时不可用↙\n {error}")return 2, None@app.route('/hf/v1/chat/completions', methods=['POST'])def chat_completions():21 unmodified linesif not isok:logger.warning(f"{current_api_key[:11]} → 暂时超过限额,该API key将在 {time} 秒后启用...")switch_api_key()return 0, Noneincrement_request_count(current_api_key)16 unmodified linesresponse = chat_session.send_message(user_message, stream=stream)else:response = gen_model.generate_content(user_message, stream=stream)return 1, responseexcept Exception as e:return handle_api_error(e, attempt, stream)52 unmodified linesyield f"data: {json.dumps(data)}\n\n"attempt = 0success = 0response = Nonefor attempt in range(1, MAX_RETRIES + 1):logger.info(f"第 {attempt}/{MAX_RETRIES} 次尝试 ...")success, response = do_request(current_api_key, attempt)if success == 1:breakelif success == 2:logger.error("该模型暂时不可用,请更换模型或稍后重试")response = {'error': {'message': '该模型暂时不可用,请更换模型或稍后重试','type': 'internal_server_error'}}return jsonify(response), 503else:logger.error(f"{MAX_RETRIES} 次尝试均失败,请调整配置或向Moonfanz反馈")response = {'error': {
app.py+25-13