-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtexttospeechalt.py
65 lines (56 loc) · 1.86 KB
/
texttospeechalt.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
import speech_recognition as sr
from time import ctime
import webbrowser
import time
import playsound
import os
import random
from gtts import gTTS
import sys
from main import bot as chatbot
r = sr.Recognizer()
def record_audio(ask = False):
with sr.Microphone() as source:
if ask:
bot_speak(ask)
audio = r.listen(source)
voice_data = ''
try:
voice_data = r.recognize_google(audio)
except sr.UnknownValueError:
bot_speak('Sorry, I did not get that')
except sr.RequestError:
bot_speak('Sorry, my speech service is down')
return voice_data
def bot_speak(audio_string):
tts = gTTS(text = audio_string, lang = 'en-GB')
r = random.randint(1,10000000)
audio_file = 'audio-' + str(r) + '.mp3'
tts.save(audio_file)
playsound.playsound(audio_file)
print(audio_string)
os.remove(audio_file)
def respond(voice_data):
print(voice_data)
if 'exit' in voice_data:
bot_speak('It was nice talking to you. Goodbye!')
sys.exit()
elif 'what is the time' in voice_data:
bot_speak(ctime())
elif 'search' in voice_data:
search = record_audio('What do you want to search for?')
url = 'https://google.com/search?q=' + search
webbrowser.get().open(url)
elif 'find location' in voice_data:
location = record_audio('What is the location you want to search for?')
url = 'https://google.nl/maps/place/' + location + '/&'
webbrowser.get().open(url)
bot_speak('Here is the location of ' + location)
else:
answer = str(chatbot.get_response(voice_data))
bot_speak(answer)
time.sleep(1)
bot_speak('How can I help you?')
while(1):
voice_data = record_audio()
respond(voice_data)