Skip to content

Commit

Permalink
ruff modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
alvertogit committed Feb 25, 2024
1 parent 2cc92d6 commit 8a52341
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 96 deletions.
18 changes: 18 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
line-length = 100
target-version = "py310"
lint.select = [
# pycodestyle
"E",
"W",
# Pyflakes
"F",
# pyupgrade
"UP",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
]
extend-include = ["*.ipynb"]
94 changes: 49 additions & 45 deletions Deep Learning MNIST prediction model with Keras.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
__copyright__ = "Copyright 2018-2024"


from config import config
from flask import Flask, render_template

from config import config
from .model import init_model


Expand Down
40 changes: 20 additions & 20 deletions app/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@


import io

from flask import Blueprint, jsonify, request
from skimage.io import imread
from .model import current_app, np, preprocess_image

from .model import current_app, np, preprocess_image

api = Blueprint("api", __name__)

Expand All @@ -21,31 +22,30 @@ def predict():
result = {"success": False}

# ensure an image was properly uploaded to our endpoint
if request.method == "POST":
if request.files.get("file"):
# read image as grayscale
image_req = request.files["file"].read()
request.files["file"].close()
image = imread(io.BytesIO(image_req), as_gray=True)
if request.method == "POST" and request.files.get("file"):
# read image as grayscale
image_req = request.files["file"].read()
request.files["file"].close()
image = imread(io.BytesIO(image_req), as_gray=True)

# preprocess the image for model
preprocessed_image = preprocess_image(image)
# preprocess the image for model
preprocessed_image = preprocess_image(image)

# classify the input image generating a list of predictions
model = current_app.config["model"]
preds = model.predict(preprocessed_image)
# classify the input image generating a list of predictions
model = current_app.config["model"]
preds = model.predict(preprocessed_image)

# add generated predictions to result
result["predictions"] = []
# add generated predictions to result
result["predictions"] = []

for i in range(0, 10):
pred = {"label": str(i), "probability": str(preds[0][i])}
result["predictions"].append(pred)
for i in range(0, 10):
pred = {"label": str(i), "probability": str(preds[0][i])}
result["predictions"].append(pred)

result["most_probable_label"] = str(np.argmax(preds[0]))
result["most_probable_label"] = str(np.argmax(preds[0]))

# indicate that the request was a success
result["success"] = True
# indicate that the request was a success
result["success"] = True

# return result dictionary as JSON response to client
return jsonify(result)
5 changes: 2 additions & 3 deletions app/app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
__copyright__ = "Copyright 2018-2024"


from tensorflow.keras.models import load_model
from skimage import transform, util
import numpy as np

from flask import current_app
from skimage import transform, util
from tensorflow.keras.models import load_model


def init_model():
Expand Down
3 changes: 2 additions & 1 deletion app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
__copyright__ = "Copyright 2018-2024"


from app import create_app
import pytest

from app import create_app


@pytest.fixture
def app():
Expand Down
52 changes: 26 additions & 26 deletions app/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ def test_api(client):
IMAGE_PATH = "../app/static/4.jpg"

# create payload with image for request
image = open(IMAGE_PATH, "rb")
payload = {"file": image}
response = client.post(SERVER_URL, data=payload)

# check response
assert response.status_code == 200

# JSON format
try:
json_response = json.loads(response.data.decode("utf8"))
except ValueError as e:
print(e)
assert False

# successful
if json_response["success"]:
# most probable label
print(json_response["most_probable_label"])
# predictions
for dic in json_response["predictions"]:
print(f"label {dic['label']} probability: {dic['probability']}")

assert json_response["most_probable_label"] == "4"
# failed
else:
assert False
with open(IMAGE_PATH, "rb") as image:
payload = {"file": image}
response = client.post(SERVER_URL, data=payload)

# check response
assert response.status_code == 200

# JSON format
try:
json_response = json.loads(response.data.decode("utf8"))
except ValueError as e:
print(e)
exit(1)

# successful
if json_response["success"]:
# most probable label
print(json_response["most_probable_label"])
# predictions
for dic in json_response["predictions"]:
print(f"label {dic['label']} probability: {dic['probability']}")

assert json_response["most_probable_label"] == "4"
# failed
else:
raise AssertionError()

0 comments on commit 8a52341

Please sign in to comment.