-
Notifications
You must be signed in to change notification settings - Fork 3
/
voice_assistant_cloud.py
87 lines (63 loc) · 1.9 KB
/
voice_assistant_cloud.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
import base64
from flask import Flask, jsonify, request, redirect
from flask_cors import CORS
from cli import voice_assistant as vs
import speech_recognition as sr
from pydub import AudioSegment
from pydub.playback import play
app = Flask("Personalised Voice Assistant")
CORS(app)
@app.route('/', methods=['GET'])
def home():
return "Personalised Voice Assistant\n"
@app.route('/about', methods=['GET'])
def about():
# return redirect('https://github.com/SkyDocs/personalised-voice-assistant')
return "This is Personalised Voice Assistant. For more visit: https://github.com/SkyDocs/personalised-voice-assistant\n"
@app.route('/', methods=['POST'])
def wav_text():
data_ret = request.get_json()
wav_file = data_ret["user_audio"]
user_id = data_ret["user_id"]
wav = open("temp.wav", "wb")
wav_file = base64.b64decode(wav_file)
wav.write(wav_file)
wav.close()
song = AudioSegment.from_wav("temp.wav")
play(song)
r = sr.Recognizer()
wav = sr.AudioFile('temp.wav')
with wav as source:
audio = r.record(source)
try:
command = r.recognize_google(audio)
print(command)
from cli import main
return main.general(command, user_id)
except Exception as e:
print("no command found, returning to the front end")
return
return "something screwed up"
@app.route('/recongise', methods=['POST'])
def recognise():
data_ret = request.get_json()
wav_file = data_ret["user_audio"]
# get the wav file, and pass it to the pedict function
wav_file = base64.b64decode(wav_file)
from cli import predict
user_id = predict.main(wav_file)
user_id = {
"user_id": user_id
}
user_id = jsonify(user_id)
return user_id
# @app.route('/tmp', methods=['POST'])
# def encrypt():
# data_ret = request.get_json()
# wav_file = data_ret["user_audio"]
# wav_file = base64.b64decode(wav_file)
# from cli import encrypt
# main(wav_file)
# return
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080)