🐛 Bug: Fix the bug that makes moral checks unusable.
Browse files
main.py
CHANGED
@@ -365,9 +365,7 @@ class StatsMiddleware(BaseHTTPMiddleware):
|
|
365 |
|
366 |
if enable_moderation and moderated_content:
|
367 |
moderation_response = await self.moderate_content(moderated_content, token)
|
368 |
-
|
369 |
-
moderation_data = json.loads(moderation_result)
|
370 |
-
is_flagged = moderation_data.get('results', [{}])[0].get('flagged', False)
|
371 |
|
372 |
if is_flagged:
|
373 |
logger.error(f"Content did not pass the moral check: %s", moderated_content)
|
@@ -447,7 +445,18 @@ class StatsMiddleware(BaseHTTPMiddleware):
|
|
447 |
# 直接调用 moderations 函数
|
448 |
response = await moderations(moderation_request, token)
|
449 |
|
450 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
451 |
|
452 |
# 配置 CORS 中间件
|
453 |
app.add_middleware(
|
|
|
365 |
|
366 |
if enable_moderation and moderated_content:
|
367 |
moderation_response = await self.moderate_content(moderated_content, token)
|
368 |
+
is_flagged = moderation_response.get('results', [{}])[0].get('flagged', False)
|
|
|
|
|
369 |
|
370 |
if is_flagged:
|
371 |
logger.error(f"Content did not pass the moral check: %s", moderated_content)
|
|
|
445 |
# 直接调用 moderations 函数
|
446 |
response = await moderations(moderation_request, token)
|
447 |
|
448 |
+
# 读取流式响应的内容
|
449 |
+
moderation_result = b""
|
450 |
+
async for chunk in response.body_iterator:
|
451 |
+
if isinstance(chunk, str):
|
452 |
+
moderation_result += chunk.encode('utf-8')
|
453 |
+
else:
|
454 |
+
moderation_result += chunk
|
455 |
+
|
456 |
+
# 解码并解析 JSON
|
457 |
+
moderation_data = json.loads(moderation_result.decode('utf-8'))
|
458 |
+
|
459 |
+
return moderation_data
|
460 |
|
461 |
# 配置 CORS 中间件
|
462 |
app.add_middleware(
|