File size: 15,527 Bytes
3ce989d
 
 
f5237ec
 
 
5a007ca
 
3ce989d
4ea25cd
136ff40
 
4ea25cd
1f53f73
5a007ca
3ce989d
 
5a007ca
3ce989d
a807c4d
afdac46
8047063
 
 
 
afdac46
 
fcb34bb
 
 
8047063
 
afdac46
 
fc05e1d
63ef86b
a6d4367
8047063
a6d4367
 
8047063
afdac46
2369fc5
a807c4d
3ce989d
 
b41805f
3ce989d
1f53f73
3ce989d
 
ec0cbf8
fcb34bb
 
1f53f73
b41805f
 
1f53f73
 
 
3ce989d
 
fc05e1d
ec0cbf8
 
dfee4f4
adecb62
ec0cbf8
3ce989d
f5237ec
b41805f
fc05e1d
 
dfee4f4
ec0cbf8
fc05e1d
dfee4f4
adecb62
6506ee8
b41805f
fcb34bb
a265e4f
 
 
 
fcb34bb
 
1f53f73
b41805f
fcb34bb
 
1f53f73
 
 
 
b41805f
1f53f73
 
 
fcb34bb
b41805f
dfee4f4
b41805f
ec0cbf8
b41805f
514de3d
8047063
36b195f
b41805f
8047063
6506ee8
136ff40
8047063
b41805f
8047063
 
 
36b195f
8047063
 
b41805f
fcb34bb
36b195f
 
adecb62
8047063
 
f5237ec
8047063
 
6506ee8
f5237ec
b41805f
 
1f53f73
8047063
f5237ec
63ef86b
dfee4f4
 
a6d4367
dfee4f4
 
3ce989d
ec0cbf8
dfee4f4
afdac46
 
1f53f73
afdac46
1f53f73
afdac46
 
1f53f73
 
 
afdac46
 
1f53f73
 
 
 
afdac46
fcb34bb
1f53f73
afdac46
8047063
 
fcb34bb
 
afdac46
 
fcb34bb
b41805f
 
 
 
 
4ea25cd
3ce989d
5e4b7c8
1f53f73
 
 
 
 
8047063
 
 
 
5e4b7c8
b41805f
5e4b7c8
1f53f73
 
5e4b7c8
 
b41805f
 
1f53f73
 
 
 
 
 
 
b41805f
f64ebd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b41805f
 
 
 
 
 
1f53f73
 
b41805f
 
f64ebd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b41805f
 
 
 
 
 
 
 
1f53f73
3ce989d
 
 
fcb34bb
3ce989d
 
fcb34bb
3ce989d
2369fc5
07101b1
 
 
1f53f73
b41805f
07101b1
60bcef1
afdac46
3ce989d
b41805f
 
 
 
 
1f53f73
b41805f
 
 
 
 
 
 
4ea25cd
 
b41805f
 
 
dfee4f4
4ea25cd
1f53f73
 
 
96154e7
60bcef1
96154e7
 
 
 
b41805f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f53f73
b41805f
 
 
 
6506ee8
b41805f
1f53f73
 
 
5e4b7c8
8047063
 
 
 
5e4b7c8
b41805f
5e4b7c8
 
6506ee8
 
b41805f
5e4b7c8
8047063
 
5e4b7c8
b41805f
5e4b7c8
dfee4f4
b41805f
 
 
 
 
dfee4f4
b41805f
4ea25cd
 
b41805f
8047063
afdac46
8047063
b41805f
afdac46
8047063
afdac46
8047063
b41805f
afdac46
 
b41805f
 
 
 
8047063
afdac46
f5237ec
8047063
4ea25cd
afdac46
b41805f
8047063
3ce989d
 
1f53f73
8047063
dfee4f4
afdac46
8047063
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""
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 synthesized into speech using both Hume and ElevenLabs text-to-speech (TTS) APIs.
Users can compare the outputs and vote for their favorite in an interactive UI.
"""

# Standard Library Imports
from concurrent.futures import ThreadPoolExecutor
import random
from typing import Union, Tuple

# Third-Party Library Imports
import gradio as gr

# Local Application Imports
from src.config import logger
from src.constants import (
    ELEVENLABS,
    HUME_AI,
    OPTION_A, 
    OPTION_B,
    PROMPT_MAX_LENGTH, 
    PROMPT_MIN_LENGTH, 
    SAMPLE_PROMPTS,
    TROPHY_EMOJI,
    UNKNOWN_PROVIDER,
    VOTE_FOR_OPTION_A,
    VOTE_FOR_OPTION_B,
)
from src.integrations import (
    AnthropicError,
    ElevenLabsError,
    generate_text_with_claude,
    get_random_hume_voice_names,
    HumeError,
    text_to_speech_with_elevenlabs,
    text_to_speech_with_hume,
)
from src.theme import CustomTheme
from src.utils import truncate_text, validate_prompt_length


def generate_text(prompt: str,) -> Tuple[Union[str, gr.update], gr.update]:
    """
    Validates the prompt and generates text using Anthropic API.

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

    Returns:
        Tuple containing:
          - The generated text (as a gr.update).
          - An update for the generated text state.
    
    Raises:
        gr.Error: On validation or API errors.
    """
    try:
        validate_prompt_length(prompt, PROMPT_MAX_LENGTH, PROMPT_MIN_LENGTH)
    except ValueError as ve:
        logger.warning(f'Validation error: {ve}')
        raise gr.Error(str(ve))

    try:
        generated_text = generate_text_with_claude(prompt)
        logger.info(f'Generated text ({len(generated_text)} characters).')
        return gr.update(value=generated_text), generated_text
    except AnthropicError as ae:
        logger.error(f'AnthropicError while generating text: {str(ae)}')
        raise gr.Error('There was an issue communicating with the Anthropic API. Please try again later.')
    except Exception as e:
        logger.error(f'Unexpected error while generating text: {e}')
        raise gr.Error('Failed to generate text. Please try again.')


def text_to_speech(prompt: str, text: str, generated_text_state: str) -> Tuple[gr.update, gr.update, dict, Union[str, None]]:
    """
    Synthesizes two text to speech outputs, loads the two audio players with the
    output audio, and updates related UI state components.
        - 50% chance to synthesize one Hume and one Elevenlabs output.
        - 50% chance to synthesize two Hume outputs.

    Args:
        prompt (str): The original prompt.
        text (str): The text to synthesize to speech.

    Returns:
        A tuple of:
         - Update for first audio player (with autoplay)
         - Update for second audio player
         - A dictionary mapping options to providers
         - The raw audio value for option B
    
    Raises:
        gr.Error: On API or unexpected errors.
    """
    if not text:
        logger.warning('Skipping text-to-speech due to empty text.')
        raise gr.Error('Please generate or enter text to synthesize.')

    # If not using generated text, then only compare Hume to Hume
    compare_hume_with_elevenlabs = (text == generated_text_state) and (random.random() < 0.5)
    
    # Pre-select two Hume voices pre-emptively in case we compare Hume to Hume to ensure we do not select the same voice twice.
    hume_voice_a, hume_voice_b = get_random_hume_voice_names()

    try:
        with ThreadPoolExecutor(max_workers=2) as executor:
            provider_a = HUME_AI
            future_audio_a = executor.submit(text_to_speech_with_hume, prompt, text, hume_voice_a)

            if compare_hume_with_elevenlabs:
                provider_b = ELEVENLABS
                future_audio_b = executor.submit(text_to_speech_with_elevenlabs, text)
            else:
                provider_b = HUME_AI
                future_audio_b = executor.submit(text_to_speech_with_hume, prompt, text, hume_voice_b)
            
            voice_a, audio_a = future_audio_a.result()
            voice_b, audio_b = future_audio_b.result()

        logger.info(f'TTS generated: {provider_a}={len(audio_a)} bytes, {provider_b}={len(audio_b)} bytes')
        options = [(audio_a, provider_a), (audio_b, provider_b)]
        random.shuffle(options)
        option_a_audio, option_b_audio = options[0][0], options[1][0]
        options_map = { OPTION_A: options[0][1], OPTION_B: options[1][1] }

        return (
            gr.update(value=option_a_audio, visible=True, autoplay=True),
            gr.update(value=option_b_audio, visible=True),
            options_map,
            option_b_audio,
        )
    except ElevenLabsError as ee:
        logger.error(f'ElevenLabsError while synthesizing speech from text: {str(ee)}')
        raise gr.Error('There was an issue communicating with the Elevenlabs API. Please try again later.')
    except HumeError as he:
        logger.error(f'HumeError while synthesizing speech from text: {str(he)}')
        raise gr.Error('There was an issue communicating with the Hume API. Please try again later.')
    except Exception as e:
        logger.error(f'Unexpected error during TTS generation: {e}')
        raise gr.Error('An unexpected error ocurred. Please try again later.')


def vote(vote_submitted: bool, option_mapping: dict, selected_button: str) -> Tuple[bool, gr.update, gr.update]:
    """
    Handles user voting.

    Args:
        vote_submitted (bool): True if a vote was already submitted
        option_mapping (dict): Maps option labels to provider names
        selected_button (str): The button clicked

    Returns:
        A tuple of:
         - True if the vote was accepted
         - Update for the selected vote button
         - Update for the unselected vote button
    """
    if not option_mapping or vote_submitted:
        return gr.skip(), gr.skip(), gr.skip()

    is_option_a = selected_button == VOTE_FOR_OPTION_A
    selected_option, other_option = (OPTION_A, OPTION_B) if is_option_a else (OPTION_B, OPTION_A)
    selected_provider = option_mapping.get(selected_option, UNKNOWN_PROVIDER)
    other_provider = option_mapping.get(other_option, UNKNOWN_PROVIDER)

    return (
        True,
        gr.update(value=f'{selected_provider} {TROPHY_EMOJI}', variant='primary', interactive=False) if is_option_a 
            else gr.update(value=other_provider, variant='secondary', interactive=False),
        gr.update(value=other_provider, variant='secondary', interactive=False) if is_option_a 
            else gr.update(value=f'{selected_provider} {TROPHY_EMOJI}', variant='primary', interactive=False),
        gr.update(interactive=True)
    )

def reset_ui() -> Tuple[gr.update, gr.update, gr.update, gr.update, None, None, bool]:
    """
    Resets UI state before generating new text.

    Returns:
        A tuple of updates for:
         - option_a_audio_player (clear audio)
         - option_b_audio_player (clear audio)
         - vote_button_a (disable and reset button text)
         - vote_button_a (disable and reset button text)
         - option_mapping_state (reset option map state)
         - option_b_audio_state (reset option B audio state)
         - vote_submitted_state (reset submitted vote state)
    """
    return (
        gr.update(value=None),
        gr.update(value=None),
        gr.update(value=VOTE_FOR_OPTION_A, variant='secondary'),
        gr.update(value=VOTE_FOR_OPTION_B, variant='secondary'),
        None,
        None,
        False,
    )


def build_input_section() -> Tuple[gr.Markdown, gr.Dropdown, gr.Textbox, gr.Button]:
    """ Builds the input section including instructions, sample prompt dropdown, prompt input, and generate button """
    instructions = gr.Markdown("""
        1. **Enter or Generate Text:** Type directly in the Text box, or optionally enter a Prompt, click "Generate text", and edit if needed.
        2. **Synthesize Speech:** Click "Synthesize speech" to generate two audio outputs.
        3. **Listen & Compare:** Playback both options (A & B) to hear the differences.
        4. **Vote for Your Favorite:** Click "Vote for option A" or "Vote for option B" to choose the best one.
    """)
    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 = gr.Textbox(
        label='Prompt',
        placeholder='Enter your prompt...',
        lines=2,
        max_lines=2,
        max_length=PROMPT_MAX_LENGTH,
        show_copy_button=True,
    )
    generate_text_button = gr.Button('Generate text', variant='secondary')
    return (
        instructions, 
        sample_prompt_dropdown, 
        prompt_input, 
        generate_text_button,
    )


def build_output_section() -> Tuple[gr.Textbox, gr.Button, gr.Audio, gr.Audio, gr.Button, gr.Button]:
    """ Builds the output section including generated text, audio players, and vote buttons. """
    text_input = gr.Textbox(
        label='Text',
        placeholder='Enter text to synthesize speech...',
        interactive=True,
        autoscroll=False,
        lines=4,
        max_lines=12,
        max_length=PROMPT_MAX_LENGTH,
        show_copy_button=True,
    )
    synthesize_speech_button = gr.Button('Synthesize speech', variant='primary')
    with gr.Row(equal_height=True):
        option_a_audio_player = gr.Audio(label=OPTION_A, type='filepath', interactive=False)
        option_b_audio_player = gr.Audio(label=OPTION_B, type='filepath', interactive=False)
    with gr.Row(equal_height=True):
        vote_button_a = gr.Button(VOTE_FOR_OPTION_A, interactive=False)
        vote_button_b = gr.Button(VOTE_FOR_OPTION_B, interactive=False)
    return (
        text_input, 
        synthesize_speech_button, 
        option_a_audio_player, 
        option_b_audio_player, 
        vote_button_a, 
        vote_button_b,
    )


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_paths='src/assets/styles.css',
    ) as demo:
        # Title
        gr.Markdown('# Expressive TTS Arena')

        # Build generate text section
        (instructions, 
        sample_prompt_dropdown, 
        prompt_input, 
        generate_text_button) = build_input_section()

        # Build synthesize speech section
        (text_input, 
        synthesize_speech_button, 
        option_a_audio_player, 
        option_b_audio_player, 
        vote_button_a, 
        vote_button_b) = build_output_section()

        # UI state components
        generated_text_state = gr.State('')     # Track generated text state
        option_b_audio_state = gr.State()       # Track generated audio for option B for playing automatically after option 1 audio finishes
        option_mapping_state = gr.State()       # Track option map (option A and option B are randomized)
        vote_submitted_state = gr.State(False)  # Track whether the user has voted on an option

        # --- Register event handlers ---

        # When a sample prompt is chosen, update the prompt textbox
        sample_prompt_dropdown.change(
            fn=lambda choice: SAMPLE_PROMPTS.get(choice, ''),
            inputs=[sample_prompt_dropdown],
            outputs=[prompt_input],
        )

        # Generate text button click handler chain:
        # 1. Disable the "Generate text" button
        # 2. Generate text
        # 3. Enable the "Generate text" button
        generate_text_button.click(
            fn=lambda: gr.update(interactive=False),
            inputs=[],
            outputs=[generate_text_button]
        ).then(
            fn=generate_text,
            inputs=[prompt_input],
            outputs=[text_input, generated_text_state],
        ).then(
            fn=lambda: gr.update(interactive=True),
            inputs=[],
            outputs=[generate_text_button]
        )
        
        # Synthesize speech button click event handler chain:
        # 1. Disable the "Synthesize speech" button
        # 2. Reset UI state
        # 3. Synthesize speech, load audio players, and display vote button
        # 4. Enable the "Synthesize speech" button and display vote buttons
        synthesize_speech_button.click(
            fn=lambda: gr.update(interactive=False),
            inputs=[],
            outputs=[synthesize_speech_button]
        ).then(
            fn=reset_ui,
            inputs=[],
            outputs=[
                option_a_audio_player,
                option_b_audio_player,
                vote_button_a, 
                vote_button_b, 
                option_mapping_state, 
                option_b_audio_state, 
                vote_submitted_state,
            ],
        ).then(
            fn=text_to_speech,
            inputs=[prompt_input, text_input, generated_text_state],
            outputs=[
                option_a_audio_player, 
                option_b_audio_player, 
                option_mapping_state, 
                option_b_audio_state,
            ],
        ).then(
            fn=lambda: (
                gr.update(interactive=True), 
                gr.update(interactive=True), 
                gr.update(interactive=True)
            ),
            inputs=[],
            outputs=[synthesize_speech_button, vote_button_a, vote_button_b]
        )

        # Vote button click event handlers
        vote_button_a.click(
            fn=vote,
            inputs=[vote_submitted_state, option_mapping_state, vote_button_a],
            outputs=[vote_submitted_state, vote_button_a, vote_button_b, synthesize_speech_button],
        )
        vote_button_b.click(
            fn=vote,
            inputs=[vote_submitted_state, option_mapping_state, vote_button_b],
            outputs=[vote_submitted_state, vote_button_a, vote_button_b, synthesize_speech_button],
        )

        # Auto-play second audio after first finishes (Workaround to play audio back-to-back)
        # Audio player A stop event handler chain:
        # 1. Clear the audio player A
        # 2. Load audio player A with audio and set auto play to True
        option_a_audio_player.stop(
            fn=lambda _: gr.update(value=None),
            inputs=[],
            outputs=[option_b_audio_player],
        ).then(
            fn=lambda audio: gr.update(value=audio, autoplay=True),
            inputs=[option_b_audio_state],
            outputs=[option_b_audio_player],
        )

        # Enable voting after second audio option playback finishes
        option_b_audio_player.stop(
            fn=lambda _: (gr.update(interactive=True), gr.update(interactive=True), gr.update(autoplay=False)),
            inputs=[],
            outputs=[vote_button_a, vote_button_b, option_b_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()