-
Notifications
You must be signed in to change notification settings - Fork 4
/
flask_api.py
executable file
·760 lines (623 loc) · 22.9 KB
/
flask_api.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#!/usr/bin/env python3
"""An API endpoint module.
Contains all the handlers for the API. Also the main code to run Flask.
"""
from sqlalchemy.exc import OperationalError, InvalidRequestError
from flask import Flask, jsonify, request
from werkzeug.exceptions import BadRequest
# traceback added for stacktrace logging
import traceback
from flask_cors import CORS
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import json
import gunicorn_config
from Entity.Calendars import Calendars
from Entity.Clubs import Clubs
from Entity.Courses import Courses
from Entity.Locations import Locations
from Entity.Sections import Sections
from Entity.Professors import Professors
from database_wrapper import (
BadDictionaryKeyError,
BadDictionaryValueError,
NimbusDatabaseError,
NimbusMySQLAlchemy,
)
from modules.formatters import WakeWordFormatter
from modules.validators import (
WakeWordValidator,
WakeWordValidatorError,
PhrasesValidator,
PhrasesValidatorError,
FeedbackValidator,
FeedbackValidatorError,
)
from Entity.AudioSampleMetaData import AudioSampleMetaData
from Entity.QuestionAnswerPair import QuestionAnswerPair
from Entity.QueryFeedback import QueryFeedback
from Entity.QuestionLog import QuestionLog
from Entity.ErrorLog import ErrorLog
from Entity.EntityToken import EntityToken
from nimbus import Nimbus
import json
BAD_REQUEST = 400
SUCCESS = 200
SERVER_ERROR = 500
CONFIG_FILE_PATH = "config.json"
app = Flask(__name__)
CORS(app)
# NOTE:
# 1. Flask "@app.route" decorated functions below commonly use a db or nimbus object
# 2. Because the decorated functions can't take parameters (because they're called by
# the flask web server) the database and nimbus objects must be global
# 3. Instantiating objects at the global level (especially ones that are resource-intensive
# to create like db and nimbus objects) is obviously bad practice
#
# Due to these points, the very un-Pythonic solution chosen is to initialize these objects as
# None at the top level, associate them with actual objects in the `initialize*()` functions,
# and do None checks in the functions below.
db = None
nimbus = None
def init_nimbus_db():
global db
global nimbus
# If not connected to db, initialize db connection and Nimbus client
if db is None:
db = NimbusMySQLAlchemy(config_file=CONFIG_FILE_PATH)
nimbus = Nimbus(db)
# If not connected, reset db and Nimbus client
else:
try:
db.engine.connect()
except OperationalError:
db = NimbusMySQLAlchemy(config_file=CONFIG_FILE_PATH)
nimbus = Nimbus(db)
# returns the question from the request body, if applicable
def get_question() -> str:
if request.is_json is False:
raise BadRequest(description="request must be JSON")
request_body = request.get_json()
question = request_body.get("question", None)
# no reason for a custom exception here
if question is None:
raise BadRequest(description="request body should include the question")
else:
return question
def handle_database_error(error):
global db
# checks if the session has any changes (new objects, changed objects,
# or deleted objects) - these should be rolled back in the case of an exception
if db.session.new or db.session.dirty or db.session.deleted:
print("Rolling back")
db.session.rollback()
if isinstance(error, OperationalError) or db is None:
# we *probably* have a bad session - try and roll it back,
# then create a new database connection.
db.session.close()
db = None
init_nimbus_db()
def log_error(error, question):
error_entry = {"question": question, "stacktrace": traceback.format_exc()}
db.insert_entity(ErrorLog, error_entry)
@app.errorhandler(Exception)
def handle_all_errors(e):
# we should still be able to extract the question from the request, if one
# was asked. We can retry the question once.
handle_database_error(e)
question = None
try:
question = get_question()
except BadRequest as e:
# the question is already None, but we need to catch this exception
pass
log_error(e, question)
return jsonify({"ErrorLog": type(e).__name__}), SUCCESS
@app.route("/", methods=["GET", "POST"])
def hello():
"""
always return SUCCESS (200) code on this route, to serve as a health check.
"""
if request.method == "POST":
request_body = request.get_json()
return jsonify({"you sent": request_body}), SUCCESS
else:
response_json = jsonify({"name": "hello {}".format(str(app))})
return response_json, SUCCESS
def generate_session_token() -> str:
return "SOME_NEW_TOKEN"
@app.route("/ask", methods=["POST"])
def handle_question():
"""
POST (not GET) request because the `question` is submitted
and an `answer` is "created." Also, some side-effects on the
server are:
* storage of the logs of this question-answer-session.
"""
try:
init_nimbus_db()
try:
question = get_question()
except (BadRequest) as e:
return e.description, BAD_REQUEST
try:
entity = db.insert_entity(QuestionLog, {"question": question})
except (Exception) as e:
print("Could not store question upon user ask: ", str(e))
response = {"answer": nimbus.answer_question(question)}
# extracting the question checks if we have json, so we should be good here
request_body = request.get_json()
if "session" in request_body:
response["session"] = request_body["session"]
else:
response["session"] = generate_session_token()
return jsonify(response), SUCCESS
except Exception as e:
log_error(e, question)
response = {"answer": "oops, something went wrong... Try another question"}
return jsonify(response), SERVER_ERROR
@app.route("/new_data/wakeword", methods=["POST"])
def save_a_recording():
"""Given the audio metadata & audio file, resamples it, saves to storage.
"""
if "wav_file" not in request.files:
return (
"Please provide an audio file under the key 'wav_file' in your FormData",
BAD_REQUEST,
)
validator = WakeWordValidator()
formatter = WakeWordFormatter()
data = request.form
issues = validator.validate(data)
if issues:
try:
data = validator.fix(data, issues)
except WakeWordValidatorError as err:
return str(err), BAD_REQUEST
formatted_data = formatter.format(data)
filename = create_filename(formatted_data)
try:
file_id = save_audiofile(filename, request.files["wav_file"])
except Exception as err:
return f"Failed to save audio file because... {err}", BAD_REQUEST
formatted_data["audio_file_id"] = file_id
init_nimbus_db()
try:
db.insert_entity(AudioSampleMetaData, formatted_data)
except BadDictionaryKeyError as e:
return str(e), BAD_REQUEST
except BadDictionaryValueError as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), BAD_REQUEST
except Exception as e:
# TODO: consider security tradeoff of displaying internal server errors
# versus development time (being able to see errors quickly)
# HINT: security always wins
raise e
return f"Successfully stored audiofile as '{filename}'", SUCCESS
@app.route("/new_data/office_hours", methods=["POST"])
def save_office_hours():
"""
Persists list of office hours
"""
init_nimbus_db()
data = json.loads(request.get_json())
for professor in data:
try:
process_office_hours(data[professor], db)
except BadDictionaryKeyError as e:
return str(e), BAD_REQUEST
except BadDictionaryValueError as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), BAD_REQUEST
except Exception as e:
# TODO: consider security tradeoff of displaying internal server errors
# versus development time (being able to see errors quickly)
# HINT: security always wins
raise e
return "SUCCESS"
@app.route("/new_data/phrase", methods=["POST"])
def save_query_phrase():
validator = PhrasesValidator()
data = json.loads(request.get_json())
try:
issues = validator.validate(data)
except:
return (
"Please format the query data: {question: {text: string, variables: list}, answer: {text: string, variables: list}}",
BAD_REQUEST,
)
if issues:
try:
data = validator.fix(data, issues)
except PhrasesValidatorError as err:
print("error", err)
return str(err), BAD_REQUEST
init_nimbus_db()
try:
entity_saved = db.insert_entity(QuestionAnswerPair, data)
except (BadDictionaryKeyError, BadDictionaryValueError) as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), SERVER_ERROR
except Exception as e:
raise e
if entity_saved:
return "Phrase has been saved", SUCCESS
else:
return "An error was encountered while saving to database", SERVER_ERROR
@app.route("/data/get_phrase/<numQueries>", methods=["GET"])
def get_phrase(numQueries):
init_nimbus_db()
try:
# if no phrases are unvalidated, will return an empty list
return {"data": db.get_unvalidated_qa_data(numQueries)}, SUCCESS
except NimbusDatabaseError as e:
return str(e), SERVER_ERROR
except Exception as e:
raise e
@app.route("/new_data/update_phrase", methods=["POST"])
def update_query_phrase():
init_nimbus_db()
data = request.get_json()
try:
updated = db.update_entity(QuestionAnswerPair, data, [])
except (BadDictionaryKeyError, BadDictionaryValueError) as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), SERVER_ERROR
except Exception as e:
raise e
return (
("Phrase updated!", SUCCESS)
if updated
else ("Failed to update phrase", SERVER_ERROR)
)
@app.route("/new_data/delete_phrase", methods=["POST"])
def delete_query_phrase():
init_nimbus_db()
data = request.get_json()
if "id" not in data or type(data["id"]) != int:
return "Please provide 'id' as an integer"
identifier = data["id"]
try:
deleted = db.delete_entity(QuestionAnswerPair, identifier)
except (BadDictionaryKeyError, BadDictionaryValueError) as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), SERVER_ERROR
except Exception as e:
raise e
return (
("Phrase deleted!", SUCCESS)
if deleted
else ("Failed to delete phrase", SERVER_ERROR)
)
@app.route("/entity_structure", methods=["GET"])
def get_entity_structure():
def get_class_info(entity):
keys = list(filter(lambda key: not key[0] == "_", entity.__dict__.keys()))
return {"attributes": keys, "synonyms": entity.synonyms}
entities = {
"COURSE": get_class_info(Courses),
"CLUB": get_class_info(Clubs),
"PROF": get_class_info(Professors),
"LOCATION": get_class_info(Locations),
}
return jsonify(entities)
@app.route("/new_data/feedback", methods=["POST"])
def save_feedback():
validator = FeedbackValidator()
data = json.loads(request.get_json())
try:
issues = validator.validate(data)
except:
return (
"Please format the query data: {question: String, answer: String, type: String, timestamp: int}",
BAD_REQUEST,
)
if issues:
try:
data = validator.fix(data, issues)
except FeedbackValidatorError as err:
print("error:", err)
return str(err), BAD_REQUEST
init_nimbus_db()
try:
entity = db.insert_entity(QueryFeedback, data)
except (BadDictionaryKeyError, BadDictionaryValueError) as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), SERVER_ERROR
except Exception as e:
raise e
if entity:
return "Feedback has been saved", SUCCESS
else:
return "An error was encountered while saving to database", SERVER_ERROR
@app.route("/new_data/courses", methods=["POST"])
def save_courses():
"""
Persists list of courses
"""
data = json.loads(request.get_json())
init_nimbus_db()
for course in data["courses"]:
try:
db.update_entity(Courses, course, ["dept", "course_num"])
except BadDictionaryKeyError as e:
return str(e), BAD_REQUEST
except BadDictionaryValueError as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), BAD_REQUEST
except Exception as e:
# TODO: consider security tradeoff of displaying internal server errors
# versus development time (being able to see errors quickly)
# HINT: security always wins
raise e
return "SUCCESS"
@app.route("/new_data/sections", methods=["POST"])
def save_sections():
"""
Persists list of sections
"""
data = json.loads(request.get_json())
init_nimbus_db()
for section in data["sections"]:
try:
db.update_entity(Sections, section, ["section_name"])
except BadDictionaryKeyError as e:
return str(e), BAD_REQUEST
except BadDictionaryValueError as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), BAD_REQUEST
except Exception as e:
# TODO: consider security tradeoff of displaying internal server errors
# versus development time (being able to see errors quickly)
# HINT: security always wins
raise e
return "SUCCESS"
@app.route("/new_data/clubs", methods=["POST"])
def save_clubs():
"""
Persists list of clubs
"""
data = json.loads(request.get_json())
init_nimbus_db()
for club in data["clubs"]:
try:
db.update_entity(Clubs, club, ["club_name"])
except BadDictionaryKeyError as e:
return str(e), BAD_REQUEST
except BadDictionaryValueError as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), BAD_REQUEST
except Exception as e:
# TODO: consider security tradeoff of displaying internal server errors
# versus development time (being able to see errors quickly)
# HINT: security always wins
raise e
return "SUCCESS"
@app.route("/new_data/locations", methods=["POST"])
def save_locations():
"""
Persists list of locations
"""
data = json.loads(request.get_json())
init_nimbus_db()
for location in data["locations"]:
try:
db.update_entity(Locations, location, ["longitude", "latitude"])
except BadDictionaryKeyError as e:
return str(e), BAD_REQUEST
except BadDictionaryValueError as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), BAD_REQUEST
except Exception as e:
# TODO: consider security tradeoff of displaying internal server errors
# versus development time (being able to see errors quickly)
# HINT: security always wins
raise e
return "SUCCESS"
@app.route("/new_data/professors", methods=["POST"])
def save_professors():
"""
Persists a list of professors
"""
data = json.loads(request.get_json())
init_nimbus_db()
for prof in data["professors"]:
try:
print("PROF:", prof)
db.update_entity(Professors, prof, ["email"])
except BadDictionaryKeyError as e:
return str(e), BAD_REQUEST
except BadDictionaryValueError as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), BAD_REQUEST
except Exception as e:
# TODO: consider security tradeoff of displaying internal server errors
# versus development time (being able to see errors quickly)
# HINT: security always wins
raise e
return "SUCCESS"
@app.route("/new_data/calendars", methods=["POST"])
def save_calendars():
"""
Persists list of calendars
"""
data = json.loads(request.get_json())
init_nimbus_db()
for calendar in data["calendars"]:
try:
db.update_entity(Calendars, calendar, ["date", "raw_events_text"])
except BadDictionaryKeyError as e:
return str(e), BAD_REQUEST
except BadDictionaryValueError as e:
return str(e), BAD_REQUEST
except NimbusDatabaseError as e:
return str(e), BAD_REQUEST
except Exception as e:
# TODO: consider security tradeoff of displaying internal server errors
# versus development time (being able to see errors quickly)
# HINT: security always wins
raise e
return "SUCCESS"
@app.route("/schema/entity_tokens", methods=["GET"])
def get_entity_tokens():
init_nimbus_db()
try:
identifiers = db.session.query(EntityToken).all()
except:
return "Could not fetch at this time", BAD_REQUEST
data = list(map(lambda token: token.get_data(), identifiers))
return jsonify(data)
@app.route("/schema/entity_tokens", methods=["POST"])
def add_entity_token():
init_nimbus_db()
data = request.get_json()
try:
new_token = EntityToken(data)
except Exception as ex:
return ex.args[0], BAD_REQUEST
token_added = db.add_entity(new_token)
if not token_added:
return "Could not add token", BAD_REQUEST
return "Added Token", SUCCESS
def create_filename(form):
"""
Creates a string filename that adheres to the Nimbus foramtting standard.
"""
order = [
"isWakeWord",
"noiseLevel",
"tone",
"location",
"gender",
"lastName",
"firstName",
"timestamp",
"username",
]
values = list(map(lambda key: str(form[key]).lower().replace(" ", "-"), order))
return "_".join(values) + ".wav"
def process_office_hours(current_prof: dict, db: NimbusMySQLAlchemy):
"""
Takes the path to a CSV, reads the data row-by-row,
and stores the data to the database
Ex: def process_office_hours(
current_prof: dict,
db: NimbusMySQLAlchemy
)
"""
# Set the entity type as the OfficeHours entity class
entity_type = db.OfficeHours
# Check if the current entity is already within the database
if (
db.get_property_from_entity(
prop="Name", entity=entity_type, identifier=current_prof["Name"]
)
!= None
):
update_office_hours = True
else:
update_office_hours = False
# String for adding each day of office hours
office_hours = ""
# Split name for first and last name
split_name = current_prof["Name"].split(",")
# Extract each property for the entity
last_name = split_name[0].replace('"', "")
first_name = split_name[1].replace('"', "")
# Check that each extracted property is not empty then add it to
# the office hours string
if current_prof["Monday"] != "":
# Check that the current property does not contain digits which
# implies that it is alternative information about availability
if any(char.isdigit() for char in current_prof["Monday"]) == False:
office_hours = current_prof["Monday"]
# Otherwise it is a time
else:
office_hours += "Monday " + current_prof["Monday"] + ", "
if current_prof["Tuesday"] != "":
office_hours += "Tuesday " + current_prof["Tuesday"] + ", "
if current_prof["Wednesday"] != "":
office_hours += "Wednesday " + current_prof["Wednesday"] + ", "
if current_prof["Thursday"] != "":
office_hours += "Thursday " + current_prof["Thursday"] + ", "
if current_prof["Friday"] != "" and current_prof["Friday"] != "\n":
office_hours += "Friday " + current_prof["Friday"] + ", "
# Generate the data structure for the database entry
sql_data = {
"name": last_name + ", " + first_name,
"last_name": last_name,
"first_name": first_name,
"office": current_prof["Office"],
"phone": current_prof["Phone"],
"email": current_prof["Email"],
"monday": current_prof["Monday"],
"tuesday": current_prof["Tuesday"],
"wednesday": current_prof["Wednesday"],
"thursday": current_prof["Thursday"],
"friday": current_prof["Friday"],
"office_hours": office_hours,
}
# Update the entity properties if the entity already exists
if update_office_hours == True:
db.update_entity(
entity_type=entity_type, data_dict=sql_data, filter_fields=["Email"]
)
# Otherwise, add the entity to the database
else:
db.insert_entity(entity_type=entity_type, data_dict=sql_data)
def resample_audio():
"""
Resample the audio file to adhere to the Nimbus audio sampling standard.
"""
pass
def save_audiofile(filename, content):
"""
Saves audio to the club Google Drive folder.
Parameters
----------
- `filename:str` the name of the file, formatted by `create_filename()`
- `content: file` audio file to store
Returns
-------
The Google Drive file id that can be used to retrieve the file
"""
# Initialize our google drive authentication object using saved credentials,
# or through the command line
gauth = GoogleAuth()
gauth.CommandLineAuth()
# This is our pydrive object
drive = GoogleDrive(gauth)
# parent is our automatically uploaded file folder. The ID should be read in from
# folder_id.txt since we probably shouldn't have that ID floating around on GitHub"""
folder_id = get_folder_id()
file = drive.CreateFile(
{
"parents": [{"kind": "drive#fileLink", "id": folder_id}],
"title": filename,
"mimeType": "audio/wav",
}
)
# Set the content of the file to the POST request's wav_file parameter.
file.content = content
file.Upload() # Upload file.
return file["id"]
def get_folder_id():
with open("folder_id.txt") as folder_id_file:
return folder_id_file.readline()
def convert_to_mfcc():
"""Get this function from https://github.com/calpoly-csai/CSAI_Voice_Assistant"""
pass
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=gunicorn_config.DEBUG_MODE, port=gunicorn_config.PORT)