From 536fc2a16407c79c4275fb1ee3962dcb95d4b3fa Mon Sep 17 00:00:00 2001 From: marcsello Date: Tue, 14 Apr 2020 17:26:33 +0200 Subject: [PATCH] Fixed stuff --- model_service/model/default.py | 5 ++--- model_service/schemas/aimodel_schema.py | 6 ++++++ model_service/schemas/default_schema.py | 5 ++--- model_service/views/model_view.py | 26 ++++++++++++------------- requirements.txt | 3 ++- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/model_service/model/default.py b/model_service/model/default.py index 12887a4..88e208a 100644 --- a/model_service/model/default.py +++ b/model_service/model/default.py @@ -4,6 +4,5 @@ from sqlalchemy.dialects.postgresql import UUID class Default(db.Model): - id = db.Column(db.Integer, primary_key=True, auto_increment=True) -# aimodel_id = db.Column(UUID(as_uuid=True), db.ForeignKey("AIModel.id"), nullable=False) - # aimodel = db.relationship("AIModel", backref=db.backref("default", lazy=True)) + aimodel_id = db.Column(UUID(as_uuid=True), db.ForeignKey("ai_model.id"), nullable=False, primary_key=True) + aimodel = db.relationship("AIModel", backref=db.backref("default", lazy=True, cascade="save-update, merge, delete, delete-orphan")) diff --git a/model_service/schemas/aimodel_schema.py b/model_service/schemas/aimodel_schema.py index cfde47b..a6c62e7 100644 --- a/model_service/schemas/aimodel_schema.py +++ b/model_service/schemas/aimodel_schema.py @@ -5,5 +5,11 @@ from model import AIModel class AIModelSchema(ModelSchema): + + default = fields.Method("boolize_default", dump_only=True) + + def boolize_default(self, ai_model) -> bool: + return bool(ai_model.default) + class Meta: model = AIModel diff --git a/model_service/schemas/default_schema.py b/model_service/schemas/default_schema.py index 09277d1..53a4e6b 100644 --- a/model_service/schemas/default_schema.py +++ b/model_service/schemas/default_schema.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 -from marshmallow import fields -from marshmallow_sqlalchemy import ModelSchema +from marshmallow import fields, Schema -class DefaultSchema(ModelSchema): +class DefaultSchema(Schema): id = fields.UUID() diff --git a/model_service/views/model_view.py b/model_service/views/model_view.py index 439bdb9..4cfbff9 100644 --- a/model_service/views/model_view.py +++ b/model_service/views/model_view.py @@ -104,29 +104,29 @@ class ModelView(FlaskView): else: m = AIModel.query.fiter_by(id=_id).first_or_404() - try: - data = storage.connection.get_object(current_app.config['MINIO_BUCKET_NAME'], str(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']) + return "asd" @route('<_id>/details') def get_details(self, _id: str): 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 + m = default.aimodel else: - m = AIModel.query.fiter_by(id=_id).first_or_404() + m = AIModel.query.filter_by(id=_id).first_or_404() return jsonify(self.aimodel_schema.dump(m)) def delete(self, _id: str): - m = AIModel.query.fiter_by(id=_id).first_or_404() + if _id == '$default': + default = Default.query.first_or_404() + m = default.aimodel + else: + m = AIModel.query.fiter_by(id=_id).first_or_404() - storage.connection.remove_object(current_app.config['MINIO_BUCKET_NAME'], m.id) + storage.connection.remove_object(current_app.config['MINIO_MODEL_BUCKET_NAME'], str(m.id)) + storage.connection.remove_object(current_app.config['MINIO_MEANS_BUCKET_NAME'], str(m.id)) db.session.delete(m) db.session.commit() @@ -138,14 +138,14 @@ class ModelView(FlaskView): def put_default(self): try: - req = self.default_schema.load(request.json()) + req = self.default_schema.load(request.json) except ValidationError as e: abort(404, str(e)) - m = AIModel.query.fiter_by(id=req['id']).first_or_404() + m = AIModel.query.filter_by(id=req['id']).first_or_404() Default.query.delete() - new_default = Default(m) + new_default = Default(aimodel=m) db.session.add(new_default) db.session.commit() diff --git a/requirements.txt b/requirements.txt index 680524a..572b546 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,5 @@ numpy eyed3 pydub scipy -matplotlib \ No newline at end of file +matplotlib +sklearn \ No newline at end of file