import argparse import logging import os import re import gradio.processing_utils as gr_pu import gradio as gr import librosa import numpy as np import soundfile from scipy.io import wavfile import tempfile import edge_tts import utils from inference.infer_tool import Svc logging.getLogger('numba').setLevel(logging.WARNING) logging.getLogger('markdown_it').setLevel(logging.WARNING) logging.getLogger('urllib3').setLevel(logging.WARNING) logging.getLogger('matplotlib').setLevel(logging.WARNING) sampling_rate = 44100 tts_voice = { "中文男": "zh-CN-YunxiNeural", "中文女": "zh-CN-XiaoyiNeural", "英文男": "en-US-EricNeural", "英文女": "en-US-AnaNeural" } hubert_dict = { "vec768l12": utils.get_speech_encoder("vec768l12", device="cpu"), "vec256l9": utils.get_speech_encoder("vec256l9", device="cpu") } def create_fn(model, spk): def svc_fn(input_audio, vc_transform, auto_f0, f0p): if input_audio is None: return 0, None sr, audio = input_audio audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32) audio = librosa.resample(audio, orig_sr=sr, target_sr=sampling_rate) if len(audio.shape) > 1: audio = librosa.to_mono(audio.transpose(1, 0)) temp_path = "temp.wav" soundfile.write(temp_path, audio, sampling_rate, format="wav") model.hubert_model = hubert_dict[model.speech_encoder] out_audio = model.slice_inference(raw_audio_path=temp_path, spk=spk, slice_db=-40, cluster_infer_ratio=0, noice_scale=0.4, clip_seconds=10, tran=vc_transform, f0_predictor=f0p, auto_predict_f0=auto_f0) model.clear_empty() os.remove(temp_path) return sampling_rate, out_audio async def tts_fn(input_text, gender, tts_rate, vc_transform, auto_f0, f0p): if input_text == '': return 0, None input_text = re.sub(r"[\n\,\(\) ]", "", input_text) voice = tts_voice[gender] ratestr = "+{:.0%}".format(tts_rate) if tts_rate >= 0 else "{:.0%}".format(tts_rate) communicate = edge_tts.Communicate(text=input_text, voice=voice, rate=ratestr) with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file: temp_path = tmp_file.name await communicate.save(temp_path) audio, sr = librosa.load(temp_path) audio = librosa.resample(audio, orig_sr=sr, target_sr=sampling_rate) os.remove(temp_path) temp_path = "temp.wav" wavfile.write(temp_path, sampling_rate, (audio * np.iinfo(np.int16).max).astype(np.int16)) sr, audio = gr_pu.audio_from_file(temp_path) input_audio = (sampling_rate, audio) return svc_fn(input_audio, vc_transform, auto_f0, f0p) return svc_fn, tts_fn if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--device', type=str, default='cpu') parser.add_argument('--api', action="store_true", default=False) parser.add_argument("--share", action="store_true", default=False, help="share gradio app") args = parser.parse_args() models = [] for f in os.listdir("models"): name = f model = Svc(fr"models/{f}/{f}.pth", f"models/{f}/config_{f}.json", device=args.device) cover = f"models/{f}/cover.png" if os.path.exists(f"models/{f}/cover.png") else f"models/{f}/cover.jpg" models.append((name, cover, create_fn(model, name))) with gr.Blocks() as app: gr.Markdown( "#