2018-06-05 12:15:48 +00:00
#!flask/bin/python
import argparse
2019-11-27 15:40:32 +00:00
import os
2019-07-19 06:46:23 +00:00
from flask import Flask , request , render_template , send_file
2019-11-27 15:40:32 +00:00
from TTS . server . synthesizer import Synthesizer
def create_argparser ( ) :
def convert_boolean ( x ) :
return x . lower ( ) in [ ' true ' , ' 1 ' , ' yes ' ]
parser = argparse . ArgumentParser ( )
parser . add_argument ( ' --tts_checkpoint ' , type = str , help = ' path to TTS checkpoint file ' )
parser . add_argument ( ' --tts_config ' , type = str , help = ' path to TTS config.json file ' )
parser . add_argument ( ' --tts_speakers ' , type = str , help = ' path to JSON file containing speaker ids, if speaker ids are used in the model ' )
2020-02-04 16:09:59 +00:00
parser . add_argument ( ' --wavernn_lib_path ' , type = str , default = None , help = ' path to WaveRNN project folder to be imported. If this is not passed, model uses Griffin-Lim for synthesis. ' )
parser . add_argument ( ' --wavernn_file ' , type = str , default = None , help = ' path to WaveRNN checkpoint file. ' )
parser . add_argument ( ' --wavernn_config ' , type = str , default = None , help = ' path to WaveRNN config file. ' )
2019-11-27 15:40:32 +00:00
parser . add_argument ( ' --is_wavernn_batched ' , type = convert_boolean , default = False , help = ' true to use batched WaveRNN. ' )
2020-02-13 14:54:30 +00:00
parser . add_argument ( ' --pwgan_lib_path ' , type = str , default = None , help = ' path to ParallelWaveGAN project folder to be imported. If this is not passed, model uses Griffin-Lim for synthesis. ' )
parser . add_argument ( ' --pwgan_file ' , type = str , default = None , help = ' path to ParallelWaveGAN checkpoint file. ' )
parser . add_argument ( ' --pwgan_config ' , type = str , default = None , help = ' path to ParallelWaveGAN config file. ' )
2019-11-27 15:40:32 +00:00
parser . add_argument ( ' --port ' , type = int , default = 5002 , help = ' port to listen on. ' )
parser . add_argument ( ' --use_cuda ' , type = convert_boolean , default = False , help = ' true to use CUDA. ' )
parser . add_argument ( ' --debug ' , type = convert_boolean , default = False , help = ' true to enable Flask debug mode. ' )
return parser
synthesizer = None
2020-02-13 14:54:30 +00:00
embedded_models_folder = os . path . join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , ' model ' )
2019-11-27 15:40:32 +00:00
2020-02-13 14:54:30 +00:00
embedded_tts_folder = os . path . join ( embedded_models_folder , ' tts ' )
tts_checkpoint_file = os . path . join ( embedded_tts_folder , ' checkpoint.pth.tar ' )
tts_config_file = os . path . join ( embedded_tts_folder , ' config.json ' )
2018-06-05 12:15:48 +00:00
2020-02-13 14:54:30 +00:00
embedded_wavernn_folder = os . path . join ( embedded_models_folder , ' wavernn ' )
wavernn_checkpoint_file = os . path . join ( embedded_wavernn_folder , ' checkpoint.pth.tar ' )
wavernn_config_file = os . path . join ( embedded_wavernn_folder , ' config.json ' )
embedded_pwgan_folder = os . path . join ( embedded_models_folder , ' pwgan ' )
pwgan_checkpoint_file = os . path . join ( embedded_pwgan_folder , ' checkpoint.pkl ' )
pwgan_config_file = os . path . join ( embedded_pwgan_folder , ' config.yml ' )
2020-02-04 10:16:48 +00:00
args = create_argparser ( ) . parse_args ( )
2020-02-13 14:54:30 +00:00
# If these were not specified in the CLI args, use default values with embedded model files
if not args . tts_checkpoint and os . path . isfile ( tts_checkpoint_file ) :
args . tts_checkpoint = tts_checkpoint_file
if not args . tts_config and os . path . isfile ( tts_config_file ) :
args . tts_config = tts_config_file
if not args . wavernn_file and os . path . isfile ( wavernn_checkpoint_file ) :
args . wavernn_file = wavernn_checkpoint_file
if not args . wavernn_config and os . path . isfile ( wavernn_config_file ) :
args . wavernn_config = wavernn_config_file
if not args . pwgan_file and os . path . isfile ( pwgan_checkpoint_file ) :
args . pwgan_file = pwgan_checkpoint_file
if not args . pwgan_config and os . path . isfile ( pwgan_config_file ) :
args . pwgan_config = pwgan_config_file
2020-02-04 10:16:48 +00:00
synthesizer = Synthesizer ( args )
2018-06-05 12:15:48 +00:00
app = Flask ( __name__ )
2018-08-02 14:34:17 +00:00
2018-06-05 12:15:48 +00:00
@app.route ( ' / ' )
def index ( ) :
return render_template ( ' index.html ' )
2018-08-02 14:34:17 +00:00
2018-06-05 12:15:48 +00:00
@app.route ( ' /api/tts ' , methods = [ ' GET ' ] )
def tts ( ) :
text = request . args . get ( ' text ' )
print ( " > Model input: {} " . format ( text ) )
data = synthesizer . tts ( text )
2018-08-02 14:34:17 +00:00
return send_file ( data , mimetype = ' audio/wav ' )
2018-06-05 12:15:48 +00:00
if __name__ == ' __main__ ' :
2020-02-04 10:16:48 +00:00
app . run ( debug = args . debug , host = ' 0.0.0.0 ' , port = args . port )