Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore labels endpoint and embedded app #39

Merged
merged 5 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ ENV/
.mypy_cache/
.idea/
.pytest_cache/

# local build files
static/
assets/data/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM codait/max-base:v1.1.0
FROM codait/max-base:v1.1.1

ARG model_bucket=http://max-assets.s3-api.us-geo.objectstorage.softlayer.net/object-detector/1.0
ARG model_file=model.tar.gz
Expand Down
2 changes: 1 addition & 1 deletion api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .metadata import ModelMetadataAPI # noqa
from .predict import ModelPredictAPI # noqa
from .predict import ModelLabelsAPI, ModelPredictAPI # noqa
36 changes: 31 additions & 5 deletions api/predict.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
from maxfw.core import MAX_API, PredictAPI
from maxfw.core import MAX_API, PredictAPI, CustomMAXAPI
from flask_restplus import fields
from werkzeug.datastructures import FileStorage
from core.model import ModelWrapper

model_label = MAX_API.model('ModelLabel', {
'id': fields.String(required=True, description='Class label identifier'),
'name': fields.String(required=True, description='Class label'),
})

labels_response = MAX_API.model('LabelsResponse', {
'count': fields.Integer(required=True,
description='Number of class labels returned'),
'labels': fields.List(fields.Nested(model_label),
description='Class labels that can be predicted by '
'the model')
})

model_wrapper = ModelWrapper()


class ModelLabelsAPI(CustomMAXAPI):

@MAX_API.doc('labels')
@MAX_API.marshal_with(labels_response)
def get(self):
"""Return the list of labels that can be predicted by the model"""
return {
'labels': model_wrapper.categories,
'count': len(model_wrapper.categories)
}


input_parser = MAX_API.parser()
input_parser.add_argument('image', type=FileStorage, location='files', required=True,
help='An image file (encoded as PNG or JPG/JPEG)')
Expand All @@ -25,8 +53,6 @@

class ModelPredictAPI(PredictAPI):

model_wrapper = ModelWrapper()

@MAX_API.doc('predict')
@MAX_API.expect(input_parser)
@MAX_API.marshal_with(predict_response)
Expand All @@ -37,8 +63,8 @@ def post(self):
args = input_parser.parse_args()
threshold = args['threshold']
image_data = args['image'].read()
image = self.model_wrapper._read_image(image_data)
label_preds = self.model_wrapper._predict(image, threshold)
image = model_wrapper._read_image(image_data)
label_preds = model_wrapper._predict(image, threshold)

result['predictions'] = label_preds
result['status'] = 'ok'
Expand Down
4 changes: 3 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from maxfw.core import MAXApp
from api import ModelMetadataAPI, ModelPredictAPI
from api import ModelMetadataAPI, ModelLabelsAPI, ModelPredictAPI
from config import API_TITLE, API_DESC, API_VERSION

max_app = MAXApp(API_TITLE, API_DESC, API_VERSION)
max_app.add_api(ModelMetadataAPI, '/metadata')
max_app.add_api(ModelLabelsAPI, '/labels')
max_app.add_api(ModelPredictAPI, '/predict')
max_app.mount_static('/app/')
max_app.run()