-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvoice.py
More file actions
36 lines (29 loc) · 1.09 KB
/
voice.py
File metadata and controls
36 lines (29 loc) · 1.09 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
from chat import bot_name, get_response
from common import *
import speech_recognition as sr
import pyttsx3, time
r = sr.Recognizer()
def TextToSpeech(command):
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()
def VoiceChat():
print(greeting)
while True:
with sr.Microphone() as source:
try:
print(you, end="")
r.adjust_for_ambient_noise(source, duration=0.2)
audio = r.listen(source)
sentence = r.recognize_google(audio)
print(sentence)
if sentence == "goodbye" or sentence == "exit":
response = "Thank you for visiting! I hope to see you again!"
print(f'{bot_name}: {response}\n')
TextToSpeech(response)
break
response = get_response(sentence)
print(f'{bot_name}: {response}\n')
TextToSpeech(response)
except Exception as e:
print(e)