def interface(query): ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN, SUBSCRIPTION_KEY) request = ai.text_request() request.lang = 'en' # optional, default value equal 'en' request.query = query response = request.getresponse() data = json.load(response) task = data['result'] if task["action"] == 'device.increase': if task["parameters"]["module"] == "volume": volume.volume_up(task["parameters"]["number"]) elif task["parameters"]["module"] == "brightness": brightness.brightness_up(task["parameters"]["number"]) elif task["action"] == "device.decrease": if task["parameters"]["module"] == "volume": volume.volume_down(task["parameters"]["number"]) elif task["parameters"]["module"] == "brightness": brightness.brightness_down(task["parameters"]["number"]) elif task["action"] == "device.switch_on": if task["parameters"]["module"] == "wifi": wifi.wifi_on() elif task["action"] == "switch_off": if task["parameters"]["module"] == "wifi": wifi.wifi_off() elif task["action"] == "browse.open": browser_open.open_site(task["parameters"]["website"]) elif task["action"] == "web.search": browser_open.web_search((task["parameters"]["service_name"]).lower(), task["parameters"]["q"]) elif task["action"] == "media.video_search": browser_open.web_search((task["parameters"]["service_name"]).lower(), task["parameters"]["q"]) elif task['metadata']['speech'] != "": tts.say(task['metadata']['speech']) else: wolfram.wolfram(query)
def gstt(): r = sr.Recognizer() m = sr.Microphone() isAwake = False try: print("A moment of silence, please...") with m as source: r.adjust_for_ambient_noise(source) print("Set minimum energy threshold to {}".format(r.energy_threshold)) while True: print("Say something!") with m as source: audio = r.listen(source) print("Got it! Now to recognize it...") try: # recognize speech using Google Speech Recognition value = r.recognize_google(audio) if isAwake: # If SAM is supposed to respond if findWholeWord('see you later')(value): # If user says Bye bye Sam to close down sam say("Bye sir.") isAwake = False else: # If user says something else interface.interface(value) elif findWholeWord('wake up')(value): # If SAM isn't awake, then wake him up say('Hello, sir.') isAwake = True else: # If SAM isn't awake, nor does the user want to wake him up # SAM doesn't do anything pass except sr.UnknownValueError: print("Oops! Didn't catch that") except sr.RequestError as e: print( "Uh oh! Couldn't request results from Google Speech Recognition service; {0}" .format(e)) time.sleep(0.5) except KeyboardInterrupt: pass
def gstt(): r = sr.Recognizer() m = sr.Microphone() isAwake = False try: print("A moment of silence, please...") with m as source: r.adjust_for_ambient_noise(source) print("Set minimum energy threshold to {}".format(r.energy_threshold)) while True: print("Say something!") with m as source: audio = r.listen(source) print("Got it! Now to recognize it...") try: # recognize speech using Google Speech Recognition value = r.recognize_google(audio) if isAwake: # If SAM is supposed to respond if findWholeWord('see you later')(value): # If user says Bye bye Sam to close down sam say("Bye sir.") isAwake = False else: # If user says something else interface.interface(value) elif findWholeWord('wake up')(value): # If SAM isn't awake, then wake him up say('Hello, sir.') isAwake = True else: # If SAM isn't awake, nor does the user want to wake him up # SAM doesn't do anything pass except sr.UnknownValueError: print("Oops! Didn't catch that") except sr.RequestError as e: print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e)) time.sleep(0.5) except KeyboardInterrupt: pass