Introduction: TotLa: Interactive Talking Robot in Python3

About: A Computer Science and Engineering student and a Hobbyist. love to make robots,electric devices,diy things and Programming them by Myself. My youtube channel https://www.youtube.com/fussebatti Follow me on gi…

Everybody wants to make a robot that can listen to him and talk back. But only a few of us know the actual procedure to do so. In this tutorial I’ll be showing you on how I made my simple and easy talking robot TotLa, using python3.

Watch the video to know how the robot works.

Step 1: Principle- Basic Workflow of a Talking Robot

To make a talking robot we need to follow three (3) basic

steps. And they are

  1. Listen – Speech to Text – The robot must listen to what user says and then convert the speech to text so that it can be processed.
  2. Decide – Process the text – Process what user have said then then based on that take decisions
  3. Respond – Talk back_ Text to Speech – After processing and knowing what to say, just say it as audio so that user feels that the robot is talking to him.

If you have noticed it, I am inspired by of course me (human). We all respond to someone following these 3 steps.

Step 2: Install Necessary Software

You need to install python3 on your computer. I am using python3.7.1 < current version is 3.7.3> . If you don’t have python installed on your computer then follow this link. Make sure to install python on your system (adding in system path).

After installing python on your computer open your command prompt and pip install these 3 libraries

1. winSound – pip install winsound

2. pyttsx3 – pip install pyttsx3

3. speech recognition – pip install speech recognition

If you have watched the video you heard a beep, it is to
notify the user that the robot is now ready to take command or is listening. It is done using winsound library.

To say things or to respond the robot needs to say (output in voice form). Pyttsx3 is an offline service. It is quite good and you can control voice + how many words to say in a minute.

The most important thing in this robot is to listen – Converting user speech into text – and could not find any easy offline solution for that. Unfortunately for some people it’s not good. But tell you what, it’s pretty fast as you have seen in the video. I was using google API. My computer was connected to my phone’s internet over WiFi. Still pretty faster than I expected.

Step 3: Code - Python Can Talk!!

I have talked about basic 3 steps. Now here is a basic workflow of the robot (See image/ below).

# Import necessary Libraries

def Listen(): command = input("Say it baby: ")

return command

def Decide(listen): print(f" Command = {listen}.")

if listen == "hi there": return "Hello"

def Respond(t): print(f"Talking the {t}") # Returns nothing so you'll get a None at the End, # Ignore it

while True: comm = Listen()

decision = Decide(comm)

print(Respond(decision))

And here is the final code. Or download code

"""
Making of a talking robot v1. Speech To Text - google api (*Online) - Takes a bit time Text To Speech - Offline - Fast.

by : Ashraf Minhaj mail: ashraf_minhaj@yahoo.com blog: ashrafminhajfb.blogspot.com """

""" Basic flow: 1. Listen - Convert user's Speech into Text 2. Decide - Take decision based on what user says 3. Respond - Talk back (responding to user) """

# Import necessary Libraries import pyttsx3 import speech_recognition as sr import winsound import time

talk = pyttsx3.init()

#possible lists of possible words or sentences with different punctuation hi_List = ['hi', 'Hi', 'Hello', 'hello', 'Hey', 'hey', 'yo', 'Yo,' 'salam', 'Salam', 'hi totla', 'Hi totla', 'totla', 'Totla'] bye_List = ['Bye', 'bye', 'Goodbye', 'goodbye', 'Good bye' 'good bye', 'byebye', 'by by', 'By by', 'Tata', 'tata', 'So long', 'so long', 'okay bye', 'ok bye', 'Ok bye', 'Okay bye'] qst1_list = ["Who are you", 'who are you', 'whats your name', 'your name', 'Your name', 'What are you', 'what are you'] res_neg_list = ['bad robot', 'Bad robot','bad boy', "Bad boy", 'you are rude totla', 'You are rude totla', ' you are a bad robot', 'You are a bad robot'] slang_list = ['Bal', 'bal', 'Crap', 'crap', 'chutiya', 'Chutiya', 'chootia', 'Chootia'] Love_list = ['i love you', 'I love you', 'Love you', 'love you'] hate_list = ['i hate you', 'I hate you', 'Hate you', 'hate you']

def Listen(): """ Takes users voice as input and converts it to text. """ speech = sr.Recognizer() #say beep before listening #take input from microphone with sr.Microphone() as source: winsound.Beep(frequency = 2500, duration = 100) #beep to inform that it's listening print("Say>>") voice = speech.listen(source) text = speech.recognize_google(voice) print(text) #print what it heard just to debug

return text #return what was heard

def Decide(listen): """ Takes decision based on what user says. """ print(f" Command = {listen}.") #just to debug

#see what user said is in which list or not if listen in hi_List: print("Resonse in Hi list") Respond("Hi there, Good to see you.")

elif listen in bye_List: print("In bye list.") Respond("I liked talking with you, okay take care.")

elif listen in Love_list: Respond("Yuk, I have a robot girl friend. No seat available") elif listen in hate_list: Respond("Hate you too.") elif listen in qst1_list: Respond("""I am Totla bot. The dumb talking robot written in python. My creator Ashraf minhaj is trying to make me smart""") elif listen in res_neg_list: Respond("I am very sorry I was just joking.")

elif listen in slang_list: Respond("You are a bad guy")

else: Respond("Sorry I don't understand Please say again.")

def Respond(t): print(f"Talking the: {t}") #to debug and see if everythings going okay

talk.say(t) talk.setProperty('rate', 90) #90 words per minute talk.runAndWait()

while True: #for ever loop

comm = Listen() #listen to what user says

Decide(comm) #take decision and respond

time.sleep(1) #after that a delay of 1 second

If you something it traverses the lists and sees if the
words you’ve said are in which list and based on that it responds. Talk to it after you hear a beep.

Remember that this is an easy and simple robot with a little bit intelligence. No machine learning is used in this code. I am still developing and adding features to this code. One day I’ll make TotLa a smarter robot with Humanoid shape with hands and legs on. To know more and get updated on the code you can follow me on here and follow the code on GitHub.

Thank you.