From b1acb78c662cf6521d7a11e093ce3aa92dcbe15b Mon Sep 17 00:00:00 2001 From: Helton Ricardo <50843386+heltonricardo@users.noreply.github.com> Date: Tue, 12 Dec 2023 21:14:09 -0300 Subject: [PATCH] refact: alterando arquivo pipeline --- .github/workflows/pipeline.yml | 54 +++---- model_registry.ipynb | 259 +++++++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+), 27 deletions(-) create mode 100644 model_registry.ipynb diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 15301a9..f683761 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -1,4 +1,4 @@ -name: Train-Pipeline +name: Train Pipeline on: push: @@ -13,6 +13,7 @@ on: - webhook jobs: test_train: + name: Test train runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 @@ -30,31 +31,31 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test files + run: pytest test_train.py + + check_python_code_style: + name: Check Python code style + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + with: + fetch-depth: 0 + + - name: Set up Python 3.9 + uses: actions/setup-python@v1 + with: + python-version: 3.9 + + - name: Install dependencies run: | - pytest test_train.py + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - # check_python_code_style: - # runs-on: ubuntu-latest - # steps: - # - uses: actions/checkout@v1 - # with: - # fetch-depth: 0 - # - # - name: Set up Python 3.9 - # uses: actions/setup-python@v1 - # with: - # python-version: 3.9 - # - # - name: Install dependencies - # run: | - # python -m pip install --upgrade pip - # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - # - # - name: Validate Python Code Style - # run: | - # pycodestyle . + - name: Validate Python Code Style + run: pycodestyle . train_pipeline: + name: Train pipeline runs-on: ubuntu-latest needs: [test_train] steps: @@ -73,17 +74,16 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Train Model - run: | - python train.py + run: python train.py build_image: + name: Build image runs-on: ubuntu-latest needs: [train_pipeline] steps: - uses: actions/checkout@v2 - - name: docker login - run: | - docker login -u ${{secrets.DOCKER_USER}} -p ${{secrets.DOCKER_PASSWORD}} + - name: Docker Login + run: docker login -u ${{secrets.DOCKER_USER}} -p ${{secrets.DOCKER_PASSWORD}} - name: Build the Docker image run: docker build . --file Dockerfile --tag ${{secrets.DOCKER_USER}}/${{secrets.DOCKER_IMAGE_NAME}} diff --git a/model_registry.ipynb b/model_registry.ipynb new file mode 100644 index 0000000..70d4ca8 --- /dev/null +++ b/model_registry.ipynb @@ -0,0 +1,259 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "## Instalando o MLflow" + ], + "metadata": { + "id": "cL8iUNRkKnnN" + } + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "8ZqLomD1JbEn" + }, + "outputs": [], + "source": [ + "!pip install mlflow==2.2.2 -q" + ] + }, + { + "cell_type": "markdown", + "source": [ + "## import das bibliotecas" + ], + "metadata": { + "id": "jAnzWtfqKsfI" + } + }, + { + "cell_type": "code", + "source": [ + "import os\n", + "import mlflow\n", + "import numpy as np" + ], + "metadata": { + "id": "MAn3URFmJihs" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Configurando o MLflow" + ], + "metadata": { + "id": "PhBP5HFCKvYb" + } + }, + { + "cell_type": "code", + "source": [ + "MLFLOW_TRACKING_URI = ''\n", + "MLFLOW_TRACKING_USERNAME = ''\n", + "MLFLOW_TRACKING_PASSWORD = ''\n", + "os.environ['MLFLOW_TRACKING_USERNAME'] = MLFLOW_TRACKING_USERNAME\n", + "os.environ['MLFLOW_TRACKING_PASSWORD'] = MLFLOW_TRACKING_PASSWORD\n", + "\n", + "mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)" + ], + "metadata": { + "id": "Bu43IC6oJj1L" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Criando um client para comunicar com o registro no DagsHub" + ], + "metadata": { + "id": "dbFFAEaGK0lX" + } + }, + { + "cell_type": "code", + "source": [ + "client = mlflow.MlflowClient(tracking_uri=MLFLOW_TRACKING_URI)" + ], + "metadata": { + "id": "wax9ipKjKShr" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "client" + ], + "metadata": { + "id": "fWlu0lRiLp3u" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Recebendo o modelo registrado e suas versões" + ], + "metadata": { + "id": "m1NMo6kfLBtq" + } + }, + { + "cell_type": "code", + "source": [ + "registered_model = client.get_registered_model('MODEL_NAME')" + ], + "metadata": { + "id": "8oSsyfv0K6j5" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "registered_model.latest_versions" + ], + "metadata": { + "id": "e2DDK_6_LPi5" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Obtendo o id da execução do modelo" + ], + "metadata": { + "id": "DfBd2wRdLRPC" + } + }, + { + "cell_type": "code", + "source": [ + "run_id = registered_model.latest_versions[-1].run_id" + ], + "metadata": { + "id": "HG-BzNlkK9oC" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Carregando o modelo" + ], + "metadata": { + "id": "WORhnvgnLYrX" + } + }, + { + "cell_type": "code", + "source": [ + "logged_model = f'runs:/{run_id}/model'\n", + "loaded_model = mlflow.pyfunc.load_model(logged_model)" + ], + "metadata": { + "id": "xa1ZTR6OK_hg" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Fazendo uma predição com o modelo carregadO" + ], + "metadata": { + "id": "IQBbf3xULc1A" + } + }, + { + "cell_type": "code", + "source": [ + "accelerations = 0\n", + "fetal_movement = 0\n", + "uterine_contractions = 0\n", + "severe_decelerations = 0" + ], + "metadata": { + "id": "SwbDW9aLJziQ" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "received_data = np.array([\n", + " accelerations,\n", + " fetal_movement,\n", + " uterine_contractions,\n", + " severe_decelerations,\n", + " ]).reshape(1, -1)" + ], + "metadata": { + "id": "s-Ed15fsJv5k" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "received_data" + ], + "metadata": { + "id": "Mpc1_Y57J_xV" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "loaded_model.predict(received_data)" + ], + "metadata": { + "id": "lV6gcA8dJmX0" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "k3qYyLsyKDVb" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file