|
|
import torch |
|
|
import torch.nn as nn |
|
|
from huggingface_hub import PyTorchModelHubMixin |
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
|
|
print('Device:', device) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
torch.manual_seed(42) |
|
|
|
|
|
model = nn.Sequential( |
|
|
nn.Linear(12, 12), |
|
|
nn.ReLU(), |
|
|
nn.Linear(12, 6), |
|
|
nn.ReLU(), |
|
|
nn.Linear(6, 1), |
|
|
nn.Sigmoid() |
|
|
) |
|
|
|
|
|
|
|
|
class MyModel(nn.Module, PyTorchModelHubMixin): |
|
|
|
|
|
def __init__(self): |
|
|
super().__init__() |
|
|
self.model = model |
|
|
|
|
|
def forward(self, x): |
|
|
return self.model(x) |
|
|
|
|
|
|
|
|
class EndpointHandler: |
|
|
def __init__(self, path=""): |
|
|
self.model = MyModel.from_pretrained("damiano216/pay-boo-2") |
|
|
self.model.to(device) |
|
|
self.model.eval() |
|
|
|
|
|
def __call__(self, data): |
|
|
|
|
|
print(f"Payload: {data}") |
|
|
|
|
|
|
|
|
payloadDataFrame = pd.DataFrame(data['chargeData']) |
|
|
print(payloadDataFrame) |
|
|
|
|
|
|
|
|
new_data_tensor = torch.tensor(payloadDataFrame.values, dtype=torch.float).to(device) |
|
|
print(f"new_data_tensor: {new_data_tensor}") |
|
|
|
|
|
|
|
|
with torch.no_grad(): |
|
|
predictions = self.model(new_data_tensor) |
|
|
|
|
|
|
|
|
print(f"Predictions: {predictions[0].item()}") |
|
|
|
|
|
return predictions[0].item() |