Skip to main content

CHATBOT WITH SPEECH RECOGNITION AND PYTTSX3 USING PYTHON

NOTE: VIEW THIS POST IN DESKTOP SITE FOR THE BEST EXPERIENCE

Hello guys. Welcome to my blog. Here I will explain to you how to create a chatbot that has speech as it’s both input and also output. So, let us get started.
In this tutorial, I am using some of the libraries in Python like SpeechRecognition, Pyaudio, Chatterbot et cetera. I am going to explain to you how to install these libraries and work with them one by one separately and at last how to integrate them.
SPEECH TO TEXT
At first, you have to install the Speech Recognition library. You should not use the Python3.7 version because it does not support speech recognition. I am using a 3.6 version for this tutorial. Now, you should install the speech recognition library from the command prompt. Use the following command.
pip3.6 install SpeechRecognition

Now, you should install PyAudio. First, you have to find the version of your Python and also the configuration of your machine.

Open your cmd and type python, and you will see all the information about it
Christoph Gohlke (University of California) hosts Windows wheels for most popular packages for nearly all modern Python versions, including the latest PyAudio. You can find it here: https://www.lfd.uci.edu/~gohlke/pythonlibs/ (download can be quite slow). Then, go to the folder where it is downloaded. Copy the downloaded file name. Now install the .whl file from cmd by the following command.

pip install <your downloaded file name here>

Now open your IDLE and create a new file. And type the following code
import speech_recognition as sr
r=sr.Recognizer()
with sr.Microphone() as source:
    while True:
        audio=r.listen(source,timeout=2,phrase_time_limit=5)
        try:
            text=r.recognize_google(audio)
            print(text)
        except:
print('sorry')
Now, test the code by running it. Say some words, then it converts it to text. You can change the timeout and phrase_time_limit in the above code according to your requirements.
TEXT TO SPEECH
pyttsx3 is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline and is compatible with both Python 2 and 3. Install it from cmd.
pip install pyttsx3
Now in the IDLE, type the following code and listen to the words your computer says.
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
You can also change the voice of the speech. To know the voices available, type the following code.
import pyttsx3
engine=pyttsx3.init()
voices=engine.getProperty('voices')
for voice in voices:
    print(voice.id)
    engine.setProperty('voice',voice.id)
    engine.say("Hello World")
    engine.runAndWait()
    engine.stop()
Run the above code and hear the different kinds of voices you have. Now copy the id of the voice you want to set into the code. For me, it is, ‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0’ . Now, set the voice of your computer by typing the below code.
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('voice','HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0')
engine.say("I will speak this text")
engine.runAndWait()
CHATTERBOT
Ok, now our task is to make a chatbot. For this, you have to install chatterbot by typing the following command in cmd.
pip install ChatterBot
Before making a chatbot, you need to install chatterbot-corpus from here: https://github.com/gunthercox/chatterbot-corpus. After installation, unzip the file and place it in your python folder. For me it is, C:\Users\hp\AppData\Local\Programs\Python\Python36
Now it is time to train our chatbot. Use the following code.
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os

bot = ChatBot('Bot')
bot.set_trainer(ListTrainer)

for files in os.listdir('C:/Users/hp/AppData/Local/Programs/Python/Python36/chatterbot-corpus-master/chatterbot_corpus/data/english/'):
    data = open('C:/Users/hp/AppData/Local/Programs/Python/Python36/chatterbot-corpus-master/chatterbot_corpus/data/english/' + files,'r').readlines()
    bot.train(data)

while True:
    if(message.strip()!=="bye")
        message=input("Speak anything: ")
        reply=bot.get_response(message)
        print("Neelu: ",reply)
    if(message.strip()=="bye"):
        print("Neelu: bye")
   

  
Now save this file as “chatbot_train.py” and run it. The List trainer which we have set to train the chatbot trains it using the data in chatterbot corpus. It takes some time. Now let us see the output of the code by asking some questions to it. It will be like…
Speak anything: hello
Neelu: -hi
Speak anything: how are you
Neelu: –I am doing well
Speak anything: bye
Neelu: bye
Ok, now if you have to run the code, again and again, the chatbot always get trained by the same data and it takes some time. The chatbot need not be get trained again and again if you are using the same data. If you go and see your working directory, for me it is C:\Users\hp\AppData\Local\Programs\Python\Python36, you can see db.sqlite backup of your trained one. So you can reuse this by creating another .py file in the current directory. It includes the following code.
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os

bot = ChatBot('Bot')
bot.set_trainer(ListTrainer)

while True:
    if(message.strip()!=="bye")
        message=input("Speak anything: ")
        reply=bot.get_response(message)
        print("Neelu: ",reply)
    if(message.strip()=="bye"):
        print("Neelu: bye")
    
Now, if you run this file, you can get the responses faster, without again training the chatbot.
INTEGRATION OF CHATBOT WITH SPEECH RECOGNITION AND PYTTSX3
Below is the final code for our tutorial. Edit the 2nd chatbot code which is already trained by using the following code.
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os
import speech_recognition as sr
import pyttsx3


bot = ChatBot('Bot')
bot.set_trainer(ListTrainer)
engine=pyttsx3.init()
engine.setProperty('voice','HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0')


r=sr.Recognizer()
with sr.Microphone() as source:
    while True:
        print("speak anything: ")
        audio=r.listen(source,timeout=2,phrase_time_limit=4)
        try:
            text=r.recognize_google(audio,language='en-IN')
        except:
            text='Sorry'
        print(text)
        reply=bot.get_response(text)
        if(text!='bye'):
            reply = bot.get_response(text)
          
        if(text == 'bye'):
            print('Neelu: Bye')
            engine.say('bye')
            engine.runAndWait()
            break
        
        print('Neelu : ',reply)
        engine.say(reply)
        engine.runAndWait()
Now, run the code and speak and listen to the responses of the bot
Now, run the code and speak and listen to the responses of the bot. You can train the bot with your own statements. For further reference, see:https://chatterbot.readthedocs.io/en/stable/
To download the code, visit my GitHub:https://github.com/VallamkondaNeelima/MyCodes/tree/master
HOPE YOU HAVE ENJOYED MY TUTORIAL. THANK YOU...





Comments

  1. I always like and search such topics and everything connected to them.Excellent and very cool idea and the subject at the top of magnificence and I am happy to comment on this topic through which we address the idea of positive reaction.
    Chatbot Company in Dubai
    Chatbot Companies in Dubai
    Chatbot Development
    AI Chatbot Development
    Chatbot Companies in UAE
    Chatbot Company in Chennai
    Chatbot Company in Mumbai
    Chatbot Company in Delhi
    Chatbot Development Companies

    ReplyDelete
  2. i create a something with speech recognition it create and delete file by the problem is: It only create and delete file when i close my application not when its running. So i need help in this

    ReplyDelete
  3. Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. professional organizing

    ReplyDelete

Post a Comment

Popular posts from this blog

LENGTH OF LONGEST VALID SUBSTRING

GIVEN A STRING OF PARENTHESIS, PRINT THE LENGTH OF THE LONGEST BALANCED SUBSTRING . FOR EXAMPLE: GIVEN,                1.  ()() ---> 4                 2.  ()())()()() ---> 6  Brute Force Approach:   It is to find all the substrings, and check whether they are balanced or not, and simultaneously updating the result if we find a larger valid substring than the previous one. But, this takes the time of O(N^3), since finding all the substrings take O(N^2), and for each substring, to find whether it is a valid substring, it takes a time of O(N), so this constitutes a total of O(N^3).  USING STACK:  In this approach, we will maintain a stack to store the indices of the traversed opening brackets i.e., '('. [YOU WILL KNOW THE REASON SOON, KEEP READING THE POST].  And a variable res , to store the length of the longest substring known till now....

RECOGNITION OF HANDWRITTEN DIGITS(MNIST DATA) PART ONE USING PYTORCH

HELLO GUYS, IN THIS BLOG POST I WANT TO SHOW THE BASIC INTRO TO IDENTIFICATION OF HANDWRITTEN DIGITS USING PYTORCH MNIST DATA. MNIST DATA IS THE COLLECTION OF HANDWRITTEN DIGITS. MNIST CONTAINS 70,000 HANDWRITTEN DIGITS, 60,000 FOR TRAINING AND REMAINING 10,000 ARE FOR TESTING.  THE  IMAGES ARE GRAYSCALE AND 28x28 PIXELS. WE CAN DOWNLOAD THE DATASET USING THE CODE BELOW. Here, the parameters batch_size is kept to 64 so that the training images are grouped into 64 each and shuffle is kept to TRUE, such that each time we run the code it shuffles the data and returns an iterable with new groups of batch_size. As the trainloader is iterable, we are iterating through it and collecting the first batch of images and it's corresponding labels into images and labels respectively. Now, run the above code and see the output. you will see something like this. torch.Size([64, 1, 28, 28]) torch.Size([64])  It shows that there are 64 images with grayscale an...