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radio.components import Textbox, Dropdown
|
| 7 |
+
|
| 8 |
+
from inference.m4singer.base_svs_infer import BaseSVSInfer
|
| 9 |
+
from utils.hparams import set_hparams
|
| 10 |
+
from utils.hparams import hparams as hp
|
| 11 |
+
import numpy as np
|
| 12 |
+
from inference.m4singer.gradio.share_btn import community_icon_html, loading_icon_html, share_js
|
| 13 |
+
|
| 14 |
+
import spaces
|
| 15 |
+
|
| 16 |
+
class GradioInfer:
|
| 17 |
+
def __init__(self, exp_name, inference_cls, title, description, article, example_inputs):
|
| 18 |
+
self.exp_name = exp_name
|
| 19 |
+
self.title = title
|
| 20 |
+
self.description = description
|
| 21 |
+
self.article = article
|
| 22 |
+
self.example_inputs = example_inputs
|
| 23 |
+
pkg = ".".join(inference_cls.split(".")[:-1])
|
| 24 |
+
cls_name = inference_cls.split(".")[-1]
|
| 25 |
+
self.inference_cls = getattr(importlib.import_module(pkg), cls_name)
|
| 26 |
+
|
| 27 |
+
@spaces.GPU(duration=180)
|
| 28 |
+
def greet(self, singer, text, notes, notes_duration):
|
| 29 |
+
PUNCS = '。?;:'
|
| 30 |
+
sents = re.split(rf'([{PUNCS}])', text.replace('\n', ','))
|
| 31 |
+
sents_notes = re.split(rf'([{PUNCS}])', notes.replace('\n', ','))
|
| 32 |
+
sents_notes_dur = re.split(rf'([{PUNCS}])', notes_duration.replace('\n', ','))
|
| 33 |
+
|
| 34 |
+
if sents[-1] not in list(PUNCS):
|
| 35 |
+
sents = sents + ['']
|
| 36 |
+
sents_notes = sents_notes + ['']
|
| 37 |
+
sents_notes_dur = sents_notes_dur + ['']
|
| 38 |
+
|
| 39 |
+
audio_outs = []
|
| 40 |
+
s, n, n_dur = "", "", ""
|
| 41 |
+
for i in range(0, len(sents), 2):
|
| 42 |
+
if len(sents[i]) > 0:
|
| 43 |
+
s += sents[i] + sents[i + 1]
|
| 44 |
+
n += sents_notes[i] + sents_notes[i+1]
|
| 45 |
+
n_dur += sents_notes_dur[i] + sents_notes_dur[i+1]
|
| 46 |
+
if len(s) >= 400 or (i >= len(sents) - 2 and len(s) > 0):
|
| 47 |
+
audio_out = self.infer_ins.infer_once({
|
| 48 |
+
'spk_name': singer,
|
| 49 |
+
'text': s,
|
| 50 |
+
'notes': n,
|
| 51 |
+
'notes_duration': n_dur,
|
| 52 |
+
})
|
| 53 |
+
audio_out = audio_out * 32767
|
| 54 |
+
audio_out = audio_out.astype(np.int16)
|
| 55 |
+
audio_outs.append(audio_out)
|
| 56 |
+
audio_outs.append(np.zeros(int(hp['audio_sample_rate'] * 0.3)).astype(np.int16))
|
| 57 |
+
s = ""
|
| 58 |
+
n = ""
|
| 59 |
+
audio_outs = np.concatenate(audio_outs)
|
| 60 |
+
return (hp['audio_sample_rate'], audio_outs), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 61 |
+
|
| 62 |
+
def run(self):
|
| 63 |
+
set_hparams(config=f'checkpoints/{self.exp_name}/config.yaml', exp_name=self.exp_name, print_hparams=False)
|
| 64 |
+
infer_cls = self.inference_cls
|
| 65 |
+
self.infer_ins: BaseSVSInfer = infer_cls(hp)
|
| 66 |
+
example_inputs = self.example_inputs
|
| 67 |
+
for i in range(len(example_inputs)):
|
| 68 |
+
singer, text, notes, notes_dur = example_inputs[i].split('<sep>')
|
| 69 |
+
example_inputs[i] = [singer, text, notes, notes_dur]
|
| 70 |
+
|
| 71 |
+
singerList = \
|
| 72 |
+
[
|
| 73 |
+
'Tenor-1', 'Tenor-2', 'Tenor-3', 'Tenor-4', 'Tenor-5', 'Tenor-6', 'Tenor-7',
|
| 74 |
+
'Alto-1', 'Alto-2', 'Alto-3', 'Alto-4', 'Alto-5', 'Alto-6', 'Alto-7',
|
| 75 |
+
'Soprano-1', 'Soprano-2', 'Soprano-3',
|
| 76 |
+
'Bass-1', 'Bass-2', 'Bass-3',
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
css = """
|
| 80 |
+
#share-btn-container {
|
| 81 |
+
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
| 82 |
+
}
|
| 83 |
+
#share-btn {
|
| 84 |
+
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;right:0;
|
| 85 |
+
}
|
| 86 |
+
#share-btn * {
|
| 87 |
+
all: unset;
|
| 88 |
+
}
|
| 89 |
+
#share-btn-container div:nth-child(-n+2){
|
| 90 |
+
width: auto !important;
|
| 91 |
+
min-height: 0px !important;
|
| 92 |
+
}
|
| 93 |
+
#share-btn-container .wrap {
|
| 94 |
+
display: none !important;
|
| 95 |
+
}
|
| 96 |
+
"""
|
| 97 |
+
with gr.Blocks(css=css) as demo:
|
| 98 |
+
gr.HTML("""<div style="text-align: center; margin: 0 auto;">
|
| 99 |
+
<div
|
| 100 |
+
style="
|
| 101 |
+
display: inline-flex;
|
| 102 |
+
align-items: center;
|
| 103 |
+
gap: 0.8rem;
|
| 104 |
+
font-size: 1.75rem;
|
| 105 |
+
"
|
| 106 |
+
>
|
| 107 |
+
<h1 style="font-weight: 900; margin-bottom: 10px; margin-top: 14px;">
|
| 108 |
+
M4Singer
|
| 109 |
+
</h1>
|
| 110 |
+
</div>
|
| 111 |
+
</div>
|
| 112 |
+
"""
|
| 113 |
+
)
|
| 114 |
+
gr.Markdown(self.description)
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column():
|
| 117 |
+
singer_l = Dropdown(choices=singerList, value=example_inputs[0][0], label="SingerID", elem_id="inp_singer")
|
| 118 |
+
inp_text = Textbox(lines=2, placeholder=None, value=example_inputs[0][1], label="input text", elem_id="inp_text")
|
| 119 |
+
inp_note = Textbox(lines=2, placeholder=None, value=example_inputs[0][2], label="input note", elem_id="inp_note")
|
| 120 |
+
inp_duration = Textbox(lines=2, placeholder=None, value=example_inputs[0][3], label="input duration", elem_id="inp_duration")
|
| 121 |
+
generate = gr.Button("Generate Singing Voice from Musical Score")
|
| 122 |
+
with gr.Column():
|
| 123 |
+
singing_output = gr.Audio(label="Result", type="numpy", elem_id="music-output")
|
| 124 |
+
|
| 125 |
+
with gr.Group(elem_id="share-btn-container"):
|
| 126 |
+
community_icon = gr.HTML(community_icon_html, visible=False)
|
| 127 |
+
loading_icon = gr.HTML(loading_icon_html, visible=False)
|
| 128 |
+
share_button = gr.Button("滔滔AI,为爱滔滔💕", elem_id="share-btn", visible=False)
|
| 129 |
+
gr.Examples(examples=self.example_inputs,
|
| 130 |
+
inputs=[singer_l, inp_text, inp_note, inp_duration],
|
| 131 |
+
outputs=[singing_output, share_button, community_icon, loading_icon],
|
| 132 |
+
fn=self.greet,
|
| 133 |
+
cache_examples=True)
|
| 134 |
+
gr.Markdown(self.article)
|
| 135 |
+
generate.click(self.greet,
|
| 136 |
+
inputs=[singer_l, inp_text, inp_note, inp_duration],
|
| 137 |
+
outputs=[singing_output, share_button, community_icon, loading_icon],)
|
| 138 |
+
demo.queue().launch(share=True, show_error=True)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
if __name__ == '__main__':
|
| 142 |
+
gradio_config = yaml.safe_load(open('inference/m4singer/gradio/gradio_settings.yaml'))
|
| 143 |
+
g = GradioInfer(**gradio_config)
|
| 144 |
+
g.run()
|