Get Free access to exclusive AI tutorials and amazing AI tools now
Request access

How to create a flask app for a text-to-speech (Coqui TTS) synthesizer

_

Hello everyone in our today article we will continue the last article about creating a TTS in python in 10 minutes

And today we will create a flask app that will serve our application as a web application accessible to everyone.

Youtube video explanation

And our TTS fine was the last thing we got. We must first create a new flask app. And for that we must install flask by typing:

pip install Flask

And then create a new app folder (called TTS for example). Then in that folder create a folder structure like this :

This means you will create a main.py file and an app folder and in that folder create two other folders called static and templates and the files __init__.py and views.py.

Here is the meaning of all those folders:

  • App: Is the global application folder
    • Static: Is for static files like images, audio files, and videos, … We will need it on the front of our app
    • Templates: This is for all the HTML templates that we will use in our application. All the HTML files must be added to this folder
    • __init__.py [file]: This file will be the entry point of our app folder
    • views.py [file]: This will be where we will declare all our routes (like the route to the home pages, and the API also)
  • main.py [file]: This is the entry point of the complete application. It is at the same level as the app folder

main.py – Entry point

In our app entry point, we will simply launch our application. The content is the following:

from app import app

def main():
    app.run(debug=1, host="::", port="5000")

if __name__ == "__main__":
    main()

Here we import our app from the app folder and we run our flask app on a specific port.

App folder – Application logic

Explaining everything in the app folder will take a lot of time. Here is the source code of the app Martha Github.

To resume in our __init__.py we create the flask app and import all our views:

from flask import Flask

app = Flask(__name__)

from app import views

in the views.py we declare the views.

from distutils.log import error
from app import app
import io
from flask import render_template, request, send_file
from app.tts import synthesizer

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/call/martha", methods = ["POST"])
def call_martha():
    if(request.form["text"]):
        text = request.form["text"]
        outputs = synthesizer.tts(text)
        out = io.BytesIO()
        synthesizer.save_wav(outputs, out)
        return send_file(out, mimetype="audio/wav")
    else:
        return {"error": "Please provide the text"}, 400 

This @app expression is a decorator, and we use it to define our routes. And we use render_template function to send an HTML file from the template folder to the frontend.

As you can see, we have added a tts.py in the app folder (It is the TTS from part 1). We have changed it a bit to look like this:

# Install all the requirements

# pip install TTS

# import all the modules that we will need to use

from TTS.utils.manage import ModelManager
from TTS.utils.synthesizer import Synthesizer
import site
location = site.getsitepackages()[0]

path = location+"/TTS/.models.json"

model_manager = ModelManager(path)

model_path, config_path, model_item = model_manager.download_model("tts_models/en/ljspeech/tacotron2-DDC")

voc_path, voc_config_path, _ = model_manager.download_model(model_item["default_vocoder"])

synthesizer = Synthesizer(
    tts_checkpoint=model_path,
    tts_config_path=config_path,
    vocoder_checkpoint=voc_path,
    vocoder_config=voc_config_path
)

And after all that, we have created a simple HTML template that will be called in the backend via a rest API call (but I will not explain that part because it is out of the scope of our work here). Here is a preview:

And that is all. We have a flask app that we can call, and it will give us the audio of the text we gave him. In the following article (that you can find here ), we will protect the website again spams by adding a Recaptcha to it on the flask side of the application

You can find the complete source code here: Martha Github.

Let's Innovate together for a better future.

We have the knowledge and the infrastructure to build, deploy and monitor Ai solutions for any of your needs.

Contact us