i tried making voice assistant..leart from youtube.. the programme was same as it was by that creator but in my case always onlt the if statement is running..pls help me out..it recognising what iam saying but anyhow it always runs the if case.. no matter what i say..................................................................................................................................................................................
code is like:
import speech_recognition as sr
import time
import datetime
import subprocess
import pywhatkit
import pyttsx3
import webbrowser
import sys
import os
import requests
import json
global commands
engine=pyttsx3.init('sapi5')
voices=engine.getProperty('voices')
engine.setProperty('voice',voices[0].id)
recognizer=sr.Recognizer()
def speak(text):
engine.say(text)
engine.runAndWait()
def continueassit():
speak('Do you want me to continue assisting you')
continue2()
def continue2():
with sr.Microphone() as source:
print('Clearing background noises..Please wait')
recognizer.adjust_for_ambient_noise(source,duration=0.5)
print('Listening..')
speak('Listening')
recordedaudio2=recognizer.listen(source)
try:
print('Recognizing..')
cmd2=recognizer.recognize_google(recordedaudio2,language='en_in')
print('Your message:',format(cmd2))
except Exception as e:
print(e)
print("I did not quite get that.. Please repeat")
speak("I did not quite get that.. Please repeat")
continue2()
if 'yes' in cmd2:
speak("at your service")
takecmd()
elif 'no' in cmd2:
speak("closing")
exit()
def wishme():
hour=int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
print("Good Morning!!!")
speak("Good Morning!!")
elif hour>=12 and hour<18:
print('Good afternoon')
speak("Good Afternoon")
else:
print("Good Evening")
speak("good evening")
def takecmd():
with sr.Microphone() as source:
speak('Clearing background noises..Please wait')
recognizer.adjust_for_ambient_noise(source,duration=0.5)
print('Listening..')
speak('Listening')
recordedaudio=recognizer.listen(source)
try:
print('Recognizing..')
cmd=recognizer.recognize_google(recordedaudio,language='en_in')
print('Your message:',format(cmd))
except Exception as e:
print(e)
print("I did not quite get that..So Please repeat")
speak("I did not quite get that.. so Please repeat")
takecmd()
if __name__ == "__main__":
wishme()
while True:
if 'youtube' or 'open youtube' in cmd:
webbrowser.open("http://youtube.com",new=1)
continueassit()
elif 'brave' or 'open brave' in cmd:
b=('opening brave..')
engine.say(b)
engine.runAndWait()
path=('D:\brave')
os.system(f"open {path}")
continueassit()
elif 'hi' or 'hello' in cmd:
speak('hi there.. tell me how can i help you')
print('Hii there..')
takecmd()
elif 'time' in cmd:
time = datetime.datetime.now().strftime('%I:%M %p')
print(Time)
engine.say(Time)
engine.runAndWait()
continueassit()
elif 'news' in cmd:
news = webbrowser.open_new_tab('https://timesofindia.indiatimes.com/home/headlines')
speak('Here are some headlines from the Times of India....Happy reading')
time.sleep(6)
continueassit()
elif 'wikipedia' in cmd:
speak('Searching Wikipedia...')
command=command.replace("wikipedia","")
results=wikipedia.summary(command,sentences=2)
speak("According to Wikipedia")
print(results)
speak(results)
time.sleep(3)
continueassit()
else:
takecmd()