abderrahmane11 commited on
Commit
f2a9aa4
·
verified ·
1 Parent(s): 6149f09

Let Compare read a paragraph too

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -61,6 +61,23 @@ def _split(text, limit=8):
61
  return out[:limit]
62
 
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  @spaces.GPU(duration=10)
65
  def say(text, voice, speed, pitch_shift, pitch_range, seed):
66
  bump("speaks")
@@ -70,27 +87,8 @@ def say(text, voice, speed, pitch_shift, pitch_range, seed):
70
  n = int(seed)
71
  if n < 0:
72
  n = random.randint(0, 2**31 - 1)
73
- chunks = _split(text)
74
  try:
75
- if len(chunks) == 1:
76
- wave, phones, _ = speak(
77
- MODEL, text, voice, float(speed), float(pitch_shift), n, float(pitch_range)
78
- )
79
- audio = wave.numpy().astype(np.float32)
80
- phonemes = " ".join(phones.symbols)
81
- detail = f"{len(phones.ids)} phonemes, seed {n}\n\n{phonemes}"
82
- else:
83
- # Same voice and seed across the paragraph, quiet gaps at the sentence
84
- # ends the way a reader pauses.
85
- pieces = []
86
- for s in chunks:
87
- wave, _, _ = speak(
88
- MODEL, s, voice, float(speed), float(pitch_shift), n, float(pitch_range)
89
- )
90
- pieces.append(wave.numpy().astype(np.float32))
91
- gap = np.zeros(int(RATE * 0.22), dtype=np.float32)
92
- audio = np.concatenate([p for pair in zip(pieces, [gap] * len(pieces)) for p in pair][:-1])
93
- detail = f"{len(chunks)} sentences, seed {n}\n\n" + "\n".join(chunks)
94
  except Exception as e:
95
  raise gr.Error(str(e)) from e
96
  # Peak normalise only if it would clip, so the sample stays honest otherwise.
@@ -98,7 +96,7 @@ def say(text, voice, speed, pitch_shift, pitch_range, seed):
98
  if peak > 0.99:
99
  audio = audio / peak * 0.99
100
  seconds = len(audio) / RATE
101
- return (RATE, audio), n, f"{seconds:.2f} s of audio, {detail}"
102
 
103
 
104
 
@@ -113,10 +111,9 @@ def compare(text, speed, seed):
113
  out = []
114
  for voice in VOICES:
115
  try:
116
- wave, _, _ = speak(MODEL, text, voice, float(speed), 0.0, n, 1.0)
117
  except Exception as e:
118
  raise gr.Error(str(e)) from e
119
- audio = wave.numpy().astype(np.float32)
120
  out.append((RATE, audio))
121
  return out[0], out[1], f"same text, same seed {n}, F2 above and M2 below", gr.update(visible=True)
122
 
 
61
  return out[:limit]
62
 
63
 
64
+ def _render(text, voice, speed, pitch_shift, pitch_range, n):
65
+ """One utterance, or a paragraph read sentence by sentence with the same voice
66
+ and seed and a short pause between. Both buttons go through this, so Compare
67
+ accepts a paragraph for the same reason Speak does."""
68
+ chunks = _split(text)
69
+ if len(chunks) == 1:
70
+ wave, phones, _ = speak(MODEL, text, voice, float(speed), float(pitch_shift), n, float(pitch_range))
71
+ return wave.numpy().astype(np.float32), f"{len(phones.ids)} phonemes\n\n" + " ".join(phones.symbols)
72
+ pieces = []
73
+ for piece in chunks:
74
+ wave, _, _ = speak(MODEL, piece, voice, float(speed), float(pitch_shift), n, float(pitch_range))
75
+ pieces.append(wave.numpy().astype(np.float32))
76
+ gap = np.zeros(int(RATE * 0.22), dtype=np.float32)
77
+ audio = np.concatenate([p for pair in zip(pieces, [gap] * len(pieces)) for p in pair][:-1])
78
+ return audio, f"{len(chunks)} sentences\n\n" + "\n".join(chunks)
79
+
80
+
81
  @spaces.GPU(duration=10)
82
  def say(text, voice, speed, pitch_shift, pitch_range, seed):
83
  bump("speaks")
 
87
  n = int(seed)
88
  if n < 0:
89
  n = random.randint(0, 2**31 - 1)
 
90
  try:
91
+ audio, detail = _render(text, voice, speed, pitch_shift, pitch_range, n)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  except Exception as e:
93
  raise gr.Error(str(e)) from e
94
  # Peak normalise only if it would clip, so the sample stays honest otherwise.
 
96
  if peak > 0.99:
97
  audio = audio / peak * 0.99
98
  seconds = len(audio) / RATE
99
+ return (RATE, audio), n, f"{seconds:.2f} s of audio, seed {n}\n\n{detail}"
100
 
101
 
102
 
 
111
  out = []
112
  for voice in VOICES:
113
  try:
114
+ audio, _ = _render(text, voice, speed, 0.0, 1.0, n)
115
  except Exception as e:
116
  raise gr.Error(str(e)) from e
 
117
  out.append((RATE, audio))
118
  return out[0], out[1], f"same text, same seed {n}, F2 above and M2 below", gr.update(visible=True)
119