Skip to content

Commit

Permalink
Merge pull request #1 from renansantosmendes/four_lecture
Browse files Browse the repository at this point in the history
Four lecture
  • Loading branch information
renansantosmendes authored Oct 29, 2022
2 parents cd02aae + 135dbbb commit ae6b575
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: FastAPI-GitHub-Heroku

on:
push:
branches:
- main
- four_lecture
pull_request:
branches:
- main

jobs:
ci_pipeline:
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
run: |
python -m pytest test.py
cd_pipeline:

runs-on: ubuntu-latest
needs: [ci_pipeline]
steps:
- uses: actions/checkout@v2
- name: Deploy to Heroku
env:
HEROKU_AUTH_TOKEN: ${{ secrets.HEROKU_AUTH_TOKEN }}
HEROKU_APP_NAME: ${{ secrets.HEROKU_APP_NAME }}
if: github.ref == 'refs/heads/master' && job.status == 'success'
run: |
git remote add heroku https://heroku:$HEROKU_AUTH_TOKEN@git.heroku.com/$HEROKU_APP_NAME.git
git push heroku HEAD:master -f
41 changes: 41 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
import os
import pickle
import numpy as np
from fastapi import FastAPI
from pydantic import BaseModel


class Data(BaseModel):
baseline_value: float
accelerations: float
fetal_movement: float
uterine_contractions: float
light_decelerations: float
severe_decelerations: float
prolongued_decelerations: float


def load_models():
model = pickle.load(open(os.path.join(os.getcwd(),
'models',
'model.pkl'), 'rb'))
scaler = pickle.load(open(os.path.join(os.getcwd(),
'models',
'scaler.pkl'), 'rb'))
return scaler, model


scaler, model = load_models()
app = FastAPI()


@app.get('/')
def home():
return {"Hello": "World"}


@app.post('/api/predict')
def predict(request: Data):
received_data = np.array([
request.baseline_value,
request.accelerations,
request.fetal_movement,
request.uterine_contractions,
request.light_decelerations,
request.severe_decelerations,
request.prolongued_decelerations
]).reshape(1, -1)
prediction = model.predict(scaler.transform(received_data))[0]
return {'prediction': prediction}
Binary file added models/model.pkl
Binary file not shown.
Binary file added models/scaler.pkl
Binary file not shown.
11 changes: 9 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import pytest
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from fastapi.testclient import TestClient
from main import app
from main import app, scaler, model

client = TestClient(app)


def test_home():
response = client.get('/')
assert response.json() == {"Hello": "World"}
assert response.json() == {"Hello": "World"}


def test_models():
assert isinstance(scaler, StandardScaler)
assert isinstance(model, RandomForestClassifier)

0 comments on commit ae6b575

Please sign in to comment.