-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
48 lines (41 loc) · 1.05 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
# libs
import speech_recognition as sr
import os
from gtts import gTTS
import playsound
import time
# my files
from nlp import nlp
from Auth.welcomer import *
# init packages
r = sr.Recognizer()
credential_path = "api_key.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
# text to speech
def ava(audio_string):
tts = gTTS(text=audio_string, lang="en", tld='ie')
file = "output_audio.mp3"
tts.save(file)
playsound.playsound(file)
os.remove(file)
# speech recognition
def get_audio():
with sr.Microphone() as source:
audio = r.listen(source)
voice_data = ''
try:
voice_data = r.recognize_google(audio)
except sr.UnknownValueError:
pass
except sr.RequestError:
print("Sorry, My services are down")
return voice_data
time.sleep(1)
# calling these functions
login()
ava("How can I help you?")
while 1: # runs this infinitely
voice_data = get_audio()
nlp(voice_data)
# print what the user said
print(voice_data)