File size: 13,774 Bytes
3ce989d
 
 
f5237ec
 
 
 
 
3ce989d
4ea25cd
136ff40
 
4ea25cd
fcb34bb
3ce989d
 
 
a807c4d
afdac46
 
 
 
 
fcb34bb
 
 
 
 
afdac46
 
fc05e1d
afdac46
 
 
 
2369fc5
a807c4d
3ce989d
 
ec0cbf8
3ce989d
ec0cbf8
 
 
3ce989d
 
ec0cbf8
fcb34bb
 
ec0cbf8
 
 
3ce989d
fc05e1d
3ce989d
fc05e1d
ec0cbf8
 
fc05e1d
 
adecb62
fc05e1d
ec0cbf8
3ce989d
fc05e1d
 
 
 
 
ec0cbf8
f5237ec
fc05e1d
 
 
 
 
 
 
 
 
 
 
ec0cbf8
fc05e1d
ec0cbf8
fc05e1d
 
 
 
 
 
ec0cbf8
adecb62
6506ee8
fcb34bb
 
 
ec0cbf8
 
 
fcb34bb
 
 
 
 
 
 
 
 
 
 
 
ec0cbf8
 
 
 
6506ee8
f5237ec
136ff40
fcb34bb
 
 
 
 
adecb62
4ea25cd
afdac46
4ea25cd
 
f5237ec
afdac46
f5237ec
4ea25cd
6506ee8
 
 
 
 
 
 
 
f5237ec
6506ee8
 
 
 
f5237ec
adecb62
3ce989d
ec0cbf8
 
afdac46
 
fcb34bb
 
 
 
 
afdac46
fcb34bb
afdac46
 
fcb34bb
 
 
afdac46
 
fcb34bb
 
 
 
 
afdac46
fcb34bb
 
afdac46
0dde48b
afdac46
0dde48b
afdac46
 
fcb34bb
 
afdac46
 
 
fcb34bb
 
 
6506ee8
4ea25cd
3ce989d
 
 
 
fcb34bb
3ce989d
 
fcb34bb
3ce989d
2369fc5
07101b1
 
 
 
 
 
60bcef1
afdac46
3ce989d
60bcef1
 
 
 
 
 
 
afdac46
6506ee8
 
 
 
 
 
afdac46
 
6506ee8
fcb34bb
 
6506ee8
 
 
 
3ce989d
f5237ec
60bcef1
3ce989d
60bcef1
6506ee8
 
e9bcee8
adecb62
afdac46
07101b1
 
ec0cbf8
afdac46
adecb62
3ce989d
6506ee8
4ea25cd
fcb34bb
 
 
 
 
 
 
 
 
 
afdac46
6506ee8
 
 
 
4ea25cd
 
fcb34bb
 
 
 
4ea25cd
 
96154e7
60bcef1
96154e7
 
 
 
3ce989d
fcb34bb
6506ee8
 
 
 
 
 
fcb34bb
6506ee8
 
fcb34bb
 
 
 
 
 
 
 
6506ee8
ec0cbf8
 
4ea25cd
ec0cbf8
6506ee8
ec0cbf8
6506ee8
ec0cbf8
fcb34bb
 
 
 
 
 
4ea25cd
 
afdac46
 
fcb34bb
 
 
 
 
 
 
 
 
 
 
afdac46
 
 
 
fcb34bb
 
 
 
 
 
 
 
 
 
 
afdac46
 
f5237ec
4ea25cd
afdac46
f5237ec
4ea25cd
 
afdac46
4ea25cd
 
3ce989d
 
afdac46
 
a7644f8
 
 
 
 
afdac46
a7644f8
 
 
 
 
afdac46
 
e9bcee8
3ce989d
 
 
e9bcee8
 
3ce989d
 
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
373
374
375
376
377
378
379
380
"""
app.py

Gradio UI for interacting with the Anthropic API, Hume TTS API, and ElevenLabs TTS API.

Users enter a prompt, which is processed using Claude by Anthropic to generate text.
The text is then converted into speech using both Hume and ElevenLabs TTS APIs.
Users can compare the outputs in an interactive UI.
"""

# Standard Library Imports
from concurrent.futures import ThreadPoolExecutor
import random
from typing import Union
# Third-Party Library Imports
import gradio as gr
# Local Application Imports
from src.config import logger
from src.constants import (
    OPTION_ONE, 
    OPTION_TWO,
    PROMPT_MAX_LENGTH, 
    PROMPT_MIN_LENGTH, 
    SAMPLE_PROMPTS,
    TROPHY_EMOJI,
    UNKNOWN_PROVIDER,
    VOTE_FOR_OPTION_ONE,
    VOTE_FOR_OPTION_TWO
)
from src.integrations import (
    AnthropicError,
    generate_text_with_claude, 
    text_to_speech_with_hume, 
    text_to_speech_with_elevenlabs
)
from src.theme import CustomTheme
from src.utils import truncate_text, validate_prompt_length


def validate_and_generate_text(prompt: str) -> tuple[Union[str, gr.update], gr.update]:
    """
    Validates the prompt before generating text.
    - If valid, returns the generated text and keeps the button disabled.
    - If invalid, returns an error message and re-enables the button.

    Args:
        prompt (str): The user-provided text prompt.

    Returns:
        tuple[Union[str, gr.update], gr.update]:
            - The generated text or an error message.
            - The updated state of the "Generate" button.
    """
    # Local prompt validation
    try:
        validate_prompt_length(prompt, PROMPT_MAX_LENGTH, PROMPT_MIN_LENGTH)
    except ValueError as ve:
        logger.warning(f'Validation error: {ve}')
        # Show validation error to user, re-enable the "Generate" button
        return str(ve), gr.update(interactive=True)

    # Call the Anthropic API to generate text
    try:
        generated_text = generate_text_with_claude(prompt)

        # Optionally handle empty or unusual responses
        if not generated_text.strip():
            logger.warning("Anthropic API returned an empty response.")
            return "Error: Anthropic API returned an empty response.", gr.update(interactive=True)

        logger.info(f'Generated text ({len(generated_text)} characters).')
        return gr.update(value=generated_text), gr.update(interactive=False)

    # Handle Anthropic-specific errors
    except AnthropicError as ae:
        # You can log the internal error details
        logger.error(f'AnthropicError while generating text: {str(ae)}')
        # Return an error message about Anthropic API failing to generate text, re-enable the "Generate" button
        return (
            "Error: There was an issue communicating with the Anthropic API. Please try again later.",
            gr.update(interactive=True),
        )

    # Catch any other unexpected exceptions
    except Exception as e:
        logger.error(f'Unexpected error while generating text: {e}')
        # Return a generic catch-all error message, re-enable the "Generate" button
        return (
            "Error: Failed to generate text. Please try again.",
            gr.update(interactive=True),
        )



def text_to_speech(prompt: str, generated_text: str) -> tuple[gr.update, gr.update, dict, str | None]:
    """
    Converts generated text to speech using Hume AI and ElevenLabs APIs.
    
    If the generated text is invalid (empty or an error message), this function
    does nothing and returns `None` values to prevent TTS from running.

    Args:
        prompt (str): The original user-provided prompt.
        generated_text (str): The generated text that will be converted to speech.

    Returns:
        tuple[gr.update, gr.update, dict, Union[str, None]]:
            - `gr.update(value=option_1_audio, autoplay=True)`: Updates the first audio player.
            - `gr.update(value=option_2_audio)`: Updates the second audio player.
            - `options_map`: A dictionary mapping OPTION_ONE and OPTION_TWO to their providers.
            - `option_2_audio`: The second audio file path or `None` if an error occurs.
    """
    if not generated_text or generated_text.startswith("Error:"):
        logger.warning("Skipping TTS generation due to invalid text.")
        return gr.update(value=None), gr.update(value=None), {}, None  # Return empty updates

    try:
        # Generate TTS output in parallel
        with ThreadPoolExecutor(max_workers=2) as executor:
            future_hume = executor.submit(text_to_speech_with_hume, prompt, generated_text)
            future_elevenlabs = executor.submit(text_to_speech_with_elevenlabs, generated_text)
            
            hume_audio = future_hume.result()
            elevenlabs_audio = future_elevenlabs.result()

        logger.info(
            f'TTS generated: Hume={len(hume_audio)} bytes, ElevenLabs={len(elevenlabs_audio)} bytes'
        )

        # Randomize audio order
        options = [(hume_audio, 'Hume AI'), (elevenlabs_audio, 'ElevenLabs')]
        random.shuffle(options)

        option_1_audio = options[0][0]
        option_2_audio = options[1][0]

        option_1_provider = options[0][1]
        option_2_provider = options[1][1]

        options_map = { OPTION_ONE: option_1_provider, OPTION_TWO: option_2_provider }

        return (
            gr.update(value=option_1_audio, autoplay=True), # Set option 1 audio
            gr.update(value=option_2_audio), # Option 2 audio
            options_map, # Set option mapping state
            option_2_audio # Set option 2 audio state
        )

    except Exception as e:
        logger.error(f'Unexpected error during TTS generation: {e}')
        return gr.update(), gr.update(), {}, None


def vote(
    vote_submitted: bool, 
    option_mapping: dict, 
    selected_button: str
) -> tuple[bool, gr.update, gr.update, gr.update]:
    """
    Handles user voting and updates the UI to reflect the selected choice.

    Args:
        vote_submitted (bool): Indicates if a vote has already been submitted.
        option_mapping (dict): Maps "Option 1" and "Option 2" to their respective TTS providers.
        selected_button (str): The button that was clicked by the user.

    Returns:
        tuple[bool, gr.update, gr.update, gr.update]:
            - `True`: Indicates the vote has been submitted.
            - `gr.update`: Updates the selected vote button.
            - `gr.update`: Updates the unselected vote button.
            - `gr.update(interactive=True)`: Enables the "Generate" button after voting.
    """
    if not option_mapping or vote_submitted:
        return True, gr.update(), gr.update(), gr.update()  # No updates if mapping is missing

    # Determine selected option
    is_option_1 = selected_button == VOTE_FOR_OPTION_ONE
    selected_option, other_option = (OPTION_ONE, OPTION_TWO) if is_option_1 else (OPTION_TWO, OPTION_ONE)

    # Get provider names
    selected_provider = option_mapping.get(selected_option, UNKNOWN_PROVIDER)
    other_provider = option_mapping.get(other_option, UNKNOWN_PROVIDER)

    # Return updated button states
    return (
        True,
        gr.update(value=f'{selected_provider} {TROPHY_EMOJI}', variant='primary') if is_option_1 else gr.update(value=other_provider, variant='secondary'),
        gr.update(value=other_provider, variant='secondary') if is_option_1 else gr.update(value=f'{selected_provider} {TROPHY_EMOJI}', variant='primary'),
        gr.update(interactive=True, variant='primary')
    )


def build_gradio_interface() -> gr.Blocks:
    """
    Builds and configures the Gradio user interface.

    Returns:
        gr.Blocks: The fully constructed Gradio UI layout.
    """
    custom_theme = CustomTheme()
    with gr.Blocks(
        title='Expressive TTS Arena', 
        theme=custom_theme, 
        fill_width=True,
        css='footer{display:none !important}'
    ) as demo:
        # Title
        gr.Markdown('# Expressive TTS Arena')

        with gr.Column(variant='compact'):
            # Instructions
            gr.Markdown(
                'Generate text using **Claude by Anthropic**, then compare text-to-speech outputs '
                'from **Hume AI** and **ElevenLabs**. Listen to both samples and vote for your favorite!'
            )

            # Sample prompt select
            sample_prompt_dropdown = gr.Dropdown(
                choices=list(SAMPLE_PROMPTS.keys()),
                label='Choose a sample prompt (or enter your own)',
                value=None,
                interactive=True,
            )

            # Prompt input
            prompt_input = gr.Textbox(
                show_label=False,
                placeholder='Enter your prompt...',
                lines=2,
                max_lines=2,
                show_copy_button=True,
            )

        # Generate Button
        generate_button = gr.Button('Generate', variant='primary')

        with gr.Column(variant='compact'):
            # Generated text
            generated_text = gr.Textbox(
                label='Generated Text',
                interactive=False,
                autoscroll=False,
                lines=6,
                max_lines=6,
                max_length=PROMPT_MAX_LENGTH,
                show_copy_button=True,
            )

            # Audio players 
            with gr.Row():
                option1_audio_player = gr.Audio(
                    label=OPTION_ONE, 
                    type='filepath', 
                    interactive=False
                )
                option2_audio_player = gr.Audio(
                    label=OPTION_TWO, 
                    type='filepath', 
                    interactive=False
                )

            # Vote buttons
            with gr.Row():
                vote_button_1 = gr.Button(VOTE_FOR_OPTION_ONE, interactive=False)
                vote_button_2 = gr.Button(VOTE_FOR_OPTION_TWO, interactive=False)

        # UI state components
        option_mapping_state = gr.State() # Track option map (option 1 and option 2 are randomized)
        option2_audio_state = gr.State() # Track generated audio for option 2 for playing automatically after option 1 audio finishes
        generated_text_state = gr.State() # Track the text which was generated from the user's prompt
        vote_submitted_state = gr.State(False) # Track whether the user has voted on an option

        # Event handlers
        sample_prompt_dropdown.change(
            fn=lambda choice: SAMPLE_PROMPTS.get(choice, ''),
            inputs=[sample_prompt_dropdown],
            outputs=[prompt_input],
        )

        generate_button.click(
            # Reset UI
            fn=lambda _: (
                gr.update(interactive=False), 
                gr.update(interactive=False, value=VOTE_FOR_OPTION_ONE, variant='secondary'),
                gr.update(interactive=False, value=VOTE_FOR_OPTION_TWO, variant='secondary'),
                None,
                None,
                False
            ),
            inputs=[],
            outputs=[
                generate_button, 
                vote_button_1, 
                vote_button_2, 
                option_mapping_state, 
                option2_audio_state, 
                vote_submitted_state
            ]
        ).then(
            # Validate and prompt and generate text
            fn=validate_and_generate_text,
            inputs=[prompt_input],
            outputs=[generated_text, generate_button]  # Ensure button gets re-enabled on failure
        ).then(
            # Validate generated text and synthesize text-to-speech
            fn=text_to_speech,
            inputs=[prompt_input, generated_text],  # Pass prompt & generated text
            outputs=[
                option1_audio_player, 
                option2_audio_player, 
                option_mapping_state, 
                option2_audio_state
            ]
        )

        vote_button_1.click(
            fn=vote,
            inputs=[
                vote_submitted_state, 
                option_mapping_state, 
                vote_button_1
            ],
            outputs=[
                vote_submitted_state, 
                vote_button_1, 
                vote_button_2, 
                generate_button
            ]
        )

        vote_button_2.click(
            fn=vote,
            inputs=[
                vote_submitted_state, 
                option_mapping_state, 
                vote_button_2
            ],
            outputs=[
                vote_submitted_state, 
                vote_button_1, 
                vote_button_2, 
                generate_button
            ]
        )

        # Auto-play second audio after first finishes
        option1_audio_player.stop(
            fn=lambda _: gr.update(value=None),
            inputs=[],
            outputs=[option2_audio_player],
        ).then(
            fn=lambda audio: gr.update(value=audio, autoplay=True),
            inputs=[option2_audio_state],
            outputs=[option2_audio_player],
        )

        # Enable voting after 2nd audio option playback finishes
        option2_audio_player.stop(
            fn=lambda _: (
                gr.update(interactive=True), 
                gr.update(interactive=True),
                gr.update(autoplay=False),
            ),  
            inputs=[],
            outputs=[
                vote_button_1, 
                vote_button_2, 
                option2_audio_player,
            ],
        )

    logger.debug('Gradio interface built successfully')
    return demo


if __name__ == '__main__':
    logger.info('Launching TTS Arena Gradio app...')
    demo = build_gradio_interface()
    demo.launch()