AI Implementation In Node.js - Cutting Through The Hype

Introduction 

 
In 1993, when the graphic web browser Mosaic was launched, the content of the Internet was revolutionized by the widespread experience.
 
Since then, mobile app designers around the world are constantly working to create and improve the online sense of sound user interface.
 
Some advances are due to technologies such as the introduction of the Internet broadband infrastructure that makes multimedia content or HTML5. Other experiments have been carried out using evidence to assess what functions and what does not work and an iterative approach to attain perfection.
 
Thus, when users' expectations change, they demand more personalized content and website visits to experience them. Therefore, website developers do not focus on conventional methods to excel in thinking about out-of-the-box concepts.
 
This is where the bigger picture of artificial intelligence redefines all traditional web technologies, moving the idea to another dimension where UX is the primary target.
 
However, it is important to know what AI is actually about before we understand more about how it supports web application creation.
 

Introduction

 
The architecture of the modern Web App is increasingly focused on creating a front-end user identity using artificial intelligence. Artificial Intelligence is used, among other uses, for web apps, not only for smart automation, but also for building recommendations, website execution, and image recognition.
 
Current innovations in Artificial Intelligence (AI) have changed a vast range of utilities and will be widespread in computer networks in the near future. In the artificial learning models or profound learning models, much work that had traditionally demanded human intervention or experience can now be captured and automated.
 

The Rising Trend In The Usage Of Node.js

 
Node.js is a run-time environment for JavaScript servers. It is open sources with Google's V8 motor, LIBUV for accessibility across platforms, and the main repository. Notably, the global window object is not revealed by Node.js so it is not running in a browser.
 
In addition to being powerful, node.js has a large active, open-source, JavaScript-based community and is also popular. Also, the compatibility between versions does not usually sever.
 
According to a survey, this map illustrates the success and traffic location of Node.js in comparison to the most popular web servers.
 
AI Implementation in Node.js: Cutting Through The Hype 
 

Building an AI Chatbot With Node.js

 
Here are the three major steps that we are going to attempt to build this web application:
  1. Speech recognition - Using the SpeechRecognition interface of the Network Speech API to listen to the voice of the recipient.
  2. API - Send the message from the user as a text string to a commercial natural language processing API.
  3. Speech synthesis - Using the SpeechSynthesis interface to provide a robotic voice after API.AI returns the answer text. 
AI Implementation in Node.js: Cutting Through The Hype
 
Let’s get started.
 
Prerequisite
 
Before proceeding any further, you should have node.js installed on your computer.
 

Setting up the framework

 
Create your app dictionary. Set up your app structure like this.
 
AI Implementation in Node.js: Cutting Through The Hype
 
After that, run this command to initialize your Node.js app.
 
AI Implementation in Node.js: Cutting Through The Hype
 
Install all dependencies which are required to build this application.
 
Now, let’s create an index.js file and listen to the server.
 
AI Implementation in Node.js: Cutting Through The Hype
 
The Speech Recognition Interface to receive speech
 
The user interface of this app is easy: just one button for voicing. Set up our file index.html and add our JavaScript front-end file (script.js) and Socket.IO, and later use it for communication.
 
AI Implementation in Node.js: Cutting Through The Hype
 
Code for styles.css is as follows:
(code of CSS)
 
Capturing Voice
 
SpeechRecognition, the Online Speech API control interface for voice recognition, is used in script.js: Invoke
  1. const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;  
  2. const recognition =newSpeechRecognition();   
We have prefixed or unprefixed objects, as Chrome supports the API with prefixed characteristics currently.
 
Also in this tutorial, we are using some ECMAScript6 syntax because in browsers that support SpeechAPI interfaces, SpeechRecognition, and SpeechSynthesis the syntax, like const and arrow functions, is accessible.
 
Optionally, to configure speech recognition you are able to set different properties:
  1. recognition.lang ='en-US';  
  2. recognition.interimResults =false;   
Then, grab the DOM for the UI button and listen to the case press to start speaking recognition.
 
AI Implementation in Node.js: Cutting Through The Hype

Once speech recognition has started, use the result event to retrieve what was said as text.
 
AI Implementation in Node.js: Cutting Through The Hype
 
This returns an item containing the result from SpeechRecognitionResultList and helps you to get the text in the tuple. This would also offer back faith in the transcript, as can be seen in the code preview.
 
Let us now use Socket.IO to relay the results to our server code.
 

Socket.IO Real-Time Communication

 
Socket.IO is a web applications library in real-time. It facilitates two-way communication between web clients and servers in real-time. We will use it to pass the browser results to the code of Node.js and then return the answer to the browser.
 
Instantiate Socket.IO in script.js somewhere:
  1. const socket =io();   
Then, insert this code where you are listening to the result event from SpeechRecognition:
  1. socket.emit('chat message', text);   
Now, let’s go back to the Node.js code to receive this text and use AI to reply to the user.
 

Receiving a reply from AI

 
We use API.AI, as it offers a free developer account and helps us to easily build a small-scale framework using its web interface and library Node.js.
 
Once you have created an account on API.AI, create an agent. Instead, first, click on the "Small Talk" preset from the connection menu, and then turn to the switch to unlock operation, instead of the complete customization route generating individuals and attempts.
 
AI Implementation in Node.js: Cutting Through The Hype
 
Using the API.AI node.js SDK
 
Let our Node.js app be hooked to API.AI with Node.js SDK later! Return to your index.js file with your token to initialize API.AI
  1. const apiai =require('apiai')(APIAI_TOKEN);  
You should hardcode the API key here if you wish to only run the code locally. There are many ways to set variables for your setting, but I typically set a.env file to add them. I hid my own credentials in the source code of GitHub by adding the.gitignore file, but look at the.env-test file for information.
 
Now we use Socket.IO server-side to collect the browser results.
 
Use the API.AI APIs to find a response to the user's message when the connection is created and the message is received.
 
AI Implementation in Node.js: Cutting Through The Hype
 
When API.AI returns the result, use Socket.IO’s socket.emit() to send it back to the browser.
 

Giving A Voice To AI With An SpeechSynthesis Interface

 
Again, return to script.js to complete the app!
 
Build a synthetic voice feature. This time, we use the GUI of the SpeechSynthesis web Speech API controller.
 
The feature uses a string that helps the browser to utter the text:
 
AI Implementation in Node.js: Cutting Through The Hype
 
First, create a reference to the window. Speech synthesis of the API input point in the feature. This time, you will see no prefixed property, which is better supported than SpeechRecognition, because all browsers that support it already have the SpeechSynthesis prefix dropped.
 
Then, generate a new instance of SpeechSynthesisUtterance() with its builder, and set the text to summarise when it is spoken. You may set other properties, for example, Speech, to select the type of voices provided by the browser and the operating system.
 
Using SpeechSynthesis.speak() for speaking at last!
 
Now get the server reply again with Socket.IO. Call the feature after the message has been sent.
 
AI Implementation in Node.js: Cutting Through The Hype
 
You are done!
 
Let's try our AI bot for a chit-chat!
 
Be mindful that you will first be asked permission by the browser to use the microphone. Like other web APIs like the Geolocation API and the Notification Api, the browser won't access your information without your permission until you give your information so your speech won't be secretly registered.
 
As AI is too easy, you will soon be bored with this conversation. The API.AI can be modified and trained, however. To make it more knowledgeable, read the API.AI documentation.
 
I expect the tutorial you enjoyed and a fun chatbot built!
 
Drive The Web Towards The Future!
 
The way consumers monitor computing and connected devices has changed voice interaction. The user interface is now changing the web with the Web Speech API. In conjunction with AI and profound learning, web applications can be smarter and provide users with improved experiences.
 
References
 
This tutorial only discusses the API's central elements, but in reality, the API is very versatile and adaptable. You should change the recognition and synthetic vocabulary, the synthesis expression, including emphasis, the pitch, and the speaking rate such as the US or UK English.