From 2cc92d67884ad080a2c4772e32e9bc0a1a5e9ed0 Mon Sep 17 00:00:00 2001 From: alvertogit <36294057+alvertogit@users.noreply.github.com> Date: Sun, 25 Feb 2024 15:47:09 +0100 Subject: [PATCH] ruff format --- app/app/__init__.py | 6 ++++-- app/app/api.py | 9 +++++---- app/app/model.py | 9 +++++---- app/config.py | 7 ++++--- app/server.py | 4 ++-- app/tests/conftest.py | 6 ++++-- app/tests/test_app.py | 8 +++++--- 7 files changed, 29 insertions(+), 20 deletions(-) diff --git a/app/app/__init__.py b/app/app/__init__.py index f55a849..005e9d4 100644 --- a/app/app/__init__.py +++ b/app/app/__init__.py @@ -2,8 +2,8 @@ __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 @@ -11,6 +11,7 @@ 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__) @@ -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"]) diff --git a/app/app/api.py b/app/app/api.py index 2f542a4..a7abea7 100644 --- a/app/app/api.py +++ b/app/app/api.py @@ -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 @@ -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(): @@ -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) diff --git a/app/app/model.py b/app/app/model.py index b5476ca..400597c 100644 --- a/app/app/model.py +++ b/app/app/model.py @@ -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 @@ -23,6 +23,7 @@ def init_model(): model.make_predict_function() return model + def preprocess_image(image): """Function that preprocess image. Returns: @@ -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 diff --git a/app/config.py b/app/config.py index 436d6b5..a6c9b0d 100644 --- a/app/config.py +++ b/app/config.py @@ -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") @@ -41,5 +42,5 @@ def init_app(cls, app): "development": DevConfig, "testing": TestConfig, "production": DefaultConfig, - "default": DefaultConfig + "default": DefaultConfig, } diff --git a/app/server.py b/app/server.py index 8ce09ee..da63542 100644 --- a/app/server.py +++ b/app/server.py @@ -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 diff --git a/app/tests/conftest.py b/app/tests/conftest.py index 253fc9f..c4ce0f7 100644 --- a/app/tests/conftest.py +++ b/app/tests/conftest.py @@ -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() diff --git a/app/tests/test_app.py b/app/tests/test_app.py index 869df07..abb4d4f 100644 --- a/app/tests/test_app.py +++ b/app/tests/test_app.py @@ -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" @@ -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