QUICK TIP - Python – Building a Virtual Assistant Robot With 50 Code Lines

- 1. QUICK TIP - Most Useful .htaccess Tricks for WordPress
- 2. QUICK TIP - How to Join and Earn From Brave Ads
As society grows, technology is also growing rapidly, we can see that more and more technology projects are coming out with smart, close and understand with humans, support a lot for people in everyday tasks, they call it AI, so what is AI?
What is AI?
- AI – Artificial Intelligence, also known as Artificial Intelligence, is a science and technology for manufacturing intelligent machines, especially intelligent computer programs.
- AI is done by studying the way people think, the way people learn, decide and work while solving a problem, and using these research results as a basis for develop intelligent software and systems, which can be applied to different purposes in life. In simple terms, AI is the use and analysis of input data to make predictions and then make a final decision.
In recent years AI has been booming and popular, it’s really hot, and I myself really like and understand this field so today we will go together to build for ourselves one. Virtual assistant like Siri or Google Assistant in python is very simple with just over 50 lines of code:
Let’s go to build it now.
Setup environments
First we need to set up the environments and install python on our machine, if you already have python on your computer, you can skip this step:
- Instructions for setup: https://realpython.com/installing-python
Ok next we need to visualise and divide the whole article into small parts, try to visualise with a virtual assistant, what functions does it need to have?
1: It must be audible
2: It must understand
3: It must be readable
Coding – Building a Virtual Assistant Robot With 50 Code Lines
Ok, now we are going to write the listening program of this robot:
I created a file named:
- First we need to install the library named
speech_recognition
pip install speechrecognition
pip install pyaudio
hearing.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import speech_recognition #Name of Libs robot_ear = speech_recognition.Recognizer() #Initiate to start listening with speech_recognition.Microphone() as mic: # activate microphone then after finishing it will be turn-off print("Robot: I'm Listening") audio = robot_ear.listen(mic) try: you = robot_ear.recognizer_google(audio) # Robot recognise sound except: you = "" # if not say or say something wrong # In try and except to catch errors. print("You: " you) |
- In order for the robot to speak, or convert words into words, we need to download a library to do this:
Run:
pip install pyttsx3
Then before running the command below, you must make sure that it is installed pip
already!
You can see how to install it pip
here.
(If you use windows and install pyttsx3, there is an error, please refer to the fix here.)
speaking.py
1 2 3 4 5 6 |
import pyttsx3 robot_brain = "I can't hear you, try again" robot_mouth = pyttsx3.init() robot_mouth.say(robot_brain) robot_mouth.runAndWait() |
- At this point, when you run the command
python3 noi.py
, your computer will speak the sentence"I can't hear you, try again"
understand.py
1 2 3 4 5 6 7 8 9 10 11 12 |
you = "Hello" # when say Hello robot will check conditions below. if you == "": robot_brain = "I can't hear you, try again" elif you == "Hello": robot_brain: "Hello Python" elif you == "Today" robot_brain: "Friday" else: robot_brain = "I'm fine thank you and you" print(robot_brain) |
ok and now I will put the other 3 files together into 1 file okay:
robot.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import speech_recognition import pyttsx3 import datatime import data, datatime robot_ear = speech_recognition.Recognizer() robot_mouth = pyttsx3.init() robot_brain = "" while True: # initiate a loop for application with speech_recognition.Microphone() as mic: print("Robot: I'm Listening") audio = robot_ear.listen(mic) print("Robot:...") try: you = robot_ear.recognizer_google(audio) except: you = "" if you == "": robot_brain = "I can't hear you, try again" elif "Hello" in you: #When say "Hello" it will check the word robot_brain: "Hello Python" elif "Today" in you: today = date.today() robot_brain = today.strftime("%B %d, %Y") elif "Time" in you: now = datetime.today() robot_brain = now.strftime("%H hours %M minutes %S seconds") elif "goodbye" in you: # turn-off application when say goodbye robot_brain = "Good Bye" break else: robot_brain = "I'm fine thank you and you" print("Robot:" + robot_brain) robot_mouth.say(robot_brain) robot_mouth.runAndWait() |
And then run the file python3 robot.py
and try chatting with it!
That’s like you saw above we have just completed a virtual assistant with python, with just over 50 lines of code is not too difficult, right now it is not very smart. You can continue to develop and customise to improve it.