Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from flask import Flask, request, jsonify, Response, stream_with_context, render_template_string
|
| 2 |
-
from google.generativeai.types import generation_types
|
| 3 |
from google.api_core.exceptions import InvalidArgument, ResourceExhausted, Aborted, InternalServerError, ServiceUnavailable, PermissionDenied
|
| 4 |
import google.generativeai as genai
|
| 5 |
import json
|
|
@@ -227,16 +227,28 @@ def handle_api_error(error, attempt):
|
|
| 227 |
switch_api_key()
|
| 228 |
return 0, None
|
| 229 |
|
| 230 |
-
elif isinstance(error,
|
| 231 |
logger.warning(f"AI输出内容被Gemini官方阻挡,代理没有得到有效回复")
|
| 232 |
switch_api_key()
|
| 233 |
return 0, None
|
| 234 |
|
| 235 |
-
elif
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
else:
|
| 241 |
logger.error(f"其他错误↙\n {error}")
|
| 242 |
return 2, None
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, Response, stream_with_context, render_template_string
|
| 2 |
+
from google.generativeai.types import BlockedPromptException, StopCandidateException, generation_types
|
| 3 |
from google.api_core.exceptions import InvalidArgument, ResourceExhausted, Aborted, InternalServerError, ServiceUnavailable, PermissionDenied
|
| 4 |
import google.generativeai as genai
|
| 5 |
import json
|
|
|
|
| 227 |
switch_api_key()
|
| 228 |
return 0, None
|
| 229 |
|
| 230 |
+
elif isinstance(error, StopCandidateException):
|
| 231 |
logger.warning(f"AI输出内容被Gemini官方阻挡,代理没有得到有效回复")
|
| 232 |
switch_api_key()
|
| 233 |
return 0, None
|
| 234 |
|
| 235 |
+
elif isinstance(error, BlockedPromptException):
|
| 236 |
+
reason = error.block_reason
|
| 237 |
+
if reason == generation_types.BlockReason.SAFETY:
|
| 238 |
+
logger.warning(f"提示因安全原因被阻止:{error.safety_ratings}")
|
| 239 |
+
return 2, None
|
| 240 |
+
elif reason == generation_types.BlockReason.BLOCKLIST:
|
| 241 |
+
logger.warning(f"提示因包含阻止列表中的术语而被阻止")
|
| 242 |
+
return 2, None
|
| 243 |
+
elif reason == generation_types.BlockReason.PROHIBITED_CONTENT:
|
| 244 |
+
logger.warning(f"提示因包含禁止内容而被阻止")
|
| 245 |
+
return 2, None
|
| 246 |
+
elif reason == generation_types.BlockReason.OTHER:
|
| 247 |
+
logger.warning(f"提示因未知原因被阻止")
|
| 248 |
+
return 2, None
|
| 249 |
+
else:
|
| 250 |
+
logger.warning(f"提示被阻止,原因未知: {reason}")
|
| 251 |
+
return 2, None
|
| 252 |
else:
|
| 253 |
logger.error(f"其他错误↙\n {error}")
|
| 254 |
return 2, None
|