def run(self, input): errorMessages = [] for key in input.keys(): slotToFill = self.form.getSlotByName(key) if not input[key] == "" and not slotToFill is None: tempSlot = copy.deepcopy(slotToFill) tempSlot.slotValue = input[key] try: if not tempSlot.isValid(): errMsg = "Slot {}. Incorrect Value {}".format( tempSlot.slotName, tempSlot.slotValue) errorMessages.append(errMsg) print(errMsg) self.form.emptyFilledSlot(slotToFill) continue except NotImplementedError: print("\n Slot {} not initialized", slotToFill.slotName) if not slotToFill.slotValue == "": print("\n Overwriting slot {} having value {}".format( slotToFill.slotName, slotToFill.slotValue)) slotToFill.slotValue = input[key] self.form.fillEmptySlot(slotToFill) if len(errorMessages) > 0: text_to_speech( "Sorry, Invalid value was provided for the following slots.") for errMsg in errorMessages: text_to_speech(errMsg) global MACHINE_CONTROL MACHINE_CONTROL = True return None
def startSpeechDetector(user_name, check_list, language_code, gender, address): speech_to_text_client = speech.SpeechClient() config = types.RecognitionConfig( encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz=RATE, language_code=language_code) streaming_config = types.StreamingRecognitionConfig(config=config, interim_results=True) text_to_speech( "Well Done" + user_name + ", new you are all set!, Just tell me the items you are carrying out when you leave the house, I'll remind you the forgotten items. Enjoy!", language_code, gender) with MicrophoneStream(RATE, CHUNK) as stream: audio_generator = stream.generator() requests = (types.StreamingRecognizeRequest(audio_content=content) for content in audio_generator) speech_to_text_responses = speech_to_text_client.streaming_recognize( streaming_config, requests) # Now, put the transcription responses to use. while True: location = geocoder.ip('me') latlng_string = str(location.latlng[0]) + ', ' + str( location.latlng[1]) estimate_time = get_time_estimation(latlng_string, address) listen_respond_loop(speech_to_text_responses, check_list, user_name, language_code, gender, address, estimate_time) sleep(0.05)
def run(self, input=None): slotResponse = "" empty_slot_obj = self.form.getNextEmptySlot() text_to_speech(empty_slot_obj.slotQuestion) user_text = speech_to_text() slotResponse = user_text if len(slotResponse) > 0: global MACHINE_CONTROL MACHINE_CONTROL = True return slotResponse
def run(self): output = None stateDict[self.currentState.value].run() while not self.currentState.value is self.stop.value: try: global MACHINE_CONTROL user_input = None input = None if MACHINE_CONTROL: if self.currentState == DIALOG_STATES.FILL: if self.dialogForm.isFilled(): self.currentState = DIALOG_STATES.REPORT else: #incorrect slot filled or some slot empty # go to ask state and in it's run we'll get a response self.currentState = DIALOG_STATES.ASK elif self.currentState == DIALOG_STATES.ASK: #process the output from previous run of ask state user_input = output elif self.currentState == DIALOG_STATES.REPORT: self.currentState = DIALOG_STATES.GREET MACHINE_CONTROL = False else: user_input = speech_to_text() while user_input == "": text_to_speech( "Sorry! I did not catch that. Would you like to try again?" ) user_input = speech_to_text() # analyze user input for intent and entities in the current or previous solicitation if not user_input is None: try: intentAndEntitiesDict = self.nlu.DiscoverIntentAndEntities( user_input) intentType = intentAndEntitiesDict["intent"] self.currentState = self.stateTable[ self.currentState.value][intentType.value] input = intentAndEntitiesDict["entities"] except: text_to_speech( "Uh Oh! I think there was something wrong with me. Got to go!" ) output = None output = self.stateDict[self.currentState.value].run(input) except: raise Exception( "Failure during state transition from state:{}".format( self.currentState))
def check_response(data): print data if data.lower() in ['hi', 'hello']: data = 'Hi hello' elif data.lower() == 'thanks': data = 'welcome' elif data.lower() == 'take photo': data = 'Take Photo' os.system('python snapshot.py') elif data.lower() == 'bye': ts.text_to_speech('Good bye') sys.exit() elif data.lower() == 'time please': from datetime import datetime data = datetime.now().strftime('%H hours %M minutes') else: data = "you said: " + data ts.text_to_speech(data) print data
def run(self, input): # after report generation is done we want to automatically go back to greet state # without processing any input. Hence, setting MACHINE_CONTROL global MACHINE_CONTROL MACHINE_CONTROL = True # TODO: this is an abuse # We should have instead iterated through the form's slots location = 'Washington' dt_tm_obj = datetime.now() dt_tm_obj.replace(hour=dt_tm_obj.hour + 1) #get weather for the next hour inputTime = None for slot in self.weather_form.filledSlots: if slot.slotName == 'LOCATION': location = slot.slotValue elif slot.slotName == 'DATE': inputDate = datetime.strptime(slot.slotValue, '%Y-%m-%d') dt_tm_obj = dt_tm_obj.replace(year=inputDate.year, month=inputDate.month, day=inputDate.day) elif slot.slotName == 'TIME': inputTime = datetime.strptime(slot.slotValue, '%H:%M') dt_tm_obj = dt_tm_obj.replace(hour=inputTime.hour, minute=inputTime.minute) text_to_speech("The weather in {} ".format(location)) dayRep, hrRep = self.weather_reporter.get_weather_report( location, dt_tm_obj) text_to_speech(dayRep) # Generate hour report only if the user provides time if not inputTime is None: text_to_speech(hrRep) return None
def listen_respond_loop(responses, check_list, user_name, language_code, gender, address, estimate_time): global past_forgotten_items """Iterates through server responses and prints them. The responses passed is a generator that will block until a response is provided by the server. Each response may contain multiple results, and each result may contain multiple alternatives; for details, see https://goo.gl/tjCPAU. Here we print only the transcription for the top alternative of the top result. In this case, responses are provided for interim results as well. If the response is an interim one, print a line feed at the end of it, to allow the next result to overwrite it, until the response is a final one. For the final one, print a newline to preserve the finalized transcription. """ num_chars_printed = 0 output = '' for response in responses: if not response.results: continue # The `results` list is consecutive. For streaming, we only care about # the first result being considered, since once it's `is_final`, it # moves on to considering the next utterance. result = response.results[0] if not result.alternatives: continue # Display the transcription of the top alternative. transcript = result.alternatives[0].transcript # Display interim results, but with a carriage return at the end of the # line, so subsequent lines will overwrite them. # # If the previous result was longer than this one, we need to print # some extra spaces to overwrite the previous result overwrite_chars = ' ' * (num_chars_printed - len(transcript)) if not result.is_final: sys.stdout.write(transcript + overwrite_chars + '\r') sys.stdout.flush() num_chars_printed = len(transcript) else: output = output + '\n' + transcript output = output.lower() print(transcript + overwrite_chars) if transcript.lower().find('good morning') != -1: t = localtime() current_time = strftime("%H:%M", t) text_to_speech( 'Good morning ' + user_name + ' now is ' + current_time + ', it takes ' + estimate_time + ' travel to ' + address, language_code, gender) sleep(0.05) # if transcript.lower().find('hello google') != -1: # output = '' # Exit recognition if any of the transcribed phrases could be # one of our keywords. if transcript.lower().find('hello google') != -1 and re.search( r'\b(missing|missed)\b', transcript.lower(), re.I): forgottenItems, badFound = findKeyword( output, ['freak', 'stupid', 'f**k', 'dumb'], check_list) # reset not_forget_this_time not_forget_this_time = [] # if have past forgotten items, check if they are included this time if past_forgotten_items != []: for past_forgotten_item in past_forgotten_items: if output.find(past_forgotten_item) != -1: not_forget_this_time.append(past_forgotten_item) # update the past_forgotten_items, since the items forgot this time is changed past_forgotten_items = [] if forgottenItems != []: for forgottenItem in forgottenItems: past_forgotten_items.append(forgottenItem) text_to_speech( sentense_converter(not_forget_this_time, forgottenItems, badFound, user_name), language_code, gender) output = '' num_chars_printed = 0 break num_chars_printed = 0
import re import sys import webbrowser from textToSpeech import text_to_speech reg_ex = re.search('open website (.+)', sys.argv[0]) if reg_ex: domain = reg_ex.group(1) text_to_speech(f'opening website {domain}'.replace('.', ' dot ')) webbrowser.open(f'https://www.{domain}') else: pass
import urllib.request import os from speechToText import speech_to_text interpreter = NaturalLanguageInterpreter.create('./models/nlu/default/chat') action_endpoint = EndpointConfig(url="http://localhost:5055/webhook") agent = Agent.load('models/dialogue', interpreter=interpreter,action_endpoint=action_endpoint) cnt = 0 speechAnswer.play_music('./mp3/noneLabel.mp3', volume = 1) print('Bot đã sẵn sàng trả lởi. Nói "dùng lại để kết thúc"') while True: cnt+=1 a = speech_to_text() print(a) if a == 'dừng lại': break responses = agent.handle_text(a) for response in responses: link_mp3 = textToSpeech.text_to_speech(response['text']) # convert string "answer" to file .mp3 time.sleep(5) # wait for FPT server create link mp3 try: urllib.request.urlretrieve(link_mp3, 'mp3/answer'+ str(cnt) +'.mp3') # download file answer.mp3 from FPT server except: time.sleep(5) urllib.request.urlretrieve(link_mp3, 'mp3/answer'+ str(cnt) +'.mp3') if os.path.exists('mp3/answer'+ str(cnt) +'.mp3'): speechAnswer.play_music('mp3/answer'+ str(cnt) +'.mp3', volume = 1) # play file "answer.mp3" else: time.sleep(3) speechAnswer.play_music('mp3/answer'+ str(cnt) +'.mp3', volume = 1) # play file "answer.mp3"
#for i, microphone_name in enumerate(mic_list): # if microphone_name == mic_name: # device_id = i #use the microphone as source for input. Here, we also specify #which device ID to specifically look for incase the microphone #is not working, an error will pop up saying "device_id undefined" with sr.Microphone( sample_rate = sample_rate, chunk_size = chunk_size) as source: #wait for a second to let the recognizer adjust the #energy threshold based on the surrounding noise level r.adjust_for_ambient_noise(source) data = "Hi Aarun " ts.text_to_speech(data) print data while True: #listens for the user's input audio = r.listen(source) try: text = r.recognize_google(audio) rs.check_response(text) #error occurs when google could not understand what was said except sr.UnknownValueError: print("Google Speech Recognition could not understand audio")
def respond_weather_info(text): location = get_location_entity(text) weather_report = get_weather_info(location) text_to_speech(weather_report[0])
from googleweather import get_weather_info from entity_extraction import get_location_entity def get_user_text(): user_text = speech_to_text() while len(user_text) == 0: user_text = speech_to_text() return user_text def respond_weather_info(text): location = get_location_entity(text) weather_report = get_weather_info(location) text_to_speech(weather_report[0]) #respond_weather_info("What\'s the weather in Pittsburgh like?") load_embed() # load embeddings for intent recognition text_to_speech("Hi! I am Joanna! What can I tell you about the weather today?") while(1): try: user_text = get_user_text() response_text, intent = get_response(user_text) text_to_speech(response_text) # if intent == "inform": # respond_weather_info(user_text) except KeyboardInterrupt: print("press control-c again to quit")
from textToSpeech import text_to_speech # make sure vlc player is installed in the system use sudo apt-get install vlc for ubuntu import urllib.request from bs4 import BeautifulSoup textToSearch = re.search('play song (.*)', sys.argv[0]) if textToSearch: query = urllib.parse.quote(textToSearch.group(1)) url = "https://www.youtube.com/results?search_query=" + query response = urllib.request.urlopen(url) html = response.read() soup = BeautifulSoup(html, 'html.parser') song_to_play = '' text_to_speech('searching song on youtube') for vid in soup.findAll(attrs={'class': 'yt-uix-tile-link'}): if 'watch?v=' in vid['href']: song_to_play = vid['href'] break else: continue if song_to_play: print(f'song url, https://www.youtube.com{song_to_play}') video_url = pafy.new(f'https://www.youtube.com{song_to_play}') best = video_url.getbest() text_to_speech(f"playing song {textToSearch.group(1)}") vlc_instance = vlc.Instance() vlc_player = vlc_instance.media_player_new() video = vlc_instance.media_new(best.url) video.get_mrl()
def getConfig(): client = speech.SpeechClient() config = types.RecognitionConfig( encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz=RATE, language_code='en_us') streaming_config = types.StreamingRecognitionConfig( config=config, interim_results=True) with speechToText.MicrophoneStream(RATE, CHUNK) as stream: audio_generator = stream.generator() requests = (types.StreamingRecognizeRequest(audio_content=content) for content in audio_generator) responses = client.streaming_recognize(streaming_config, requests) # default values user_name = '' address = '' check_list = [] language_code = 'en_au' gender = 'F' # user's name text_to_speech("Hello, I am your morning sanity checker, Misciker. I'm here in case you forget something. Let's get started with some settings.", language_code, gender) text_to_speech("What's your name?", language_code, gender) transcript = listen(responses) # transcript = input("a") user_name = transcript.lower().split()[-1] print(user_name) text_to_speech("Nice to meet you, " + user_name + ". Do you know you can not only change my voice type and also my accent? It is all up to you. What type of accent do you want me to speak?", language_code, gender) # accent choice language_code = '' count = 0 while language_code == '': transcript = listen(responses) # transcript = input("a") accent = transcript.lower() if accent.find('british') != -1: language_code = 'en-gb' elif accent.lower().find('america') != -1: language_code = 'en-us' elif accent.lower().find('india') != -1: language_code = 'en-in' elif accent.lower().find('austrilia') != -1: language_code = 'en-au' else: if count == 2: text_to_speech('I don\'t understand that, can you repeat that?', language_code, gender) count = 0 count += 1 print(language_code) # voice type text_to_speech("Great! Nice choice!", language_code, gender) text_to_speech("Since I am your personal smart reminder, you can custmize my voice type. Which gender do you prefer?", language_code, gender) gender = '' count = 0 while gender == '': transcript = listen(responses) # transcript = input("a") gender_string = transcript.lower() if gender_string.lower().find('female') != -1: gender = 'F' elif gender_string.lower().find('male') != -1: gender = 'M' else: if count == 2: text_to_speech('I don\'t understand that, can you repeat that?', language_code, gender) count = 0 count += 1 print(gender) # address text_to_speech('Well done! ' + user_name + ' ,could I have your work address? You can type in the concole.', language_code, gender) address = input('work address: ') print(address) # checklist text_to_speech("Awesome! " + user_name + ", now we are talking! You can include as many items as you want into your checklist, so that I can remind you what items you have forgot to carry before you leave the house everytime. What items do you want to add into your checklist?", language_code, gender) check_list_string = input('checklist, seperate use white space: ') check_list = check_list_string.split() print(check_list) return user_name, check_list, language_code, gender, address
def run(self, input=None): text_to_speech(random.choice(self.closureMsg)) return None
def run(self, input=None): if not self.visited: text_to_speech(random.choice(self.greetings)) self.visited = True else: text_to_speech(random.choice(self.revisitMsg))