Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tempfile
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
|
| 5 |
+
from pocket_tts import TTSModel
|
| 6 |
+
|
| 7 |
+
# -------------------------------------------------
|
| 8 |
+
# Load Pocket-TTS model ONCE (HF CPU-safe)
|
| 9 |
+
# -------------------------------------------------
|
| 10 |
+
model = TTSModel.load_model()
|
| 11 |
+
|
| 12 |
+
# -------------------------------------------------
|
| 13 |
+
# All official Kyutai voices (from kyutai/tts-voices)
|
| 14 |
+
# -------------------------------------------------
|
| 15 |
+
VOICES = {
|
| 16 |
+
"Alba – Casual": "hf://kyutai/tts-voices/alba-mackenna/casual.wav",
|
| 17 |
+
"Alba – Announcer": "hf://kyutai/tts-voices/alba-mackenna/announcer.wav",
|
| 18 |
+
"Alba – Merchant": "hf://kyutai/tts-voices/alba-mackenna/merchant.wav",
|
| 19 |
+
"Alba – A Moment By": "hf://kyutai/tts-voices/alba-mackenna/a-moment-by.wav",
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# -------------------------------------------------
|
| 23 |
+
# TTS generation (voice state per request)
|
| 24 |
+
# -------------------------------------------------
|
| 25 |
+
def generate_tts(text, voice_name):
|
| 26 |
+
if not text.strip():
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
voice_path = VOICES[voice_name]
|
| 30 |
+
|
| 31 |
+
# IMPORTANT:
|
| 32 |
+
# Create voice state per request
|
| 33 |
+
voice_state = model.get_state_for_audio_prompt(voice_path)
|
| 34 |
+
|
| 35 |
+
audio = model.generate_audio(
|
| 36 |
+
voice_state=voice_state,
|
| 37 |
+
text=text,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 41 |
+
sf.write(tmp.name, audio, samplerate=24000)
|
| 42 |
+
|
| 43 |
+
return tmp.name
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# -------------------------------------------------
|
| 47 |
+
# Gradio UI
|
| 48 |
+
# -------------------------------------------------
|
| 49 |
+
with gr.Blocks(title="Pocket TTS – Kyutai Voices") as demo:
|
| 50 |
+
gr.Markdown(
|
| 51 |
+
"""
|
| 52 |
+
# 🗣️ Pocket TTS (Kyutai)
|
| 53 |
+
CPU-only • Hugging Face native • All official voices
|
| 54 |
+
"""
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
voice_select = gr.Dropdown(
|
| 58 |
+
choices=list(VOICES.keys()),
|
| 59 |
+
value="Alba – Casual",
|
| 60 |
+
label="Voice"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
text_input = gr.Textbox(
|
| 64 |
+
label="Text",
|
| 65 |
+
lines=4,
|
| 66 |
+
placeholder="Type something and generate speech..."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
generate_btn = gr.Button("Generate")
|
| 70 |
+
|
| 71 |
+
audio_output = gr.Audio(
|
| 72 |
+
label="Output",
|
| 73 |
+
autoplay=False
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
generate_btn.click(
|
| 77 |
+
fn=generate_tts,
|
| 78 |
+
inputs=[text_input, voice_select],
|
| 79 |
+
outputs=audio_output
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
demo.launch()
|