-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (56 loc) · 1.74 KB
/
main.py
File metadata and controls
66 lines (56 loc) · 1.74 KB
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
import speech_recognition as sr
from gtts import gTTS
from playsound import playsound
import os
import json
import time
class ChatBot():
text = ''
def __init__(self, name):
print("----- starting up", name, "-----")
self.name = name
with open("fixes.json", 'r') as f:
self.fixes = json.load(f)
def fix(self):
for word in self.text.split(' '):
if word in self.fixes:
self.text = self.text.replace(word, self.fixes[word])
def unfixed(self, text: str):
for word in text.split(' '):
for key, val in self.fixes.items():
if word == val:
text = text.replace(word, key)
return text
def speech_to_text(self):
recognizer = sr.Recognizer()
with sr.Microphone() as mic:
print("listening...")
audio = recognizer.listen(mic)
try:
self.text = recognizer.recognize_google(audio)
self.fix()
print("me --> ", self.text)
except:
print("me --> ERROR")
def wake_up(self, text):
self.text = ''
return self.name.lower() in text.lower()
def text_to_speech(self, text):
print("AI --> ", text)
text = self.unfixed(text)
print(text)
speaker = gTTS(text=text, lang="en", slow=False)
speaker.save("res.mp3")
playsound('res.mp3')
os.remove("res.mp3")
# Execute the AI
if __name__ == "__main__":
ai = ChatBot(name="Huncho")
while True:
ai.speech_to_text()
res = None
if ai.wake_up(ai.text):
res = "Hello I am Huncho the AI, what can I do for you?"
if res:
ai.text_to_speech(res)
ai.text = ''