-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathchatbot_voice.py
84 lines (79 loc) · 2.88 KB
/
chatbot_voice.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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 20 11:19:53 2017
@author: Chintan
"""
import random, os, broize
from gtts import gTTS
from playsound import playsound
import speech_recognition as hear
def greeting(sentence):
for word in sentence:
if word.lower() in GREETING_KEYWORDS:
return random.choice(GREETING_RESPONSES)
if __name__ == "__main__":
#for reference of first response
#GREETING_KEYWORDS = ("hello", "hi", "hey", "hey there", "hola", "greetings", "what's up")
#GREETING_RESPONSES = ("hi", "hello there", "hiya", "heya", "*nods*", "hola")
flag = 0
if os.path.exists('counter.txt'):
file = open('counter.txt', 'r')
flag = int(file.read())
file.close()
else:
file = open('counter.txt', 'w')
file.write(str(flag))
file.close()
##########################GET CHOICE###############################
## r = hear.Recognizer()
## with hear.Microphone() as source:
## print("Talk or chat?")
## print("To chat using keyboard, say chat. Otherwise, shoot away!!")
## audio = r.listen(source)
## sentence = "chat"
## try:
## sentence = str(r.recognize_google(audio))
## print("YOU : ", sentence)
## except hear.UnknownValueError:
## print("Sorry! Couldn't understand")
## except hear.RequestError as e:
## print("Could not request Google API Services ", e.format(0))
## ################################CHAT###############################
if True:
#if sentence == "chat":
print("BOT : Hi! You are?")
name = str(input("YOU : "))
print("BOT : Hello " + name + "! How are you doing?")
while(True):
sentence = str(input("YOU : "))
if sentence == "bye":
break
response = str(broize.broback(sentence))
print("BOT : " + response)
##################################TALK#################################
else:
while(True):
with hear.Microphone() as source:
print("I'm listening...")
audio = r.listen(source)
sentence = ''
try:
sentence = str(r.recognize_google(audio))
print("YOU : ", sentence)
except hear.UnknownValueError:
print("Sorry! Couldn't understand")
except hear.RequestError as e:
print("Could not request Google API Services ", e.format(0))
if sentence == "bye":
break
response = str(broize.broback(sentence))
s = gTTS(text=response, lang='en')
print(flag)
s.save(str(flag)+'.mp3')
playsound(str(flag)+'.mp3')
flag+=1
file = open('counter.txt', 'w')
file.write(str(flag))
file.close()
#print(flag)
print("BOT : " + response)