Introduction

Either you are a fresher or an experienced developer, you need a project that shines out of the crowd. Today we will build one such project. This is an excellent project to have it on your resume with latest technology. Stay tuned for this 2 parts article and add this feather 🪶 in your cap ! 🧢

Learn from the detailed step-by-step video below –

The Project 🗒️

Today we will “build our own Text-To-Speech application” which will take following input on a web page

  • Text – to generate the speech.
  • Source Language – of the input text.
  • Target Language – in which the audio (speech) is required to generate.

and display the audio file on the web page as response.

The Preparation ✍🏻

In this project we will

  • Create a simple nodejs package which will have a frontend html page having an Input form for Text and Two drop-downs to select Source Language and Target Language.
  • Integrate a Text Translator (Google Translate API or some other public API) using Rapid API.
  • Integrate Open AI API to generate Audio file for the input text. Read API documentation here.
  • Upload the playable audio file on web page.

The Pre-Requirements 🛠️

  • None. We will setup everything from scratch.

Technology & APIs used 🕹️

  • Text Translator from RapidAPI. Read the documentation from here.
  • Open AI public audio speech API. Read the documentation from here.
  • Node Js – Open-source Javascript run-time platform available for Mac, Windows, Linux etc.
  • Express Js – Node.js web application framework to develop RESTful APIs with Node.js. Read here
  • Axios – Node.js library that allows making HTTP requests from web browser or Node.js
  • Postman – Postman is an application used for API Testing. This application allows to test HTTP or HTTPS requests.

Don’t worry, we will setup all of it in our project.

All Set, Let’s Go ! 🚤

Setup 🌱

  • Download Postman application for testing your application (Recommended but you can skip the steps involving postman testing).
  • Create a free account in Rapid API (Rapid API is an API marketplace that allows developers to create, test and connect to APIs).
  • Now let’s setup Node.js on our computer. To do that, you can download Node.js from the main website – https://nodejs.org/en
  • Go to desktop and create a folder with your project name. I am naming this application – Techshshila Translator.
  • Open any IDE (vscode, intellij etc.) and open the folder we created above.
  • We will now initialise node modules by running the command in the terminal –
~ % npm init
  • Once you have run the above command, you will see a package.json file.
  • Now we will create a new javascript file and call it – index.js
  • We will also create a new html file and call it – index.html
  • We will then install few important packages which will be required through our development. These packages are –
  • Run the following commands to install the above packages
~ % npm i express

~ % npm i axios
  • Once these packages are installed we are good to go.

Coding – Getting the basics right !

  • Let’s add some basic code in the index.js file.
// import express and axios
const express = require('express');
const app = express();
const axios = require('axios');
// define the local port 8000 or use the port number setup as the environment variable
const PORT = process.env.PORT || 8000;


// open the listener to our PORT. Any activity at PORT will now be detected
app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
});


// application logic to be added
  • Now our objective is to take the inputs from the HTML page and use that input to call the translate and audio generator APIs. So let’s create an HTML page with input text fields.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="mainContent">
    <div id="card">
        <h1>Techshshila Translator</h1>

        <!-- Creat an input form -->
        <form id="translationForm">
            <!-- Creat an input field to take input - Text-->
            <label for="textInput">Text:</label>
            <div id="textInputContainer">
                <textarea id="textInput" name="text" placeholder="Enter Text To Translate"></textarea>
            </div>

            <!-- Creat an empty drop-down field to select Source Language -->
            <label for="sourceSelect">Source:</label>
            <select id="sourceSelect" name="source" required>
            </select>

            <!-- Creat an empty drop-down field to select Target Language -->
            <label for="targetSelect">Target:</label>
            <select id="targetSelect" name="source" required>
            </select>

            <!-- Add a button to click to generate audio file-->
            <button id= "translateButton" type="button">Generate Translated Audio</button>
        </form>
    </div>
</div>
</body>
</html>
  • The above html page generates a simple form without styling. Let’s add styling to it. Create a new css file – styles.css

Keep it in style – 🎨

  • Let’s link this css to our index.html like below –
<!DOCTYPE html>
<html lang="en">
<head>
    ...
    <link rel="stylesheet" href="./styles.css">
</head>
...
  • Now this will style input form but our drop-downs are still empty. Let’s add languages to our source and target drop-downs.

Adding the languages for selection !

  • To do that we will create a new javascript file and call it – constants.js
  • Before we add languages we must know which languages our TextTranslator API supports. We will be using Text Translator API present in Rapid API – here. I have that information, so i will add it in the constants file, you can copy it.
  • Let’s update the constants file –
  • We will import this constants file in our index.html file and add it to our drop-downs using a function. Naturally we would not want to add them one by one ! 😁
  • Let’s see the modified index.html code –
  • Tadaaaa !! 🎉 We have the successfully created an input form for our application. Now this input will be passed on to the back-end logic which we will use to call our translation and audio generation API.

Application Logic 💡

  • Now its time to add our application logic.

Step 1 – Adding Text Translation API

If you read the Text Translator document, you will see how to call the API. We will copy the header, URL and method options for the API and use it in our application. Here is how we will change our index.js file.

Let’s test the above –

  • First, let’s call run our service, by running the command
~ % npm start
  • Now Open Postman Application and create a POST request –
    • URL : http://localhost:8000/
    • Body :
      The source language is english and target language is hindi. (💡Tip: Try yourself with your favourite 🤩 target language.)
{
    "text": "I love programming !",
    "source" : "en",
    "target" : "hi"
}
  • You should get a response:
{
    "text": "I love programming !",
    "sourceLanguage": "en",
    "targetLanguage": "hi",
    "translatedText": "मुझे प्रोग्रामिंग बहुत पसंद है!"
}

Congratulations ! 🎉 We have successfully translated our source language. Kudos for stretching this far, we are almost there. In the next part, we will generate the speech for the translated text too and display it on our website !

Leave a Reply

Your email address will not be published. Required fields are marked *