File size: 2,462 Bytes
a132885
 
11bfd4b
 
a132885
11bfd4b
 
a132885
11bfd4b
 
 
 
 
 
 
 
a132885
 
 
11bfd4b
 
a132885
11bfd4b
 
a132885
11bfd4b
 
a132885
11bfd4b
 
 
 
 
 
 
 
 
 
 
a132885
11bfd4b
a132885
 
 
 
11bfd4b
a132885
 
 
 
 
 
 
 
 
 
11bfd4b
a132885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11bfd4b
a132885
 
 
 
 
11bfd4b
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import torch
import numpy as np
from kokoro import KModel, KPipeline

# Check if CUDA is available
CUDA_AVAILABLE = torch.cuda.is_available()

# Initialize the model
model = KModel().to('cuda' if CUDA_AVAILABLE else 'cpu').eval()

# Initialize pipelines for different language codes (using 'a' for English)
pipelines = {'a': KPipeline(lang_code='a', model=False)}

# Custom pronunciation for "kokoro"
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'

def text_to_audio(text, speed=1.0):
    """Convert text to audio using Kokoro model"""
    if not text:
        return None
    
    pipeline = pipelines['a']  # Use English pipeline
    voice = "af_heart"  # Default voice (US English, female, Heart)
    
    # Process the text
    pack = pipeline.load_voice(voice)
    
    for _, ps, _ in pipeline(text, voice, speed):
        ref_s = pack[len(ps)-1]
        
        # Generate audio
        try:
            audio = model(ps, ref_s, speed)
        except Exception as e:
            raise gr.Error(f"Error generating audio: {str(e)}")
        
        # Return the audio with 24kHz sample rate
        return 24000, audio.numpy()
    
    return None

# Create Gradio interface
with gr.Blocks(title="Kokoro Text-to-Audio") as app:
    gr.Markdown("# 🎵 Kokoro Text-to-Audio Converter")
    gr.Markdown("Convert text to speech using the Kokoro-82M model")
    
    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(
                label="Enter your text",
                placeholder="Type something to convert to audio...",
                lines=5
            )
            speed_slider = gr.Slider(
                minimum=0.5,
                maximum=2.0,
                value=1.0,
                step=0.1,
                label="Speech Speed"
            )
            submit_btn = gr.Button("Generate Audio")
        
        with gr.Column():
            audio_output = gr.Audio(label="Generated Audio", type="numpy")
    
    submit_btn.click(
        fn=text_to_audio,
        inputs=[text_input, speed_slider],
        outputs=[audio_output]
    )
    
    gr.Markdown("### Usage Tips")
    gr.Markdown("- For best results, keep your text reasonably short (up to ~500 characters)")
    gr.Markdown("- Adjust the speed slider to modify the pace of speech")
    gr.Markdown("- The model may take a moment to load on first use")

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