I tried to make a virtual assistant using python, earlier it was only printing listening. Now it is browsing myntra's website for every command given

Viewed 26

''' on executing the following code, when I am giving any instruction to this voice assistant :

  1. it is not printing whatever i am saying
  2. it is opening myntra's website for every instruction I am giving

'''
import pyttsx3 #text to speech import speech_recognition as sr #recognizes speech import wikipedia import webbrowser import datetime import os import smtplib

    engine = pyttsx3.init('sapi5')
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[0].id) #activates voice
    
    
    def speak(audio):
# enables system to speak
        engine.say(audio)
        engine.runAndWait()
    
    
    def wishme():

to take know the current time

        hour = int(datetime.datetime.now().hour) 
    
        if hour >= 00 and hour < 12:
            speak("good morning!")
    
        elif hour >= 12 and hour < 18:
            speak("good afternoon!")
    
        else:
            speak("good evening!")
    
        speak("I am JARVIS sir , how may i help you ?")
    
    
    def takeCommand():
    '''It takes microphone input from user and returns string output'''
# I added r.adjust_for_ambient_noise(source) and reduced threshold frequency
    
        r = sr.Recognizer() # to recognize the user's voice
        with sr.Microphone() as source:
            print("Listening...")
            r.pause_threshold = 1
            r.energy_threshold = 50 #to listen low frequency voices
            r.adjust_for_ambient_noise(source) #to avoid noise
            audio = r.listen(source)
    
        try:
            print("Recognizing...")
           # to recognize user's voice
            query = r.recognize_google(audio , Language = "en-in")
            print(f"user said: {query}\n")
    
        except Exception as e:
            print("Say that again please...")
            return "None"
        return query
    
    
    
    
    
    if __name__ == "__main__":
        # speak("Akshima is a good girl")
        wishme()
    
    while True:
        query = takeCommand().lower()
    
        # logic for executing tasks based on query
# maybe there is an error in this part of the code
        if 'wikipedia' in query:
            speak('Searching Wikipedia...')
            query = query.replace("wikipedia", "")
            result = wikipedia.summary(query, sentences=2)
            speak("according to wikipedia")
            print(result)
            speak(result)
    
    
        elif 'open youtube' in query:
         # to open youtube 
            webbrowser.open("youtube.com")
    
        elif 'open google' in query:
            webbrowser.open("google.com") #to open google
    
        elif 'play music' in query:
            webbrowser.open("spotify.com")
    
        elif 'open myntra' or 'open any online shopping site ' in query:
            webbrowser.open("myntra.com")
    
        elif 'open instagram' in query:
            webbrowser.open("instagram.com")
    
        elif 'chill' in query:
            webbrowser.open("netflix.com")
    
        elif "the time" in query:
            # tell user the current time 
            strTime = datetime.datetime.now().srtftime("%H:%M:%S")
            speak(f"The time is {strTime}\n")
    
        elif 'open code' in query:
          #to  open vs code
            path = "C:\\Users\\HP\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
            os.startfile(path)
    
        
0 Answers
Related