script.py 3.5 KB

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