-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
162 lines (124 loc) · 5.64 KB
/
app.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
153
154
155
156
157
158
159
160
161
162
import io
import os
import base64
from uuid import uuid4
from config import MEDIA_DIR, ALLOWED_LANGUAGES
from utilities.file_extensions import allowed_file, get_file_extension
from flask import Flask, send_file, jsonify, request, url_for
from flask_cors import CORS
from werkzeug.utils import secure_filename
from voice_engine import speech_to_text, text_to_speech
from brain_engine import gpt_engine
app = Flask(__name__)
CORS(app=app)
@app.route("/download/<file_name>")
def download(file_name: str):
file_path = os.path.join(MEDIA_DIR, file_name)
if not os.path.exists(file_path):
return jsonify({"data": None,
"message": "There is no file with this name",
"status": "error"}), 404
return_data = io.BytesIO()
with open(file_path, 'rb') as file:
return_data.write(file.read())
return_data.seek(0)
os.remove(file_path)
return send_file(path_or_file=return_data,
download_name=file_name,
mimetype='audio/wav',
as_attachment=True)
@app.route("/supported_language")
def supported_language():
return jsonify({"data": ALLOWED_LANGUAGES,
"message": None,
"status": "success"}), 200
@app.route("/ask/<language>", methods=["POST"])
def ask(language: str):
for file_key, file in request.files.items():
if file.filename == "":
return jsonify({"data": None,
"message": "No selected file is available",
"status": "error"}), 400
if file and allowed_file(file.filename):
secure_file_name = secure_filename(file.filename)
secure_file_extension = get_file_extension(file_name=secure_file_name)
secure_file_path = os.path.join(MEDIA_DIR, f"{str(uuid4())}{secure_file_extension}")
file.save(secure_file_path)
prompt = speech_to_text.convert(file_path=secure_file_path,
language=language)
if prompt is None:
return jsonify({"data": None,
"message": "Google Speech Recognition could not understand audio",
"status": "success"}), 400
os.remove(secure_file_path)
engine = gpt_engine.GptEngine()
answer_text = engine.command(prompt=prompt)
exact_answer_voice_name = text_to_speech.convert(text=answer_text,
language=language)
data = {
"prompt": prompt,
"prompt_language": language,
"answer": {
"text": answer_text,
"file_name": exact_answer_voice_name,
"answer_language": language,
"voice_path": url_for("download", file_name=exact_answer_voice_name, _external=True,
_scheme="http")
}
}
return jsonify({"data": data,
"message": None,
"status": "success"}), 200
else:
return jsonify({"data": None,
"message": "File type not allowed. Only mp3 and wav files are acceptable",
"status": "error"}), 400
return jsonify({"data": None,
"message": "No file part is available",
"status": "error"}), 400
@app.route("/ask/web/<language>", methods=["POST"])
def ask_web(language: str):
data = request.get_json()
file_name = data['file_name']
file_content = data['file_content']
if file_name == "":
return jsonify({"data": None,
"message": "No selected file is available",
"status": "error"}), 400
if file_name and file_content and allowed_file(file_name=file_name):
secure_file_name = secure_filename(file_name)
secure_file_extension = get_file_extension(file_name=secure_file_name)
secure_file_path = os.path.join(MEDIA_DIR, f"{str(uuid4())}{secure_file_extension}")
print(secure_file_path)
with open(secure_file_path, 'wb') as file:
file.write(base64.b64decode(file_content))
prompt = speech_to_text.convert(file_path=secure_file_path,
language=language)
if prompt is None:
return jsonify({"data": None,
"message": "Google Speech Recognition could not understand audio",
"status": "success"}), 400
os.remove(secure_file_path)
engine = gpt_engine.GptEngine()
answer_text = engine.command(prompt=prompt)
exact_answer_voice_name = text_to_speech.convert(text=answer_text,
language=language)
data = {
"prompt": prompt,
"prompt_language": language,
"answer": {
"text": answer_text,
"file_name": exact_answer_voice_name,
"answer_language": language,
"voice_path": url_for("download", file_name=exact_answer_voice_name, _external=True, _scheme="http")
}
}
return jsonify({"data": data,
"message": None,
"status": "success"}), 200
else:
return jsonify({"data": None,
"message": "File type not allowed. Only mp3 and wav files are acceptable",
"status": "error"}), 400
if __name__ == '__main__':
app.run()