-
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 renansantosmendes/four_lecture
Four lecture
- Loading branch information
Showing
5 changed files
with
97 additions
and
2 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,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 |
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 |
---|---|---|
@@ -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 not shown.
Binary file not shown.
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 |
---|---|---|
@@ -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) |