Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,16 @@ import io
|
|
| 3 |
import torch
|
| 4 |
import torchaudio
|
| 5 |
import numpy as np
|
| 6 |
-
import scipy.io.wavfile as wavfile
|
| 7 |
import gradio as gr
|
|
|
|
| 8 |
from pocket_tts import TTSModel
|
| 9 |
|
| 10 |
-
# Load the
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
VOICES = [
|
| 17 |
"alba",
|
| 18 |
"marius",
|
|
@@ -44,7 +44,7 @@ def change_speed_pitch_preserved(audio_np: np.ndarray, sample_rate: int, speed:
|
|
| 44 |
)
|
| 45 |
return stretched_tensor.squeeze(0).numpy()
|
| 46 |
except Exception as e:
|
| 47 |
-
print(f"SoX tempo stretch fallback
|
| 48 |
try:
|
| 49 |
import librosa
|
| 50 |
return librosa.effects.time_stretch(audio_np.astype(np.float32), rate=speed)
|
|
@@ -63,19 +63,23 @@ def synthesize(text: str, voice: str, speed: float = 1.0):
|
|
| 63 |
if clean_voice not in VOICES:
|
| 64 |
clean_voice = "alba"
|
| 65 |
|
| 66 |
-
# Clamp speed between 0.5x and 2.0x
|
| 67 |
speed_factor = max(0.5, min(2.0, float(speed) if speed else 1.0))
|
| 68 |
|
| 69 |
-
# 1.
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
audio_np = audio_tensor.cpu().float().numpy().squeeze()
|
| 76 |
-
else:
|
| 77 |
-
audio_np = np.array(audio_tensor, dtype=np.float32).squeeze()
|
| 78 |
|
|
|
|
| 79 |
sample_rate = getattr(model, "sample_rate", 24000)
|
| 80 |
|
| 81 |
# 2. Adjust speed with pitch preservation
|
|
@@ -87,7 +91,7 @@ def synthesize(text: str, voice: str, speed: float = 1.0):
|
|
| 87 |
if max_val > 0:
|
| 88 |
audio_np = (audio_np / max_val) * 0.95
|
| 89 |
|
| 90 |
-
# Return in Gradio (sample_rate,
|
| 91 |
int16_audio = (audio_np * 32767).astype(np.int16)
|
| 92 |
return (sample_rate, int16_audio)
|
| 93 |
|
|
|
|
| 3 |
import torch
|
| 4 |
import torchaudio
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
+
import pocket_tts
|
| 8 |
from pocket_tts import TTSModel
|
| 9 |
|
| 10 |
+
# 1. Load the Pocket-TTS model using the official pocket_tts API
|
| 11 |
+
print("Loading Kyutai Pocket-TTS model...")
|
| 12 |
+
model = TTSModel.load_model()
|
| 13 |
+
print("Pocket-TTS model loaded successfully!")
|
| 14 |
|
| 15 |
+
# Official Kyutai Pocket-TTS voice list
|
| 16 |
VOICES = [
|
| 17 |
"alba",
|
| 18 |
"marius",
|
|
|
|
| 44 |
)
|
| 45 |
return stretched_tensor.squeeze(0).numpy()
|
| 46 |
except Exception as e:
|
| 47 |
+
print(f"SoX tempo stretch fallback: {e}")
|
| 48 |
try:
|
| 49 |
import librosa
|
| 50 |
return librosa.effects.time_stretch(audio_np.astype(np.float32), rate=speed)
|
|
|
|
| 63 |
if clean_voice not in VOICES:
|
| 64 |
clean_voice = "alba"
|
| 65 |
|
|
|
|
| 66 |
speed_factor = max(0.5, min(2.0, float(speed) if speed else 1.0))
|
| 67 |
|
| 68 |
+
# 1. Prepare voice state & stream chunks
|
| 69 |
+
voice_state = model.get_voice_state(clean_voice)
|
| 70 |
+
generation_state = model.get_state_for_audio_generation(clean_text, voice_state)
|
| 71 |
+
|
| 72 |
+
audio_chunks = []
|
| 73 |
+
for chunk in model.generate_audio_stream(generation_state):
|
| 74 |
+
if isinstance(chunk, torch.Tensor):
|
| 75 |
+
audio_chunks.append(chunk.detach().cpu().float().numpy().squeeze())
|
| 76 |
+
else:
|
| 77 |
+
audio_chunks.append(np.array(chunk, dtype=np.float32).squeeze())
|
| 78 |
|
| 79 |
+
if not audio_chunks:
|
| 80 |
+
raise gr.Error("No audio was generated by the model.")
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
audio_np = np.concatenate(audio_chunks)
|
| 83 |
sample_rate = getattr(model, "sample_rate", 24000)
|
| 84 |
|
| 85 |
# 2. Adjust speed with pitch preservation
|
|
|
|
| 91 |
if max_val > 0:
|
| 92 |
audio_np = (audio_np / max_val) * 0.95
|
| 93 |
|
| 94 |
+
# Return in Gradio (sample_rate, numpy_int16_array) format
|
| 95 |
int16_audio = (audio_np * 32767).astype(np.int16)
|
| 96 |
return (sample_rate, int16_audio)
|
| 97 |
|