damiano216 commited on
Commit
c173a25
·
verified ·
1 Parent(s): cc5c1ac

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +17 -29
handler.py CHANGED
@@ -1,43 +1,31 @@
1
  import torch
2
  import torch.nn as nn
3
- import torch.optim as optim
4
- import os
5
  from huggingface_hub import PyTorchModelHubMixin
6
 
 
 
 
7
 
8
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Set device
9
- print('device >>> ', device)
 
 
 
10
 
 
 
11
 
12
-
13
-
14
-
15
- class EndpointHandler():
16
-
17
-
18
- # Define model class that loads a pretrained model
19
- class MyModel(nn.Module, PyTorchModelHubMixin):
20
- def __init__(self, model_name="damiano216/pay-boo-2"):
21
- super().__init__()
22
- # Load pretrained model from Hugging Face
23
- self.model = MyModel.from_pretrained(model_name)
24
-
25
- def forward(self, x):
26
- return self.model(x)
27
-
28
-
29
- net = MyModel()
30
-
31
  def __init__(self, path=""):
32
- self.model = MyModel() # Load the pretrained model
33
- self.model.to(device) # Move to GPU if available
34
- self.model.eval() # Set to evaluation mode
35
-
36
 
37
  def __call__(self, data):
38
- new_data_tensor = torch.tensor(data['chargeData'], dtype=torch.float32).to(device) # Convert input to tensor
39
 
40
- # 3. Make predictions
41
  with torch.no_grad():
42
  predictions = self.model(new_data_tensor)
43
 
 
1
  import torch
2
  import torch.nn as nn
 
 
3
  from huggingface_hub import PyTorchModelHubMixin
4
 
5
+ # Set device
6
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
7
+ print('Device:', device)
8
 
9
+ # Define model class
10
+ class MyModel(nn.Module, PyTorchModelHubMixin):
11
+ def __init__(self):
12
+ super().__init__() # Initialize nn.Module
13
+ # Model layers will be loaded via from_pretrained()
14
 
15
+ def forward(self, x):
16
+ return self.model(x) # Assume this model has a defined forward pass
17
 
18
+ # EndpointHandler class
19
+ class EndpointHandler:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def __init__(self, path=""):
21
+ self.model = MyModel.from_pretrained("damiano216/pay-boo-2") # Load from Hugging Face
22
+ self.model.to(device) # Move model to GPU if available
23
+ self.model.eval() # Set model to evaluation mode
 
24
 
25
  def __call__(self, data):
26
+ new_data_tensor = torch.tensor(data['chargeData'], dtype=torch.float32).to(device) # Ensure tensor is on device
27
 
28
+ # Make predictions
29
  with torch.no_grad():
30
  predictions = self.model(new_data_tensor)
31