rcell commited on
Commit
d950ac3
Β·
1 Parent(s): ac38b6b

complete app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -4
app.py CHANGED
@@ -1,7 +1,78 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import os
3
+ os.system('cd monotonic_align && python setup.py build_ext --inplace && cd ..')
4
 
 
 
5
 
6
+ import json
7
+ import math
8
+ import torch
9
+ from torch import nn
10
+ from torch.nn import functional as F
11
+ from torch.utils.data import DataLoader
12
+
13
+ import commons
14
+ import utils
15
+ from data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate
16
+ from models import SynthesizerTrn
17
+ from text.symbols import symbols
18
+ from text import text_to_sequence
19
+
20
+ from scipy.io.wavfile import write
21
+
22
+
23
+ def get_text(text, hps):
24
+ text_norm = text_to_sequence(text, hps.data.text_cleaners)
25
+ if hps.data.add_blank:
26
+ text_norm = commons.intersperse(text_norm, 0)
27
+ text_norm = torch.LongTensor(text_norm)
28
+ # print(text_norm.shape)
29
+ return text_norm
30
+
31
+
32
+ hps_ms = utils.get_hparams_from_file("/configs/japanese_base.json")
33
+ hps = utils.get_hparams_from_file("/configs/japanese_base.json")
34
+ net_g_ms = SynthesizerTrn(
35
+ len(symbols),
36
+ hps_ms.data.filter_length // 2 + 1,
37
+ hps_ms.train.segment_size // hps.data.hop_length,
38
+ n_speakers=hps_ms.data.n_speakers,
39
+ **hps_ms.model)
40
+
41
+
42
+ def jtts(spkid, text):
43
+ sid = torch.LongTensor([spkid]) # speaker identity
44
+ stn_tst = get_text(text, hps_ms)
45
+
46
+ with torch.no_grad():
47
+ x_tst = stn_tst.unsqueeze(0)
48
+ x_tst_lengths = torch.LongTensor([stn_tst.size(0)])
49
+ # print(stn_tst.size())
50
+ audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][
51
+ 0, 0].data.float().numpy()
52
+ return
53
+
54
+ _ = utils.load_checkpoint("/output.pth", net_g_ms, None)
55
+
56
+
57
+ def tts(text):
58
+ sid = torch.LongTensor([2]) # speaker identity
59
+ stn_tst = get_text(text, hps_ms)
60
+
61
+ with torch.no_grad():
62
+ x_tst = stn_tst.unsqueeze(0)
63
+ x_tst_lengths = torch.LongTensor([stn_tst.size(0)])
64
+ # print(stn_tst.size())
65
+ audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][
66
+ 0, 0].data.float().numpy()
67
+ return "成功", (hps.data.sampling_rate, audio)
68
+
69
+ app = gr.Blocks()
70
+ with app:
71
+ tts_input1 = gr.TextArea(label="θ―·θΎ“ε…₯ζ—₯θ―­ζ–‡ζœ¬", value="こんにけは。")
72
+ # tts_input2 = gr.Dropdown(label="Speaker", choices=hps.speakers, type="index", value=hps.speakers[0])
73
+ tts_submit = gr.Button("Generate", variant="primary")
74
+ tts_output1 = gr.Textbox(label="Output Message")
75
+ tts_output2 = gr.Audio(label="Output Audio")
76
+ tts_submit.click(tts, [tts_input1], [tts_output1, tts_output2])
77
+
78
+ app.launch()