File size: 11,668 Bytes
d6dc92d
 
 
 
 
cc372ac
5da0109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc372ac
02b5975
 
 
5da0109
02b5975
 
5da0109
 
cc372ac
 
 
 
 
 
 
 
5da0109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d24817e
5da0109
 
d24817e
5da0109
 
 
4388640
5da0109
 
 
 
 
cc372ac
5da0109
d24817e
5da0109
 
 
 
 
 
 
 
 
 
d24817e
 
5da0109
 
 
 
 
 
4388640
5da0109
 
 
 
 
4388640
5da0109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4388640
5da0109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4388640
5da0109
 
 
 
 
 
4388640
5da0109
 
 
 
 
d24817e
 
5da0109
 
 
 
 
 
 
 
4388640
5da0109
 
 
 
 
 
 
 
4388640
 
5da0109
02b5975
 
 
 
 
5da0109
906428b
 
5da0109
 
 
 
 
d24817e
906428b
5da0109
 
 
 
 
906428b
 
5da0109
 
 
 
 
906428b
 
5da0109
 
02b5975
 
 
5da0109
 
906428b
 
5da0109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906428b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5da0109
 
 
 
906428b
 
 
 
 
 
 
 
 
 
5da0109
 
 
 
906428b
 
 
 
d6dc92d
5da0109
 
 
 
906428b
 
 
 
 
d6dc92d
5da0109
 
 
 
 
 
906428b
 
 
 
 
 
5da0109
 
 
 
 
 
 
 
 
 
906428b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5da0109
 
906428b
 
 
d6dc92d
5da0109
 
 
 
 
 
 
 
 
 
 
 
 
906428b
 
 
 
 
 
5da0109
 
 
 
 
 
 
 
 
 
906428b
 
02b5975
 
 
 
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#
# SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
# SPDX-License-Identifier: Apache-2.0
#

import atexit
import math
import torch
import gradio as gr
from config import (
    AVAILABLE_VOICES,
    DEFAULT_VOICE,
    DEFAULT_MODEL_VARIANT,
    DEFAULT_TEMPERATURE,
    DEFAULT_LSD_DECODE_STEPS,
    DEFAULT_EOS_THRESHOLD,
    DEFAULT_NOISE_CLAMP,
    DEFAULT_FRAMES_AFTER_EOS,
    MAXIMUM_INPUT_LENGTH,
    VOICE_MODE_PRESET,
    VOICE_MODE_CLONE,
    EXAMPLE_PROMPTS,
    ACCELERATOR_ENABLED,
    PYTORCH_COMPUTATION_THREADS,
    PYTORCH_INTEROP_THREADS
)
torch.set_num_threads(PYTORCH_COMPUTATION_THREADS)
torch.set_num_interop_threads(PYTORCH_INTEROP_THREADS)
from src.core.authentication import authenticate_huggingface
authenticate_huggingface()
if ACCELERATOR_ENABLED:
    from src.accelerator.client import start_accelerator_daemon, stop_accelerator_daemon
    accelerator_started = start_accelerator_daemon()
    if accelerator_started:
        print("Accelerator daemon started successfully", flush=True)
    else:
        print("Accelerator daemon not available, using Python fallback", flush=True)
    atexit.register(stop_accelerator_daemon)
from src.core.memory import start_background_cleanup_thread
start_background_cleanup_thread()
from src.generation.handler import (
    perform_speech_generation,
    request_generation_stop
)
from src.ui.state import (
    check_generate_button_state,
    calculate_character_count_display,
    determine_clear_button_visibility,
    update_voice_mode_visibility
)
from src.ui.handlers import (
    switch_to_generating_state,
    switch_to_idle_state,
    perform_clear_action,
    create_example_handler,
    format_example_button_label
)
from assets.css.styles import CSS
from assets.static.title import TITLE
from assets.static.header import HEADER
from assets.static.footer import FOOTER
from assets.static.sidebar import SIDEBAR

with gr.Blocks(css=CSS, fill_height=False, fill_width=True) as app:
    ui_state = gr.State({"generating": False})

    with gr.Sidebar():
        gr.HTML(SIDEBAR())

    with gr.Column(elem_classes="header-section"):
        gr.HTML(TITLE())
        gr.HTML(HEADER())

    with gr.Row():
        with gr.Column():
            audio_output_component = gr.Audio(
                label="Generated Speech Output",
                type="filepath",
                interactive=False
            )

            with gr.Accordion("Voice Selection", open=True):
                voice_mode_radio = gr.Radio(
                    label="Voice Mode",
                    choices=[
                        VOICE_MODE_PRESET,
                        VOICE_MODE_CLONE
                    ],
                    value=VOICE_MODE_PRESET,
                    info="Choose between preset voices or clone a voice from uploaded audio",
                    elem_id="voice-mode"
                )

                with gr.Column(visible=True) as preset_voice_container:
                    voice_preset_dropdown = gr.Dropdown(
                        label="Select Preset Voice",
                        choices=AVAILABLE_VOICES,
                        value=DEFAULT_VOICE
                    )

                with gr.Column(visible=False) as clone_voice_container:
                    voice_clone_audio_input = gr.Audio(
                        label="Upload Audio for Voice Cloning",
                        type="filepath"
                    )

            with gr.Accordion("Model Parameters", open=False):
                with gr.Row():
                    temperature_slider = gr.Slider(
                        label="Temperature",
                        minimum=0.1,
                        maximum=2.0,
                        step=0.05,
                        value=DEFAULT_TEMPERATURE,
                        info="Higher values produce more expressive speech"
                    )
                    
                    lsd_decode_steps_slider = gr.Slider(
                        label="LSD Decode Steps",
                        minimum=1,
                        maximum=20,
                        step=1,
                        value=DEFAULT_LSD_DECODE_STEPS,
                        info="More steps may improve quality but slower"
                    )

                with gr.Row():
                    noise_clamp_slider = gr.Slider(
                        label="Noise Clamp",
                        minimum=0.0,
                        maximum=2.0,
                        step=0.05,
                        value=DEFAULT_NOISE_CLAMP,
                        info="Maximum noise sampling value (0 = disabled)"
                    )
                    
                    eos_threshold_slider = gr.Slider(
                        label="End of Sequence Threshold",
                        minimum=-10.0,
                        maximum=0.0,
                        step=0.25,
                        value=DEFAULT_EOS_THRESHOLD,
                        info="Smaller values cause earlier completion"
                    )

            with gr.Accordion("Advanced Settings", open=False):
                model_variant_textbox = gr.Textbox(
                    label="Model Variant Identifier",
                    value=DEFAULT_MODEL_VARIANT,
                    info="Model signature for generation"
                )

                with gr.Row():
                    enable_custom_frames_checkbox = gr.Checkbox(
                        label="Enable Custom Frames After EOS",
                        value=False,
                        info="Manually control post-EOS frame generation"
                    )
                    
                    frames_after_eos_slider = gr.Slider(
                        label="Frames After EOS",
                        minimum=0,
                        maximum=100,
                        step=1,
                        value=DEFAULT_FRAMES_AFTER_EOS,
                        info="Additional frames after end-of-sequence (80ms per frame)"
                    )

        with gr.Column(scale=1):
            text_input_component = gr.Textbox(
                label="Prompt",
                placeholder="Enter the text you want to convert to speech...",
                lines=2,
                max_lines=20,
                max_length=MAXIMUM_INPUT_LENGTH,
                autoscroll=True
            )

            character_count_display = gr.HTML(
                f"""
                <div class="character-count">
                    <span>0 / {MAXIMUM_INPUT_LENGTH}</span>
                </div>
                """,
                visible=False
            )

            generate_button = gr.Button(
                "Generate",
                variant="primary",
                size="lg",
                interactive=False
            )

            stop_button = gr.Button(
                "Stop",
                variant="stop",
                size="lg",
                visible=False
            )

            clear_button = gr.Button(
                "Clear",
                variant="secondary",
                size="lg",
                visible=False
            )

            gr.HTML(
                """
                <div class="example-prompts">
                    <h3>Example Prompts</h3>
                    <p>Click any example to generate speech with its assigned voice</p>
                </div>
                """
            )

            example_buttons_list = []
            num_examples = len(EXAMPLE_PROMPTS)
            examples_per_row = 2
            num_rows = math.ceil(num_examples / examples_per_row)

            for row_idx in range(num_rows):
                with gr.Row():
                    start_idx = row_idx * examples_per_row
                    end_idx = min(start_idx + examples_per_row, num_examples)
                    for i in range(start_idx, end_idx):
                        btn = gr.Button(
                            format_example_button_label(
                                EXAMPLE_PROMPTS[i]["text"],
                                EXAMPLE_PROMPTS[i]["voice"]
                            ),
                            size="sm",
                            variant="secondary"
                        )
                        example_buttons_list.append(btn)

    gr.HTML(FOOTER())

    generation_inputs = [
        text_input_component,
        voice_mode_radio,
        voice_preset_dropdown,
        voice_clone_audio_input,
        model_variant_textbox,
        lsd_decode_steps_slider,
        temperature_slider,
        noise_clamp_slider,
        eos_threshold_slider,
        frames_after_eos_slider,
        enable_custom_frames_checkbox
    ]

    voice_mode_radio.change(
        fn=update_voice_mode_visibility,
        inputs=[voice_mode_radio],
        outputs=[
            preset_voice_container,
            clone_voice_container
        ]
    )

    text_input_component.change(
        fn=calculate_character_count_display,
        inputs=[text_input_component],
        outputs=[character_count_display]
    )

    text_input_component.change(
        fn=check_generate_button_state,
        inputs=[
            text_input_component,
            ui_state
        ],
        outputs=[generate_button]
    )

    text_input_component.change(
        fn=determine_clear_button_visibility,
        inputs=[
            text_input_component,
            ui_state
        ],
        outputs=[clear_button]
    )

    generate_button.click(
        fn=switch_to_generating_state,
        inputs=[ui_state],
        outputs=[
            generate_button,
            stop_button,
            clear_button,
            ui_state
        ]
    ).then(
        fn=perform_speech_generation,
        inputs=generation_inputs,
        outputs=[audio_output_component]
    ).then(
        fn=switch_to_idle_state,
        inputs=[
            text_input_component,
            ui_state
        ],
        outputs=[
            generate_button,
            stop_button,
            clear_button,
            ui_state
        ]
    )

    stop_button.click(
        fn=request_generation_stop,
        outputs=[stop_button]
    )

    clear_button.click(
        fn=perform_clear_action,
        outputs=[
            text_input_component,
            audio_output_component,
            clear_button,
            voice_mode_radio,
            voice_preset_dropdown,
            voice_clone_audio_input
        ]
    )

    for button_index, example_button in enumerate(example_buttons_list):
        example_text = EXAMPLE_PROMPTS[button_index]["text"]
        example_voice = EXAMPLE_PROMPTS[button_index]["voice"]

        example_button.click(
            fn=switch_to_generating_state,
            inputs=[ui_state],
            outputs=[
                generate_button,
                stop_button,
                clear_button,
                ui_state
            ]
        ).then(
            fn=create_example_handler(example_text, example_voice),
            outputs=[
                text_input_component,
                voice_mode_radio,
                voice_preset_dropdown
            ]
        ).then(
            fn=perform_speech_generation,
            inputs=generation_inputs,
            outputs=[audio_output_component]
        ).then(
            fn=switch_to_idle_state,
            inputs=[
                text_input_component,
                ui_state
            ],
            outputs=[
                generate_button,
                stop_button,
                clear_button,
                ui_state
            ]
        )

app.launch(
    server_name="0.0.0.0",
    max_file_size="1mb"
)