script.py 3.5 KB

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