From 8b153bc1717fbea5b30bdb287dbccbdb07e1518c Mon Sep 17 00:00:00 2001 From: alvertogit <36294057+alvertogit@users.noreply.github.com> Date: Wed, 1 Jan 2025 09:52:06 +0100 Subject: [PATCH] type hints and updated docs --- README.md | 4 ++-- app/app/__init__.py | 4 ++-- app/app/api.py | 6 +++--- app/app/model.py | 7 ++++--- app/app/templates/dlflask.html | 2 +- app/config.py | 10 ++++++---- app/server.py | 2 +- app/tests/conftest.py | 8 +++++--- app/tests/test_app.py | 8 +++++--- 9 files changed, 29 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 2bf22bd..30498bb 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ It is possible to execute tests of [Flask] microservice created with [pytest] fr ~/app# make test ... ============================= test session starts ============================== -platform linux -- Python 3.12.7, pytest-8.3.3, pluggy-1.5.0 +platform linux -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0 rootdir: /app/tests collected 2 items @@ -227,7 +227,7 @@ A POST example using [curl] from outside [Docker] container is shown below: ## CREDITS author: alvertogit -copyright: 2018-2024 +copyright: 2018-2025 [Python]: https://www.python.org/ [Flask]: https://flask.palletsprojects.com/en/1.1.x/ diff --git a/app/app/__init__.py b/app/app/__init__.py index 343e427..1158764 100644 --- a/app/app/__init__.py +++ b/app/app/__init__.py @@ -3,7 +3,7 @@ """ __author__ = "alvertogit" -__copyright__ = "Copyright 2018-2024" +__copyright__ = "Copyright 2018-2025" from config import config @@ -13,7 +13,7 @@ from .model import init_model -def create_app(config_name="default"): +def create_app(config_name: str = "default") -> Flask: """Create and configure an instance of the Flask application.""" app = Flask(__name__) app.config.from_object(config[config_name]) diff --git a/app/app/api.py b/app/app/api.py index b48bbdd..f6fda01 100644 --- a/app/app/api.py +++ b/app/app/api.py @@ -3,12 +3,12 @@ """ __author__ = "alvertogit" -__copyright__ = "Copyright 2018-2024" +__copyright__ = "Copyright 2018-2025" import io -from flask import Blueprint, jsonify, request +from flask import Blueprint, Response, jsonify, request from skimage.io import imread from .model import current_app, np, preprocess_image @@ -17,7 +17,7 @@ @api.route("/predictlabel", methods=["POST"]) -def predict(): +def predict() -> Response: """ Predict the label of an uploaded image with the Deep Learning model. diff --git a/app/app/model.py b/app/app/model.py index aa4c950..f5ed555 100644 --- a/app/app/model.py +++ b/app/app/model.py @@ -3,16 +3,17 @@ """ __author__ = "alvertogit" -__copyright__ = "Copyright 2018-2024" +__copyright__ = "Copyright 2018-2025" import numpy as np from flask import current_app from skimage import transform, util +from tensorflow.keras import Model from tensorflow.keras.models import load_model -def init_model(): +def init_model() -> Model: """ Load the pre-trained Deep Learning model. @@ -25,7 +26,7 @@ def init_model(): return model -def preprocess_image(image): +def preprocess_image(image: np.ndarray) -> np.ndarray: """ Preprocess an image for the Deep Learning model. diff --git a/app/app/templates/dlflask.html b/app/app/templates/dlflask.html index 90fcf84..d03171f 100644 --- a/app/app/templates/dlflask.html +++ b/app/app/templates/dlflask.html @@ -72,7 +72,7 @@

POST example using curl


CREDITS
-

author: alvertogit
copyright: 2018-2024

+

author: alvertogit
copyright: 2018-2025

diff --git a/app/config.py b/app/config.py index fc82c4e..c9c9813 100644 --- a/app/config.py +++ b/app/config.py @@ -3,11 +3,13 @@ """ __author__ = "alvertogit" -__copyright__ = "Copyright 2018-2024" +__copyright__ = "Copyright 2018-2025" import os +from flask import Flask + class DefaultConfig: """ @@ -21,7 +23,7 @@ class DefaultConfig: MODEL_PATH = os.environ.get("MODEL_PATH") or "/app/mnist_model.keras" @staticmethod - def init_app(app): + def init_app(app: Flask) -> None: """ Initialize the application with the default configuration. """ @@ -37,7 +39,7 @@ class DevConfig(DefaultConfig): DEBUG = True @classmethod - def init_app(cls, app): + def init_app(cls, app: Flask) -> None: """ Initialize the application with the development configuration. """ @@ -53,7 +55,7 @@ class TestConfig(DefaultConfig): TESTING = True @classmethod - def init_app(cls, app): + def init_app(cls, app: Flask) -> None: """ Initialize the application with the testing configuration. """ diff --git a/app/server.py b/app/server.py index da63542..e7f701f 100644 --- a/app/server.py +++ b/app/server.py @@ -3,7 +3,7 @@ """ __author__ = "alvertogit" -__copyright__ = "Copyright 2018-2024" +__copyright__ = "Copyright 2018-2025" from app import create_app diff --git a/app/tests/conftest.py b/app/tests/conftest.py index 3cef143..c0861fb 100644 --- a/app/tests/conftest.py +++ b/app/tests/conftest.py @@ -3,16 +3,18 @@ """ __author__ = "alvertogit" -__copyright__ = "Copyright 2018-2024" +__copyright__ = "Copyright 2018-2025" import pytest +from flask import Flask +from flask.testing import FlaskClient from app import create_app @pytest.fixture -def app(): +def app() -> Flask: """ Create a Flask app instance for testing. @@ -25,7 +27,7 @@ def app(): @pytest.fixture -def client(app): +def client(app: Flask) -> FlaskClient: """ Create a Flask test client for the app. diff --git a/app/tests/test_app.py b/app/tests/test_app.py index e1f349f..f1102c5 100644 --- a/app/tests/test_app.py +++ b/app/tests/test_app.py @@ -3,13 +3,15 @@ """ __author__ = "alvertogit" -__copyright__ = "Copyright 2018-2024" +__copyright__ = "Copyright 2018-2025" import json +from flask.testing import FlaskClient -def test_index(client): + +def test_index(client: FlaskClient) -> None: """ Test the index route. @@ -26,7 +28,7 @@ def test_index(client): assert response.data == b"Deep Learning on Flask" -def test_api(client): +def test_api(client: FlaskClient) -> None: """ Test the API endpoint to predict the label of an uploaded image with the Deep Learning model.