File size: 10,004 Bytes
05b45a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import shutil

import gradio as gr

from . import api, files


def setup_event_handlers(components: dict, disable_local_saving: bool = False):
    """Set up all event handlers for the UI components."""

    def refresh_status():
        try:
            is_available, voices = api.check_api_status()
            status = "Available" if is_available else "Waiting for Service..."

            if is_available and voices:
                # Preserve current voice selection if it exists and is still valid
                current_voice = components["model"]["voice"].value
                default_voice = current_voice if current_voice in voices else voices[0]
                return [
                    gr.update(
                        value=f"🔄 TTS Service: {status}",
                        interactive=True,
                        variant="secondary",
                    ),
                    gr.update(choices=voices, value=default_voice),
                ]
            return [
                gr.update(
                    value=f"⌛ TTS Service: {status}",
                    interactive=True,
                    variant="secondary",
                ),
                gr.update(choices=[], value=None),
            ]
        except Exception as e:
            print(f"Error in refresh status: {str(e)}")
            return [
                gr.update(
                    value="❌ TTS Service: Connection Error",
                    interactive=True,
                    variant="secondary",
                ),
                gr.update(choices=[], value=None),
            ]

    def handle_file_select(filename):
        if filename:
            try:
                text = files.read_text_file(filename)
                if text:
                    preview = text[:200] + "..." if len(text) > 200 else text
                    return gr.update(value=preview)
            except Exception as e:
                print(f"Error reading file: {e}")
        return gr.update(value="")

    def handle_file_upload(file):
        if file is None:
            return (
                ""
                if disable_local_saving
                else [gr.update(choices=files.list_input_files())]
            )

        try:
            # Read the file content
            with open(file.name, "r", encoding="utf-8") as f:
                text_content = f.read()

            if disable_local_saving:
                # When saving is disabled, put content directly in text input
                # Normalize whitespace by replacing newlines with spaces
                normalized_text = " ".join(text_content.split())
                return normalized_text
            else:
                # When saving is enabled, save file and update dropdown
                filename = os.path.basename(file.name)
                target_path = os.path.join(files.INPUTS_DIR, filename)

                # Handle duplicate filenames
                base, ext = os.path.splitext(filename)
                counter = 1
                while os.path.exists(target_path):
                    new_name = f"{base}_{counter}{ext}"
                    target_path = os.path.join(files.INPUTS_DIR, new_name)
                    counter += 1

                shutil.copy2(file.name, target_path)
                return [gr.update(choices=files.list_input_files())]

        except Exception as e:
            print(f"Error handling file: {e}")
            return (
                ""
                if disable_local_saving
                else [gr.update(choices=files.list_input_files())]
            )

    def generate_from_text(text, voice, format, speed):
        """Generate speech from direct text input"""
        is_available, _ = api.check_api_status()
        if not is_available:
            gr.Warning("TTS Service is currently unavailable")
            return [None, gr.update(choices=files.list_output_files())]

        if not text or not text.strip():
            gr.Warning("Please enter text in the input box")
            return [None, gr.update(choices=files.list_output_files())]

        # Only save text if local saving is enabled
        if not disable_local_saving:
            files.save_text(text)

        result = api.text_to_speech(text, voice, format, speed)
        if result is None:
            gr.Warning("Failed to generate speech. Please try again.")
            return [None, gr.update(choices=files.list_output_files())]

        return [
            result,
            gr.update(
                choices=files.list_output_files(), value=os.path.basename(result)
            ),
        ]

    def generate_from_file(selected_file, voice, format, speed):
        """Generate speech from selected file"""
        is_available, _ = api.check_api_status()
        if not is_available:
            gr.Warning("TTS Service is currently unavailable")
            return [None, gr.update(choices=files.list_output_files())]

        if not selected_file:
            gr.Warning("Please select a file")
            return [None, gr.update(choices=files.list_output_files())]

        text = files.read_text_file(selected_file)
        result = api.text_to_speech(text, voice, format, speed)
        if result is None:
            gr.Warning("Failed to generate speech. Please try again.")
            return [None, gr.update(choices=files.list_output_files())]

        return [
            result,
            gr.update(
                choices=files.list_output_files(), value=os.path.basename(result)
            ),
        ]

    def play_selected(file_path):
        if file_path and os.path.exists(file_path):
            return gr.update(value=file_path, visible=True)
        return gr.update(visible=False)

    def clear_files(voice, format, speed):
        """Delete all input files and clear UI components while preserving model settings"""
        files.delete_all_input_files()
        return [
            gr.update(value=None, choices=[]),  # file_select
            None,  # file_upload
            gr.update(value=""),  # file_preview
            None,  # audio_output
            gr.update(choices=files.list_output_files()),  # output_files
            gr.update(value=voice),  # voice
            gr.update(value=format),  # format
            gr.update(value=speed),  # speed
        ]

    def clear_outputs():
        """Delete all output audio files and clear audio components"""
        files.delete_all_output_files()
        return [
            None,  # audio_output
            gr.update(choices=[], value=None),  # output_files
            gr.update(visible=False),  # selected_audio
        ]

    # Connect event handlers
    components["model"]["status_btn"].click(
        fn=refresh_status,
        outputs=[components["model"]["status_btn"], components["model"]["voice"]],
    )

    # Connect text submit button (always present)
    components["input"]["text_submit"].click(
        fn=generate_from_text,
        inputs=[
            components["input"]["text_input"],
            components["model"]["voice"],
            components["model"]["format"],
            components["model"]["speed"],
        ],
        outputs=[
            components["output"]["audio_output"],
            components["output"]["output_files"],
        ],
    )

    # Only connect file-related handlers if components exist
    if components["input"]["file_select"] is not None:
        components["input"]["file_select"].change(
            fn=handle_file_select,
            inputs=[components["input"]["file_select"]],
            outputs=[components["input"]["file_preview"]],
        )

    if components["input"]["file_upload"] is not None:
        # File upload handler - output depends on disable_local_saving
        components["input"]["file_upload"].upload(
            fn=handle_file_upload,
            inputs=[components["input"]["file_upload"]],
            outputs=[
                components["input"]["text_input"]
                if disable_local_saving
                else components["input"]["file_select"]
            ],
        )

    if components["output"]["play_btn"] is not None:
        components["output"]["play_btn"].click(
            fn=play_selected,
            inputs=[components["output"]["output_files"]],
            outputs=[components["output"]["selected_audio"]],
        )

    if components["input"]["clear_files"] is not None:
        components["input"]["clear_files"].click(
            fn=clear_files,
            inputs=[
                components["model"]["voice"],
                components["model"]["format"],
                components["model"]["speed"],
            ],
            outputs=[
                components["input"]["file_select"],
                components["input"]["file_upload"],
                components["input"]["file_preview"],
                components["output"]["audio_output"],
                components["output"]["output_files"],
                components["model"]["voice"],
                components["model"]["format"],
                components["model"]["speed"],
            ],
        )

    if components["output"]["clear_outputs"] is not None:
        components["output"]["clear_outputs"].click(
            fn=clear_outputs,
            outputs=[
                components["output"]["audio_output"],
                components["output"]["output_files"],
                components["output"]["selected_audio"],
            ],
        )

    if components["input"]["file_submit"] is not None:
        components["input"]["file_submit"].click(
            fn=generate_from_file,
            inputs=[
                components["input"]["file_select"],
                components["model"]["voice"],
                components["model"]["format"],
                components["model"]["speed"],
            ],
            outputs=[
                components["output"]["audio_output"],
                components["output"]["output_files"],
            ],
        )