-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from heltonricardo/config-train
Arquivos de configuração, treinamento e teste
- Loading branch information
Showing
8 changed files
with
732 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]))} |
Oops, something went wrong.