I am making my own voice assistant on python
I want to open websites from it, for example I am saying "open google" and program opening google.com
I was trying to write webbrowser.open(url) in the commands, but when I start the program it opens website URL without any command
This is what I have for now:
import pyttsx3
import speech_recognition as sr
import sys
import subprocess
import webbrowser
def recognize_speech_from_mic(recognizer, microphone) -> dict:
if not isinstance(recognizer, sr.Recognizer):
raise TypeError("`recognizer` must be `Recognizer` instance")
if not isinstance(microphone, sr.Microphone):
raise TypeError("`microphone` must be `Microphone` instance")
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
response = {"success": True,
"error": None,
"transcription": None}
try:
response["transcription"] = recognizer.recognize_google(audio)
except sr.RequestError:
response["success"] = False
response["error"] = "API unavailable"
except sr.UnknownValueError:
response["error"] = "Unable to recognize speech"
return response
my_phrases = {
# Names
'Elsea': ["Hey there", None],
'elsea': ["Hey there", None],
'Elsa': ["I'm listening", None],
'elsa': ["I'm listening", None],
'Elsia': ["I'm here", None],
'elsia': ["I'm here", None],
'Chelsea': ["Go ahead", None],
'chelsea': ["Go ahead", None],
# Main
'hello': ['Hi!, How are you?', None],
'what can you do': ["I can open application and that's all :)", None],
# Stop
'stop': ['Turning off', 'exit'],
'exit': ['Goodbye ;)', 'exit'],
'turn off': ['One moment please...', 'exit'],
# Programs
'open url': ['Yes,sir', webbrowser.open('google.com')],
# Chrome
'Chrome': ['Okay, opening Chrome', chrome],
'open Chrome': ['Opening....', chrome],
'chrome': ['Alright, opening Chrome', chrome],
'open chrome': ['Yes sir', chrome],
}
unknown_command_phrase = ["", None]
engine = pyttsx3.init()
en_voice_id_m = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0"
en_voice_id_f = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0"
gb_voice_id_f = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-GB_HAZEL_11.0"
voices = engine.getProperty('voices')
engine.setProperty('voice', en_voice_id_f)
engine.setProperty('rate', 195)
while True:
engine.runAndWait()
recognizer = sr.Recognizer()
microphone = sr.Microphone()
print("Say something!")
response = recognize_speech_from_mic(recognizer, microphone)
pattern = response['transcription'] # get transcription from response dict
say, command = my_phrases.get(pattern, unknown_command_phrase) # retrieve response from my_phrases
engine = pyttsx3.init()
engine.say(say)
if command == None:
print(f'Looks like you said:\n{pattern}.\n')
pass
elif command == 'exit':
sys.exit()
else:
subprocess.check_output(command, shell=True) # assumes you have these properly configured
pass
And if I write webbrowser.open(url) it opens it when I start the program, but I want to open it only if I say "Open..."
I don't know how to do it
Guys help please