wangwei0 commited on
Commit
bdc9425
1 Parent(s): e399ba0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +42 -42
main.py CHANGED
@@ -132,48 +132,48 @@ def chat_completions():
132
  },
133
  )
134
  else:
135
- response = requests.post(f"{base_url}/queue/join", json=json_prompt)
136
- url = f"{base_url}/queue/data?session_hash={session_hash}"
137
- data_stream = requests.get(url, stream=True)
138
-
139
- time_now = int(time.time())
140
- collected_data = []
141
-
142
- try:
143
- for line in data_stream.iter_lines():
144
- if line:
145
- try:
146
- decoded_line = line.decode("utf-8")
147
- json_line = json.loads(decoded_line[6:]) # 确保这里的切片索引是正确的
148
- collected_data.append(json_line)
149
- if json_line.get("msg") == "process_completed":
150
- break
151
- except json.JSONDecodeError:
152
- print("Error decoding JSON from response")
153
- continue # 跳过不能解码的行
154
-
155
- if not collected_data:
156
- return jsonify({"error": "No data received from the server", "word_count": 0})
157
-
158
- # 检查数据是否有效并尝试生成最终响应
159
- try:
160
- res_data = gen_res_data(collected_data[-1], time_now=time_now) if collected_data else {"error": "No valid data processed", "word_count": 0}
161
- print("Response data:", res_data) # 打印输出res_data
162
- except KeyError as e:
163
- res_data = {"error": f"Missing key in data: {str(e)}", "word_count": 0}
164
- print("KeyError:", res_data) # 错误情况下也打印res_data
165
- except IndexError as e:
166
- res_data = {"error": f"Index error in data: {str(e)}", "word_count": 0}
167
- print("IndexError:", res_data)
168
- except Exception as e:
169
- res_data = {"error": f"An unexpected error occurred: {str(e)}", "word_count": 0}
170
- print("Exception:", res_data)
171
-
172
- except Exception as e:
173
- res_data = {"error": f"An error occurred while processing stream: {str(e)}", "word_count": 0}
174
- print("Stream processing exception:", res_data)
175
-
176
- return jsonify(res_data)
177
 
178
 
179
  def gen_res_data(data, time_now=0, start=False):
 
132
  },
133
  )
134
  else:
135
+ response = requests.post(f"{base_url}/queue/join", json=json_prompt)
136
+ url = f"{base_url}/queue/data?session_hash={session_hash}"
137
+ data_stream = requests.get(url, stream=True)
138
+
139
+ time_now = int(time.time())
140
+ collected_data = []
141
+
142
+ try:
143
+ for line in data_stream.iter_lines():
144
+ if line:
145
+ try:
146
+ decoded_line = line.decode("utf-8")
147
+ json_line = json.loads(decoded_line[6:]) # 确保这里的切片索引是正确的
148
+ collected_data.append(json_line)
149
+ if json_line.get("msg") == "process_completed":
150
+ break
151
+ except json.JSONDecodeError:
152
+ print("Error decoding JSON from response")
153
+ continue # 跳过不能解码的行
154
+
155
+ if not collected_data:
156
+ return jsonify({"error": "No data received from the server", "word_count": 0})
157
+
158
+ # 检查数据是否有效并尝试生成最终响应
159
+ try:
160
+ res_data = gen_res_data(collected_data[-1], time_now=time_now) if collected_data else {"error": "No valid data processed", "word_count": 0}
161
+ print("Response data:", res_data) # 打印输出res_data
162
+ except KeyError as e:
163
+ res_data = {"error": f"Missing key in data: {str(e)}", "word_count": 0}
164
+ print("KeyError:", res_data) # 错误情况下也打印res_data
165
+ except IndexError as e:
166
+ res_data = {"error": f"Index error in data: {str(e)}", "word_count": 0}
167
+ print("IndexError:", res_data)
168
+ except Exception as e:
169
+ res_data = {"error": f"An unexpected error occurred: {str(e)}", "word_count": 0}
170
+ print("Exception:", res_data)
171
+
172
+ except Exception as e:
173
+ res_data = {"error": f"An error occurred while processing stream: {str(e)}", "word_count": 0}
174
+ print("Stream processing exception:", res_data)
175
+
176
+ return jsonify(res_data)
177
 
178
 
179
  def gen_res_data(data, time_now=0, start=False):