File size: 1,751 Bytes
1269a6e
 
867ffb1
 
 
1269a6e
867ffb1
 
1269a6e
867ffb1
 
 
 
 
 
 
 
1269a6e
867ffb1
 
 
 
 
 
 
 
 
 
1269a6e
867ffb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1269a6e
 
867ffb1
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
import torch
import soundfile as sf
import tempfile
from kokoro_onnx import Kokoro

# Load Kokoro TTS Model (No need for external files)
kokoro = Kokoro()

# Fetch available voices dynamically (if supported)
try:
    voices = kokoro.get_voices()  # If `get_voices()` exists, use it
except AttributeError:
    # Default voice list if `get_voices()` isn't available
    voices = ['af', 'af_bella', 'af_nicole', 'af_sarah', 'af_sky', 
              'am_adam', 'am_michael', 'bf_emma', 'bf_isabella', 
              'bm_george', 'bm_lewis']

def generate_speech(text, voice, speed, show_transcript):
    """Convert input text to speech using Kokoro TTS"""
    samples, sample_rate = kokoro.create(text, voice=voice, speed=float(speed))
    
    # Save audio file temporarily
    temp_file = tempfile.mktemp(suffix=".wav")
    sf.write(temp_file, samples, sample_rate)
    
    # Return audio and optional transcript
    return temp_file, text if show_transcript else None

# Gradio UI
interface = gr.Interface(
    fn=generate_speech,
    inputs=[
        gr.Textbox(label="Input Text", lines=5, placeholder="Type here..."),
        gr.Dropdown(choices=voices, label="Select Voice", value=voices[0]),
        gr.Slider(minimum=0.5, maximum=2.0, value=1.0, step=0.1, label="Speech Speed"),
        gr.Checkbox(label="Show Transcript", value=True)
    ],
    outputs=[
        gr.Audio(label="Generated Speech"),
        gr.Textbox(label="Transcript", visible=True)
    ],
    title="Educational Text-to-Speech",
    description="Enter text, choose a voice, and generate speech. Use the transcript option to follow along while listening.",
    allow_flagging="never"
)

# Launch the app
if __name__ == "__main__":
    interface.launch()