damiano216 commited on
Commit
a3bd5a7
·
verified ·
1 Parent(s): ef213a8

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +28 -13
handler.py CHANGED
@@ -1,8 +1,7 @@
1
  import torch
2
- from transformers import pipeline
3
- from PIL import Image
4
- import requests
5
  import os
 
 
6
 
7
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Set device
8
  print('device >>> ', device)
@@ -10,19 +9,35 @@ print('device >>> ', device)
10
 
11
  class EndpointHandler():
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def __init__(self, path=""):
14
- #pipe = pipeline("image-segmentation", "nvidia/segformer-b1-finetuned-cityscapes-1024-1024") #semantic_segmentation
15
- # pipe = pipeline("image-segmentation", "facebook/mask2former-swin-large-cityscapes-instance") #instance_segmentation
16
- pipe = pipeline("image-segmentation", "facebook/mask2former-swin-large-cityscapes-panoptic") #panoptic_segmentation
17
- self.model = pipe # No need to move model to device for this algo
18
 
19
 
20
  def __call__(self, data):
21
- url = data["image_url"]
22
- image = Image.open(requests.get(url, stream=True).raw)
23
- print('image >>> ', image)
24
 
25
- results = self.model(image)
26
- print('results >>> ', results)
 
 
 
 
 
27
 
28
- return results
 
1
  import torch
 
 
 
2
  import os
3
+ from huggingface_hub import PyTorchModelHubMixin
4
+
5
 
6
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Set device
7
  print('device >>> ', device)
 
9
 
10
  class EndpointHandler():
11
 
12
+ class MyModel(
13
+ nn.Module,
14
+ PyTorchModelHubMixin,
15
+ ):
16
+ def __init__(self):
17
+ super().__init__()
18
+ self.model = model
19
+
20
+
21
+ def forward(self, x):
22
+ x = self.model(x)
23
+ return x
24
+
25
+ net = MyModel()
26
+
27
  def __init__(self, path=""):
28
+ model = MyModel.from_pretrained("damiano216/pay-boo-2")
29
+ self.model = model
30
+ #POTENTIALLY WILL NEED TO MOVE THE MODEL TO DEVICE HERE
 
31
 
32
 
33
  def __call__(self, data):
 
 
 
34
 
35
+ new_data_tensor = data['chargeData']
36
+ # 3. Make predictions
37
+ with torch.no_grad():
38
+ predictions = self.model(new_data_tensor)
39
+
40
+ # 4. Interpret predictions
41
+ print(f"predictions >>> : {predictions[0][0]}")
42
 
43
+ return predictions