-
Notifications
You must be signed in to change notification settings - Fork 33
/
app.py
165 lines (134 loc) · 5.26 KB
/
app.py
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
#!flask/bin/python
import os, traceback, json
import hashlib
import argparse
from flask_cors import CORS, cross_origin
from flask import Flask, request, render_template, jsonify, \
send_from_directory, make_response, send_file
from synthesizer import Synthesizer
from utils import str2bool, makedirs, add_postfix
import base64
ROOT_PATH = "web"
AUDIO_DIR = "audio"
AUDIO_PATH = os.path.join(ROOT_PATH, AUDIO_DIR)
base_path = os.path.dirname(os.path.realpath(__file__))
static_path = os.path.join(base_path, 'web/static')
global_config = None
synthesizer = Synthesizer()
app = Flask(__name__, root_path=ROOT_PATH, static_url_path='')
CORS(app)
def generate_audio_response(text, condition_on_ref, ref_audio, ratios):
hashed_text = hashlib.md5(text.encode('utf-8')).hexdigest()
relative_dir_path = os.path.join(AUDIO_DIR, 'tacotron2-vae')
relative_audio_path = os.path.join(
relative_dir_path, "{}.wav".format(hashed_text))
real_path = os.path.join(ROOT_PATH, relative_audio_path)
makedirs(os.path.dirname(real_path))
print(ref_audio)
if condition_on_ref:
ref_audio = ref_audio.replace('/uploads', '/home/jhoh/dataset')
try:
synthesizer.synthesize(text, real_path, condition_on_ref, ref_audio, ratios)
except Exception as e:
traceback.print_exc()
return jsonify(success=False), 400
return send_file(
relative_audio_path,
mimetype="audio/wav",
as_attachment=True,
attachment_filename=hashed_text + ".wav")
def generate_api_response(args):
print(args)
text = args['text']
print(text)
condition_on_ref = False
ref_audio = None
n = float(args['neu'])
s = float(args['sad'])
h = float(args['hap'])
a = float(args['ang'])
sigma = n+s+h+a
if sigma:
ratios = [round(x / sigma * 100)/100 for x in [n, s, h, a]]
else:
ratios = [1.0, 0.0, 0.0, 0.0]
hashed_text = hashlib.md5(text.encode('utf-8')).hexdigest()
relative_dir_path = os.path.join(AUDIO_DIR, 'tacotron2-vae')
relative_audio_path = os.path.join(
relative_dir_path, "{}.wav".format(hashed_text))
real_path = os.path.join(ROOT_PATH, relative_audio_path)
makedirs(os.path.dirname(real_path))
if condition_on_ref:
ref_audio = ref_audio.replace('/uploads', '/home/jhoh/dataset')
try:
synthesizer.synthesize(text, real_path, condition_on_ref, ref_audio, ratios)
except Exception as e:
traceback.print_exc()
return jsonify(success=False), 400
b64_data = base64.b64encode(open(real_path, "rb").read())
return json.dumps({"params":{
"text":text,
"neu": n, "hap": h, "sad": s, "ang": a},
"data": str(b64_data.decode('utf-8'))})
@app.route('/')
def index():
text = request.args.get('text') or "듣고 싶은 문장을 입력해 주세요."
return render_template('index.html', text=text)
@app.route('/api', methods=['POST'])
def API():
args = json.loads(request.data)
return generate_api_response(args)
@app.route('/generate')
def view_method():
text = request.args.get('text')
condition_on_ref = request.args.get('con')
if text:
if condition_on_ref=='true':
ref_audio = request.args.get('ref')
condition_on_ref = True
ratios = None
return generate_audio_response(text, condition_on_ref, ref_audio, ratios) # ref_audi, ratios
else:
n = float(request.args.get('n'))
s = float(request.args.get('s'))
h = float(request.args.get('h'))
a = float(request.args.get('a'))
sigma = n+s+h+a
if sigma:
ratios = [round(x / sigma * 100)/100 for x in [n, s, h, a]]
else:
ratios = [1.0, 0.0, 0.0, 0.0]
ref_audio = None
condition_on_ref = False
return generate_audio_response(text, condition_on_ref, ref_audio, ratios) # ref_audi, ratios
else:
return {}
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory(
os.path.join(static_path, 'js'), path)
@app.route('/css/<path:path>')
def send_css(path):
return send_from_directory(
os.path.join(static_path, 'css'), path)
@app.route('/audio/<path:path>')
def send_audio(path):
return send_from_directory(
os.path.join(static_path, 'audio'), path)
@app.route('/uploads/<path:path>')
def send_uploads(path):
return send_from_directory(
os.path.join(static_path, 'uploads'), path)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--checkpoint_path', required=True)
parser.add_argument('--waveglow_path', required=True)
parser.add_argument('--port', default=51000, type=int)
parser.add_argument('--debug', default=False, type=str2bool)
parser.add_argument('--is_korean', default=True, type=str2bool)
config = parser.parse_args()
if os.path.exists(config.checkpoint_path):
synthesizer.load(config.checkpoint_path, config.waveglow_path)
else:
print(" [!] load_path not found: {}".format(config.checkpoint_path))
app.run(host='0.0.0.0', threaded=True, port=config.port, debug=config.debug)