Skip to content

Commit

Permalink
ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
alvertogit committed Feb 25, 2024
1 parent c7ceaba commit 2cc92d6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
6 changes: 4 additions & 2 deletions app/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
__init__.py: Flask server with Deep Learning model.
"""

__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2020"
__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"


from flask import Flask, render_template

from config import config
from .model import init_model


def create_app(config_name="default"):
"""Create and configure an instance of the Flask application."""
app = Flask(__name__)
Expand All @@ -21,6 +22,7 @@ def create_app(config_name="default"):
app.config["model"] = init_model()

from .api import api

app.register_blueprint(api, url_prefix="/api")

@app.route("/dlflask", methods=["GET"])
Expand Down
9 changes: 5 additions & 4 deletions app/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
api.py: api views used by Flask server.
"""

__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"
__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"


import io
Expand All @@ -12,7 +12,8 @@
from .model import current_app, np, preprocess_image


api = Blueprint('api', __name__)
api = Blueprint("api", __name__)


@api.route("/predictlabel", methods=["POST"])
def predict():
Expand All @@ -37,7 +38,7 @@ def predict():
# add generated predictions to result
result["predictions"] = []

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

Expand Down
9 changes: 5 additions & 4 deletions app/app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
model.py: Functions related to Deep Learning model based on Keras.
"""

__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"
__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"


from tensorflow.keras.models import load_model
Expand All @@ -23,6 +23,7 @@ def init_model():
model.make_predict_function()
return model


def preprocess_image(image):
"""Function that preprocess image.
Returns:
Expand All @@ -32,8 +33,8 @@ def preprocess_image(image):
# invert grayscale image
image = util.invert(image)
# resize and reshape image for model
image = transform.resize(image, (28,28), anti_aliasing=True, mode="constant")
image = transform.resize(image, (28, 28), anti_aliasing=True, mode="constant")
image = np.array(image)
image = image.reshape((1,28*28))
image = image.reshape((1, 28 * 28))

return image
7 changes: 4 additions & 3 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
config.py: Configurations used by Flask server.
"""

__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"
__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"


import os


class DefaultConfig:
if os.environ.get("SECRET_KEY"):
SECRET_KEY = os.environ.get("SECRET_KEY")
Expand Down Expand Up @@ -41,5 +42,5 @@ def init_app(cls, app):
"development": DevConfig,
"testing": TestConfig,
"production": DefaultConfig,
"default": DefaultConfig
"default": DefaultConfig,
}
4 changes: 2 additions & 2 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
server.py: Run Flask server with Deep Learning model.
"""

__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"
__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"


from app import create_app
Expand Down
6 changes: 4 additions & 2 deletions app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
conftest.py: It contents fixture functions used in tests.
"""

__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"
__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"


from app import create_app
import pytest


@pytest.fixture
def app():
app = create_app("testing")
return app


@pytest.fixture
def client(app):
return app.test_client()
8 changes: 5 additions & 3 deletions app/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
test_app.py: It contents flask app tests.
"""

__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"
__author__ = "alvertogit"
__copyright__ = "Copyright 2018-2024"


import json


def test_index(client):
response = client.get("/")
# check response
assert response.status_code == 200
assert response.data == b"Deep Learning on Flask"


def test_api(client):
# server REST API endpoint url and example image path
SERVER_URL = "http://127.0.0.1:5000/api/predictlabel"
Expand All @@ -29,7 +31,7 @@ def test_api(client):

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

0 comments on commit 2cc92d6

Please sign in to comment.