jlynxdev commited on
Commit
70197b6
·
verified ·
1 Parent(s): 8cc9b23

Add a custom handler for Inference Endpoints

Browse files

I've added a custom `EndpointHandler` implementation used by Inference Endpoints. I've also added the needed `requirements.txt` - without it, necessary packages won't be installed automatically. I've also did not add any transforms (apart from the `ToTensor()`) which you said are needed .

Files changed (3) hide show
  1. .gitignore +2 -0
  2. handler.py +54 -0
  3. requirements.txt +0 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .venv
2
+ .idea
handler.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torchvision import transforms
3
+ from huggingface_hub import hf_hub_download
4
+ import json
5
+ import io
6
+ import base64
7
+ from PIL import Image
8
+ from omegaconf import OmegaConf
9
+
10
+ from model import Generator
11
+
12
+
13
+ class EndpointHandler:
14
+
15
+ def __init__(self, path=''):
16
+ self.transform = transforms.Compose([
17
+ transforms.ToTensor()
18
+ ])
19
+
20
+ repo_id = "Kiwinicki/sat2map-generator"
21
+ generator_path = hf_hub_download(repo_id=repo_id, filename="generator.pth")
22
+ config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
23
+ model_path = hf_hub_download(repo_id=repo_id, filename="model.py")
24
+
25
+ with open(config_path, "r") as f:
26
+ config_dict = json.load(f)
27
+ cfg = OmegaConf.create(config_dict)
28
+
29
+ self.generator = Generator(cfg)
30
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
31
+ self.generator.load_state_dict(torch.load(generator_path, map_location=self.device))
32
+ self.generator.eval()
33
+
34
+
35
+ def __call__(self, data: dict[str, any]) -> dict[str, str]:
36
+ base64_image = data.get('inputs')
37
+ input_tensor = self._decode_base64_image(base64_image)
38
+ # print('Input tensor shape: ' + str(input_tensor.shape))
39
+ output_tensor = self.generator(input_tensor.to(self.device))
40
+ output_tensor = output_tensor.squeeze(0)
41
+ output_image = transforms.ToPILImage()(output_tensor)
42
+ output_image = output_image.convert('RGB')
43
+ output_buffer = io.BytesIO()
44
+ output_image.save(output_buffer, format="png")
45
+ base64_output = base64.b64encode(output_buffer.getvalue()).decode('utf-8')
46
+ return {"output": base64_output}
47
+
48
+
49
+ def _decode_base64_image(self, base64_image: str) -> torch.Tensor:
50
+ image_decoded = base64.b64decode(base64_image)
51
+ image = Image.open(io.BytesIO(image_decoded)).convert('RGB')
52
+ image_tensor: torch.Tensor = self.transform(image)
53
+ image_tensor = image_tensor.unsqueeze(0)
54
+ return image_tensor
requirements.txt ADDED
Binary file (924 Bytes). View file