From cefd1e045baf15c68fbdcac15fb1978850f8987f Mon Sep 17 00:00:00 2001 From: S95Sedan <89262821+S95Sedan@users.noreply.github.com> Date: Mon, 15 Jan 2024 21:51:00 +0100 Subject: [PATCH] Temperature and Repetition_penalty modification Modified script.py to enable temperature and repetition_penalty to be parsed and tts_server.py to recieve those variables. --- script.py | 37 ++++++++++++++++++++++++------------- tts_server.py | 18 ++++++++++-------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/script.py b/script.py index 677dd2d..e569ff8 100644 --- a/script.py +++ b/script.py @@ -489,12 +489,14 @@ def voice_preview(string): output_file = Path(params["output_folder_wav"]) / "voice_preview.wav" # Generate the audio language_code = languages.get(params["language"]) + temperature = params["local_temperature"] + repetition_penalty = params["local_repetition_penalty"] # Convert the WindowsPath object to a string before using it in JSON payload output_file_str = output_file.as_posix() # Lock before making the generate request with process_lock: generate_response = send_generate_request( - string, params["voice"], language_code, output_file_str + string, params["voice"], language_code, temperature, repetition_penalty, output_file_str ) # Check if lock is already acquired if process_lock.locked(): @@ -573,6 +575,8 @@ def output_modifier(string, state): return string = cleaned_string language_code = languages.get(params["language"]) + temperature = params["local_temperature"] + repetition_penalty = params["local_repetition_penalty"] # Create a list to store generated audio paths audio_files = [] if process_lock.acquire(blocking=False): @@ -635,7 +639,7 @@ def output_modifier(string, state): output_file_str = output_file.as_posix() output_file = get_output_filename(state) generate_response = send_generate_request( - cleaned_part, params["voice"], language_code, output_file_str + cleaned_part, params["voice"], language_code, temperature, repetition_penalty, output_file_str ) audio_path = generate_response.get("data", {}).get("audio_path") final_output_file = audio_path @@ -684,12 +688,14 @@ def get_output_filename(state): ############################################### #### SEND GENERATION REQUEST TO TTS ENGINE #### ############################################### -def send_generate_request(text, voice, language, output_file): +def send_generate_request(text, voice, language, temperature, repetition_penalty, output_file): url = f"{base_url}/api/generate" payload = { "text": text, "voice": voice, "language": language, + "temperature": temperature, + "repetition_penalty": repetition_penalty, "output_file": output_file, } headers = {"Content-Type": "application/json"} @@ -737,11 +743,13 @@ def input_modifier(string, state): def ui(): with gr.Accordion(params["branding"] + " TTS (XTTSv2)"): + # Activate alltalk_tts, Enable autoplay, Show text with gr.Row(): activate = gr.Checkbox(value=params['activate'], label='Enable TTS') autoplay = gr.Checkbox(value=params['autoplay'], label='Autoplay TTS') show_text = gr.Checkbox(value=params['show_text'], label='Show Text') - + + # Low vram enable, Deepspeed enable, Remove trailing dots with gr.Row(): low_vram = gr.Checkbox(value=params['low_vram'], label='Enable Low VRAM Mode') low_vram_play = gr.HTML(visible=False) @@ -749,6 +757,7 @@ def ui(): deepspeed_checkbox_play = gr.HTML(visible=False) remove_trailing_dots = gr.Checkbox(value=params['remove_trailing_dots'], label='Remove trailing "."') + # TTS method, Character voice selection with gr.Row(): model_loader_choices=["API TTS", "API Local", "XTTSv2 Local"] if finetuned_model: @@ -769,17 +778,19 @@ def ui(): voice = gr.Dropdown(available_voices, label="Character Voice", value=default_voice, allow_custom_value=True, ) create_refresh_button(voice, lambda: None, lambda: {"choices": get_available_voices(), "value": params["voice"],}, "refresh-button",) + # Language, Narrator voice with gr.Row(): language = gr.Dropdown(languages.keys(), label="Language", value=params["language"]) with gr.Row(): narrator_voice_gr = gr.Dropdown(get_available_voices(), label="Narrator Voice", allow_custom_value=True, value=params["narrator_voice"]) create_refresh_button(narrator_voice_gr, lambda: None, lambda: {"choices": get_available_voices(), "value": params["narrator_voice"],}, "refresh-button") - # Temperature and Repetition Penalty (Not yet parsed to api/implemented) - # with gr.Row(): - # local_temperature_gr = gr.Slider(minimum=0, maximum=1, step=0.05, label="Temperature", value=params["local_temperature"]) - # local_repetition_penalty_gr = gr.Slider(minimum=0, maximum=20, step=0.5, label="Repetition Penalty", value=params["local_repetition_penalty"]) + # Temperature, Repetition Penalty + with gr.Row(): + local_temperature_gr = gr.Slider(minimum=0.05, maximum=1, step=0.05, label="Temperature", value=params["local_temperature"]) + local_repetition_penalty_gr = gr.Slider(minimum=0.5, maximum=20, step=0.5, label="Repetition Penalty", value=params["local_repetition_penalty"]) + # Narrator enable, Non quoted text, Explanation text with gr.Row(): with gr.Row(): narrator_enabled_gr = gr.Radio(choices={"Enabled": "true", "Disabled": "false"}, label="Narrator", value="Enabled" if params.get("narrator_enabled") else "Disabled") @@ -787,6 +798,7 @@ def ui(): explanation_text = gr.HTML( f"

⚙️ Settings and Documentation Page⚙️
- Low VRAM Mode and Deepspeed take 15 seconds to be enabled or disabled.
- The DeepSpeed checkbox is only visible if DeepSpeed is present.

") + # Preview speech with gr.Row(): preview_text = gr.Text( show_label=False, @@ -795,7 +807,7 @@ def ui(): ) preview_play = gr.Button("Preview") preview_audio = gr.HTML(visible=False) - + with gr.Row(): convert = gr.Button("Permanently replace audios with the message texts") convert_cancel = gr.Button("Cancel", visible=False) @@ -864,10 +876,9 @@ def ui(): voice.change(lambda x: params.update({"voice": x}), voice, None) language.change(lambda x: params.update({"language": x}), language, None) - - # TS Settings (Not yet parsed to api/implemented) - # local_temperature_gr.change(lambda x: params.update({"local_temperature": x}), local_temperature_gr, None) - # local_repetition_penalty_gr.change(lambda x: params.update({"local_repetition_penalty": x}), local_repetition_penalty_gr, None) + # TSS Settings + local_temperature_gr.change(lambda x: params.update({"local_temperature": x}), local_temperature_gr, None) + local_repetition_penalty_gr.change(lambda x: params.update({"local_repetition_penalty": x}), local_repetition_penalty_gr, None) # Narrator selection actions narrator_enabled_gr.change(update_narrator_enabled, narrator_enabled_gr, None) diff --git a/tts_server.py b/tts_server.py index 4d2c48b..c431f40 100644 --- a/tts_server.py +++ b/tts_server.py @@ -473,16 +473,16 @@ async def deepspeed(request: Request, new_deepspeed_value: bool): ######################## # TTS VOICE GENERATION METHODS (called from voice_preview and output_modifer) -async def generate_audio(text, voice, language, output_file, streaming=False): +async def generate_audio(text, voice, language, temperature, repetition_penalty, output_file, streaming=False): # Get the async generator from the internal function - response = generate_audio_internal(text, voice, language, output_file, streaming) + response = generate_audio_internal(text, voice, language, temperature, repetition_penalty, output_file, streaming) # If streaming, then return the generator as-is, otherwise just exhaust it and return if streaming: return response async for _ in response: pass -async def generate_audio_internal(text, voice, language, output_file, streaming): +async def generate_audio_internal(text, voice, language, temperature, repetition_penalty, output_file, streaming): global model if params["low_vram"] and device == "cpu": await switch_device() @@ -504,9 +504,9 @@ async def generate_audio_internal(text, voice, language, output_file, streaming) "language": language, "gpt_cond_latent": gpt_cond_latent, "speaker_embedding": speaker_embedding, - "temperature": float(params["local_temperature"]), + "temperature": float(temperature), "length_penalty": float(model.config.length_penalty), - "repetition_penalty": float(params["local_repetition_penalty"]), + "repetition_penalty": float(repetition_penalty), "top_k": int(model.config.top_k), "top_p": float(model.config.top_p), "enable_text_splitting": True @@ -560,9 +560,9 @@ async def generate_audio_internal(text, voice, language, output_file, streaming) file_path=output_file, speaker_wav=[f"{this_dir}/voices/{voice}"], language=language, - temperature=float(params["local_temperature"]), + temperature=temperature, length_penalty=model.config.length_penalty, - repetition_penalty=float(params["local_repetition_penalty"]), + repetition_penalty=repetition_penalty, top_k=model.config.top_k, top_p=model.config.top_p, ) @@ -602,10 +602,12 @@ async def generate(request: Request): text = data["text"] voice = data["voice"] language = data["language"] + temperature = data["temperature"] + repetition_penalty = data["repetition_penalty"] output_file = data["output_file"] streaming = False # Generation logic - response = await generate_audio(text, voice, language, output_file, streaming) + response = await generate_audio(text, voice, language, temperature, repetition_penalty, output_file, streaming) if streaming: return StreamingResponse(response, media_type="audio/wav") return JSONResponse(