How to Convert Text to Speech in Django?

Published On: 31/08/2022 | Category: Django


Hi Dev,

In this quick example, let's see how to convert text to speech in django. you can see django speech to text. step by step explain django text to speech convert tutorial. We will look at example of how to convert text to speech in python. Here, Creating a basic example of speech to text in django.

So, there are several APIs available to convert text to speech in Python. One of such APIs is the Google Text to Speech API commonly known as the gTTS API. gTTS is a very easy to use tool which converts the text entered, into audio which can be saved as a mp3 file.

The Django-Gtts convert text to speech (.mp3) format and cache the file.

Here i explained simply step by step example of how to convert text to speech in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 manage.py startapp core
Step 3: Install Django-Gtts Library

First of all we need to installed Django-Gtts library following through below command and then register 'gTTS' INSTALLED_APPS in setting.py file as well as.

pip install Django-Gtts
Step 4: Update setting.py

Next, then update INSTALLED_APPS within our settings.py file to notify Django about the app.

Next, you need to add it in the settings.py file as follows:

settings.py

....
INSTALLED_APPS = [
    
    ...

    'gTTS',
    'core',
]

Next, you need to migrate your database using the following command:

python manage.py makemigrations gTTS
python manage.py migrate gTTS
Step 5: How to use Django-Gtts

In this step we will show how to use Django-Gtts in django template below show basic syntax of Gtts in audio tag and load gTTS in first of your django template.

Syntax:
{% load gTTS %}
<audio
    src="{% say 'language' 'text to say' %}"
    controls
></audio>
List of supported languages:
'af' : 'Afrikaans' 'sq' : 'Albanian' 'ar' : 'Arabic' 'hy' : 'Armenian' 'bn' : 'Bengali' 'ca' : 'Catalan' 'zh' : 'Chinese' 'zh-cn' : 'Chinese (Mandarin/China)' 'zh-tw' : 'Chinese (Mandarin/Taiwan)' 'zh-yue' : 'Chinese (Cantonese)' 'hr' : 'Croatian' 'cs' : 'Czech' 'da' : 'Danish' 'nl' : 'Dutch' 'en' : 'English' 'en-au' : 'English (Australia)' 'en-uk' : 'English (United Kingdom)' 'en-us' : 'English (United States)' 'eo' : 'Esperanto' 'fi' : 'Finnish' 'fr' : 'French' 'de' : 'German' 'el' : 'Greek' 'hi' : 'Hindi' 'hu' : 'Hungarian' 'is' : 'Icelandic' 'id' : 'Indonesian' 'it' : 'Italian' 'ja' : 'Japanese' 'km' : 'Khmer (Cambodian)' 'ko' : 'Korean' 'la' : 'Latin' 'lv' : 'Latvian' 'mk' : 'Macedonian' 'no' : 'Norwegian' 'pl' : 'Polish' 'pt' : 'Portuguese' 'ro' : 'Romanian' 'ru' : 'Russian' 'sr' : 'Serbian' 'si' : 'Sinhala' 'sk' : 'Slovak' 'es' : 'Spanish' 'es-es' : 'Spanish (Spain)' 'es-us' : 'Spanish (United States)' 'sw' : 'Swahili' 'sv' : 'Swedish' 'ta' : 'Tamil' 'th' : 'Thai' 'tr' : 'Turkish' 'uk' : 'Ukrainian' 'vi' : 'Vietnamese' 'cy' : 'Welsh'
Step 6: Creating the Views

If you want to send a text from views, follow this code:

In this step, we need to configure views. The index page will just be a template open the core/views.py file and add:

core/views.py
from django.shortcuts import render
from gTTS.templatetags.gTTS import say
from django.http import HttpResponse

# Create your views here.
def text_to_speech(request):
    obj = say(language='en-us', text="Welcome to Tuts-Station.com")
    return render(request, 'index.html', {'obj':obj})
Step 7: Creating the Templates

Next, then with your text editor create new templates files: core/templates/index.html file and the add:

core/templates/index.html
{% load gTTS %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Tuts-Station.com</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h4>How to Convert Text to Speech in Django? - <span class="text-primary">Tuts-Station.com</span></h4>
                    </div>
                    <div class="card-body">
                        <div class="col-md-12 text-center">
                            <audio
                                src="{{obj}}"
                                controls
                            ></audio>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
Step 8: Creating URLs

In this section, we need a urls.py file within the core app however Django doesn't create one for us with the startapp command. Create core/urls.py with your text editor and within this file we'll import yet-to-be-created function for each--text_to_speech set a path for each. Note as well that we set an optional URL name for each.

Here's what it looks like:

core/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index,name='index'),
    path('text-to-speech/', views.text_to_speech, name="text_to_speech"),
]

Next, we require to add a URL path for our example app which can be done by importing include and setting a path for it.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python manage.py runserver

Next, go to the http://localhost:8000/text-to-speech address with a web browser.

I hope it will help you....