2020-04-13 21:14:06 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-04-14 13:48:11 +02:00
|
|
|
import tempfile
|
2020-04-14 17:03:15 +02:00
|
|
|
import os
|
2020-10-02 03:28:40 +02:00
|
|
|
from flask import request, jsonify, current_app, abort, Response, url_for
|
2020-04-13 21:14:06 +02:00
|
|
|
from flask_classful import FlaskView, route
|
2020-07-28 20:19:52 +02:00
|
|
|
from model import db, Default, AIModel, AIModelType, SVMDetails
|
|
|
|
from minio.error import NoSuchKey
|
2020-10-02 03:28:40 +02:00
|
|
|
from schemas import AIModelSchema, InfoSchema
|
2020-04-14 14:02:35 +02:00
|
|
|
from marshmallow.exceptions import ValidationError
|
2020-10-02 03:28:40 +02:00
|
|
|
from utils import storage, ensure_buckets, multipart_required
|
|
|
|
from pyAudioAnalysis.audioTrainTest import load_model
|
2020-04-13 21:14:06 +02:00
|
|
|
|
|
|
|
|
2020-07-28 20:19:52 +02:00
|
|
|
class SVMView(FlaskView):
|
2020-09-14 02:05:14 +02:00
|
|
|
route_base = 'svm'
|
|
|
|
|
2020-04-14 13:48:11 +02:00
|
|
|
aimodel_schema = AIModelSchema(many=False)
|
2020-04-14 17:03:15 +02:00
|
|
|
info_schema = InfoSchema(many=False)
|
|
|
|
|
2020-10-03 14:42:44 +02:00
|
|
|
MODEL_DIRECTORY = "model/"
|
|
|
|
MEANS_DIRECTORY = "means/"
|
|
|
|
|
2020-10-02 03:28:40 +02:00
|
|
|
@multipart_required
|
2020-04-13 21:14:06 +02:00
|
|
|
def post(self):
|
2020-04-14 17:03:15 +02:00
|
|
|
|
|
|
|
# get important data from the request
|
|
|
|
try:
|
|
|
|
info = self.info_schema.loads(request.form.get('info'))
|
|
|
|
except ValidationError as e:
|
|
|
|
abort(400, str(e))
|
|
|
|
|
|
|
|
# check for conflict
|
|
|
|
m = AIModel.query.filter_by(id=info['id']).first()
|
|
|
|
if m:
|
|
|
|
abort(409)
|
|
|
|
|
|
|
|
# get and validate file
|
|
|
|
model_file = request.files['modelFile']
|
|
|
|
|
|
|
|
if model_file.content_length <= 0:
|
|
|
|
abort(411, f"Content length for modelFile is not a positive integer or missing.")
|
|
|
|
|
|
|
|
means_file = request.files['meansFile']
|
|
|
|
|
|
|
|
if means_file.content_length <= 0:
|
|
|
|
abort(411, f"Content length for meansFile is not a positive integer or missing.")
|
|
|
|
|
|
|
|
# create bucket if necessary
|
2020-07-28 20:19:52 +02:00
|
|
|
ensure_buckets()
|
2020-04-14 17:03:15 +02:00
|
|
|
|
|
|
|
# Temporarily save the file, because pyAudioAnalysis can only read files
|
2020-10-01 19:20:06 +02:00
|
|
|
temp_model_handle, temp_model_filename = tempfile.mkstemp()
|
2020-04-14 17:03:15 +02:00
|
|
|
temp_means_filename = temp_model_filename + "MEANS"
|
|
|
|
|
2020-10-01 19:20:06 +02:00
|
|
|
os.close(temp_model_handle) # BRUUUUH
|
|
|
|
|
2020-04-14 17:03:15 +02:00
|
|
|
model_file.save(temp_model_filename)
|
|
|
|
means_file.save(temp_means_filename)
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
2020-10-01 21:30:41 +02:00
|
|
|
_, _, _, classes, mid_window, mid_step, short_window, short_step, compute_beat \
|
2020-07-28 20:19:52 +02:00
|
|
|
= load_model(temp_model_filename)
|
2020-04-14 17:03:15 +02:00
|
|
|
|
2020-10-01 21:30:41 +02:00
|
|
|
if info['target_class_name'] not in classes:
|
|
|
|
abort(400, f"This model does not have a class named {info['target_class_name']}")
|
|
|
|
|
2020-04-14 17:03:15 +02:00
|
|
|
# Because of pyAudiomeme the files already saved, so we just use the file uploader functions
|
|
|
|
storage.connection.fput_object(
|
2020-07-28 20:19:52 +02:00
|
|
|
current_app.config['MINIO_SVM_BUCKET_NAME'],
|
2020-10-03 14:42:44 +02:00
|
|
|
self.MODEL_DIRECTORY + str(info['id']),
|
2020-04-14 17:03:15 +02:00
|
|
|
temp_model_filename
|
|
|
|
)
|
|
|
|
|
|
|
|
storage.connection.fput_object(
|
2020-07-28 20:19:52 +02:00
|
|
|
current_app.config['MINIO_SVM_BUCKET_NAME'],
|
2020-10-03 14:42:44 +02:00
|
|
|
self.MEANS_DIRECTORY + str(info['id']),
|
2020-04-14 17:03:15 +02:00
|
|
|
temp_means_filename
|
|
|
|
)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
os.remove(temp_model_filename)
|
|
|
|
os.remove(temp_means_filename)
|
|
|
|
|
2020-10-02 03:28:40 +02:00
|
|
|
m = AIModel(id=info['id'], type=AIModelType.svm, target_class_name=info['target_class_name'])
|
2020-07-28 20:19:52 +02:00
|
|
|
|
|
|
|
d = SVMDetails(
|
|
|
|
aimodel=m,
|
|
|
|
mid_window=mid_window,
|
|
|
|
mid_step=mid_step,
|
|
|
|
short_window=short_window,
|
|
|
|
short_step=short_step,
|
|
|
|
compute_beat=compute_beat
|
|
|
|
)
|
2020-04-14 17:03:15 +02:00
|
|
|
|
|
|
|
db.session.add(m)
|
2020-07-28 20:19:52 +02:00
|
|
|
db.session.add(d)
|
2020-04-14 17:03:15 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return jsonify(self.aimodel_schema.dump(m)), 200
|
2020-04-13 21:14:06 +02:00
|
|
|
|
2020-10-02 03:44:23 +02:00
|
|
|
def delete(self, id_: str):
|
2020-04-14 13:48:11 +02:00
|
|
|
|
2020-10-02 03:44:23 +02:00
|
|
|
if id_ == "$default":
|
2020-10-02 03:28:40 +02:00
|
|
|
default = Default.query.filter_by(type=AIModelType.svm).first_or_404()
|
2020-04-14 17:26:33 +02:00
|
|
|
m = default.aimodel
|
|
|
|
else:
|
2020-10-02 03:44:23 +02:00
|
|
|
m = AIModel.query.filter_by(type=AIModelType.svm, id=id_).first_or_404()
|
2020-04-14 02:01:44 +02:00
|
|
|
|
2020-10-03 14:42:44 +02:00
|
|
|
storage.connection.remove_object(current_app.config['MINIO_SVM_BUCKET_NAME'], self.MEANS_DIRECTORY + str(m.id))
|
|
|
|
storage.connection.remove_object(current_app.config['MINIO_SVM_BUCKET_NAME'], self.MODEL_DIRECTORY + str(m.id))
|
2020-04-14 02:01:44 +02:00
|
|
|
|
2020-04-14 13:48:11 +02:00
|
|
|
db.session.delete(m)
|
2020-04-14 02:01:44 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
2020-04-14 13:48:11 +02:00
|
|
|
return '', 204
|
|
|
|
|
2020-10-02 03:28:40 +02:00
|
|
|
# builtin file proxy
|
2020-10-02 03:44:23 +02:00
|
|
|
@route('<id_>/file')
|
|
|
|
def get_file(self, id_: str):
|
2020-04-14 14:02:35 +02:00
|
|
|
|
2020-10-02 03:44:23 +02:00
|
|
|
if id_ == "$default":
|
2020-10-02 03:28:40 +02:00
|
|
|
default = Default.query.filter_by(type=AIModelType.svm).first_or_404()
|
|
|
|
m = default.aimodel
|
|
|
|
else:
|
2020-10-02 03:44:23 +02:00
|
|
|
m = AIModel.query.filter_by(type=AIModelType.svm, id=id_).first_or_404()
|
2020-04-14 14:02:35 +02:00
|
|
|
|
2020-10-02 03:28:40 +02:00
|
|
|
if "means" in request.args:
|
2020-10-03 14:42:44 +02:00
|
|
|
path = self.MEANS_DIRECTORY + str(m.id)
|
2020-10-02 03:28:40 +02:00
|
|
|
else:
|
2020-10-03 14:42:44 +02:00
|
|
|
path = self.MODEL_DIRECTORY + str(m.id)
|
2020-04-14 14:02:35 +02:00
|
|
|
|
2020-10-02 03:28:40 +02:00
|
|
|
try:
|
|
|
|
data = storage.connection.get_object(current_app.config['MINIO_SVM_BUCKET_NAME'], path)
|
|
|
|
except NoSuchKey:
|
|
|
|
abort(500, "The ID is stored in the database but not int the Object Store")
|
2020-04-14 14:02:35 +02:00
|
|
|
|
2020-10-02 03:28:40 +02:00
|
|
|
return Response(data.stream(), mimetype=data.headers['Content-type'])
|