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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Want to collab on Katana? This repo is public and open! #51

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3
FROM python:3.8

WORKDIR /usr/src/app

Expand Down
6 changes: 2 additions & 4 deletions apis/models/house.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class BostonHouseRequestModel(BaseModel):
9. RAD index of accessibility to radial highways
10. TAX full-value property-tax rate per $10,000
11. PTRATIO pupil-teacher ratio by town
12. B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks
by town
12. B 1000(Bk - 0.63)^2 Biased variable
13. LSTAT % lower status of the population
"""

Expand Down Expand Up @@ -70,8 +69,7 @@ class BostonHouseRequestModel(BaseModel):
)
discriminateProportion: float = Field(
example=396.30,
description="1000(Bk - 0.63)^2 where Bk is the proportion \
of colored by town",
description="Discriminate proportion",
)
percentLowerStatPopulation: float = Field(
example=4.30,
Expand Down
14 changes: 14 additions & 0 deletions apis/models/masklang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pydantic import BaseModel, Field


class MaskLanguageModelRequestModel(BaseModel):
predictionId: str = "f75ef3b8-f414-422c-87b1-1e21e684661c"
text: str = "Am I a [MASK] person if I save my green planet?"


class MaskLanguageModelResponseModel(BaseModel):
score: float = 0.1
token: int = 100
token_str: str = "token string"
orginal_sequence: str = "Sequence with [MASK]"
sequence: str = "Sequence with [MASK] replaced with token word"
10 changes: 3 additions & 7 deletions apis/v1/boston.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

router = APIRouter(prefix="/boston")
# Load trained model. Dummy model being trained on startup...
logger.info("Training/Loading iris classification model")
logger.info("Training/Loading boston linear reg model")
trainer = BostonHousePriceTrainerInstance()
boston_model = trainer.train()
logger.info("Training completed")


@router.post(
"/trainModel", tags=["boston"], response_model=TrainingStatusResponse
)
@router.post("/trainModel", tags=["boston"], response_model=TrainingStatusResponse)
async def boston_train():
training_id = uuid.uuid1()
# Queue training / start training via RabbitMQ, Queue, etc..
Expand All @@ -27,9 +25,7 @@ async def boston_train():
}


@router.post(
"/predictPrice", tags=["boston"], response_model=BostonHouseResponseModel
)
@router.post("/predictPrice", tags=["boston"], response_model=BostonHouseResponseModel)
async def boston_price_prediction(body: BostonHouseRequestModel):
request = body.dict()
payload = [x for x in request.values()]
Expand Down
4 changes: 1 addition & 3 deletions apis/v1/iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
logger.info("Training completed")


@router.post(
"/trainModel", tags=["iris"], response_model=TrainingStatusResponse
)
@router.post("/trainModel", tags=["iris"], response_model=TrainingStatusResponse)
async def iris_train():
training_id = uuid.uuid1()
# Queue training / start training via RabbitMQ, Queue, etc..
Expand Down
39 changes: 39 additions & 0 deletions apis/v1/masklang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import time
from loguru import logger
from fastapi.routing import APIRouter
from core.pipeline import MaskLanguageModelPipe
from apis.models.base import TrainingStatusResponse
from apis.models.masklang import (
MaskLanguageModelRequestModel,
MaskLanguageModelResponseModel,
)
from typing import List

router = APIRouter(prefix="/mask")
# Load trained model. Dummy model being trained on startup...
logger.info("Training/Loading Mask language model")
masking_pipeline = MaskLanguageModelPipe()
logger.info("Model load completed")

# Warm loading / Warm up
start = time.time()
logger.info("Model warm loading...")
masking_pipeline.predict("[MASK] I am good")
end = time.time()
logger.info(f"Model warm loading completed in {round(end-start,2)} secs")


@router.post(
"/predictMask",
tags=["langmask"],
response_model=List[MaskLanguageModelResponseModel],
)
async def mask_lang_prediction(payload: MaskLanguageModelRequestModel):
text = payload.text
logger.info(f"Recieved payload as {text}")
logger.info(f"Processing predictions...")
predictions = masking_pipeline.predict(text)
for pred in predictions:
pred["orginal_sequence"] = text
logger.info(f"Processed successfully")
return predictions
11 changes: 11 additions & 0 deletions core/pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from transformers import pipeline


class MaskLanguageModelPipe:
def __init__(self):
self.task = "fill-mask"
self.model_name = "distilbert-base-uncased"

def predict(self, text):
model = pipeline(self.task, model=self.model_name)
return model(text)
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi.responses import RedirectResponse
from apis.v1.iris import router as iris_ns
from apis.v1.boston import router as boston_ns
from apis.v1.masklang import router as masklang_ns

# Initialize logging
logger.add("./logs/katana.log", rotation="500 MB")
Expand All @@ -17,6 +18,8 @@
app.include_router(iris_ns)
logger.info("Adding Boston namespace route")
app.include_router(boston_ns)
logger.info("Adding Mask Lang namespace route")
app.include_router(masklang_ns)


@app.get("/", include_in_schema=False)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ flake8==3.8.4
loguru==0.5.3
scikit-learn==0.24.1
uvicorn==0.13.4
transformers==4.21.0
torch==1.12.0