Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +52 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from kokoro import TTS
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model once at startup
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
# You can pick a voice that exists in Kokoro (e.g. 'af_heart', 'af_calypso', 'am_adam', etc.)
|
| 9 |
+
VOICE = "af_heart"
|
| 10 |
+
|
| 11 |
+
print("π Loading Kokoro-TTS model...")
|
| 12 |
+
tts = TTS(voice=VOICE, device=device)
|
| 13 |
+
print("β
Model loaded successfully!")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def generate_audio(text):
|
| 17 |
+
if not text.strip():
|
| 18 |
+
return None, "Please enter some text."
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
print("π€ Generating audio...")
|
| 22 |
+
audio = tts.generate(text) # returns numpy array (float32)
|
| 23 |
+
return (44100, audio), "Success!"
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return None, f"Error: {str(e)}"
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
### GRADIO UI ###
|
| 29 |
+
with gr.Blocks(title="Kokoro-TTS Text β Audio") as demo:
|
| 30 |
+
gr.Markdown("## π§ Kokoro-TTS: Text to Speech Demo")
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
input_text = gr.Textbox(
|
| 34 |
+
label="Enter text",
|
| 35 |
+
placeholder="Type something to speak...",
|
| 36 |
+
lines=4
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
with gr.Row():
|
| 40 |
+
output_audio = gr.Audio(label="Generated Audio")
|
| 41 |
+
status = gr.Textbox(label="Status")
|
| 42 |
+
|
| 43 |
+
generate_button = gr.Button("Generate")
|
| 44 |
+
|
| 45 |
+
generate_button.click(
|
| 46 |
+
fn=generate_audio,
|
| 47 |
+
inputs=input_text,
|
| 48 |
+
outputs=[output_audio, status]
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
kokoro-tts
|
| 3 |
+
torch
|
| 4 |
+
numpy
|
| 5 |
+
|