Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from kokoro import KPipeline
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Initialize Kokoro pipeline
|
| 7 |
+
pipeline = KPipeline(lang_code='a') # Change to 'e' for Spanish, 'f' for French, etc.
|
| 8 |
+
|
| 9 |
+
def synthesize(text, voice, speed):
|
| 10 |
+
"""Generate TTS audio from text input."""
|
| 11 |
+
generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
|
| 12 |
+
|
| 13 |
+
# Process generated audio
|
| 14 |
+
output_files = []
|
| 15 |
+
for i, (gs, ps, audio) in enumerate(generator):
|
| 16 |
+
file_path = f"output_{i}.wav"
|
| 17 |
+
sf.write(file_path, audio, 24000) # Save each audio segment
|
| 18 |
+
output_files.append(file_path)
|
| 19 |
+
|
| 20 |
+
return output_files[0] if output_files else None # Return first file
|
| 21 |
+
|
| 22 |
+
# Gradio UI for user interaction
|
| 23 |
+
iface = gr.Interface(
|
| 24 |
+
fn=synthesize,
|
| 25 |
+
inputs=[
|
| 26 |
+
gr.Textbox(label="Text Input", placeholder="Enter your text..."),
|
| 27 |
+
gr.Textbox(label="Voice", value="af_heart"), # Default voice
|
| 28 |
+
gr.Slider(0.5, 2.0, value=1, label="Speed") # Adjust speech speed
|
| 29 |
+
],
|
| 30 |
+
outputs=gr.File(label="Generated Speech"),
|
| 31 |
+
title="Kokoro TTS API",
|
| 32 |
+
description="Enter text and get synthesized speech.",
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
iface.launch(share=True)
|