Spaces:
Build error
Build error
File size: 7,840 Bytes
fabced5 f5460b4 fabced5 f5460b4 fabced5 09a6b36 fabced5 09a6b36 fabced5 09a6b36 fabced5 09a6b36 fabced5 bcf198e fabced5 bcf198e fabced5 a36c53d fabced5 a36c53d fabced5 a36c53d fabced5 a36c53d fabced5 a36c53d fabced5 a36c53d fabced5 88cfae4 fabced5 88cfae4 fabced5 88cfae4 fabced5 88cfae4 fabced5 88cfae4 fabced5 5c74855 fabced5 88cfae4 fabced5 f5460b4 fabced5 88cfae4 fabced5 88cfae4 fabced5 5c74855 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
"""
TODO:
+ [x] Load Configuration
+ [ ] Checking
+ [ ] Better saving directory
"""
import numpy as np
from pathlib import Path
import torch.nn as nn
import torch
import torchaudio
from transformers import pipeline
from pathlib import Path
# local import
import sys
from espnet2.bin.tts_inference import Text2Speech
from transformers import AutoTokenizer, AutoFeatureExtractor, AutoModelForCTC# pdb.set_trace()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
sys.path.append("src")
import gradio as gr
# ASR part
audio_files = [
str(x)
for x in sorted(
Path(
"/home/kevingeng/Disk2/laronix/laronix_automos/data/20230103_video"
).glob("**/*wav")
)
]
# audio_files = [str(x) for x in sorted(Path("./data/Patient_sil_trim_16k_normed_5_snr_40/Rainbow").glob("**/*wav"))]
# transcriber = pipeline(
# "automatic-speech-recognition",
# model="KevinGeng/PAL_John_128_train_dev_test_seed_1",
# )
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
processor = AutoProcessor.from_pretrained("KevinGeng/whipser_medium_en_PAL300_step25")
model = AutoModelForSpeechSeq2Seq.from_pretrained("KevinGeng/whipser_medium_en_PAL300_step25")
transcriber = pipeline("automatic-speech-recognition", model="KevinGeng/whipser_medium_en_PAL300_step25")
# @title English multi-speaker pretrained model { run: "auto" }
lang = "English"
tag = "kan-bayashi/libritts_xvector_vits"
# vits needs no vocoder
vocoder_tag = "parallel_wavegan/vctk_parallel_wavegan.v1.long" # @param ["none", "parallel_wavegan/vctk_parallel_wavegan.v1.long", "parallel_wavegan/vctk_multi_band_melgan.v2", "parallel_wavegan/vctk_style_melgan.v1", "parallel_wavegan/vctk_hifigan.v1", "parallel_wavegan/libritts_parallel_wavegan.v1.long", "parallel_wavegan/libritts_multi_band_melgan.v2", "parallel_wavegan/libritts_hifigan.v1", "parallel_wavegan/libritts_style_melgan.v1"] {type:"string"}
from espnet2.bin.tts_inference import Text2Speech
from espnet2.utils.types import str_or_none
# local import
text2speech = Text2Speech.from_pretrained(
train_config = "TTS_model/config.yaml",
model_file="TTS_model/train.total_count.ave_10best.pth",
vocoder_tag=str_or_none(vocoder_tag),
device="cuda",
use_att_constraint=False,
backward_window=1,
forward_window=3,
speed_control_alpha=1.0,
)
import glob
import os
import numpy as np
import kaldiio
# Get model directory path
from espnet_model_zoo.downloader import ModelDownloader
d = ModelDownloader()
model_dir = os.path.dirname(d.download_and_unpack(tag)["train_config"])
# Speaker x-vector selection
xvector_ark = [
p
for p in glob.glob(
f"xvector/test-clean/spk_xvector.ark", recursive=True
)
if "test" in p
][0]
xvectors = {k: v for k, v in kaldiio.load_ark(xvector_ark)}
spks = list(xvectors.keys())
male_spks = {
"Male1": "2300_131720",
"Male2": "1320_122612",
}
# "M3": "1188_133604",
# "M4": "61_70970",
female_spks = {"Female1": "2961_961", "Female2": "8463_287645", }
# "F3": "121_121726"
spks = dict(male_spks, **female_spks)
spk_names = sorted(spks.keys())
def ASRTTS(audio_file, spk_name, ref_text=""):
spk = spks[spk_name]
spembs = xvectors[spk]
if ref_text == "":
reg_text = transcriber(audio_file)["text"]
else:
reg_text = ref_text
speech, sr = torchaudio.load(
audio_file, channels_first=True
) # Mono channel
wav_tensor_spembs = text2speech(
text=reg_text, speech=speech, spembs=spembs
)["wav"]
wav_numpy = wav_tensor_spembs.unsqueeze(1).to("cpu")
sample_rate = 22050
save_id = (
"./wav/" + Path(audio_file).stem + "_" + spk_name + "_spkembs.wav"
)
torchaudio.save(
save_id,
src=wav_tensor_spembs.unsqueeze(0).to("cpu"),
sample_rate=22050,
)
return save_id, reg_text
def ASRTTS_clean(audio_file, spk_name):
spk = spks[spk_name]
spembs = xvectors[spk]
reg_text = transcriber(audio_file)["text"]
speech, sr = torchaudio.load(
audio_file, channels_first=True
) # Mono channel
wav_tensor_spembs = text2speech(
text=reg_text, speech=speech, spembs=spembs
)["wav"]
wav_numpy = wav_tensor_spembs.unsqueeze(1).to("cpu")
sample_rate = 22050
save_id = (
"./wav/" + Path(audio_file).stem + "_" + spk_name + "_spkembs.wav"
)
torchaudio.save(
save_id,
src=wav_tensor_spembs.unsqueeze(0).to("cpu"),
sample_rate=22050,
)
return save_id
reference_textbox = gr.Textbox(
value="",
placeholder="Input reference here",
label="Reference",
)
recognization_textbox = gr.Textbox(
value="",
placeholder="Output recognization here",
label="recognization_textbox",
)
speaker_option = gr.Radio(choices=spk_names, label="Speaker")
input_audio = gr.Audio(
source="upload", type="filepath", label="Audio_to_Evaluate"
)
output_audio = gr.Audio(
source="upload", file="filepath", label="Synthesized Audio"
)
examples = [
["./samples/001.wav", "M1", ""],
["./samples/002.wav", "M2", ""],
["./samples/003.wav", "F1", ""],
["./samples/004.wav", "F2", ""],
]
def change_audiobox(choice):
if choice == "upload":
input_audio = gr.Audio.update(source="upload", visible=True)
elif choice == "microphone":
input_audio = gr.Audio.update(source="microphone", visible=True)
else:
input_audio = gr.Audio.update(visible=False)
return input_audio
def show_icon(choice):
if choice == "Male1":
spk_icon = gr.Image.update(value="speaker_icons/male1.png", visible=True)
elif choice == "Male2":
spk_icon = gr.Image.update(value="speaker_icons/male2.png", visible=True)
elif choice == "Female1":
spk_icon = gr.Image.update(value="speaker_icons/female1.png", visible=True)
elif choice == "Female2":
spk_icon = gr.Image.update(value="speaker_icons/female2.png", visible=True)
return spk_icon
def get_download_file(audio_file=None):
if audio_file == None:
output_audio_file = gr.File.update(visible=False)
else:
output_audio_file = gr.File.update(visible=True)
return output_audio_file
def download_file(audio_file):
return gr.File(value=audio_file)
# pdb.set_trace()
with gr.Blocks(
analytics_enabled=False,
css=".gradio-container {background-color: #78BD91}",
) as demo:
with gr.Column(elem_id="Column"):
input_format = gr.Radio(
choices=["microphone", "upload"], label="Choose your input format", elem_id="input_format"
)
input_audio = gr.Audio(
source="microphone",
type="filepath",
label="Input Audio",
interactive=True,
visible=False,
elem_id="input_audio"
)
input_format.change(
fn=change_audiobox, inputs=input_format, outputs=input_audio
)
speaker_option = gr.Radio(choices=spk_names, value="Male1", label="Choose your voice profile")
spk_icon = gr.Image(value="speaker_icons/male1.png",
type="filepath",
image_mode="RGB",
source="upload",
shape=[50, 50],
interactive=True,
visible=True)
speaker_option.change(
fn=show_icon, inputs=speaker_option, outputs=spk_icon
)
b2 = gr.Button("Convert")
output_audio = gr.Audio(
source="upload", file="filepath", label="Converted Audio", interactive=False
)
b2.click(
ASRTTS_clean,
inputs=[input_audio, speaker_option],
outputs=output_audio,
api_name="convert"
)
demo.launch(share=False)
|