import paho.mqtt.publish as publish #import paho.mqtt.client as mqtt import speech_recognition as sr import pywhatkit import pyttsx3 import wikipedia #MQTT_SERVER = "127.0.0.1" MQTT_SERVER = "mqtt.eclipseproject.io" MQTT_PATH = "test_channel" import time listener = sr.Recognizer() #to recognize your voice engine = pyttsx3.init() #initialize this library """to change the voice of the assistant to a female voice""" voices = engine.getProperty('voices') engine.setProperty('voice', voices[1].id) #chooses 2nd voice in index """To enable the assistant to talk to you""" engine.say('Hello, your assistant is here. Please say assistant play song name or who is person name') engine.runAndWait() def talk(text): """To enable the assistant to talk to you""" engine.say(text) engine.runAndWait() while True: publish.single(MQTT_PATH, "Connected to Pi!", hostname=MQTT_SERVER) # send data continuously every 10 seconds time.sleep(10) def receive_command(): #in case the microphone doesn't work #command = "" try: with sr.Microphone() as source: # source of our audio print('listening...') voice = listener.listen(source) #listener should listen to the source i.e. microphone command = listener.recognize_google(voice) #uses Google API #Wake word detection command = command.lower() if 'assistant' in command: command = command.replace('assistant','') #excludes the word assistant from the command print(command) except: pass return command engine.say('If the Pi has heard your command, it will print it') def run_assistant(): command = receive_command() #returns command without 'assistant' word print(command) #to play a song, recognize the word play if 'play' in command: # detects 'play' in the command song = command.replace('play','') # exclude 'play' from your command talk('playing' + song) print(song) pywhatkit.playonyt(song) #play on YouTube elif 'who is' in command: #wikipedia search person = command.replace('who is','') info = wikipedia.summary(person,1) print(info) talk(info) while True: run_assistant()