Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/serve #5

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: 3.8
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# ML pipelines

Simple ML pipeline repo for experimenting with CI/CD / DevOps / MLOps.

## To do

- Use Python 3.11 in CI github action
- make pipelines functions
- add loggers to stuff
- add db conn / func to save inference cases
- add deployment code...
- add versioning to training... in deployment?
56 changes: 51 additions & 5 deletions ml_pipelines/logic/serve/serve.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
def validate_sample(sample: dict):
x1_upper_bound = 5
if sample["X1"] > x1_upper_bound:
return False
return True
import pandas as pd
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
from sklearn.linear_model import LogisticRegression

from ml_pipelines.logic.common.feature_eng import (
FeatureEngineeringParams,
transform_features,
)
from ml_pipelines.logic.common.model import predict


class Point(BaseModel):
prediction_id: str
x1: float = Field(..., description="Value of x1, must be smaller than 5", lt=5)
x2: float


def create_fastapi_app(
model: LogisticRegression, feature_eng_params: FeatureEngineeringParams
):
app = FastAPI()

@app.get("/predict/")
async def _(inputs: Point | list[Point]):
"""
Endpoint to predict probabilities for the given list of points.
:param points: List of points (x1, x2) (or single point)
:return: List of probability predictions (or single prediction)
"""
try:
single = False
if isinstance(inputs, Point):
single = True
inputs = [inputs]
# Convert the input points to a numpy array
x_matrix = pd.DataFrame([{"X1": p.x1, "X2": p.x2} for p in inputs])
x_matrix = transform_features(x_matrix, feature_eng_params)

# Make predictions using the pre-trained model
predictions = predict(model, x_matrix).tolist()

if single:
predictions = predictions[0]

return {"predictions": predictions}

except Exception:
raise HTTPException(status_code=500, detail="Internal server error")

return app