Skip to content

Commit

Permalink
Merge pull request #1 from heltonricardo/config-train
Browse files Browse the repository at this point in the history
Arquivos de configuração, treinamento e teste
  • Loading branch information
heltonricardo committed Dec 13, 2023
2 parents 4b98d5d + 8ee6170 commit 7f75950
Show file tree
Hide file tree
Showing 8 changed files with 732 additions and 0 deletions.
91 changes: 91 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Train Pipeline

on:
push:
pull_request:
branches:
- layers
- config-train
workflow_dispatch:
# Allows external webhook trigger
repository_dispatch:
types:
- webhook
jobs:
test_train:
name: Test train
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: 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: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Validate Python Code Style
run: pycodestyle .

train_pipeline:
name: Train pipeline
runs-on: ubuntu-latest
needs: [test_train]
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: Train Model
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: Build the Docker image
run: docker build . --file Dockerfile --tag ${{secrets.DOCKER_USER}}/${{secrets.DOCKER_IMAGE_NAME}}

- name: Docker Push
run: docker push ${{secrets.DOCKER_USER}}/${{secrets.DOCKER_IMAGE_NAME}}
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.9
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./app /code/app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
6 changes: 6 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: mlops-env
dependencies:
- python=3.9
- pip
- pip:
- -r requirements.txt
117 changes: 117 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import os
import mlflow
import numpy as np
from pydantic import BaseModel
from fastapi import FastAPI


class FetalHealthData(BaseModel):
accelerations: float
fetal_movement: float
uterine_contractions: float
severe_decelerations: float


app = FastAPI(
title="Fetal Health API",
openapi_tags=[
{"name": "Health", "description": "Get api health"},
{"name": "Prediction", "description": "Model prediction"},
],
)


def load_model():
"""
Loads a pre-trained model from an MLflow server.
This function connects to an MLflow server using the provided tracking URI,
username, and password.
It retrieves the latest version of the 'fetal_health' model registered on
the server.
The function then loads the model using the specified run ID and returns
the loaded model.
Returns:
loaded_model: The loaded pre-trained model.
Raises:
None
"""
print("reading model...")
MLFLOW_TRACKING_URI = \
"https://dagshub.com/renansantosmendes/mlops-ead.mlflow"
MLFLOW_TRACKING_USERNAME = "renansantosmendes"
MLFLOW_TRACKING_PASSWORD = "b63baf8c662a23fa00deb74ba86600278769e5dd"
os.environ["MLFLOW_TRACKING_USERNAME"] = MLFLOW_TRACKING_USERNAME
os.environ["MLFLOW_TRACKING_PASSWORD"] = MLFLOW_TRACKING_PASSWORD
print("setting mlflow...")
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
print("creating client..")
client = mlflow.MlflowClient(tracking_uri=MLFLOW_TRACKING_URI)
print("getting registered model...")
registered_model = client.get_registered_model("fetal_health")
print("read model...")
run_id = registered_model.latest_versions[-1].run_id
logged_model = f"runs:/{run_id}/model"
loaded_model = mlflow.pyfunc.load_model(logged_model)
print(loaded_model)
return loaded_model


@app.on_event(event_type="startup")
def startup_event():
"""
A function that is called when the application starts up. It loads a model
into the global variable `loaded_model`.
Parameters:
None
Returns:
None
"""
global loaded_model
loaded_model = load_model()


@app.get(path="/", tags=["Health"])
def api_health():
"""
A function that represents the health endpoint of the API.
Returns:
dict: A dictionary containing the status of the API, with the key
"status" and the value "healthy".
"""
return {"status": "healthy"}


@app.post(path="/predict", tags=["Prediction"])
def predict(request: FetalHealthData):
"""
Predicts the fetal health based on the given request data.
Args:
request (FetalHealthData): The request data containing the fetal
health parameters.
Returns:
dict: A dictionary containing the prediction of the fetal health.
Raises:
None
"""
global loaded_model
received_data = np.array(
[
request.accelerations,
request.fetal_movement,
request.uterine_contractions,
request.severe_decelerations,
]
).reshape(1, -1)
print(received_data)
prediction = loaded_model.predict(received_data)
print(prediction)
return {"prediction": str(np.argmax(prediction[0]))}
Loading

0 comments on commit 7f75950

Please sign in to comment.