HOW TO GREAT YOUR VOICE ASSISTANT
import speech_recognition as sr
import pyttsx3
# Initialize the speech recognizer and text-to-speech engine
recognizer = sr.Recognizer()
engine = pyttsx3.init()
# Function to convert text to speech
def speak(text):
engine.say(text)
engine.runAndWait()
# Function to listen to user input
def listen():
with sr.Microphone() as source:
print("Listening...")
recognizer.adjust_for_ambient_noise(source) # Adjust for background noise
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio) # Use Google Speech Recognition
print("User: " + text)
return text
except sr.UnknownValueError:
print("Sorry, I didn't catch that.")
except sr.RequestError as e:
print("Oops! An error occurred: " + str(e))
return ""
# Main loop
while True:
query = listen()
if "hello" in query:
speak("Hello! How can I assist you?")
elif "goodbye" in query:
speak("Goodbye!")
break
else:
speak("I'm sorry, I can't help with that.")
SCAN THE QR CODE BELOW TO GET THE TITLE OF THE BOOK.

alexar voice assistant in python

  • 1.
    HOW TO GREATYOUR VOICE ASSISTANT import speech_recognition as sr import pyttsx3 # Initialize the speech recognizer and text-to-speech engine recognizer = sr.Recognizer() engine = pyttsx3.init() # Function to convert text to speech def speak(text): engine.say(text) engine.runAndWait() # Function to listen to user input def listen(): with sr.Microphone() as source: print("Listening...") recognizer.adjust_for_ambient_noise(source) # Adjust for background noise audio = recognizer.listen(source) try: text = recognizer.recognize_google(audio) # Use Google Speech Recognition print("User: " + text) return text except sr.UnknownValueError:
  • 2.
    print("Sorry, I didn'tcatch that.") except sr.RequestError as e: print("Oops! An error occurred: " + str(e)) return "" # Main loop while True: query = listen() if "hello" in query: speak("Hello! How can I assist you?") elif "goodbye" in query: speak("Goodbye!") break else: speak("I'm sorry, I can't help with that.") SCAN THE QR CODE BELOW TO GET THE TITLE OF THE BOOK.