kevinwang676 commited on
Commit
1ab2f1b
·
verified ·
1 Parent(s): 82fbbf5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import importlib
2
+ import re
3
+
4
+ import gradio as gr
5
+ import yaml
6
+ from gradio.inputs import Textbox
7
+
8
+ from inference.svs.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
+
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, 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
+ 'text': s,
46
+ 'notes': n,
47
+ 'notes_duration': n_dur,
48
+ })
49
+ audio_out = audio_out * 32767
50
+ audio_out = audio_out.astype(np.int16)
51
+ audio_outs.append(audio_out)
52
+ audio_outs.append(np.zeros(int(hp['audio_sample_rate'] * 0.3)).astype(np.int16))
53
+ s = ""
54
+ n = ""
55
+ audio_outs = np.concatenate(audio_outs)
56
+ return hp['audio_sample_rate'], audio_outs
57
+
58
+ def run(self):
59
+ set_hparams(exp_name=self.exp_name, print_hparams=False)
60
+ infer_cls = self.inference_cls
61
+ self.infer_ins: BaseSVSInfer = infer_cls(hp)
62
+ example_inputs = self.example_inputs
63
+ for i in range(len(example_inputs)):
64
+ text, notes, notes_dur = example_inputs[i].split('<sep>')
65
+ example_inputs[i] = [text, notes, notes_dur]
66
+
67
+ iface = gr.Interface(fn=self.greet,
68
+ inputs=[
69
+ Textbox(lines=2, placeholder=None, default=example_inputs[0][0], label="input text"),
70
+ Textbox(lines=2, placeholder=None, default=example_inputs[0][1], label="input note"),
71
+ Textbox(lines=2, placeholder=None, default=example_inputs[0][2], label="input duration")]
72
+ ,
73
+ outputs="audio",
74
+ allow_flagging="never",
75
+ title=self.title,
76
+ description=self.description,
77
+ article=self.article,
78
+ examples=example_inputs,
79
+ enable_queue=True)
80
+ iface.launch(share=True, show_error=True)
81
+
82
+
83
+ if __name__ == '__main__':
84
+ gradio_config = yaml.safe_load(open('inference/svs/gradio/gradio_settings.yaml'))
85
+ g = GradioInfer(**gradio_config)
86
+ g.run()
87
+
88
+
89
+ # python inference/svs/gradio/infer.py --config usr/configs/midi/cascade/opencs/ds60_rel.yaml --exp_name 0303_opencpop_ds58_midi
90
+ # python inference/svs/ds_cascade.py --config usr/configs/midi/cascade/opencs/ds60_rel.yaml --exp_name 0303_opencpop_ds58_midi
91
+ # CUDA_VISIBLE_DEVICES=3 python inference/svs/gradio/infer.py --config usr/configs/midi/e2e/opencpop/ds100_adj_rel.yaml --exp_name 0228_opencpop_ds100_rel