-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
100 lines (93 loc) · 3.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import speech_recognition as sr # For sppech recognition using google ai
import pyttsx3 # For voice output
import os
import numpy as np # To generate random choice
from nlp_pipeline.chatbot import chatbot
class ChatBot():
def __init__(self):
print("----- Warming up -----")
# Sets Name of chatbot
def set_name(self,name):
self.name = name
# Retuens Name of chatbot
def get_name(self):
return self.name
# Converts Speech to text
def speech_to_text(self):
recognizer = sr.Recognizer()
with sr.Microphone() as mic:
print("Currently Listening...")
recognizer.adjust_for_ambient_noise(mic,duration=1)
audio = recognizer.listen(mic,timeout=15)
text = "Error"
try:
text = recognizer.recognize_google(audio)
print("Me -> ", text)
return text
except sr.RequestError as e:
print("404 -> Could not request results; {0}".format(e))
return text
except sr.UnknownValueError:
print("404 -> Unknown error occurred")
return text
# Converts text to speech
def text_to_speech(self,text):
print("AI -> ", text)
speaker = pyttsx3.init()
voice = speaker.getProperty('voices')
speaker.setProperty('voice', voice[1].id)
speaker.say(text)
speaker.runAndWait()
# Returnes NLP response
def chat(self,text):
chat = chatbot(text)
return chat
if __name__ == "__main__":
ai = ChatBot()
while True:
ai.text_to_speech("Do you want to chat or speak with me?")
action = int(input("(1 to chat 2 to speak) \nMe -> "))
# If user choose to chat
if action == 1:
inp = input("AI -> What do you want to call me?\nMe -> ")
ai.set_name(name=inp)
print("AI -> Great what's on your mind?")
while True:
inp = input("Me ->")
if any(i in inp for i in ["quit","exit","close","shut down","bye"]):
break
elif any(i in inp for i in ["your name","who are you"]):
print("AI -> I'm " + ai.get_name())
else:
output = ai.chat(inp)
print("AI ->",output)
# If user choose to speak
elif action == 2:
ai.text_to_speech("What do you want to call me?")
while True:
res = ai.speech_to_text()
if res == "Error":
ai.text_to_speech("Sorry, come again?")
else:
break
ai.set_name(name=res)
ai.text_to_speech("Great what's on your mind?")
while True:
res = ai.speech_to_text()
if any(i in res for i in ["thank","thanks"]):
res = np.random.choice(["you're welcome!","anytime!","no problem!","cool!","I'm here if you need me!","mention not"])
elif any(i in res for i in ["your name","who are you"]):
res = "I'm " + ai.get_name()
ai.text_to_speech(res)
elif any(i in res for i in ["exit","close","quit","bye"]):
break
else:
if res=="Error":
res="Sorry, come again?"
else:
output = ai.chat(res)
ai.text_to_speech(output)
# Good bye text
res = np.random.choice(["Tata","Have a good day","Bye","Goodbye","Hope to meet soon","peace out!"])
ai.text_to_speech(res)
break