import spaces import os import random import argparse import torch import gradio as gr import numpy as np import ChatTTS from OpenVoice import se_extractor from OpenVoice.api import ToneColorConverter import soundfile print("loading ChatTTS model...") chat = ChatTTS.Chat() chat.load_models() def generate_seed(): new_seed = random.randint(1, 100000000) return { "__type__": "update", "value": new_seed } @spaces.GPU def chat_tts(text, temperature, top_P, top_K, audio_seed_input, text_seed_input, refine_text_flag, refine_text_input, output_path=None): torch.manual_seed(audio_seed_input) rand_spk = torch.randn(768) params_infer_code = { 'spk_emb': rand_spk, 'temperature': temperature, 'top_P': top_P, 'top_K': top_K, } params_refine_text = {'prompt': '[oral_2][laugh_0][break_6]'} torch.manual_seed(text_seed_input) if refine_text_flag: if refine_text_input: params_refine_text['prompt'] = refine_text_input text = chat.infer(text, skip_refine_text=False, refine_text_only=True, params_refine_text=params_refine_text, params_infer_code=params_infer_code ) print("Text has been refined!") wav = chat.infer(text, skip_refine_text=True, params_refine_text=params_refine_text, params_infer_code=params_infer_code ) audio_data = np.array(wav[0]).flatten() sample_rate = 24000 text_data = text[0] if isinstance(text, list) else text if output_path is None: return [(sample_rate, audio_data), text_data] else: soundfile.write(output_path, audio_data, sample_rate) return text_data # OpenVoice Clone ckpt_converter = 'OpenVoice/checkpoints/converter' device = "cuda:0" if torch.cuda.is_available() else "cpu" tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device=device) tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth') def generate_audio(text, audio_ref, temperature, top_P, top_K, audio_seed_input, text_seed_input, refine_text_flag, refine_text_input): save_path = "output.wav" if audio_ref != "" : # Run the base speaker tts src_path = "tmp.wav" text_data = chat_tts(text, temperature, top_P, top_K, audio_seed_input, text_seed_input, refine_text_flag, refine_text_input, src_path) print("Ready for voice cloning!") source_se, audio_name = se_extractor.get_se(src_path, tone_color_converter, target_dir='processed', vad=True) reference_speaker = audio_ref target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, target_dir='processed', vad=True) print("Get voices segment!") # Run the tone color converter # convert from file tone_color_converter.convert( audio_src_path=src_path, src_se=source_se, tgt_se=target_se, output_path=save_path) else: chat_tts(text, temperature, top_P, top_K, audio_seed_input, text_seed_input, refine_text_flag, refine_text_input, save_path) print("Finished!") return [save_path, text_data] with gr.Blocks() as demo: gr.Markdown("#