J Z commited on
Commit
3b0d23a
·
verified ·
1 Parent(s): a746a1f

Update tts.py

Browse files
Files changed (1) hide show
  1. tts.py +28 -5
tts.py CHANGED
@@ -33,6 +33,7 @@ _model = None
33
  _tokenizer = None
34
  _snac_model = None
35
  _ref_tokens = None # Pre-encoded reference audio tokens
 
36
  _initialized = False
37
 
38
 
@@ -108,6 +109,22 @@ def initialize():
108
  # 4. Pre-encode reference audio for voice cloning
109
  _ref_tokens = _load_reference_audio()
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  _initialized = True
112
  logger.info(f"[{_ts()}] [TTS] ✓ Orpheus ready in {time.time()-t0:.2f}s (voice cloning: {_ref_tokens is not None})")
113
 
@@ -204,7 +221,7 @@ def _build_prompt_tokens(text):
204
  end = torch.tensor([[TOKEN_END_OF_TEXT, TOKEN_END_OF_HUMAN, TOKEN_START_OF_AI, TOKEN_AUDIO_START]], dtype=torch.int64)
205
  final = torch.tensor([[TOKEN_AUDIO_END, TOKEN_END_OF_AI]], dtype=torch.int64)
206
 
207
- ref_text = _tokenizer("", return_tensors="pt").input_ids
208
  ref_input = torch.cat([
209
  start, ref_text, end,
210
  torch.tensor([_ref_tokens], dtype=torch.int64),
@@ -309,17 +326,23 @@ def generate_audio(text: str, output_filename: str = None) -> str:
309
  attention_mask = torch.ones_like(input_ids)
310
  logger.info(f"[{_ts()}] [TTS] Prompt: {input_ids.shape[1]} tokens")
311
 
312
- # Generate
 
 
 
 
 
 
313
  t1 = time.time()
314
  with torch.no_grad():
315
  generated_ids = _model.generate(
316
  input_ids=input_ids,
317
  attention_mask=attention_mask,
318
- max_new_tokens=2048,
319
  do_sample=True,
320
- temperature=0.6,
321
  top_p=0.9,
322
- repetition_penalty=1.2,
323
  eos_token_id=TOKEN_AUDIO_END,
324
  )
325
  t2 = time.time()
 
33
  _tokenizer = None
34
  _snac_model = None
35
  _ref_tokens = None # Pre-encoded reference audio tokens
36
+ _ref_transcript = "" # Transcript of reference audio
37
  _initialized = False
38
 
39
 
 
109
  # 4. Pre-encode reference audio for voice cloning
110
  _ref_tokens = _load_reference_audio()
111
 
112
+ # 5. Load reference transcript if available (improves voice cloning quality)
113
+ global _ref_transcript
114
+ _ref_transcript = ""
115
+ for d in [VOICE_REF_DIR, "/app"]:
116
+ for name in ["voice_transcript.txt", "transcript.txt"]:
117
+ path = os.path.join(d, name)
118
+ if os.path.exists(path):
119
+ _ref_transcript = open(path).read().strip()
120
+ logger.info(f"[{_ts()}] [TTS] Reference transcript loaded: \"{_ref_transcript[:60]}...\"")
121
+ break
122
+ if _ref_transcript:
123
+ break
124
+ if not _ref_transcript and _ref_tokens:
125
+ logger.info(f"[{_ts()}] [TTS] No transcript file found. Voice cloning may be less consistent.")
126
+ logger.info(f"[{_ts()}] [TTS] Add voice_transcript.txt with the spoken text for better results.")
127
+
128
  _initialized = True
129
  logger.info(f"[{_ts()}] [TTS] ✓ Orpheus ready in {time.time()-t0:.2f}s (voice cloning: {_ref_tokens is not None})")
130
 
 
221
  end = torch.tensor([[TOKEN_END_OF_TEXT, TOKEN_END_OF_HUMAN, TOKEN_START_OF_AI, TOKEN_AUDIO_START]], dtype=torch.int64)
222
  final = torch.tensor([[TOKEN_AUDIO_END, TOKEN_END_OF_AI]], dtype=torch.int64)
223
 
224
+ ref_text = _tokenizer(_ref_transcript, return_tensors="pt").input_ids
225
  ref_input = torch.cat([
226
  start, ref_text, end,
227
  torch.tensor([_ref_tokens], dtype=torch.int64),
 
326
  attention_mask = torch.ones_like(input_ids)
327
  logger.info(f"[{_ts()}] [TTS] Prompt: {input_ids.shape[1]} tokens")
328
 
329
+ # Generate - scale max tokens to text length to prevent audio hallucination
330
+ # ~7 SNAC tokens per audio frame, ~40 frames per second, ~2.5 words per second
331
+ # So roughly 70 audio tokens per word. Cap at reasonable limits.
332
+ word_count = len(text.split())
333
+ max_tokens = min(max(word_count * 100, 300), 1200) # 300-1200 range
334
+ logger.info(f"[{_ts()}] [TTS] Words: {word_count}, max_new_tokens: {max_tokens}")
335
+
336
  t1 = time.time()
337
  with torch.no_grad():
338
  generated_ids = _model.generate(
339
  input_ids=input_ids,
340
  attention_mask=attention_mask,
341
+ max_new_tokens=max_tokens,
342
  do_sample=True,
343
+ temperature=0.7,
344
  top_p=0.9,
345
+ repetition_penalty=1.3,
346
  eos_token_id=TOKEN_AUDIO_END,
347
  )
348
  t2 = time.time()