Spaces:
Building
Building
Upload 4 files
Browse files
app.py
CHANGED
@@ -177,10 +177,10 @@ def increment_request_count(api_key):
|
|
177 |
request_counts[api_key] = deque()
|
178 |
request_counts[api_key].append(now)
|
179 |
|
180 |
-
def handle_api_error(error, attempt
|
181 |
if attempt > MAX_RETRIES:
|
182 |
logger.error(f"{MAX_RETRIES} 次尝试后仍然失败,请修改预设或输入")
|
183 |
-
return
|
184 |
'error': {
|
185 |
'message': f"{MAX_RETRIES} 次尝试后仍然失败,请修改预设或输入",
|
186 |
'type': 'max_retries_exceeded'
|
@@ -191,7 +191,7 @@ def handle_api_error(error, attempt, stream=False):
|
|
191 |
logger.error(f"{current_api_key[:11]} → 无效,可能已过期或被删除")
|
192 |
key_manager.blacklist_key(current_api_key)
|
193 |
switch_api_key()
|
194 |
-
return
|
195 |
|
196 |
elif isinstance(error, (ResourceExhausted, Aborted, InternalServerError, ServiceUnavailable)):
|
197 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
@@ -202,16 +202,16 @@ def handle_api_error(error, attempt, stream=False):
|
|
202 |
key_manager.blacklist_key(current_api_key)
|
203 |
switch_api_key()
|
204 |
time.sleep(delay)
|
205 |
-
return
|
206 |
|
207 |
elif isinstance(error, generation_types.StopCandidateException):
|
208 |
logger.warning(f"AI输出内容被Gemini官方阻挡,代理没有得到有效回复")
|
209 |
switch_api_key()
|
210 |
-
return
|
211 |
|
212 |
else:
|
213 |
-
logger.error(f"
|
214 |
-
return
|
215 |
|
216 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
217 |
def chat_completions():
|
@@ -239,7 +239,7 @@ def chat_completions():
|
|
239 |
if not isok:
|
240 |
logger.warning(f"{current_api_key[:11]} → 暂时超过限额,该API key将在 {time} 秒后启用...")
|
241 |
switch_api_key()
|
242 |
-
return
|
243 |
|
244 |
increment_request_count(current_api_key)
|
245 |
|
@@ -262,7 +262,7 @@ def chat_completions():
|
|
262 |
response = chat_session.send_message(user_message, stream=stream)
|
263 |
else:
|
264 |
response = gen_model.generate_content(user_message, stream=stream)
|
265 |
-
return
|
266 |
except Exception as e:
|
267 |
return handle_api_error(e, attempt, stream)
|
268 |
|
@@ -321,15 +321,27 @@ def chat_completions():
|
|
321 |
yield f"data: {json.dumps(data)}\n\n"
|
322 |
|
323 |
attempt = 0
|
324 |
-
success =
|
325 |
response = None
|
326 |
|
327 |
-
|
328 |
-
attempt += 1
|
329 |
logger.info(f"第 {attempt}/{MAX_RETRIES} 次尝试 ...")
|
330 |
success, response = do_request(current_api_key, attempt)
|
331 |
|
332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
logger.error(f"{MAX_RETRIES} 次尝试均失败,请调整配置或向Moonfanz反馈")
|
334 |
response = {
|
335 |
'error': {
|
|
|
177 |
request_counts[api_key] = deque()
|
178 |
request_counts[api_key].append(now)
|
179 |
|
180 |
+
def handle_api_error(error, attempt):
|
181 |
if attempt > MAX_RETRIES:
|
182 |
logger.error(f"{MAX_RETRIES} 次尝试后仍然失败,请修改预设或输入")
|
183 |
+
return 0, jsonify({
|
184 |
'error': {
|
185 |
'message': f"{MAX_RETRIES} 次尝试后仍然失败,请修改预设或输入",
|
186 |
'type': 'max_retries_exceeded'
|
|
|
191 |
logger.error(f"{current_api_key[:11]} → 无效,可能已过期或被删除")
|
192 |
key_manager.blacklist_key(current_api_key)
|
193 |
switch_api_key()
|
194 |
+
return 0, None
|
195 |
|
196 |
elif isinstance(error, (ResourceExhausted, Aborted, InternalServerError, ServiceUnavailable)):
|
197 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
|
|
202 |
key_manager.blacklist_key(current_api_key)
|
203 |
switch_api_key()
|
204 |
time.sleep(delay)
|
205 |
+
return 0, None
|
206 |
|
207 |
elif isinstance(error, generation_types.StopCandidateException):
|
208 |
logger.warning(f"AI输出内容被Gemini官方阻挡,代理没有得到有效回复")
|
209 |
switch_api_key()
|
210 |
+
return 0, None
|
211 |
|
212 |
else:
|
213 |
+
logger.error(f"该模型暂时不可用↙\n {error}")
|
214 |
+
return 2, None
|
215 |
|
216 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
217 |
def chat_completions():
|
|
|
239 |
if not isok:
|
240 |
logger.warning(f"{current_api_key[:11]} → 暂时超过限额,该API key将在 {time} 秒后启用...")
|
241 |
switch_api_key()
|
242 |
+
return 0, None
|
243 |
|
244 |
increment_request_count(current_api_key)
|
245 |
|
|
|
262 |
response = chat_session.send_message(user_message, stream=stream)
|
263 |
else:
|
264 |
response = gen_model.generate_content(user_message, stream=stream)
|
265 |
+
return 1, response
|
266 |
except Exception as e:
|
267 |
return handle_api_error(e, attempt, stream)
|
268 |
|
|
|
321 |
yield f"data: {json.dumps(data)}\n\n"
|
322 |
|
323 |
attempt = 0
|
324 |
+
success = 0
|
325 |
response = None
|
326 |
|
327 |
+
for attempt in range(1, MAX_RETRIES + 1):
|
|
|
328 |
logger.info(f"第 {attempt}/{MAX_RETRIES} 次尝试 ...")
|
329 |
success, response = do_request(current_api_key, attempt)
|
330 |
|
331 |
+
if success == 1:
|
332 |
+
break
|
333 |
+
elif success == 2:
|
334 |
+
|
335 |
+
logger.error("该模型暂时不可用,请更换模型或稍后重试")
|
336 |
+
response = {
|
337 |
+
'error': {
|
338 |
+
'message': '该模型暂时不可用,请更换模型或稍后重试',
|
339 |
+
'type': 'internal_server_error'
|
340 |
+
}
|
341 |
+
}
|
342 |
+
return jsonify(response), 503
|
343 |
+
|
344 |
+
else:
|
345 |
logger.error(f"{MAX_RETRIES} 次尝试均失败,请调整配置或向Moonfanz反馈")
|
346 |
response = {
|
347 |
'error': {
|