Create AI Assistant For Beginners without Machine Learning Part - 1
Iron Man Jarvis Like AI
In this article, you will learn how you can make one AI Assistant using python.
I will teach you how to create a basic AI Voice Assistant and basic features to like sending emails, playing songs, searching on google, etc.
It is a Beginner level course and more following courses will be coming up soon.
This is best way to learn and practice your python knowledge.
Topics
- Text to Speech
- Different Voice Options and speak rate
- Datetime Function
- Greeting Function
- Speech Recognition
- Main Function
- Wikipedia search
- Send Email
- Chrome Search
- Logout, Shutdown, Restart Function’
- Play Songs
- Remember Function
- ScreenShot
- CPU & Battery Update
- Jokes Function
I want to provide to the Source Code link in 2nd part of this article.
Text to Speech or tts
TTS is the process of converting text data into audio data. In python, we simply using python library pyttsx3.
import pyttsx3 #pip install pyttsx3engine = pyttsx3.init() #for initialize the pytts functionengine.say('this is learn with learners .')engine.runAndWait()
OR Simply create fucton name Speak
def speak(words):
engine.say(words)
engine.runAndWait()speak('this is function speak')
Different Voice Options and speak rate
If you want to change the voice version like other male or female voice with different speak rate then add
voice = engine.getProperty('voices')engine.setProperty('voice',voices[0].id) # 0 for female 1 for anoter 2 for another voice so on...#change the rate of voice
new_voice_rate=170 engine.setProperty('rate',newvoice_rate)
after that, you call to speak() to test your changes,
Now play with some new experiment using datetime library. datatime lib is use to show current times and date of the pc.
# for get and speak current timedef times():
Time = datetime.datetime.now().strftime("%H:%M:%S")
speak(Time)times()#for date def date():
year = int(datetime.datetime.now().year)
month= int(datetime.datetime.now().month)
day = int(datetime.datetime.now().day) speak('the current date is')
speak(day)
speak(month)
speak(year)date()
Greeting Function
In this section i will use the speak function to wish me good morning ,and all other So lets start,
def wishme():
speak('welcome back sir!')
times()
date()
speak('me in your service, how can i help you ?') hour=datetime.datetime.now().hour if hour >=6 and hour<12:
speak('good morning')
elif hour >= 12 and hour< 18:
speak("good afternoon")
elif hour >=18 and hour <24:
speak("good evening")
else:
speak("good night")wishme()
Speech Recognition
Using the speech recognition library of python to convert our voice into the text data. simply you install speech recognition by using pip command.
Note :- Internet connection in must with good speed.
import speech_recognition as sr # pip install speechrecognition#need internet connection mustdef takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("listening....")
r.pause_threshold = 1
audio = r.listen(source) try: print("Recognizing'...")
query = r.recognize_google(audio,'en-us')
print(query)
except Exception as e:
print(e)
speak("say that again please...") return "None" return querytakecommand()
call the takecommand() fuction and the file and after execution you speak someting it’s capture and print there.
Main Function
Create the main function to call all function in one main functioin for understanding the code in a one bolck you can download the code from by github account . below I give small part of main function
if __name__ == "__main__":wishme()while True: query = takecommand().lower() print(query) if "time" in query:
times()
elif "date" in query:
date()
elif "offline" in query :
quit()
Wikipedia Search
Lots of information in wikipedia or you can access the wikipedia by your AI Assistant without any high level coding then you use python wikipedia library.
Below the code you add in you main() in while loop elif condition.
import wikipedia #pip install wikipediaelif "wikipedia" in query:
speak("Searching.......")
query = query.replace("wikipedia","")
result= wikipedia.summary(query,sentences = 2)
#sent 2 means it read only two sentence of your search result
speak(result)
Other next topic will be come on next part soon
after read this topic please comment me to improve ure writing skils and you also face any problem after executing this code in practice you can message me instagram and facekbook @learnwithlearners .
I hope you learn somthing for us .