Ko-TTS-Arena Contributors commited on
Commit
ee45344
·
1 Parent(s): f54c69b

feat: Add retry logic for Typecast API (3 retries with exponential backoff)

Browse files
Files changed (1) hide show
  1. tts.py +22 -8
tts.py CHANGED
@@ -445,8 +445,10 @@ def predict_humelo_tts(text: str, voice: str = "리아", emotion: str = "neutral
445
  return f.name
446
 
447
 
448
- def predict_typecast_tts(text: str, voice_id: str, model: str = "ssfm-v30") -> str:
449
- """Typecast TTS API 호출 (v3.0 experiment API)"""
 
 
450
  api_key = TYPECAST_API_KEY
451
  if not api_key:
452
  raise ValueError("TYPECAST_API_KEY 환경 변수가 설정되지 않았습니다.")
@@ -464,12 +466,24 @@ def predict_typecast_tts(text: str, voice_id: str, model: str = "ssfm-v30") -> s
464
  "Content-Type": "application/json"
465
  }
466
 
467
- response = requests.post(url, headers=headers, json=payload, timeout=60)
468
- response.raise_for_status()
469
-
470
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
471
- f.write(response.content)
472
- return f.name
 
 
 
 
 
 
 
 
 
 
 
 
473
 
474
 
475
  def predict_gemini_tts(text: str, voice: str = "Aoede", model: str = "gemini-2.5-flash-tts") -> str:
 
445
  return f.name
446
 
447
 
448
+ def predict_typecast_tts(text: str, voice_id: str, model: str = "ssfm-v30", max_retries: int = 3) -> str:
449
+ """Typecast TTS API 호출 (v3.0 experiment API) with retry logic"""
450
+ import time
451
+
452
  api_key = TYPECAST_API_KEY
453
  if not api_key:
454
  raise ValueError("TYPECAST_API_KEY 환경 변수가 설정되지 않았습니다.")
 
466
  "Content-Type": "application/json"
467
  }
468
 
469
+ last_error = None
470
+ for attempt in range(max_retries):
471
+ try:
472
+ response = requests.post(url, headers=headers, json=payload, timeout=60)
473
+ response.raise_for_status()
474
+
475
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
476
+ f.write(response.content)
477
+ return f.name
478
+ except requests.exceptions.RequestException as e:
479
+ last_error = e
480
+ print(f"[Typecast] Attempt {attempt + 1}/{max_retries} failed: {e}")
481
+ if attempt < max_retries - 1:
482
+ wait_time = (attempt + 1) * 2 # 2초, 4초, 6초...
483
+ print(f"[Typecast] Retrying in {wait_time}s...")
484
+ time.sleep(wait_time)
485
+
486
+ raise ValueError(f"Typecast TTS API 오류 (재시도 {max_retries}회 실패): {last_error}")
487
 
488
 
489
  def predict_gemini_tts(text: str, voice: str = "Aoede", model: str = "gemini-2.5-flash-tts") -> str: