2023-03-22 14:55:03 -04:00
|
|
|
import re
|
2023-03-06 17:38:36 -05:00
|
|
|
from pathlib import Path
|
2023-03-06 17:28:53 -05:00
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
import elevenlabs
|
2023-05-06 09:57:34 -04:00
|
|
|
import gradio as gr
|
2023-07-03 23:03:30 -04:00
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
from modules import chat, shared
|
2023-07-03 23:03:30 -04:00
|
|
|
from modules.utils import gradio
|
2023-07-11 18:00:37 -04:00
|
|
|
from modules.logging_colors import logger
|
2023-04-07 13:42:03 -04:00
|
|
|
|
2023-03-06 17:28:53 -05:00
|
|
|
params = {
|
|
|
|
'activate': True,
|
2023-05-06 09:56:31 -04:00
|
|
|
'api_key': None,
|
2023-03-06 17:28:53 -05:00
|
|
|
'selected_voice': 'None',
|
2023-05-06 09:56:31 -04:00
|
|
|
'autoplay': False,
|
|
|
|
'show_text': True,
|
2023-07-11 18:00:37 -04:00
|
|
|
'model': 'eleven_monolingual_v1',
|
2023-03-06 17:28:53 -05:00
|
|
|
}
|
2023-03-06 17:46:46 -05:00
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
voices = None
|
2023-03-06 17:28:53 -05:00
|
|
|
wav_idx = 0
|
2023-07-11 18:00:37 -04:00
|
|
|
LANG_MODELS = ['eleven_monolingual_v1', 'eleven_multilingual_v1']
|
2023-03-06 17:28:53 -05:00
|
|
|
|
|
|
|
|
2023-05-16 19:21:36 -04:00
|
|
|
def update_api_key(key):
|
|
|
|
params['api_key'] = key
|
|
|
|
if key is not None:
|
|
|
|
elevenlabs.set_api_key(key)
|
|
|
|
|
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
def refresh_voices():
|
|
|
|
global params
|
2023-05-16 12:34:49 -04:00
|
|
|
your_voices = elevenlabs.voices()
|
2023-05-06 09:56:31 -04:00
|
|
|
voice_names = [voice.name for voice in your_voices]
|
|
|
|
return voice_names
|
2023-04-06 23:15:45 -04:00
|
|
|
|
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
def refresh_voices_dd():
|
|
|
|
all_voices = refresh_voices()
|
|
|
|
return gr.Dropdown.update(value=all_voices[0], choices=all_voices)
|
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-07-03 23:03:30 -04:00
|
|
|
def remove_tts_from_history(history):
|
|
|
|
for i, entry in enumerate(history['internal']):
|
|
|
|
history['visible'][i] = [history['visible'][i][0], entry[1]]
|
|
|
|
|
|
|
|
return history
|
2023-05-06 09:57:34 -04:00
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
|
2023-07-03 23:03:30 -04:00
|
|
|
def toggle_text_in_history(history):
|
|
|
|
for i, entry in enumerate(history['visible']):
|
2023-05-06 09:56:31 -04:00
|
|
|
visible_reply = entry[1]
|
|
|
|
if visible_reply.startswith('<audio'):
|
|
|
|
if params['show_text']:
|
2023-07-03 23:03:30 -04:00
|
|
|
reply = history['internal'][i][1]
|
|
|
|
history['visible'][i] = [history['visible'][i][0], f"{visible_reply.split('</audio>')[0]}</audio>\n\n{reply}"]
|
2023-05-06 09:56:31 -04:00
|
|
|
else:
|
2023-07-03 23:03:30 -04:00
|
|
|
history['visible'][i] = [history['visible'][i][0], f"{visible_reply.split('</audio>')[0]}</audio>"]
|
|
|
|
|
|
|
|
return history
|
2023-05-06 09:57:34 -04:00
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-03-06 17:28:53 -05:00
|
|
|
def remove_surrounded_chars(string):
|
2023-03-22 00:47:54 -04:00
|
|
|
# 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'
|
2023-04-06 23:15:45 -04:00
|
|
|
return re.sub('\*[^\*]*?(\*|$)', '', string)
|
|
|
|
|
2023-03-06 17:28:53 -05:00
|
|
|
|
2023-05-05 17:53:03 -04:00
|
|
|
def state_modifier(state):
|
2023-05-24 09:06:38 -04:00
|
|
|
if not params['activate']:
|
|
|
|
return state
|
|
|
|
|
2023-05-05 17:53:03 -04:00
|
|
|
state['stream'] = False
|
|
|
|
return state
|
|
|
|
|
|
|
|
|
2023-03-06 17:28:53 -05:00
|
|
|
def input_modifier(string):
|
2023-05-24 09:06:38 -04:00
|
|
|
if not params['activate']:
|
|
|
|
return string
|
2023-05-21 13:11:46 -04:00
|
|
|
|
|
|
|
shared.processing_message = "*Is recording a voice message...*"
|
|
|
|
return string
|
|
|
|
|
|
|
|
|
|
|
|
def history_modifier(history):
|
2023-05-06 09:56:31 -04:00
|
|
|
# Remove autoplay from the last reply
|
2023-05-21 13:11:46 -04:00
|
|
|
if len(history['internal']) > 0:
|
|
|
|
history['visible'][-1] = [
|
|
|
|
history['visible'][-1][0],
|
|
|
|
history['visible'][-1][1].replace('controls autoplay>', 'controls>')
|
2023-05-06 09:56:31 -04:00
|
|
|
]
|
2023-05-06 09:57:34 -04:00
|
|
|
|
2023-05-21 13:11:46 -04:00
|
|
|
return history
|
2023-03-06 17:28:53 -05:00
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-03-06 17:28:53 -05:00
|
|
|
def output_modifier(string):
|
2023-05-06 10:03:12 -04:00
|
|
|
global params, wav_idx
|
2023-04-06 23:15:45 -04:00
|
|
|
|
|
|
|
if not params['activate']:
|
2023-03-06 17:28:53 -05:00
|
|
|
return string
|
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
original_string = string
|
2023-03-06 17:28:53 -05:00
|
|
|
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-04-06 23:15:45 -04:00
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
output_file = Path(f'extensions/elevenlabs_tts/outputs/{wav_idx:06d}.mp3'.format(wav_idx))
|
2023-05-21 13:11:46 -04:00
|
|
|
print(f'Outputting audio to {str(output_file)}')
|
2023-05-06 09:56:31 -04:00
|
|
|
try:
|
2023-07-11 18:00:37 -04:00
|
|
|
audio = elevenlabs.generate(text=string, voice=params['selected_voice'], model=params['model'])
|
2023-05-06 09:56:31 -04:00
|
|
|
elevenlabs.save(audio, str(output_file))
|
|
|
|
|
|
|
|
autoplay = 'autoplay' if params['autoplay'] else ''
|
|
|
|
string = f'<audio src="file/{output_file.as_posix()}" controls {autoplay}></audio>'
|
|
|
|
wav_idx += 1
|
|
|
|
except elevenlabs.api.error.UnauthenticatedRateLimitError:
|
|
|
|
string = "🤖 ElevenLabs Unauthenticated Rate Limit Reached - Please create an API key to continue\n\n"
|
|
|
|
except elevenlabs.api.error.RateLimitError:
|
|
|
|
string = "🤖 ElevenLabs API Tier Limit Reached\n\n"
|
|
|
|
except elevenlabs.api.error.APIError as err:
|
|
|
|
string = f"🤖 ElevenLabs Error: {err}\n\n"
|
|
|
|
|
|
|
|
if params['show_text']:
|
|
|
|
string += f'\n\n{original_string}'
|
|
|
|
|
|
|
|
shared.processing_message = "*Is typing...*"
|
2023-03-06 17:28:53 -05:00
|
|
|
return string
|
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-03-06 17:28:53 -05:00
|
|
|
def ui():
|
2023-05-06 09:56:31 -04:00
|
|
|
global voices
|
|
|
|
if not voices:
|
|
|
|
voices = refresh_voices()
|
2023-07-11 18:00:37 -04:00
|
|
|
selected = params['selected_voice']
|
|
|
|
if selected == 'None':
|
|
|
|
params['selected_voice'] = voices[0]
|
|
|
|
elif selected not in voices:
|
|
|
|
logger.error(f'Selected voice {selected} not available, switching to {voices[0]}')
|
|
|
|
params['selected_voice'] = voices[0]
|
2023-05-06 09:57:34 -04:00
|
|
|
|
2023-03-06 17:28:53 -05:00
|
|
|
# Gradio elements
|
|
|
|
with gr.Row():
|
|
|
|
activate = gr.Checkbox(value=params['activate'], label='Activate TTS')
|
2023-05-06 09:56:31 -04:00
|
|
|
autoplay = gr.Checkbox(value=params['autoplay'], label='Play TTS automatically')
|
|
|
|
show_text = gr.Checkbox(value=params['show_text'], label='Show message text under audio player')
|
2023-05-06 09:57:34 -04:00
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
with gr.Row():
|
|
|
|
voice = gr.Dropdown(value=params['selected_voice'], choices=voices, label='TTS Voice')
|
|
|
|
refresh = gr.Button(value='Refresh')
|
2023-05-06 09:57:34 -04:00
|
|
|
|
2023-03-06 17:28:53 -05:00
|
|
|
with gr.Row():
|
2023-07-11 18:00:37 -04:00
|
|
|
if params['api_key']:
|
|
|
|
api_key = gr.Textbox(value=params['api_key'], label='API Key')
|
|
|
|
update_api_key(params['api_key'])
|
|
|
|
else:
|
|
|
|
api_key = gr.Textbox(placeholder="Enter your API key.", label='API Key')
|
|
|
|
|
|
|
|
with gr.Row():
|
2023-07-12 14:33:25 -04:00
|
|
|
model = gr.Dropdown(value=params['model'], choices=LANG_MODELS, label='Language model')
|
2023-05-06 09:57:34 -04:00
|
|
|
|
2023-05-06 09:56:31 -04:00
|
|
|
with gr.Row():
|
|
|
|
convert = gr.Button('Permanently replace audios with the message texts')
|
|
|
|
convert_cancel = gr.Button('Cancel', visible=False)
|
|
|
|
convert_confirm = gr.Button('Confirm (cannot be undone)', variant="stop", visible=False)
|
|
|
|
|
2023-07-03 23:03:30 -04:00
|
|
|
if shared.is_chat():
|
|
|
|
# Convert history with confirmation
|
|
|
|
convert_arr = [convert_confirm, convert, convert_cancel]
|
|
|
|
convert.click(lambda: [gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)], None, convert_arr)
|
|
|
|
convert_confirm.click(
|
|
|
|
lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, convert_arr).then(
|
|
|
|
remove_tts_from_history, gradio('history'), gradio('history')).then(
|
|
|
|
chat.save_persistent_history, gradio('history', 'character_menu', 'mode'), None).then(
|
|
|
|
chat.redraw_html, shared.reload_inputs, gradio('display'))
|
|
|
|
|
|
|
|
convert_cancel.click(lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, convert_arr)
|
|
|
|
|
|
|
|
# Toggle message text in history
|
|
|
|
show_text.change(
|
|
|
|
lambda x: params.update({"show_text": x}), show_text, None).then(
|
|
|
|
toggle_text_in_history, gradio('history'), gradio('history')).then(
|
|
|
|
chat.save_persistent_history, gradio('history', 'character_menu', 'mode'), None).then(
|
|
|
|
chat.redraw_html, shared.reload_inputs, gradio('display'))
|
2023-05-06 09:57:34 -04:00
|
|
|
|
2023-03-06 17:28:53 -05: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)
|
2023-05-16 19:21:36 -04:00
|
|
|
api_key.change(update_api_key, api_key, None)
|
2023-07-11 18:00:37 -04:00
|
|
|
model.change(lambda x: params.update({'model': x}), model, None)
|
2023-05-06 09:56:31 -04:00
|
|
|
# connect.click(check_valid_api, [], connection_status)
|
|
|
|
refresh.click(refresh_voices_dd, [], voice)
|
|
|
|
# Event functions to update the parameters in the backend
|
|
|
|
autoplay.change(lambda x: params.update({"autoplay": x}), autoplay, None)
|