-
Notifications
You must be signed in to change notification settings - Fork 3
/
voice_assistant_front_end.py
152 lines (119 loc) · 3.51 KB
/
voice_assistant_front_end.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import base64
import requests
import pyttsx3
import json
import time
import pyaudio
import wave
import validators
user_id = 0
# def recognise():
# print("\033[31m[*]\033[0m You will be asked to speak for few seconds for the recognition of the speaker.")
# time.sleep(3)
# print("\033[31m[*]\033[0m Get Ready!")
def listen():
""" Taking the voice input """
chunk = 1024 # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16 # 16 bits per sample
channels = 2
fs = 16000 # Record at 16000 samples per second
seconds = 3
filename = "predict.wav"
p = pyaudio.PyAudio() # Create an interface to PortAudio
# print("-------------------------------------------------------------------------------------------")
print("\033[31m[*]\033[0m Say a command!")
stream = p.open(format=sample_format,
channels=channels,
rate=fs,
frames_per_buffer=chunk,
input=True)
frames = [] # Initialize array to store frames
# Store data in chunks for 1 seconds
for i in range(0, int(fs / chunk * seconds)):
data = stream.read(chunk)
frames.append(data)
# Stop and close the stream
stream.stop_stream()
stream.close()
# Terminate the PortAudio interface
p.terminate()
print("\033[31m[*]\033[0m Sending it to the server")
# print("-------------------------------------------------------------------------------------------")
# Save the recorded data as a WAV file
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()
wav_file = base64.b64encode(filename.encode('utf-8'))
wav_file = base64.b64encode(open("predict.wav", "rb").read())
# wav_file = str(wav_file, "utf-8")
return wav_file
# def listen():
# mic = sr.Microphone()
# r = sr.Recognizer()
# with mic as source:
# print("listening")
# audio = r.listen(source, phrase_time_limit = 5)
# try:
# command = r.recognize_google(audio).lower()
# print(command)
# except sr.UnknownValueError:
# command = listen()
# if command == "recognise":
# recognise()
# if command = "search":
# from cli.utils import search
# search()
# return command
def validate(response):
try:
text, url = response.split("\n")
bot(text, user_id)
if validators.url(url):
webbrowser.open(url)
else:
bot("Sorry this is not a valid url")
except:
bot(response, user_id)
def bot(response, user_id):
if user_id == 0:
voice_id = "english-north"
engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', 190)
volume = engine.getProperty('volume')
engine.setProperty('volume', 1.0)
sound = engine.getProperty('voices')
engine.setProperty('voice', voice_id)
for i in str(response).splitlines():
engine.say(response)
engine.runAndWait()
else:
print("user is not general", user_id)
return
def get_command():
command = listen()
# command = base64.b64encode(command.encode("utf-8"))
command = str(command, "utf-8")
# command = command.decode("utf-8")
return command
def main():
url = "http://127.0.0.1:8080"
try:
command = get_command()
response = requests.post(url,json = {"user_audio":command, "user_id":user_id})
except:
print("no command found")
print("Sending the post request again")
command = get_command()
response = response.post(url, json = {"user_audio":command, "user_id":user_id})
response = response.text.strip()
print(response)
response = json.loads(response)
response = response['response']
print(response)
validate(response)
while True:
main()