Compute Engine Google Cloud

Install Deep Learning based Mozilla/TTS on Ubuntu – Google Cloud

Install Deep Learning based Mozilla/TTS on Ubuntu – Google Cloud. In this guide you are going to learn how to setup a Text to Speech solution based on deep learning with Python Flask application and configure Nginx and secure it with SSL.

Mozilla TTS is a deep learning solution which favors simplicity over complex and large models and yet, it aims to achieve state of the art results. Currently there are 2 model architectures, plotted on Tacotron and Tacotron2. In this guide you are going to learn how to setup a Python virtual environment and install the Tacotron2 based TTS on Ubuntu.

This setup is tested on Google Cloud with a machine type of 2vCPU and 4 GB RAM running Ubuntu 18.04.

Choose Best Hosting for your Business

PlatformReviewsPricing
Siteground★★★★★$3.95
Kinsta – Google Cloud★★★★★$30

Prerequisites

Make sure you have a Compute Engine VM instance running with the above mentioned specifications.

Install Nginx

Install Nginx using the following command.

sudo apt install nginx

If you have configured UFW firewall, you need to allow HTTP and HTTPS ports for Nginx.

sudo ufw allow 'Nginx Full'

Install required Python packages

Install some required packages for Flask and TTS.

sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev uwsgi uwsgi-plugin-python3 espeak libsndfile1 python3-venv 

Setup Virtual Environment

sudo -H pip3 install --upgrade pip
sudo -H pip3 install virtualenv
mkdir ~/speech
cd ~/myprojectdir
virtualenv myprojectenv

Activate the virtual environment by typing

source myprojectenv/bin/activate

Your prompt should change to indicate that you are now operating within a Python virtual environment. It will look something like this: (speech)[email protected]:~/speech$

Install packages inside Virtual Environment

Install Wheel and other tools for the setup.

pip install -U pip setuptools wheel 
pip install -U https://github.com/reuben/TTS/releases/download/ljspeech-fwd-attn-pwgan/TTS-0.0.1+92aea2a-py3-none-any.whl

You can download the latest Python package from the official GitHub wiki page.

This will take some time to complete the installation.

Once the installation is complete you can configure uWSGI and Nginx.

Configure uWSGI for Mozilla TTS

Create a new file for uWSGI configuration.

sudo nano ~/speech/uwsgi.ini

Paste the following configuration in the file.

[uwsgi]
plugin = python3
socket = /home/username/speech/tts.sock
chmod-socket = 666
wsgi-disable-file-wrapper = true
virtualenv = /home/username/speech/speech
processes = 10
stats = 127.0.0.1:9191
manage-script-name = true
module = TTS.server.server:app

Hit CTRL + X followed by Y and Enter to save the file.

Start the Flask application using uWSGI

Here we will start the application using nohup command in the background.

nohup uwsgi --ini uwsgi.ini &

Now your application starts running and you need to configure Nginx for that.

Configure Nginx for uWSGI

Remove default Nginx configurations.

sudo rm -rf /etc/nginx/sites-available/default
sudo rm -rf /etc/nginx/sites-enabled/default

Create a new configuration for Python Application.

sudo nano /etc/nginx/sites-available/speech.conf

Paste the following configurations in the file.

server {
     listen 80;

     server_name speech.domain.com;

     location / {
         try_files $uri @tts;
     }

     location @tts {
         include uwsgi_params;
         uwsgi_pass unix:/home/username/speech/tts.sock;
     }
}

Enable the configuration by creating a symbolic link to sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/speech.conf /etc/nginx/sites-enabled/speech.conf

Check your Nginx configuration and restart Nginx.

sudo nginx -t
sudo service nginx restart

Now check your domain name on the browser, you will see the homepage where you can convert text to speech.

Text2Speech

Setup Let’s Encrypt SSL for TTS

HTTPS
HTTPS is a protocol for secure communication between a server (instance) and a client (web browser). Due to the introduction of Let’s Encrypt, which provides free SSL certificates, HTTPS are adopted by everyone and also provides trust to your audiences.

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

Now we have installed Certbot by Let’s Encrypt for Ubuntu 18.04, run this command to receive your certificates.

sudo certbot --nginx certonly

Enter your email and agree to the terms and conditions, then you will receive the list of domains you need to generate SSL certificate.

To select all domains simply hit Enter

The Certbot client will automatically generate the new certificate for your domain. Now we need to update the Nginx config.

Redirect HTTP Traffic to HTTPS with www in Nginx

Open your site’s Nginx configuration file add replace everything with the following. Replacing the file path with the one you received when obtaining the SSL certificate. The ssl_certificate directive should point to your fullchain.pem file, and the ssl_certificate_key directive should point to your privkey.pem file.

sudo nano /etc/nginx/sites-available/speech.conf
server {
     listen [::]:80;
     listen 80;

     server_name speech.domain.com;
          location / {
         try_files $uri @tts;
     }

     location @tts {
         include uwsgi_params;
         uwsgi_pass unix:/home/username/speech/tts.sock;
     }

     return 301 https://speech.domain.com$request_uri;
} 

server {
     listen [::]:443 ssl;
     listen 443 ssl;

     server_name speech.domain.com;

     ssl_certificate /etc/letsencrypt/live/speech.domain.com/fullchain.pem;
     ssl_certificate_key /etc/letsencrypt/live/speech.domain.com/privkey.pem;

     location / {
         try_files $uri @tts;
     }

     location @tts {
         include uwsgi_params;
         uwsgi_pass unix:/home/username/speech/tts.sock;
     }
}

Hit CTRL+X followed by Y to save the changes.

Check your configuration and restart Nginx for the changes to take effect.

sudo nginx -t
sudo service nginx restart

Renewing SSL Certificate

Certificates provided by Let’s Encrypt are valid for 90 days only, so you need to renew them often. Now you set up a cronjob to check for the certificate which is due to expire in next 30 days and renew it automatically.

sudo crontab -e

Add this line at the end of the file

0 0,12 * * * certbot renew >/dev/null 2>&1

Hit CTRL+X followed by Y to save the changes.

Now your application will be available in HTTPS.

Additional Security setup

By default this application is accessible for everyone on the internet. If you need to make this application protected you can configure HTTP Basic auth using Nginx.

Get your Professional Google Cloud Architect certificate with this easy to learn course now.

Conclusion

Now you have learned how to setup a TTS application with deep learning on Google Cloud Platform.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

If you find this helpful? Support me!
Buy me a coffee Donation Please buy me a coffee