smgc commited on
Commit
552ed6b
1 Parent(s): f573954

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -8,9 +8,6 @@ import requests
8
  import logging
9
  from threading import Event
10
 
11
- # 如果使用 GPT 模型的 tokenization,可以引入 tiktoken
12
- # import tiktoken # 如果需要使用 GPT 的 token 化库
13
-
14
  app = Flask(__name__)
15
  logging.basicConfig(level=logging.INFO)
16
 
@@ -175,10 +172,15 @@ def messages():
175
  def on_query_progress(data):
176
  nonlocal response_text
177
  if 'text' in data:
178
- text = json.loads(data['text'])
179
- chunk = text['chunks'][-1] if text['chunks'] else None
180
- if chunk:
181
- response_text.append(chunk)
 
 
 
 
 
182
 
183
  # 检查是否是最终响应
184
  if data.get('final', False):
@@ -273,10 +275,15 @@ def handle_non_stream(previous_messages, msg_id, model, input_tokens):
273
  def on_query_progress(data):
274
  nonlocal response_text
275
  if 'text' in data:
276
- text = json.loads(data['text'])
277
- chunk = text['chunks'][-1] if text['chunks'] else None
278
- if chunk:
279
- response_text.append(chunk)
 
 
 
 
 
280
 
281
  # 检查是否是最终响应
282
  if data.get('final', False):
 
8
  import logging
9
  from threading import Event
10
 
 
 
 
11
  app = Flask(__name__)
12
  logging.basicConfig(level=logging.INFO)
13
 
 
172
  def on_query_progress(data):
173
  nonlocal response_text
174
  if 'text' in data:
175
+ try:
176
+ # 解析嵌套的 JSON 字符串
177
+ text_content = json.loads(data['text']) # data['text'] 是一个 JSON 字符串
178
+ chunk = text_content['chunks'][-1] if 'chunks' in text_content and text_content['chunks'] else None
179
+ if chunk:
180
+ response_text.append(chunk)
181
+ except json.JSONDecodeError as e:
182
+ logging.error(f"Failed to decode JSON from 'text' field: {e}")
183
+ response_text.append(f"Error decoding response: {e}")
184
 
185
  # 检查是否是最终响应
186
  if data.get('final', False):
 
275
  def on_query_progress(data):
276
  nonlocal response_text
277
  if 'text' in data:
278
+ try:
279
+ # 解析嵌套的 JSON 字符串
280
+ text_content = json.loads(data['text']) # data['text'] 是一个 JSON 字符串
281
+ chunk = text_content['chunks'][-1] if 'chunks' in text_content and text_content['chunks'] else None
282
+ if chunk:
283
+ response_text.append(chunk)
284
+ except json.JSONDecodeError as e:
285
+ logging.error(f"Failed to decode JSON from 'text' field: {e}")
286
+ response_text.append(f"Error decoding response: {e}")
287
 
288
  # 检查是否是最终响应
289
  if data.get('final', False):