text-generation-webui/extensions/elevenlabs_tts/script.py

123 lines
3.5 KiB
Python
Raw Normal View History

2023-03-22 19:55:03 +01:00
import re
2023-03-06 23:38:36 +01:00
from pathlib import Path
2023-03-06 23:28:53 +01:00
2023-03-06 23:38:36 +01:00
import gradio as gr
2023-03-16 14:18:34 +01:00
from elevenlabslib import ElevenLabsUser
from elevenlabslib.helpers import save_bytes_to_path
2023-03-06 23:28:53 +01:00
2023-04-07 19:42:03 +02:00
import modules.shared as shared
2023-03-06 23:28:53 +01:00
params = {
'activate': True,
'api_key': '12345',
'selected_voice': 'None',
}
2023-03-06 23:46:46 +01:00
2023-03-06 23:28:53 +01:00
initial_voice = ['None']
wav_idx = 0
user = ElevenLabsUser(params['api_key'])
user_info = None
if not shared.args.no_stream:
print("Please add --no-stream. This extension is not meant to be used with streaming.")
raise ValueError
2023-03-06 23:46:46 +01:00
# Check if the API is valid and refresh the UI accordingly.
2023-03-06 23:28:53 +01:00
def check_valid_api():
2023-03-06 23:28:53 +01:00
global user, user_info, params
user = ElevenLabsUser(params['api_key'])
user_info = user._get_subscription_data()
print('checking api')
if not params['activate']:
2023-03-06 23:28:53 +01:00
return gr.update(value='Disconnected')
elif user_info is None:
print('Incorrect API Key')
return gr.update(value='Disconnected')
else:
print('Got an API Key!')
return gr.update(value='Connected')
2023-03-06 23:46:46 +01:00
# Once the API is verified, get the available voices and update the dropdown list
2023-03-06 23:28:53 +01:00
def refresh_voices():
2023-03-06 23:28:53 +01:00
global user, user_info
2023-03-06 23:28:53 +01:00
your_voices = [None]
if user_info is not None:
for voice in user.get_available_voices():
your_voices.append(voice.initialName)
return gr.Dropdown.update(choices=your_voices)
2023-03-06 23:28:53 +01:00
else:
return
2023-03-06 23:28:53 +01:00
def remove_surrounded_chars(string):
# this expression matches to 'as few symbols as possible (0 upwards) between any asterisks' OR
# 'as few symbols as possible (0 upwards) between an asterisk and the end of the string'
return re.sub('\*[^\*]*?(\*|$)', '', string)
2023-03-06 23:28:53 +01:00
def input_modifier(string):
"""
This function is applied to your text inputs before
they are fed into the model.
"""
return string
2023-03-06 23:28:53 +01:00
def output_modifier(string):
"""
This function is applied to the model outputs.
"""
2023-03-06 23:46:46 +01:00
2023-03-06 23:28:53 +01:00
global params, wav_idx, user, user_info
if not params['activate']:
2023-03-06 23:28:53 +01:00
return string
elif user_info is None:
2023-03-06 23:28:53 +01:00
return string
string = remove_surrounded_chars(string)
string = string.replace('"', '')
string = string.replace('', '')
string = string.replace('\n', ' ')
string = string.strip()
if string == '':
string = 'empty reply, try regenerating'
2023-03-06 23:46:46 +01:00
output_file = Path(f'extensions/elevenlabs_tts/outputs/{wav_idx:06d}.wav'.format(wav_idx))
2023-03-06 23:28:53 +01:00
voice = user.get_voices_by_name(params['selected_voice'])[0]
audio_data = voice.generate_audio_bytes(string)
2023-03-06 23:46:46 +01:00
save_bytes_to_path(Path(f'extensions/elevenlabs_tts/outputs/{wav_idx:06d}.wav'), audio_data)
2023-03-06 23:28:53 +01:00
string = f'<audio src="file/{output_file.as_posix()}" controls></audio>'
wav_idx += 1
return string
2023-03-06 23:28:53 +01:00
def ui():
2023-03-06 23:46:46 +01:00
2023-03-06 23:28:53 +01:00
# Gradio elements
with gr.Row():
activate = gr.Checkbox(value=params['activate'], label='Activate TTS')
connection_status = gr.Textbox(value='Disconnected', label='Connection Status')
voice = gr.Dropdown(value=params['selected_voice'], choices=initial_voice, label='TTS Voice')
with gr.Row():
api_key = gr.Textbox(placeholder="Enter your API key.", label='API Key')
connect = gr.Button(value='Connect')
2023-03-06 23:46:46 +01:00
2023-03-06 23:28:53 +01:00
# Event functions to update the parameters in the backend
activate.change(lambda x: params.update({'activate': x}), activate, 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)
connect.click(check_valid_api, [], connection_status)
connect.click(refresh_voices, [], voice)