Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Kyutai TTS Gradio App
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
from moshi.models.loaders import CheckpointInfo
|
| 7 |
+
from moshi.models.tts import DEFAULT_DSM_TTS_REPO, DEFAULT_DSM_TTS_VOICE_REPO, TTSModel
|
| 8 |
+
|
| 9 |
+
# Load model at startup
|
| 10 |
+
checkpoint_info = CheckpointInfo.from_hf_repo(DEFAULT_DSM_TTS_REPO)
|
| 11 |
+
tts_model = TTSModel.from_checkpoint_info(
|
| 12 |
+
checkpoint_info, n_q=32, temp=0.6, device=torch.device("cuda" if torch.cuda.is_available() else "cpu"),
|
| 13 |
+
dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Default voice
|
| 17 |
+
default_voice = "expresso/ex03-ex01_happy_001_channel1_334s.wav"
|
| 18 |
+
voice_path = tts_model.get_voice_path(default_voice)
|
| 19 |
+
|
| 20 |
+
def tts_function(text, cfg_coef):
|
| 21 |
+
entries = tts_model.prepare_script([text], padding_between=1)
|
| 22 |
+
condition_attributes = tts_model.make_condition_attributes(
|
| 23 |
+
[voice_path], cfg_coef=cfg_coef
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
pcms = []
|
| 27 |
+
|
| 28 |
+
def _on_frame(frame):
|
| 29 |
+
if (frame != -1).all():
|
| 30 |
+
pcm = tts_model.mimi.decode(frame[:, 1:, :]).cpu().numpy()
|
| 31 |
+
pcms.append(np.clip(pcm[0, 0], -1, 1))
|
| 32 |
+
|
| 33 |
+
tts_model.stream(entries, condition_attributes, on_frame=_on_frame)
|
| 34 |
+
|
| 35 |
+
if pcms:
|
| 36 |
+
audio = np.concatenate(pcms)
|
| 37 |
+
return (44100, audio)
|
| 38 |
+
else:
|
| 39 |
+
return "No audio generated."
|
| 40 |
+
|
| 41 |
+
# Gradio UI
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("# 🎙️ Kyutai TTS - Real-Time Streaming Voice AI")
|
| 44 |
+
with gr.Row():
|
| 45 |
+
text_input = gr.Textbox(label="Enter Text", lines=5, placeholder="Type something...")
|
| 46 |
+
cfg_slider = gr.Slider(minimum=0.0, maximum=5.0, value=2.0, label="CFG Coefficient")
|
| 47 |
+
generate_btn = gr.Button("Generate Audio")
|
| 48 |
+
audio_output = gr.Audio(label="Generated Audio", type="numpy")
|
| 49 |
+
|
| 50 |
+
generate_btn.click(fn=tts_function, inputs=[text_input, cfg_slider], outputs=audio_output)
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
demo.launch()
|