Skip to content

Instantly share code, notes, and snippets.

Avatar
😎

Tivadar Danka cosmic-cortex

😎
View GitHub Profile
@cosmic-cortex
cosmic-cortex / model_abstract.py
Created Jan 27, 2020
Abstract interface for the machine learning model
View model_abstract.py
class Model:
def train(self, X, y):
pass
def predict(self, X):
pass
def save(self):
pass
@cosmic-cortex
cosmic-cortex / main_skeleton.py
Last active Jan 27, 2020
FastAPI application skeleton
View main_skeleton.py
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel
class PredictRequest(BaseModel):
data: List[List[float]]
@cosmic-cortex
cosmic-cortex / model.py
Last active Jan 27, 2020
Implementation of the machine learning model for the API
View model.py
import joblib
import numpy as np
from pathlib import Path
from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import load_boston
class Model:
def __init__(self, model_path: str = None):
View predict_final.py
import numpy as np
from fastapi import Depends
from .ml.model import get_model
@app.post("/predict", response_model=PredictResponse)
def predict(input: PredictRequest, model: Model = Depends(get_model)):
X = np.array(input.data)
y_pred = model.predict(X)
@cosmic-cortex
cosmic-cortex / predictrequest_final.py
Created Jan 27, 2020
Additional validation for the PredictRequest model
View predictrequest_final.py
from pydantic import BaseModel, ValidationError, validator
from .ml.model import n_features
class PredictRequest(BaseModel):
data: List[List[float]]
@validator("data")
def check_dimensionality(cls, v):
@cosmic-cortex
cosmic-cortex / mocks.py
Created Jan 27, 2020
Mock for unit testing the API
View mocks.py
import numpy as np
class MockModel:
def __init__(self, model_path: str = None):
self._model_path = None
self._model = None
def predict(self, X: np.ndarray) -> np.ndarray:
n_instances = len(X)
@cosmic-cortex
cosmic-cortex / conftest.py
Created Jan 27, 2020
Configurations and fixtures for API testing
View conftest.py
import pytest
from starlette.testclient import TestClient
from ..main import app
from ..ml.model import get_model
from .mocks import MockModel
def get_model_override():
@cosmic-cortex
cosmic-cortex / test_predict.py
Created Jan 27, 2020
Testing the API endpoint
View test_predict.py
import pytest
import random
from starlette.testclient import TestClient
from starlette.status import HTTP_200_OK
from api.ml.model import n_features
@pytest.mark.parametrize("n_instances", range(1, 10))
@cosmic-cortex
cosmic-cortex / test_predict_with_wrong_input.py
Created Jan 27, 2020
Testing the API endpoint with invalid user data
View test_predict_with_wrong_input.py
from itertools import product
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
@pytest.mark.parametrize(
"n_instances, test_data_n_features",
product(range(1, 10), [n for n in range(1, 20) if n != n_features]),
)
def test_predict_with_wrong_input(
@cosmic-cortex
cosmic-cortex / predict_csv.py
Last active Jan 28, 2020
predict_csv endpoint
View predict_csv.py
import pandas as pd
from fastapi import File, UploadFile, HTTPException
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
@app.post("/predict_csv")
def predict_csv(csv_file: UploadFile = File(...), model: Model = Depends(get_model)):
try:
df = pd.read_csv(csv_file.file).astype(float)