|
| 1 | +import speech_recognition |
| 2 | +import pyttsx3 as tts |
| 3 | +from neuralintents import GenericAssistant |
| 4 | +import sys |
| 5 | + |
| 6 | +''' |
| 7 | +The below code must be stored in a JSON file so that the python program can access it whenver needed. |
| 8 | +{"intents": [ |
| 9 | + { |
| 10 | + "tag": "greeting", |
| 11 | + "patterns": ["Hey", "Hello", "Hi", "What's up?", "Good Day"], |
| 12 | + "responses": ["Hello there!", "Hello, what can I do for you?"] |
| 13 | + }, |
| 14 | + { |
| 15 | + "tag": "create_note", |
| 16 | + "patterns": ["New note", "Create a note"], |
| 17 | + "responses": [""] |
| 18 | + }, |
| 19 | + { |
| 20 | + "tag": "add_todo", |
| 21 | + "patterns": ["New item", "Add an item"], |
| 22 | + "responses": [""] |
| 23 | + }, |
| 24 | + { |
| 25 | + "tag": "show_todos", |
| 26 | + "patterns": ["Show my todos", "What is on my list"], |
| 27 | + "responses": [""] |
| 28 | + }, |
| 29 | + { |
| 30 | + "tag": "exit", |
| 31 | + "patterns": ["Bye", "See you", "Quit", "Exit"], |
| 32 | + "responses": ["Thank you for spending time with me."] |
| 33 | + }, |
| 34 | +]} |
| 35 | +''' |
| 36 | + |
| 37 | +recognizer = speech_recognizer.Recognizer() |
| 38 | +speaker = tts.init() |
| 39 | +speaker.setProperty('rate', 150) #rate is property, 150 is the value |
| 40 | +#Creating an object to access the todo list |
| 41 | +todo_list = ['Go Shopping', 'Clean Room'] |
| 42 | + |
| 43 | +#Greeting the user |
| 44 | +def greeting(): |
| 45 | + speaker.say("Hello, What can I do for you?") |
| 46 | + speaker.runAndWait() |
| 47 | + |
| 48 | + #Function to create and add new note |
| 49 | +def create_note(): |
| 50 | + global recognizer #Making the variable global |
| 51 | + speaker.say("What do you want to write as note?") |
| 52 | + speaker.runAndWait() #Asking for user input |
| 53 | + done = True |
| 54 | + #The try block is used in case the microphone fails |
| 55 | + while done: |
| 56 | + try: |
| 57 | + with speech_recognition.Microphone() as mic: |
| 58 | + recognizer.adjust_for_ambient_noise(mic, duration = 0.2) |
| 59 | + #Accepting user voice input |
| 60 | + audio = recognizer.listen(mic) |
| 61 | + note = recognizer.recognize_google(audio) |
| 62 | + note = note.lower() |
| 63 | + speaker.say("Choose a filename!") |
| 64 | + speaker.runAndWait() |
| 65 | + recognizer.adjust_for_ambient_noise(mic, duration = 0.2) |
| 66 | + #Accepting user filename |
| 67 | + audio = recognizer.listen(mic) |
| 68 | + filename = recognizer.recognize_google(audio) |
| 69 | + filename = filename.lower() |
| 70 | + with open(filename, 'w') as f: |
| 71 | + f.write(note) |
| 72 | + done = False |
| 73 | + #Terminating the while loop if listened properly |
| 74 | + speaker.say("New note successfully created") |
| 75 | + speaker.runAndWait() |
| 76 | + except speech_recognition.UnknownValueError: |
| 77 | + recognizer = speech_recognizer.Recognizer() |
| 78 | + speaker.say("I did not understand you. Please try again!") |
| 79 | + speaker.runAndWait() |
| 80 | + |
| 81 | +#Speaking out the list |
| 82 | +def show_todo(): |
| 83 | + speaker.say("Your list contains the following elements") |
| 84 | + for item in todo_list: |
| 85 | + speaker.say(item) |
| 86 | + speaker.runAndWait() |
| 87 | + |
| 88 | +#Adding elements to a todo list |
| 89 | +def add_todo(); |
| 90 | + global recognizer |
| 91 | + speaker.say("What item do you want to add?") |
| 92 | + speaker.runAndWait() |
| 93 | + done = True |
| 94 | + while done: |
| 95 | + try: |
| 96 | + with speech_recognition.Microphone() as mic: |
| 97 | + recognizer.adjust_for_ambient_noise(mic, duration = 0.3) |
| 98 | + audio = recognizer.listen(mic) |
| 99 | + item = recognizer.recognize_google(audio) |
| 100 | + item = item.lower() |
| 101 | + todo_list.append(item) |
| 102 | + done = False |
| 103 | + speaker.say(item+" was added to the list!") |
| 104 | + speaker.runAndWait() |
| 105 | + except speech_recognition.UnknownValueError: |
| 106 | + recognizer = speech_recognition.Recognizer() |
| 107 | + speaker.say("I'm sorry, can you repeat it again!") |
| 108 | + speaker.runAndWait() |
| 109 | + |
| 110 | +#Exiting from your assistant |
| 111 | +def close(): |
| 112 | + speaker.say("Bye. Coming back soon!") |
| 113 | + speaker.runAndWait() |
| 114 | + sys.exit(0) |
| 115 | + |
| 116 | +mappings = { |
| 117 | + "greeting": hello, |
| 118 | + "create_node": create_node, |
| 119 | + "add_todo": add_todo, |
| 120 | + "show_todo": show_todo, |
| 121 | + "exit": close |
| 122 | +} |
| 123 | + |
| 124 | +#Training a model to recognize the intents |
| 125 | +assistant = GenericAssistant('intents.json',intent_methods=mappings) |
| 126 | +assistant.train_model() |
| 127 | +assistant.request() |
| 128 | + |
| 129 | +while True: |
| 130 | + try: |
| 131 | + with speech_recognition.Microphone() as mic: |
| 132 | + recognizer.adjust_for_ambient_sound9mic, duration = 0.2) |
| 133 | + audio = recognizer.listen(mic) |
| 134 | + message = recognizer.recognize_google(audio) |
| 135 | + message = message.audio() |
| 136 | + assistant.request(message) |
| 137 | + except speech_recogniton.UnkownValueError: |
| 138 | + recognizer = speech_recognition.Recognizer() |
0 commit comments