AI Assistance Using python


AI(Artificial intelligence)  is an Intelligence demonstrated by Machines, Which will help to maximize human achievement successfully. 
Like that we have famous chatbot which will respond by machine on questioning of humans and we termed it as AI chatbot or AI assistance. In this tutorial we are going to see how we can implement small AI assistance applications, that can search anything over Wikipedia on your voice command, Can open any application from your desktop. 

Important Packages and Information:

To implement any AI assistance we have to use some packages to recognize your voice, to read any text, and finally to search on the web, for all of this Python already have batteries which are very helpful to us while implement. 

1) pyttsx3 :


         When AI/robot get any input or output he should express that thing in such way all humankind should understand, and most of the human can easily understand when we speak and express the things. In the same to speak any character, word pyttsx3 help to AI to speak in different languages with a different tone. 
         To install  pyttsx3 use below command 

         pip install pyttsx3


2) SpeechRecognition:


        When humans speak to AI, it should able to recognize that language and understand it, then only AI can rectify query and give the proper response. To recognize the language and simplify it speech-recognition pkg help to AI.


         pip install speechRecognition


3) Wikipedia: 

        In our application we are going to use search query via Wikipedia, so Python already has the batterie for that and that can be installed by below command 

        pip install wikipedia


4) Pyaudio:


     To Listen your audio, AI needs this pkg. Most people fail to load this pkg on Linux platform and can face some issues  while listening,  you can solve that by installing 3 packages and one small code change by referring my answer  here               

        sudo apt-get install portaudio19-dev 
        sudo apt-get install portaudio19-dev python-all-dev python3-all-dev && sudo pip install pyaudio
        pip install pyaudio


Implementation :

The Implementation is very simple, using python. I can explain this in steps 

1) Give the voice to your AI Assistance : 

    import pyttsx3
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[11].id)


Import pyttsx3,  using pyttsx3.init() you can fetch all the property of the AI, for Linux or Mac you need to follow the same as mentioned, but for the window, you can use Microsoft Inbuild Speech API i.e. "sapi5" (pyttsx3.init('sapi5') only for windows). 
When you initialize this you get the nearly 16 languages, you can choose that by setProperty(),

engine.setPropert('voice', voice[11].id), which describe the English language is setting to AI bot. 

2)  Let your AI listen to your voice :


   import speech_recognition as sr
   re = sr.Recognizer()
   with sr.Microphone() as source:
      audio = re.listen(source, timeout=3, phrase_time_limit=3)


This code will tell to AI listen to the query or string via a microphone, here you may face problem like 

ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave

To solve this follow my answer here.

3) Searching Query on Wikipedia:


  import wikipedia
  if 'wikipedia' in query:
      talk("Searching in wikipedia")
      query = query.replace("Wikipedia", "")
      results = wikipedia.summary(query, sentences=2)
      talk("According to Wikipedia")


this code part will confuse you, what is exactly is happing, you can see the full code from the code section with the same topic name.

That's all about some basic knowledge of which are the tools are required to install and steps to implement the AI using Python.
Happy Pythoning !!!!


© 2020 Blog, All rights reserved.   This work is licensed under  Creative Commons License