HY-2012 commited on
Commit
5cb4c08
·
verified ·
1 Parent(s): 0dcc544

Fix the full cache issue

Browse files
Files changed (1) hide show
  1. ax_spoken_communication_demo.py +30 -1
ax_spoken_communication_demo.py CHANGED
@@ -196,6 +196,20 @@ class QwenTranslationAPI:
196
  self.api_url = api_url
197
  self.session_id = f"speech_translate_{int(time.time())}"
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  def translate(self, text_content, max_retries=3, timeout=120):
200
  """调用千问API进行处理"""
201
  if not text_content or text_content.strip() == "":
@@ -232,6 +246,7 @@ class QwenTranslationAPI:
232
  start_time = time.time()
233
  full_translation = ""
234
  last_chunk = ""
 
235
 
236
  while time.time() - start_time < timeout:
237
  try:
@@ -239,7 +254,15 @@ class QwenTranslationAPI:
239
  result_data = result_response.json()
240
 
241
  # 获取当前chunk
242
- current_chunk = result_data.get("response", "")#.strip()
 
 
 
 
 
 
 
 
243
  full_translation += current_chunk
244
 
245
  # 检查是否完成
@@ -256,6 +279,12 @@ class QwenTranslationAPI:
256
  break
257
  continue
258
 
 
 
 
 
 
 
259
  print(f"[API] 轮询超时,尝试第 {attempt + 1} 次重试")
260
 
261
  except requests.exceptions.RequestException as e:
 
196
  self.api_url = api_url
197
  self.session_id = f"speech_translate_{int(time.time())}"
198
 
199
+ def reset_context(self):
200
+ """重置API上下文"""
201
+ try:
202
+ reset_url = f"{self.api_url}/api/reset"
203
+ response = requests.post(reset_url, json={}, timeout=5)
204
+ if response.status_code == 200:
205
+ print("[API] 上下文重置成功")
206
+ return True
207
+ else:
208
+ print(f"[API] 重置失败,状态码: {response.status_code}")
209
+ except Exception as e:
210
+ print(f"[API] 重置上下文失败: {e}")
211
+ return False
212
+
213
  def translate(self, text_content, max_retries=3, timeout=120):
214
  """调用千问API进行处理"""
215
  if not text_content or text_content.strip() == "":
 
246
  start_time = time.time()
247
  full_translation = ""
248
  last_chunk = ""
249
+ error_detected = False
250
 
251
  while time.time() - start_time < timeout:
252
  try:
 
254
  result_data = result_response.json()
255
 
256
  # 获取当前chunk
257
+ current_chunk = result_data.get("response", "")#.strip()
258
+
259
+ # 检查是否有setkvcache failed错误
260
+ if "error:" in current_chunk.lower() or "setkvcache failed" in current_chunk.lower():
261
+ print(f"[API] 检测到错误: {current_chunk}")
262
+ error_detected = True
263
+ self.reset_context()
264
+ break
265
+
266
  full_translation += current_chunk
267
 
268
  # 检查是否完成
 
279
  break
280
  continue
281
 
282
+ # 如果检测到错误且还有重试次数,继续重试
283
+ if error_detected and attempt < max_retries - 1:
284
+ print(f"[API] 等待1秒后重试...")
285
+ time.sleep(1)
286
+ continue
287
+
288
  print(f"[API] 轮询超时,尝试第 {attempt + 1} 次重试")
289
 
290
  except requests.exceptions.RequestException as e: