kevinwang676 commited on
Commit
f350130
1 Parent(s): 16974bc

Create app.py

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