Commit
•
ab63513
1
Parent(s):
4918cc3
Upload 5 files
Browse files- Dockerfile +13 -0
- classification_gaussian_binary_model_0v.pt +3 -0
- main.py +47 -0
- model.py +17 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY ./requirements.txt /app/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
8 |
+
|
9 |
+
COPY ./ /app
|
10 |
+
|
11 |
+
EXPOSE 8000
|
12 |
+
|
13 |
+
CMD ["uvicorn","main:app","--host","0.0.0.0","--port","8000"]
|
classification_gaussian_binary_model_0v.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1a63771c2a046b3a514d302a93739f68011be2f9c40031ec5740df5530826d9f
|
3 |
+
size 5640
|
main.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List,Dict
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import numpy as np
|
4 |
+
from fastapi import FastAPI
|
5 |
+
import torch
|
6 |
+
from model import BinaryClassificationWithLogits
|
7 |
+
import __main__
|
8 |
+
|
9 |
+
model_path="classification_gaussian_binary_model_0v.pt"
|
10 |
+
model=BinaryClassificationWithLogits(in_features=4,
|
11 |
+
out_features=1,
|
12 |
+
hidden_features=10)
|
13 |
+
|
14 |
+
model = torch.jit.load(model_path,map_location="cpu")
|
15 |
+
|
16 |
+
|
17 |
+
class ClassificationFeatures(BaseModel):
|
18 |
+
feature_1:float
|
19 |
+
feature_2:float
|
20 |
+
feature_3:float
|
21 |
+
feature_4:float
|
22 |
+
|
23 |
+
|
24 |
+
# Creando una instacnia de FastAPI
|
25 |
+
app=FastAPI()
|
26 |
+
# Definiendo la ruta raiz
|
27 |
+
@app.get("/")
|
28 |
+
def home_page():
|
29 |
+
return "Welcome the API with pytorch"
|
30 |
+
# Definiendo ruta para inferencias
|
31 |
+
@app.post("/predict")
|
32 |
+
def predict_sample(cls_features:ClassificationFeatures) -> Dict:
|
33 |
+
|
34 |
+
input_data=np.array([[
|
35 |
+
cls_features.feature_1,
|
36 |
+
cls_features.feature_2,
|
37 |
+
cls_features.feature_3,
|
38 |
+
cls_features.feature_4,
|
39 |
+
]])
|
40 |
+
X=torch.tensor(input_data,dtype=torch.float32)
|
41 |
+
model.eval()
|
42 |
+
with torch.inference_mode():
|
43 |
+
logit=model(X)
|
44 |
+
pred_prob=torch.sigmoid(logit)
|
45 |
+
pred_label=torch.round(pred_prob)
|
46 |
+
|
47 |
+
return {"prediction":pred_label.item()}
|
model.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from torch import nn
|
3 |
+
|
4 |
+
class BinaryClassificationWithLogits(nn.Module):
|
5 |
+
def __init__(self,in_features,out_features, hidden_features):
|
6 |
+
super().__init__()
|
7 |
+
self.linear_block1=nn.Linear(in_features=in_features,out_features=hidden_features)
|
8 |
+
self.linear_block2=nn.Linear(in_features=hidden_features,out_features=out_features)
|
9 |
+
|
10 |
+
def forward(self,X):
|
11 |
+
|
12 |
+
x=self.linear_block1(X)
|
13 |
+
|
14 |
+
x=self.linear_block2(x)
|
15 |
+
|
16 |
+
# return LOGITS
|
17 |
+
return x
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
fastapi
|
3 |
+
uvicorn
|
4 |
+
scikit-learn
|
5 |
+
|