Skip to main content

How to make calculator in python kivy

  How make calculator in python We make a calculator in python using kivy module . Kivy module helps us to make android apps . First wait , If you run a kivy program in your terminal then you see a error like this To solve this problem you have to go kivy docs and follow the instruction of kivy docs.  First we have to make a virtual environment and then in the virtual environment you have to install kivy . After that you have to write  kivy_venv\Scripts\activate in your terminal and then run the file . If this method not work then write the comment below. LETS START CODING First we import kivy .For this we hve to write  from  kivy.app  import  App from  kivy.uix.label  import  Label from  kivy.uix.button  import  Button from  kivy.core.window  import  Window from  kivy.uix.gridlayout  import  GridLayout from  kivy.uix.widget  import  Widget from  kivy.lang  import ...

How to make Desktop Voice Assistant in python

 What is Desktop Voice Assistant

Desktop voice assistant is a computer program which listen commands and complete task assigned by the user. It is a example of weak AI which only execute and perform task designed by the programmer.


How to make desktop Voice Assistant

We have to write a program in python for desktop voice assistant.

Modules Required

pyttsx3 - pyttsx3 is a text to speech conversion module in python. pyttsx3 helps us to convert text into speech . We have to import pyttsx3. we write import pyttsx3 to import pyttsx3.

For changing the gender of voice we have to write 

engine= pyttsx3.init()
voices=engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)

Note- If pyttsx3 is not install for some reason , first you have to check the pip list for this we have to write pip list. After that  you should go to https://pypi.org/project/pyttsx3  and click the release version and select the version which is in pip list and copy the version paste it in your terminal .

speech_recognition - speech_recognition is a speech to text conversion module in python. speech_recognition helps us to convert speech into text. We have to import speech_recognition . We write import speech_recognition as sr

NOTE- we use sr in place of speech_recognition.

datetime -datetime module provide time .It help us to display date in the program. we write import datetime to import datetime .

webbrowser - webbrowser module help us to collect data from web. we write import webbrowser to import webbrowser.

wikipedia- wikipedia module help us to collect data from wikipedia. we write import wikipedia to import wikipedia.

os- os module help us to open any file, folder ,desktop application .we write import os to import os.

Set up speech engine

We have to store pyttsx3 module in a variable. After that we make a function say().

For this we have to write engine= pyttsx3.init()


def say(audio):
    engine.say(audio)
    engine.runAndWait()

Function to greet the user

We define a function wish() for the AI assistant to greet user. the now().hour function abstract the hour from the current time .

If the hour is greater than zero and less than 12 , then assistant wishes user with the message of "Good Morning".

If the hour is greater than 12 and less than 18, then assistant wishes user with the message of "Good Afternoon".

If the hour is greater than 18 then assistant wishes user with the message of "Good Evening".

Set up command function 

we have to define a function listen() for the AI assistant to understand the human language . the microphone capture the human speech and recognizer recognizes the speech to take command 

We have to take exception handling is used to handle the exception during the run time . We use  recognize_google function which uses google audio to recognize speech. For this we have to write 

def listen():
    r=sr.Recognizer()
    with sr.Microphone() as source:
        print("listening...")
        
        r.adjust_for_ambient_noise(source)
        audio=r.listen(source)

        try:
            print("Recognition...")
            query=r.recognize_google(audio,language='en-in')
            print(f"You said:{query}")
        except Exception as e:
            print("Say that again please...")
            print(e)
            return "None"
        return query


Main function

The main function will check the condition .If the condition is true than it will return output . We use if, else condition for this . we store the human speech stored in variable . So, we make a variable query

Conditions

'open google' in query- When user say open google , then AI assistant will say ' opening google' and  open google.com in browser and off the program automatic .

'open youtube' in query- When user say open youtube , then AI assistant will say 'opening youtube' and open youtube in browser and off the program automatic.

'wikipedia' or 'what is' or 'who is' in query- When user say wikipedia or what is or who is , then AI assistant will say 'Searching wikipedia' and say the results .

'play music' in query - When user say play music , then AI assistant will say playing music and play the music and off the program automatic .

'time' in query -When user ask time ,then AI assistant will say the time.

'who are you' in query- When user say who are you ,then AI assistant will say I am Desktop Assistant.

'ok bye' in query - When user say ok bye , then AI assistant will say bye and off the program.

NOTE - you can add more conditions .


HERE ARE THE CODE



OUTPUT