File size: 7,784 Bytes
a818502
 
 
 
dfe4207
a818502
1d84a6c
 
 
dfe4207
 
 
 
 
 
 
 
 
f8386f3
dfe4207
a818502
 
1d84a6c
a818502
1d84a6c
f8386f3
dfe4207
a818502
 
 
 
 
 
 
1d84a6c
 
43d1773
 
1d84a6c
 
 
a818502
f8386f3
a818502
 
 
f8386f3
a818502
 
 
 
 
 
 
 
1d84a6c
a818502
1d84a6c
a818502
 
 
 
 
1d84a6c
 
 
a818502
 
 
 
 
1d84a6c
 
 
f8386f3
1d84a6c
a818502
f8386f3
a818502
 
f8386f3
 
 
 
 
 
 
 
 
a818502
 
 
 
 
 
 
 
f8386f3
a818502
 
 
 
 
 
 
 
 
f8386f3
a818502
1d84a6c
 
be6bdf9
1d84a6c
43d1773
1d84a6c
be6bdf9
a818502
be6bdf9
a818502
 
 
be6bdf9
a818502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8386f3
a818502
 
f8386f3
a818502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be6bdf9
a818502
be6bdf9
a818502
 
 
 
be6bdf9
a818502
 
 
 
 
 
 
 
 
 
 
 
 
1d84a6c
a818502
f8386f3
a818502
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import spaces
import os
import sys
import tempfile
import subprocess
import numpy as np
import gradio as gr
import soundfile as sf

# Clone NeuTTS-Air repository if not present
NEUTTS_DIR = "neutts-air"
if not os.path.exists(NEUTTS_DIR):
    try:
        subprocess.run(["git", "clone", "https://github.com/neuphonic/neutts-air.git", NEUTTS_DIR], check=True)
        print(f"Successfully cloned NeuTTS-Air to {NEUTTS_DIR}")
    except Exception as e:
        print(f"Warning: Could not clone NeuTTS-Air: {e}")

# Add NeuTTS-Air to path - aligned with official implementation
sys.path.append(NEUTTS_DIR)

# Global variables for lazy loading
kokoro_pipe = None
neutts_model = None

# NeuTTS-Air configuration - aligned with official neutts-air/app.py
SAMPLES_PATH = os.path.join(os.getcwd(), NEUTTS_DIR, "samples")
DEFAULT_REF_TEXT = "So I'm live on radio. And I say, well, my dear friend James here clearly, and the whole room just froze. Turns out I'd completely misspoken and mentioned our other friend."
DEFAULT_REF_PATH = os.path.join(SAMPLES_PATH, "dave.wav")
DEFAULT_GEN_TEXT = "My name is Dave, and um, I'm from London."

# ------------------------------------------------------------------
# 1. Lazy loaders
# ------------------------------------------------------------------
def load_kokoro():
    global kokoro_pipe
    if kokoro_pipe is None:
        from kokoro import KPipeline
        kokoro_pipe = KPipeline(lang_code='a')
    return kokoro_pipe

def load_neutts():
    """Initialize NeuTTS-Air model - aligned with official implementation"""
    global neutts_model
    if neutts_model is None:
        from neuttsair.neutts import NeuTTSAir
        # Configuration matches official neutts-air/app.py lines 14-19
        neutts_model = NeuTTSAir(
            backbone_repo="neuphonic/neutts-air",
            backbone_device="cuda",
            codec_repo="neuphonic/neucodec",
            codec_device="cuda"
        )
    return neutts_model

# ------------------------------------------------------------------
# 2. Kokoro TTS inference
# ------------------------------------------------------------------
@spaces.GPU()
def kokoro_infer(text, voice, speed):
    if not text.strip():
        raise gr.Error("Please enter some text.")
    
    pipe = load_kokoro()
    generator = pipe(text, voice=voice, speed=speed)
    for gs, ps, audio in generator:
        # Save to temporary file
        fd, tmp = tempfile.mkstemp(suffix='.wav')
        os.close(fd)
        sf.write(tmp, audio, 24000)
        return tmp
    raise RuntimeError("Kokoro generation failed")

# ------------------------------------------------------------------
# 3. NeuTTS-Air inference - aligned with official implementation
# ------------------------------------------------------------------
@spaces.GPU()
def neutts_infer(ref_text: str, ref_audio_path: str, gen_text: str) -> tuple[int, np.ndarray]:
    """
    Generates speech using NeuTTS-Air given a reference audio and text, and new text to synthesize.
    
    Implementation aligned with official neutts-air/app.py lines 22-45.
    
    Args:
        ref_text (str): The text corresponding to the reference audio.
        ref_audio_path (str): The file path to the reference audio.
        gen_text (str): The new text to synthesize.
    Returns:
        tuple [int, np.ndarray]: A tuple containing the sample rate (24000) and the generated audio waveform as a numpy array.
    """
    if not gen_text.strip():
        raise gr.Error("Please enter text to generate.")
    if not ref_audio_path:
        raise gr.Error("Please provide reference audio.")
    if not ref_text.strip():
        raise gr.Error("Please provide reference text.")
    
    # Info messages aligned with official implementation
    gr.Info("Starting inference request!")
    gr.Info("Encoding reference...")
    
    tts = load_neutts()
    ref_codes = tts.encode_reference(ref_audio_path)
    
    gr.Info(f"Generating audio for input text: {gen_text}")
    wav = tts.infer(gen_text, ref_codes, ref_text)
    
    # Return format aligned with official implementation (line 45)
    return (24_000, wav)

# ------------------------------------------------------------------
# 4. Gradio UI with model selection
# ------------------------------------------------------------------
css = """footer {visibility: hidden}"""

with gr.Blocks(css=css, title="Text2Audio - Kokoro & NeuTTS-Air") as demo:
    gr.Markdown("# 🎙️ Text-to-Audio Generation")
    gr.Markdown("Choose between **Kokoro TTS** (fast English TTS) or **NeuTTS-Air** (voice cloning with reference audio)")
    
    # Model selection
    model_choice = gr.Radio(
        choices=["Kokoro TTS", "NeuTTS-Air"],
        value="Kokoro TTS",
        label="Select TTS Engine",
        interactive=True
    )
    
    # Kokoro TTS Interface
    with gr.Group(visible=True) as kokoro_group:
        gr.Markdown("### 🎧 Kokoro TTS Settings")
        with gr.Row():
            with gr.Column():
                kokoro_voice = gr.Dropdown(
                    label="Voice",
                    choices=['af_heart', 'af_sky', 'af_mist', 'af_dusk'],
                    value='af_heart'
                )
                kokoro_speed = gr.Slider(0.5, 2.0, 1.0, step=0.1, label="Speed")
            
            with gr.Column(scale=3):
                kokoro_text = gr.Textbox(
                    label="Text to speak",
                    placeholder="Type or paste text here…",
                    lines=6,
                    max_lines=12
                )
                kokoro_btn = gr.Button("🎧 Synthesise with Kokoro", variant="primary")
                kokoro_audio_out = gr.Audio(label="Generated speech", type="filepath")
        
        gr.Markdown("**Kokoro** – fast, high-quality English TTS. Audio is returned as 24 kHz WAV.")
    
    # NeuTTS-Air Interface - aligned with official implementation
    with gr.Group(visible=False) as neutts_group:
        gr.Markdown("### ☁️ NeuTTS-Air Settings")
        # Interface structure aligned with official neutts-air/app.py lines 47-57
        neutts_ref_text = gr.Textbox(
            label="Reference Text",
            value=DEFAULT_REF_TEXT,
            lines=3
        )
        neutts_ref_audio = gr.Audio(
            type="filepath",
            label="Reference Audio",
            value=DEFAULT_REF_PATH if os.path.exists(DEFAULT_REF_PATH) else None
        )
        neutts_gen_text = gr.Textbox(
            label="Text to Generate",
            value=DEFAULT_GEN_TEXT,
            lines=3
        )
        neutts_btn = gr.Button("☁️ Generate with NeuTTS-Air", variant="primary")
        neutts_audio_out = gr.Audio(label="Generated Speech", type="numpy")
        
        gr.Markdown("**NeuTTS-Air** – Upload a reference audio sample, provide the reference text, and enter new text to synthesize.")
    
    # Event handlers
    def toggle_interface(choice):
        if choice == "Kokoro TTS":
            return gr.update(visible=True), gr.update(visible=False)
        else:
            return gr.update(visible=False), gr.update(visible=True)
    
    model_choice.change(
        fn=toggle_interface,
        inputs=[model_choice],
        outputs=[kokoro_group, neutts_group]
    )
    
    kokoro_btn.click(
        kokoro_infer,
        inputs=[kokoro_text, kokoro_voice, kokoro_speed],
        outputs=kokoro_audio_out
    )
    
    neutts_btn.click(
        neutts_infer,
        inputs=[neutts_ref_text, neutts_ref_audio, neutts_gen_text],
        outputs=neutts_audio_out
    )

if __name__ == "__main__":
    # Launch configuration aligned with official implementation (line 60)
    demo.launch(allowed_paths=[SAMPLES_PATH] if os.path.exists(SAMPLES_PATH) else None, mcp_server=True, inbrowser=True)