antigravity commited on
Commit
13663f0
·
1 Parent(s): fffeb52

fix: ToneSandhi IndexError and proper error handling for missing output files

Browse files
Files changed (2) hide show
  1. app.py +7 -1
  2. genie_tts/G2P/Chinese/ToneSandhi.py +9 -2
app.py CHANGED
@@ -189,13 +189,19 @@ async def dynamic_tts(
189
  out_path = f"/tmp/out_dyn_{int(time.time())}.wav"
190
  genie_tts.tts(character_name, text, save_path=out_path, play=False, text_language=text_lang, speed=speed)
191
 
192
- # 🟢 同样增加文件等
193
  wait_time = 0
194
  while not os.path.exists(out_path) and wait_time < 50:
195
  time.sleep(0.1)
196
  wait_time += 1
197
 
 
 
 
 
198
  return StreamingResponse(open(out_path, "rb"), media_type="audio/wav")
 
 
199
  except Exception as e:
200
  print(f"❌ Error: {e}")
201
  raise HTTPException(status_code=500, detail=str(e))
 
189
  out_path = f"/tmp/out_dyn_{int(time.time())}.wav"
190
  genie_tts.tts(character_name, text, save_path=out_path, play=False, text_language=text_lang, speed=speed)
191
 
192
+ # 🟢 等待文件生成(最多5秒)
193
  wait_time = 0
194
  while not os.path.exists(out_path) and wait_time < 50:
195
  time.sleep(0.1)
196
  wait_time += 1
197
 
198
+ # 🔴 修复:检查文件是否实际生成,避免返回不存在的文件
199
+ if not os.path.exists(out_path):
200
+ raise HTTPException(status_code=500, detail="TTS processing failed. Output file was not generated.")
201
+
202
  return StreamingResponse(open(out_path, "rb"), media_type="audio/wav")
203
+ except HTTPException:
204
+ raise
205
  except Exception as e:
206
  print(f"❌ Error: {e}")
207
  raise HTTPException(status_code=500, detail=str(e))
genie_tts/G2P/Chinese/ToneSandhi.py CHANGED
@@ -301,10 +301,17 @@ class ToneSandhi:
301
  assert len(sub_finals_list) == len(seg)
302
  merge_last = [False] * len(seg)
303
  for i, (word, pos) in enumerate(seg):
 
 
 
 
 
 
 
304
  if (
305
  i - 1 >= 0
306
- and sub_finals_list[i - 1][-1][-1] == "3"
307
- and sub_finals_list[i][0][-1] == "3"
308
  and not merge_last[i - 1]
309
  ):
310
  # if the last word is reduplication, not merge, because reduplication need to be _neural_sandhi
 
301
  assert len(sub_finals_list) == len(seg)
302
  merge_last = [False] * len(seg)
303
  for i, (word, pos) in enumerate(seg):
304
+ # 🔴 修复:添加边界检查,防止空字符串导致 IndexError
305
+ prev_finals = sub_finals_list[i - 1] if i - 1 >= 0 else []
306
+ curr_finals = sub_finals_list[i]
307
+
308
+ prev_last = prev_finals[-1] if prev_finals and len(prev_finals[-1]) > 0 else ""
309
+ curr_first = curr_finals[0] if curr_finals and len(curr_finals[0]) > 0 else ""
310
+
311
  if (
312
  i - 1 >= 0
313
+ and len(prev_last) > 0 and prev_last[-1] == "3"
314
+ and len(curr_first) > 0 and curr_first[-1] == "3"
315
  and not merge_last[i - 1]
316
  ):
317
  # if the last word is reduplication, not merge, because reduplication need to be _neural_sandhi