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.
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 ChatterBotBefore 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\Python36Now 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: helloNeelu: -hiSpeak anything: how are youNeelu: –I am doing wellSpeak anything: byeNeelu: byeOk, 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 PYTTSX3Below 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 botNow, 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/masterHOPE YOU HAVE ENJOYED MY TUTORIAL. THANK YOU...
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.
ReplyDeleteChatbot 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
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
ReplyDeleteGreat article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. professional organizing
ReplyDelete