script.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from pathlib import Path
  2. import gradio as gr
  3. from elevenlabslib import ElevenLabsUser
  4. from elevenlabslib.helpers import save_bytes_to_path
  5. params = {
  6. 'activate': True,
  7. 'api_key': '12345',
  8. 'selected_voice': 'None',
  9. }
  10. initial_voice = ['None']
  11. wav_idx = 0
  12. user = ElevenLabsUser(params['api_key'])
  13. user_info = None
  14. # Check if the API is valid and refresh the UI accordingly.
  15. def check_valid_api():
  16. global user, user_info, params
  17. user = ElevenLabsUser(params['api_key'])
  18. user_info = user._get_subscription_data()
  19. print('checking api')
  20. if params['activate'] == False:
  21. return gr.update(value='Disconnected')
  22. elif user_info is None:
  23. print('Incorrect API Key')
  24. return gr.update(value='Disconnected')
  25. else:
  26. print('Got an API Key!')
  27. return gr.update(value='Connected')
  28. # Once the API is verified, get the available voices and update the dropdown list
  29. def refresh_voices():
  30. global user, user_info
  31. your_voices = [None]
  32. if user_info is not None:
  33. for voice in user.get_available_voices():
  34. your_voices.append(voice.initialName)
  35. return gr.Dropdown.update(choices=your_voices)
  36. else:
  37. return
  38. def remove_surrounded_chars(string):
  39. new_string = ""
  40. in_star = False
  41. for char in string:
  42. if char == '*':
  43. in_star = not in_star
  44. elif not in_star:
  45. new_string += char
  46. return new_string
  47. def input_modifier(string):
  48. """
  49. This function is applied to your text inputs before
  50. they are fed into the model.
  51. """
  52. return string
  53. def output_modifier(string):
  54. """
  55. This function is applied to the model outputs.
  56. """
  57. global params, wav_idx, user, user_info
  58. if params['activate'] == False:
  59. return string
  60. elif user_info == None:
  61. return string
  62. string = remove_surrounded_chars(string)
  63. string = string.replace('"', '')
  64. string = string.replace('“', '')
  65. string = string.replace('\n', ' ')
  66. string = string.strip()
  67. if string == '':
  68. string = 'empty reply, try regenerating'
  69. output_file = Path(f'extensions/elevenlabs_tts/outputs/{wav_idx:06d}.wav'.format(wav_idx))
  70. voice = user.get_voices_by_name(params['selected_voice'])[0]
  71. audio_data = voice.generate_audio_bytes(string)
  72. save_bytes_to_path(Path(f'extensions/elevenlabs_tts/outputs/{wav_idx:06d}.wav'), audio_data)
  73. string = f'<audio src="file/{output_file.as_posix()}" controls></audio>'
  74. wav_idx += 1
  75. return string
  76. def ui():
  77. # Gradio elements
  78. with gr.Row():
  79. activate = gr.Checkbox(value=params['activate'], label='Activate TTS')
  80. connection_status = gr.Textbox(value='Disconnected', label='Connection Status')
  81. voice = gr.Dropdown(value=params['selected_voice'], choices=initial_voice, label='TTS Voice')
  82. with gr.Row():
  83. api_key = gr.Textbox(placeholder="Enter your API key.", label='API Key')
  84. connect = gr.Button(value='Connect')
  85. # Event functions to update the parameters in the backend
  86. activate.change(lambda x: params.update({'activate': x}), activate, None)
  87. voice.change(lambda x: params.update({'selected_voice': x}), voice, None)
  88. api_key.change(lambda x: params.update({'api_key': x}), api_key, None)
  89. connect.click(check_valid_api, [], connection_status)
  90. connect.click(refresh_voices, [], voice)