File size: 6,753 Bytes
9ab845a
1445f36
 
 
 
 
 
 
22ff792
 
 
 
 
1445f36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ab845a
1445f36
 
 
 
 
 
 
 
9ab845a
1445f36
 
 
9ab845a
1445f36
 
 
9ab845a
1445f36
 
 
 
 
 
 
 
 
 
9ab845a
1445f36
 
9ab845a
1445f36
 
 
 
 
 
 
 
 
 
 
 
9ab845a
 
1445f36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ab845a
 
1445f36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ab845a
1445f36
 
 
9ab845a
 
1445f36
 
 
9ab845a
1445f36
 
 
 
 
9ab845a
1445f36
 
 
 
 
 
 
9ab845a
1445f36
 
 
 
 
9ab845a
1445f36
9ab845a
1445f36
 
9ab845a
 
 
 
 
1445f36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ab845a
1445f36
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

import gradio as gr
import os
import re
import uuid
import scipy.io.wavfile
import torch
from pocket_tts import TTSModel
#for voice clone
from huggingface_hub import login
hf_token = os.getenv("HF_TOKEN")
if hf_token:
    login(token=hf_token)

print("Loading TTS Model...")
try:
    tts_model = TTSModel.load_model()
    print("Model loaded successfully.")
except Exception as e:
    print(f"Error loading model: {e}")

def get_tts_file_name(text, language="en"):
    temp_audio_dir = "./ai_tts_voice/"
    os.makedirs(temp_audio_dir, exist_ok=True)

    clean = re.sub(r'[^a-zA-Z\s]', '', text or "")
    clean = clean.lower().strip().replace(" ", "_")[:20] or "audio"

    uid = uuid.uuid4().hex[:8].upper()
    language = language.lower().strip()

    return os.path.join(
        temp_audio_dir,
        f"{clean}_{language}_{uid}.wav"
    )

DEFAULT_VOICES = [
    "alba", "marius", "javert", "jean",
    "fantine", "cosette", "eponine", "azelma"
]

def generate_speech(text, mode, preset_voice, clone_audio_path):
    if not text:
        raise gr.Error("Please enter text to generate speech.")

    state = None

    if mode == "Default Voices":
        print(f"Using preset voice: {preset_voice}")
        state = tts_model.get_state_for_audio_prompt(preset_voice)

    else:
        if not clone_audio_path:
            raise gr.Error("Please upload a reference audio file for cloning.")

        print(f"Cloning voice from: {clone_audio_path}")
        try:
            state = tts_model.get_state_for_audio_prompt(clone_audio_path)
        except Exception as e:
            error_msg = f"Error loading reference audio: {str(e)}. Please upload a valid WAV file."
            print(error_msg)
            raise gr.Error(error_msg)

    try:
        audio_tensor = tts_model.generate_audio(state, text)

        output_filename = get_tts_file_name(text)
        scipy.io.wavfile.write(output_filename, tts_model.sample_rate, audio_tensor.numpy())

        return output_filename
    except Exception as e:
        raise gr.Error(f"Generation failed: {str(e)}")

def toggle_inputs(mode):
    if mode == "Default Voices":
        return gr.update(visible=True), gr.update(visible=False)
    else:
        return gr.update(visible=False), gr.update(visible=True)


CUSTOM_CSS = """
.gradio-container {
    font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, sans-serif;
}
.header-container {
    text-align: center;
    margin-bottom: 20px;
}
.logo-img {
    margin: 0 auto;
    display: block;
    max-width: 100%;
    transition: transform 0.2s;
}
.logo-img:hover {
    transform: scale(1.02);
    opacity: 0.9;
}
.links-container a {
    text-decoration: none;
    color: #4a90e2;
    font-weight: 500;
}
.links-container a:hover {
    text-decoration: underline;
}
"""

HEADER_HTML = """
<div class="header-container" style="text-align:center;">

    <a href="https://kyutai.org/tts" target="_blank" title="Visit Kyutai TTS">
        <img src="https://raw.githubusercontent.com/kyutai-labs/pocket-tts/refs/heads/main/docs/logo.png"
             class="logo-img" width="200">
    </a>

    <div class="links-container"
         style="
            margin-top: 18px;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 14px;
            flex-wrap: wrap;
         ">

        <a href="https://github.com/kyutai-labs/pocket-tts"
           target="_blank"
           style="text-decoration:none;">
            🐱 GitHub Repository
        </a>

        <span style="color: gray;">|</span>

        <a href="https://huggingface.co/kyutai/pocket-tts"
           target="_blank"
           style="text-decoration:none;">
            🤗 Hugging Face Model Card
        </a>

        <span style="color: gray;">|</span>

        <a href="https://colab.research.google.com/github/NeuralFalconYT/Voice-Clone/blob/main/Pocket_TTS_Colab.ipynb"
           target="_blank"
           style="
                display: inline-flex;
                align-items: center;
           ">
            <img src="https://colab.research.google.com/assets/colab-badge.svg"
                 alt="Open in Colab"
                 height="26">
        </a>

    </div>

    <p style="font-size: 0.8em; color: gray; margin-top: 10px;">
        <i>Note: This is not an official demo from Kyutai Labs</i>
    </p>

</div>
"""



with gr.Blocks(theme='JohnSmith9982/small_and_pretty', css=CUSTOM_CSS) as demo:
    gr.HTML(HEADER_HTML)

    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(
                label="Text Input",
                placeholder="Hi, how are you?",
                lines=3,
                value="Hi, how are you?"
            )

            mode_radio = gr.Radio(
                choices=["Default Voices", "Voice Clone"],
                value="Default Voices",
                label="TTS Mode"
            )

            with gr.Group():
                dropdown_input = gr.Dropdown(
                    choices=DEFAULT_VOICES,
                    value="alba",
                    label="Select Voice",
                    visible=True
                )

                audio_upload = gr.Audio(
                    label="Upload Reference Audio (WAV recommended)",
                    type="filepath",
                    visible=False
                )

            generate_btn = gr.Button("Generate Audio", variant="primary")

            example_audio_url = "https://huggingface.co/kyutai/tts-voices/resolve/main/alba-mackenna/casual.wav"
            

        with gr.Column():
            output_audio = gr.Audio(label="Generated Speech", type="filepath")

    gr.Examples(
                examples=[
                    ["Hello, I am Fantine. Nice to meet you.", "Default Voices", "fantine", None],
                    ["I am Cosette, and the weather is lovely.", "Default Voices", "cosette", None],
                    ["Hey there, Eponine here.", "Default Voices", "eponine", None],
                    ["Greetings from Azelma.", "Default Voices", "azelma", None],
                    ["This is a voice cloning test using the uploaded reference audio.", "Voice Clone", None, example_audio_url],
                ],
                inputs=[text_input, mode_radio, dropdown_input, audio_upload],
                label="Click on an Example to Try"
            )
    mode_radio.change(
        fn=toggle_inputs,
        inputs=[mode_radio],
        outputs=[dropdown_input, audio_upload]
    )

    generate_btn.click(
        fn=generate_speech,
        inputs=[text_input, mode_radio, dropdown_input, audio_upload],
        outputs=[output_audio]
    )

if __name__ == "__main__":
    demo.queue().launch(share=False, debug=False)