From 8e9cafc0a4a07459b92ae5a9d9cda722bfda1c38 Mon Sep 17 00:00:00 2001 From: Arvin Singh <29691465+arvinsingh@users.noreply.github.com> Date: Sat, 9 Nov 2024 02:35:45 +0000 Subject: [PATCH 1/3] feat: separated inference and build services --- src/model/model_builder.py | 18 +++++++++++++ src/model/model_inference.py | 36 ++++++++++++++++++++++++++ src/model/model_service.py | 36 -------------------------- src/runner_builder.py | 14 ++++++++++ src/{runner.py => runner_inference.py} | 0 5 files changed, 68 insertions(+), 36 deletions(-) create mode 100644 src/model/model_builder.py create mode 100644 src/model/model_inference.py delete mode 100644 src/model/model_service.py create mode 100644 src/runner_builder.py rename src/{runner.py => runner_inference.py} (100%) diff --git a/src/model/model_builder.py b/src/model/model_builder.py new file mode 100644 index 0000000..bf525e3 --- /dev/null +++ b/src/model/model_builder.py @@ -0,0 +1,18 @@ +from loguru import logger + +from config import model_settings +from model.pipeline.model import build_model + + +class ModelBuilderService: + + def __init__(self): + self.model_path = model_settings.model_path + self.model_name = model_settings.model_name + + def load_model(self): + logger.info( + f'Building the model file at ' + f'{self.model_path}/{self.model_name}', + ) + build_model() diff --git a/src/model/model_inference.py b/src/model/model_inference.py new file mode 100644 index 0000000..219195f --- /dev/null +++ b/src/model/model_inference.py @@ -0,0 +1,36 @@ +from pathlib import Path +import pickle as pkl + +from loguru import logger + +from config import model_settings + + +class ModelInferenceService: + + def __init__(self): + self.model = None + self.model_path = model_settings.model_path + self.model_name = model_settings.model_name + + def load_model(self): + logger.info( + f'Checking for existing model file at ' + f'{self.model_path}/{self.model_name}', + ) + model_path = Path( + f'{self.model_path}/{self.model_name}', + ) + + if not model_path.exists(): + raise FileNotFoundError( + f'Model file {self.model_name} does not exist!', + ) + + logger.info(f'Loading existing model -> {model_path}') + with open(model_path, 'rb') as model_file: + self.model = pkl.load(model_file) + + def predict(self, input_parameters): + logger.info('Making predictions') + return self.model.predict([input_parameters]) diff --git a/src/model/model_service.py b/src/model/model_service.py deleted file mode 100644 index d325f5c..0000000 --- a/src/model/model_service.py +++ /dev/null @@ -1,36 +0,0 @@ -from pathlib import Path -import pickle as pkl - -from loguru import logger - -from config import model_settings -from model.pipeline.model import build_model - - -class ModelService: - - def __init__(self): - self.model = None - - def load_model(self): - logger.info( - f'Loading model from ' - f'{model_settings.model_path}/{model_settings.model_name}', - ) - model_path = Path( - f'{model_settings.model_path}/{model_settings.model_name}', - ) - - if not model_path.exists(): - logger.warning( - f'Model not found at {model_path} ' - f'-> Building model {model_settings.model_name}', - ) - build_model() - - logger.info(f'Loading model from {model_path}') - self.model = pkl.load(open(model_path, 'rb')) - - def predict(self, input_parameters): - logger.info('Making predictions') - return self.model.predict([input_parameters]) diff --git a/src/runner_builder.py b/src/runner_builder.py new file mode 100644 index 0000000..7cf5870 --- /dev/null +++ b/src/runner_builder.py @@ -0,0 +1,14 @@ +from loguru import logger + +from model.model_builder import ModelBuilderService + + +@logger.catch +def main(): + logger.info('Running builder application') + ml_svc = ModelBuilderService() + ml_svc.train_model() + + +if __name__ == '__main__': + main() diff --git a/src/runner.py b/src/runner_inference.py similarity index 100% rename from src/runner.py rename to src/runner_inference.py From 415c786c6c5c5d79a4791a2a1872e9c370de15f4 Mon Sep 17 00:00:00 2001 From: Arvin Singh <29691465+arvinsingh@users.noreply.github.com> Date: Sat, 9 Nov 2024 02:38:00 +0000 Subject: [PATCH 2/3] feat: runner and builder job pipelines separated --- Makefile | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 4ecf11b..3cd3d67 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,11 @@ -.PHONY: run install check clean runner -.DEFAULT_GOAL := runner +.PHONY: run-builder run-inference install check clean runner-builder runner-inference +.DEFAULT_GOAL := runner-inference -run: install - cd src; poetry run python runner.py; +run-builder: install + cd src; poetry run python runner_builder.py; + +run-inference: install + cd src; poetry run python runner_inference.py; install: poetry install @@ -13,4 +16,6 @@ check: clean: rm -rf `find . -type d -name __pycache__` -runner: check run clean +runner-builder: check run-builder clean + +runner-inference: check run-inference clean \ No newline at end of file From 91056ed4e2d0973992f9db10c1a51cadcbdbcc57 Mon Sep 17 00:00:00 2001 From: Arvin Singh <29691465+arvinsingh@users.noreply.github.com> Date: Sat, 9 Nov 2024 02:38:59 +0000 Subject: [PATCH 3/3] feat: added github actions for automated linting and building --- .github/actions/build-application/action.yml | 33 ++++++++++++++++++++ .github/workflows/lint.yml | 25 +++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 .github/actions/build-application/action.yml create mode 100644 .github/workflows/lint.yml diff --git a/.github/actions/build-application/action.yml b/.github/actions/build-application/action.yml new file mode 100644 index 0000000..8d891af --- /dev/null +++ b/.github/actions/build-application/action.yml @@ -0,0 +1,33 @@ + +name: Build application +description: This action builds the application + +runs: + + using: 'composite' + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Python 3.11 + uses: actions/setup-python@v5 + with: + python-version: 3.11 + + - name: Install Poetry + uses: snok/install-poetry@v1 + with: + virtualenvs-in-project: true # for caching dependencies + + - name: Load cached virtualenv + uses: actions/cache@v2 + id: cache-poetry-dependencies + with: + path: .venv + key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }} + + - name: Install dependencies using Poetry + if: steps.cache-poetry-dependencies.outputs.cache-hit != 'true' + run: poetry run make install + shell: bash diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..507c7b5 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,25 @@ + +name: Lint workflow + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Build application + uses: ./.github/actions/build-application + + - name: Lint with Flake8 + run: poetry run make check \ No newline at end of file