From f97228b61c876f1af28ca8325e79f402e7e00707 Mon Sep 17 00:00:00 2001 From: marcsello Date: Tue, 14 Apr 2020 13:48:11 +0200 Subject: [PATCH] Implemeted getters --- model_service/model/__init__.py | 2 +- .../model/{details.py => aimodel.py} | 10 ++-- model_service/model/default.py | 7 ++- model_service/schemas/__init__.py | 2 +- .../{details_schema.py => aimodel_schema.py} | 6 +- model_service/utils/__init__.py | 1 + model_service/utils/storage.py | 3 + model_service/views/model_view.py | 56 +++++++++++++------ requirements.txt | 9 ++- 9 files changed, 65 insertions(+), 31 deletions(-) rename model_service/model/{details.py => aimodel.py} (59%) rename model_service/schemas/{details_schema.py => aimodel_schema.py} (58%) create mode 100644 model_service/utils/storage.py diff --git a/model_service/model/__init__.py b/model_service/model/__init__.py index ada7b03..a724685 100644 --- a/model_service/model/__init__.py +++ b/model_service/model/__init__.py @@ -1,4 +1,4 @@ #!/usr/bin/env python3 from .db import db from .default import Default -from .details import Details \ No newline at end of file +from .aimodel import AIModel \ No newline at end of file diff --git a/model_service/model/details.py b/model_service/model/aimodel.py similarity index 59% rename from model_service/model/details.py rename to model_service/model/aimodel.py index 00a0b63..054e5a9 100644 --- a/model_service/model/details.py +++ b/model_service/model/aimodel.py @@ -1,16 +1,18 @@ #!/usr/bin/env python3 from .db import db from sqlalchemy.sql import func +from sqlalchemy.dialects.postgresql import UUID +import uuid -class Details(db.Model): - id = db.Column(db.Integer, primary_key=True, auto_increment=True) - name = db.Column(db.String(30), key=True) +class AIModel(db.Model): + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False) timestamp = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now()) + # details mid_window = db.Column(db.Float) mid_step = db.Column(db.Float) short_window = db.Column(db.Float) short_step = db.Column(db.Float) compute_beat = db.Column(db.Float) - type = db.Column(db.String(15)) \ No newline at end of file + type = db.Column(db.String(15)) diff --git a/model_service/model/default.py b/model_service/model/default.py index 61faea1..7d469eb 100644 --- a/model_service/model/default.py +++ b/model_service/model/default.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 from .db import db +from sqlalchemy.dialects.postgresql import UUID -class Default(db.Model): - default_id = db.Column(db.Integer, db.ForeignKey("details.id"), nullable=False) - default = db.relationship("Details") \ No newline at end of file +class Default(db.AIModel): + default_id = db.Column(UUID(as_uuid=True), db.ForeignKey("aimodel.id"), nullable=False) + default = db.relationship("AIModel") diff --git a/model_service/schemas/__init__.py b/model_service/schemas/__init__.py index 6cf4bf9..1b46bdf 100644 --- a/model_service/schemas/__init__.py +++ b/model_service/schemas/__init__.py @@ -1,3 +1,3 @@ #!/usr/bin/env python3 from .default_schema import DefaultSchema -from .details_schema import DetailsSchema +from .aimodel_schema import AIModelSchema diff --git a/model_service/schemas/details_schema.py b/model_service/schemas/aimodel_schema.py similarity index 58% rename from model_service/schemas/details_schema.py rename to model_service/schemas/aimodel_schema.py index cfc9f68..cfde47b 100644 --- a/model_service/schemas/details_schema.py +++ b/model_service/schemas/aimodel_schema.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 from marshmallow import fields from marshmallow_sqlalchemy import ModelSchema -from model import Details +from model import AIModel -class DetailsSchema(ModelSchema): +class AIModelSchema(ModelSchema): class Meta: - model = Details + model = AIModel diff --git a/model_service/utils/__init__.py b/model_service/utils/__init__.py index 6c6531e..dbe1d9e 100644 --- a/model_service/utils/__init__.py +++ b/model_service/utils/__init__.py @@ -1,3 +1,4 @@ #!/usr/bin/env python3 from .require_decorators import json_required from .error_handlers import register_all_error_handlers +from .storage import storage \ No newline at end of file diff --git a/model_service/utils/storage.py b/model_service/utils/storage.py new file mode 100644 index 0000000..0259793 --- /dev/null +++ b/model_service/utils/storage.py @@ -0,0 +1,3 @@ +from flask_minio import Minio + +storage = Minio() \ No newline at end of file diff --git a/model_service/views/model_view.py b/model_service/views/model_view.py index 827f19b..a8ba4c7 100644 --- a/model_service/views/model_view.py +++ b/model_service/views/model_view.py @@ -1,14 +1,15 @@ #!/usr/bin/env python3 -from flask import request, jsonify +import tempfile +from flask import request, jsonify, current_app, abort, Response from flask_classful import FlaskView, route -from model import db, Default, Details -from schema import DetailsSchema, DefaultSchema -from utils import json_required +from model import db, Default, AIModel +from minio.error import BucketAlreadyExists, BucketAlreadyOwnedByYou, ResponseError, NoSuchKey +from schemas import AIModelSchema, DefaultSchema +from utils import json_required, storage class ModelView(FlaskView): - - details_schema = DetailsSchema(many=False) + aimodel_schema = AIModelSchema(many=False) default_schema = DefaultSchema(many=False) @json_required @@ -16,24 +17,43 @@ class ModelView(FlaskView): return jsonify({}) def get(self, _id: str): - return jsonify({"id": _id}) + + if _id == "$default": + default = Default.query.first_or_404() # TODO: Kitalálni, hogy inkább a latestestest-el térjen-e vissza + m = default.default + else: + m = AIModel.query.fiter_by(id=_id).first_or_404() + + try: + data = storage.connection.get_object(current_app.config['MINIO_BUCKET_NAME'], m.id) + except NoSuchKey: + abort(500, "The ID is stored in the database but not int the Object Store") + + return Response(data.stream(), mimetype=data.headers['Content-type']) @route('<_id>/details') def get_details(self, _id: str): - return jsonify({"id": _id}) + + if _id == "$default": + default = Default.query.first_or_404() # TODO: Kitalálni, hogy inkább a latestestest-el térjen-e vissza + m = default.default + else: + m = AIModel.query.fiter_by(id=_id).first_or_404() + + return jsonify(self.aimodel_schema.dump(m)) def delete(self, _id: str): - return jsonify({"id": _id}) + + m = AIModel.query.fiter_by(id=_id).first_or_404() + + storage.connection.remove_object(current_app.config['MINIO_BUCKET_NAME'], m.id) + + db.session.delete(m) + db.session.commit() + + return '', 204 @json_required @route('$default', methods=['PUT']) def put_default(self): - - Default.query.delete() - #new_default = Default() - - - db.session.add(new_default) - db.session.commit() - - return jsonify(self.default_schema.dump(new_default)) + return jsonify({}) diff --git a/requirements.txt b/requirements.txt index 648fcc1..680524a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,11 @@ SQLAlchemy marshmallow-sqlalchemy psycopg2-binary flask_minio -sentry-sdk \ No newline at end of file +sentry-sdk + +pyAudioanalysis +numpy +eyed3 +pydub +scipy +matplotlib \ No newline at end of file