Skip to content

Commit

Permalink
type hints and updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
alvertogit committed Jan 1, 2025
1 parent aa0f230 commit 8b153bc
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/
Expand Down
4 changes: 2 additions & 2 deletions app/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

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


from config import config
Expand All @@ -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])
Expand Down
6 changes: 3 additions & 3 deletions app/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions app/app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion app/app/templates/dlflask.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ <h4>POST example using curl</h4>
</div>
<br>
<h5>CREDITS</h5>
<p>author: alvertogit<br>copyright: 2018-2024</p>
<p>author: alvertogit<br>copyright: 2018-2025</p>
</div>
</body>
</html>
10 changes: 6 additions & 4 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"""

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


import os

from flask import Flask


class DefaultConfig:
"""
Expand All @@ -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.
"""
Expand All @@ -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.
"""
Expand All @@ -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.
"""
Expand Down
2 changes: 1 addition & 1 deletion app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

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


from app import create_app
Expand Down
8 changes: 5 additions & 3 deletions app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,7 +27,7 @@ def app():


@pytest.fixture
def client(app):
def client(app: Flask) -> FlaskClient:
"""
Create a Flask test client for the app.
Expand Down
8 changes: 5 additions & 3 deletions app/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 8b153bc

Please sign in to comment.