File size: 1,238 Bytes
ab63513 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
from typing import List,Dict
from pydantic import BaseModel
import numpy as np
from fastapi import FastAPI
import torch
from model import BinaryClassificationWithLogits
import __main__
model_path="classification_gaussian_binary_model_0v.pt"
model=BinaryClassificationWithLogits(in_features=4,
out_features=1,
hidden_features=10)
model = torch.jit.load(model_path,map_location="cpu")
class ClassificationFeatures(BaseModel):
feature_1:float
feature_2:float
feature_3:float
feature_4:float
# Creando una instacnia de FastAPI
app=FastAPI()
# Definiendo la ruta raiz
@app.get("/")
def home_page():
return "Welcome the API with pytorch"
# Definiendo ruta para inferencias
@app.post("/predict")
def predict_sample(cls_features:ClassificationFeatures) -> Dict:
input_data=np.array([[
cls_features.feature_1,
cls_features.feature_2,
cls_features.feature_3,
cls_features.feature_4,
]])
X=torch.tensor(input_data,dtype=torch.float32)
model.eval()
with torch.inference_mode():
logit=model(X)
pred_prob=torch.sigmoid(logit)
pred_label=torch.round(pred_prob)
return {"prediction":pred_label.item()} |