|
|
import os |
|
|
import uuid |
|
|
import gradio as gr |
|
|
import numpy as np |
|
|
import soundfile as sf |
|
|
from kokoro import KPipeline |
|
|
|
|
|
|
|
|
AUDIO_DIR = "audio_files" |
|
|
os.makedirs(AUDIO_DIR, exist_ok=True) |
|
|
|
|
|
|
|
|
pipeline = KPipeline(lang_code='a') |
|
|
|
|
|
def generate_tts(text, voice="af_sky", speed=1.0): |
|
|
"""Generate TTS from input text and return a URL to the audio file.""" |
|
|
if not text.strip(): |
|
|
return "Please enter some text to convert to speech." |
|
|
|
|
|
try: |
|
|
|
|
|
segments = list( |
|
|
pipeline( |
|
|
text, |
|
|
voice=voice, |
|
|
speed=speed, |
|
|
split_pattern=r'$^' |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
audio_full = np.concatenate([audio for (_, _, audio) in segments]) |
|
|
|
|
|
|
|
|
session_id = str(uuid.uuid4())[:8] |
|
|
filename = f"{session_id}_audio.wav" |
|
|
filepath = os.path.join(AUDIO_DIR, filename) |
|
|
|
|
|
|
|
|
sf.write(filepath, audio_full, 24000) |
|
|
|
|
|
|
|
|
audio_url = f"/file={filepath}" |
|
|
|
|
|
return audio_url, filepath |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error generating speech: {str(e)}", None |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# Kokoro Text-to-Speech Generator") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
text_input = gr.Textbox( |
|
|
label="Enter text to convert to speech", |
|
|
placeholder="Type your text here...", |
|
|
lines=5 |
|
|
) |
|
|
voice_selector = gr.Dropdown( |
|
|
choices=["af_sky", "af_breeze", "af_rays", "af_glow", "af_ember"], |
|
|
value="af_sky", |
|
|
label="Voice" |
|
|
) |
|
|
speed_slider = gr.Slider( |
|
|
minimum=0.5, |
|
|
maximum=2.0, |
|
|
value=1.0, |
|
|
step=0.1, |
|
|
label="Speech Speed" |
|
|
) |
|
|
submit_btn = gr.Button("Generate Speech") |
|
|
|
|
|
with gr.Column(): |
|
|
audio_output = gr.Audio(label="Generated Speech", type="filepath") |
|
|
url_output = gr.Textbox(label="Audio URL") |
|
|
|
|
|
|
|
|
submit_btn.click( |
|
|
fn=generate_tts, |
|
|
inputs=[text_input, voice_selector, speed_slider], |
|
|
outputs=[url_output, audio_output] |
|
|
) |
|
|
|
|
|
gr.Markdown(""" |
|
|
## How to Use |
|
|
1. Enter the text you want to convert to speech in the text box |
|
|
2. Select a voice from the dropdown menu |
|
|
3. Adjust the speech speed if needed |
|
|
4. Click "Generate Speech" to create the audio |
|
|
5. The audio will play automatically and a URL will be provided for download |
|
|
""") |
|
|
|
|
|
|
|
|
demo.launch(share=True) |