Nanny7 commited on
Commit
eca0010
·
1 Parent(s): c340d5b

Fix streaming analysis None value concatenation error and improve error handling

Browse files
Files changed (1) hide show
  1. app.py +37 -4
app.py CHANGED
@@ -846,8 +846,8 @@ async def streaming_ai_analysis(text: str, keywords: List[str], country: str, la
846
  chunk = json.loads(data_str)
847
  if 'choices' in chunk and len(chunk['choices']) > 0:
848
  delta = chunk['choices'][0].get('delta', {})
849
- if 'content' in delta:
850
- yield delta['content']
851
  except json.JSONDecodeError:
852
  continue
853
  else:
@@ -891,7 +891,8 @@ def comprehensive_text_audit_streaming(text, keywords="", country="China", langu
891
  while True:
892
  try:
893
  chunk = loop.run_until_complete(async_gen.__anext__())
894
- accumulated_content += chunk
 
895
 
896
  # 实时输出累积内容
897
  yield f"""
@@ -994,7 +995,39 @@ def comprehensive_text_audit_streaming(text, keywords="", country="China", langu
994
 
995
  except Exception as e:
996
  print(f"Streaming analysis error: {e}")
997
- yield generate_error_card(str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
998
 
999
  # 更新审核函数以支持新参数
1000
  def comprehensive_text_audit_with_options(text, keywords="", country="中国", language="简体中文"):
 
846
  chunk = json.loads(data_str)
847
  if 'choices' in chunk and len(chunk['choices']) > 0:
848
  delta = chunk['choices'][0].get('delta', {})
849
+ if 'content' in delta and delta['content']:
850
+ yield str(delta['content'])
851
  except json.JSONDecodeError:
852
  continue
853
  else:
 
891
  while True:
892
  try:
893
  chunk = loop.run_until_complete(async_gen.__anext__())
894
+ if chunk: # 确保chunk不是None
895
+ accumulated_content += str(chunk)
896
 
897
  # 实时输出累积内容
898
  yield f"""
 
995
 
996
  except Exception as e:
997
  print(f"Streaming analysis error: {e}")
998
+ error_msg = f"Analysis failed: {str(e)}"
999
+ yield f"""
1000
+ <div style="
1001
+ max-width: 900px;
1002
+ margin: 0 auto;
1003
+ padding: 30px;
1004
+ background: linear-gradient(135deg, #E8E3DE 0%, #F2EDE9 100%);
1005
+ border-radius: 20px;
1006
+ font-family: 'Microsoft YaHei', 'Noto Sans SC', sans-serif;
1007
+ ">
1008
+ <div style="
1009
+ background-color: #F2EDE9;
1010
+ border-radius: 15px;
1011
+ padding: 25px;
1012
+ box-shadow: 0 10px 30px rgba(0,0,0,0.1);
1013
+ text-align: center;
1014
+ ">
1015
+ <h2 style="color: #B85450; margin-bottom: 15px; font-size: 24px;">⚠️ Analysis Error</h2>
1016
+ <p style="color: #5B5B5B; margin-bottom: 20px; font-size: 16px;">AI analysis encountered an issue:</p>
1017
+ <div style="
1018
+ background-color: rgba(184, 84, 80, 0.1);
1019
+ padding: 15px;
1020
+ border-radius: 10px;
1021
+ border-left: 4px solid #B85450;
1022
+ text-align: left;
1023
+ margin-bottom: 20px;
1024
+ ">
1025
+ <code style="color: #B85450; font-size: 14px;">{error_msg}</code>
1026
+ </div>
1027
+ <p style="color: #8C8C8C; font-size: 14px;">Please try again later or check your network connection</p>
1028
+ </div>
1029
+ </div>
1030
+ """
1031
 
1032
  # 更新审核函数以支持新参数
1033
  def comprehensive_text_audit_with_options(text, keywords="", country="中国", language="简体中文"):