Advertisement
Guest User

coqui gpu 2

a guest
Nov 3rd, 2024
28
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import html
  2. import json
  3. import os
  4. import random
  5. import time
  6. from pathlib import Path
  7.  
  8. import gradio as gr
  9. import torch
  10. from TTS.api import TTS
  11. from TTS.utils.synthesizer import Synthesizer
  12.  
  13. from modules import chat, shared, ui_chat
  14. from modules.ui import create_refresh_button
  15. from modules.utils import gradio
  16.  
  17. os.environ["COQUI_TOS_AGREED"] = "1"
  18.  
  19. params = {
  20.     "activate": True,
  21.     "autoplay": True,
  22.     "show_text": False,
  23.     "remove_trailing_dots": False,
  24.     "voice": "female_01.wav",
  25.     "language": "English",
  26.     "model_name": "tts_models/multilingual/multi-dataset/xtts_v2",
  27.     "device": "cuda:2"
  28. }
  29.  
  30. this_dir = str(Path(__file__).parent.resolve())
  31. model = None
  32. with open(Path(f"{this_dir}/languages.json"), encoding='utf8') as f:
  33.     languages = json.load(f)
  34.  
  35.  
  36. def get_available_voices():
  37.     return sorted([voice.name for voice in Path(f"{this_dir}/voices").glob("*.wav")])
  38.  
  39.  
  40. def preprocess(raw_input):
  41.     raw_input = html.unescape(raw_input)
  42.     # raw_input = raw_input.strip("\"")
  43.     return raw_input
  44.  
  45.  
  46. def new_split_into_sentences(self, text):
  47.     sentences = self.seg.segment(text)
  48.     if params['remove_trailing_dots']:
  49.         sentences_without_dots = []
  50.         for sentence in sentences:
  51.             if sentence.endswith('.') and not sentence.endswith('...'):
  52.                 sentence = sentence[:-1]
  53.  
  54.             sentences_without_dots.append(sentence)
  55.  
  56.         return sentences_without_dots
  57.     else:
  58.         return sentences
  59.  
  60.  
  61. Synthesizer.split_into_sentences = new_split_into_sentences
  62.  
  63.  
  64. def load_model():
  65.     model = TTS(params["model_name"]).to(params["device"])
  66.     return model
  67.  
  68.  
  69. def remove_tts_from_history(history):
  70.     for i, entry in enumerate(history['internal']):
  71.         history['visible'][i] = [history['visible'][i][0], entry[1]]
  72.  
  73.     return history
  74.  
  75.  
  76. def toggle_text_in_history(history):
  77.     for i, entry in enumerate(history['visible']):
  78.         visible_reply = entry[1]
  79.         if visible_reply.startswith('<audio'):
  80.             if params['show_text']:
  81.                 reply = history['internal'][i][1]
  82.                 history['visible'][i] = [history['visible'][i][0], f"{visible_reply.split('</audio>')[0]}</audio>\n\n{reply}"]
  83.             else:
  84.                 history['visible'][i] = [history['visible'][i][0], f"{visible_reply.split('</audio>')[0]}</audio>"]
  85.  
  86.     return history
  87.  
  88.  
  89. def random_sentence():
  90.     with open(Path("extensions/coqui_tts/harvard_sentences.txt")) as f:
  91.         return random.choice(list(f))
  92.  
  93.  
  94. def voice_preview(string):
  95.     string = html.unescape(string) or random_sentence()
  96.  
  97.     output_file = Path('extensions/coqui_tts/outputs/voice_preview.wav')
  98.     model.tts_to_file(
  99.         text=string,
  100.         file_path=output_file,
  101.         speaker_wav=[f"{this_dir}/voices/{params['voice']}"],
  102.         language=languages[params["language"]]
  103.     )
  104.  
  105.     return f'<audio src="file/{output_file.as_posix()}?{int(time.time())}" controls autoplay></audio>'
  106.  
  107.  
  108. def history_modifier(history):
  109.     # Remove autoplay from the last reply
  110.     if len(history['internal']) > 0:
  111.         history['visible'][-1] = [
  112.             history['visible'][-1][0],
  113.             history['visible'][-1][1].replace('controls autoplay>', 'controls>')
  114.         ]
  115.  
  116.     return history
  117.  
  118.  
  119. def state_modifier(state):
  120.     if not params['activate']:
  121.         return state
  122.  
  123.     state['stream'] = False
  124.     return state
  125.  
  126.  
  127. def input_modifier(string, state):
  128.     if not params['activate']:
  129.         return string
  130.  
  131.     shared.processing_message = "*Is recording a voice message...*"
  132.     return string
  133.  
  134.  
  135. def output_modifier(string, state):
  136.     if not params['activate']:
  137.         return string
  138.  
  139.     original_string = string
  140.     string = preprocess(html.unescape(string))
  141.     if string == '':
  142.         string = '*Empty reply, try regenerating*'
  143.     else:
  144.         output_file = Path(f'extensions/coqui_tts/outputs/{state["character_menu"]}_{int(time.time())}.wav')
  145.         model.tts_to_file(
  146.             text=string,
  147.             file_path=output_file,
  148.             speaker_wav=[f"{this_dir}/voices/{params['voice']}"],
  149.             language=languages[params["language"]]
  150.         )
  151.  
  152.         autoplay = 'autoplay' if params['autoplay'] else ''
  153.         string = f'<audio src="file/{output_file.as_posix()}" controls {autoplay}></audio>'
  154.         if params['show_text']:
  155.             string += f'\n\n{original_string}'
  156.  
  157.     shared.processing_message = "*Is typing...*"
  158.     return string
  159.  
  160.  
  161. def custom_css():
  162.     path_to_css = Path(f"{this_dir}/style.css")
  163.     return open(path_to_css, 'r').read()
  164.  
  165.  
  166. def setup():
  167.     global model
  168.     print("[XTTS] Loading XTTS...")
  169.     model = load_model()
  170.     print("[XTTS] Done!")
  171.     Path(f"{this_dir}/outputs").mkdir(parents=True, exist_ok=True)
  172.  
  173.  
  174. def ui():
  175.     with gr.Accordion("Coqui TTS (XTTSv2)"):
  176.         with gr.Row():
  177.             activate = gr.Checkbox(value=params['activate'], label='Activate TTS')
  178.             autoplay = gr.Checkbox(value=params['autoplay'], label='Play TTS automatically')
  179.  
  180.         with gr.Row():
  181.             show_text = gr.Checkbox(value=params['show_text'], label='Show message text under audio player')
  182.             remove_trailing_dots = gr.Checkbox(value=params['remove_trailing_dots'], label='Remove trailing "." from text segments before converting to audio')
  183.  
  184.         with gr.Row():
  185.             with gr.Row():
  186.                 voice = gr.Dropdown(get_available_voices(), label="Voice wav", value=params["voice"])
  187.                 create_refresh_button(voice, lambda: None, lambda: {'choices': get_available_voices(), 'value': params["voice"]}, 'refresh-button')
  188.  
  189.             language = gr.Dropdown(languages.keys(), label="Language", value=params["language"])
  190.  
  191.         with gr.Row():
  192.             preview_text = gr.Text(show_label=False, placeholder="Preview text", elem_id="silero_preview_text")
  193.             preview_play = gr.Button("Preview")
  194.             preview_audio = gr.HTML(visible=False)
  195.  
  196.         with gr.Row():
  197.             convert = gr.Button('Permanently replace audios with the message texts')
  198.             convert_cancel = gr.Button('Cancel', visible=False)
  199.             convert_confirm = gr.Button('Confirm (cannot be undone)', variant="stop", visible=False)
  200.  
  201.     # Convert history with confirmation
  202.     convert_arr = [convert_confirm, convert, convert_cancel]
  203.     convert.click(lambda: [gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)], None, convert_arr)
  204.     convert_confirm.click(
  205.         lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, convert_arr).then(
  206.         remove_tts_from_history, gradio('history'), gradio('history')).then(
  207.         chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None).then(
  208.         chat.redraw_html, gradio(ui_chat.reload_arr), gradio('display'))
  209.  
  210.     convert_cancel.click(lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, convert_arr)
  211.  
  212.     # Toggle message text in history
  213.     show_text.change(
  214.         lambda x: params.update({"show_text": x}), show_text, None).then(
  215.         toggle_text_in_history, gradio('history'), gradio('history')).then(
  216.         chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None).then(
  217.         chat.redraw_html, gradio(ui_chat.reload_arr), gradio('display'))
  218.  
  219.     # Event functions to update the parameters in the backend
  220.     activate.change(lambda x: params.update({"activate": x}), activate, None)
  221.     autoplay.change(lambda x: params.update({"autoplay": x}), autoplay, None)
  222.     remove_trailing_dots.change(lambda x: params.update({"remove_trailing_dots": x}), remove_trailing_dots, None)
  223.     voice.change(lambda x: params.update({"voice": x}), voice, None)
  224.     language.change(lambda x: params.update({"language": x}), language, None)
  225.  
  226.     # Play preview
  227.     preview_text.submit(voice_preview, preview_text, preview_audio)
  228.     preview_play.click(voice_preview, preview_text, preview_audio)
  229.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement