Improve readability

This commit is contained in:
oobabooga 2023-03-06 19:46:46 -03:00
parent 99f69dfcaa
commit eebec65075

View File

@ -15,13 +15,14 @@ params = {
'api_key': '12345', 'api_key': '12345',
'selected_voice': 'None', 'selected_voice': 'None',
} }
initial_voice = ['None'] initial_voice = ['None']
wav_idx = 0 wav_idx = 0
user = ElevenLabsUser(params['api_key']) user = ElevenLabsUser(params['api_key'])
user_info = None user_info = None
"Check if the API is valid and refresh the UI accordingly." # Check if the API is valid and refresh the UI accordingly.
def check_valid_api(): def check_valid_api():
global user, user_info, params global user, user_info, params
@ -38,7 +39,7 @@ def check_valid_api():
print('Got an API Key!') print('Got an API Key!')
return gr.update(value='Connected') return gr.update(value='Connected')
"Once the API is verified, get the available voices and update the dropdown list" # Once the API is verified, get the available voices and update the dropdown list
def refresh_voices(): def refresh_voices():
global user, user_info global user, user_info
@ -73,6 +74,7 @@ def output_modifier(string):
""" """
This function is applied to the model outputs. This function is applied to the model outputs.
""" """
global params, wav_idx, user, user_info global params, wav_idx, user, user_info
if params['activate'] == False: if params['activate'] == False:
@ -89,18 +91,17 @@ def output_modifier(string):
if string == '': if string == '':
string = 'empty reply, try regenerating' string = 'empty reply, try regenerating'
output_file = Path('extensions/elevenlabs_tts/outputs/{}.wav'.format(wav_idx)) output_file = Path(f'extensions/elevenlabs_tts/outputs/{wav_idx:06d}.wav'.format(wav_idx))
voice = user.get_voices_by_name(params['selected_voice'])[0] voice = user.get_voices_by_name(params['selected_voice'])[0]
audio_data = voice.generate_audio_bytes(string) audio_data = voice.generate_audio_bytes(string)
save_bytes_to_path("extensions/elevenlabs_tts/outputs/{}.wav".format(wav_idx), audio_data) save_bytes_to_path(Path(f'extensions/elevenlabs_tts/outputs/{wav_idx:06d}.wav'), audio_data)
string = f'<audio src="file/{output_file.as_posix()}" controls></audio>' string = f'<audio src="file/{output_file.as_posix()}" controls></audio>'
wav_idx += 1 wav_idx += 1
return string return string
def ui(): def ui():
# Gradio elements # Gradio elements
with gr.Row(): with gr.Row():
activate = gr.Checkbox(value=params['activate'], label='Activate TTS') activate = gr.Checkbox(value=params['activate'], label='Activate TTS')
@ -109,10 +110,10 @@ def ui():
with gr.Row(): with gr.Row():
api_key = gr.Textbox(placeholder="Enter your API key.", label='API Key') api_key = gr.Textbox(placeholder="Enter your API key.", label='API Key')
connect = gr.Button(value='Connect') connect = gr.Button(value='Connect')
# Event functions to update the parameters in the backend # Event functions to update the parameters in the backend
activate.change(lambda x: params.update({'activate': x}), activate, None) activate.change(lambda x: params.update({'activate': x}), activate, None)
voice.change(lambda x: params.update({'selected_voice': x}), voice, None) voice.change(lambda x: params.update({'selected_voice': x}), voice, None)
api_key.change(lambda x: params.update({'api_key': x}), api_key, None) api_key.change(lambda x: params.update({'api_key': x}), api_key, None)
connect.click(check_valid_api, [], connection_status) connect.click(check_valid_api, [], connection_status)
connect.click(refresh_voices, [], voice) connect.click(refresh_voices, [], voice)