Kiwinicki jlynxdev commited on
Commit
4df01b4
·
verified ·
1 Parent(s): 8cc9b23

Add a custom handler for Inference Endpoints (#1)

Browse files

- Add a custom handler for Inference Endpoints (70197b67f05f194a4e06072c22668cc6313922be)
- extend transforms and change requirements.txt encoding (7c184f56abb85493b97e2c5ccf035a4918b23335)


Co-authored-by: Jacek Nowak <[email protected]>

Files changed (3) hide show
  1. .gitignore +2 -0
  2. handler.py +58 -0
  3. requirements.txt +25 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .venv
2
+ .idea
handler.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ [
18
+ transforms.ToImage(),
19
+ transforms.ToDtype(torch.float32, scale=True),
20
+ transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
21
+ ]
22
+ )
23
+
24
+ repo_id = "Kiwinicki/sat2map-generator"
25
+ generator_path = hf_hub_download(repo_id=repo_id, filename="generator.pth")
26
+ config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
27
+ model_path = hf_hub_download(repo_id=repo_id, filename="model.py")
28
+
29
+ with open(config_path, "r") as f:
30
+ config_dict = json.load(f)
31
+ cfg = OmegaConf.create(config_dict)
32
+
33
+ self.generator = Generator(cfg)
34
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
35
+ self.generator.load_state_dict(torch.load(generator_path, map_location=self.device))
36
+ self.generator.eval()
37
+
38
+
39
+ def __call__(self, data: dict[str, any]) -> dict[str, str]:
40
+ base64_image = data.get('inputs')
41
+ input_tensor = self._decode_base64_image(base64_image)
42
+ # print('Input tensor shape: ' + str(input_tensor.shape))
43
+ output_tensor = self.generator(input_tensor.to(self.device))
44
+ output_tensor = output_tensor.squeeze(0)
45
+ output_image = transforms.ToPILImage()(output_tensor)
46
+ output_image = output_image.convert('RGB')
47
+ output_buffer = io.BytesIO()
48
+ output_image.save(output_buffer, format="png")
49
+ base64_output = base64.b64encode(output_buffer.getvalue()).decode('utf-8')
50
+ return {"output": base64_output}
51
+
52
+
53
+ def _decode_base64_image(self, base64_image: str) -> torch.Tensor:
54
+ image_decoded = base64.b64decode(base64_image)
55
+ image = Image.open(io.BytesIO(image_decoded)).convert('RGB')
56
+ image_tensor: torch.Tensor = self.transform(image)
57
+ image_tensor = image_tensor.unsqueeze(0)
58
+ return image_tensor
requirements.txt ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ antlr4-python3-runtime==4.9.3
2
+ certifi==2024.12.14
3
+ charset-normalizer==3.4.1
4
+ colorama==0.4.6
5
+ filelock==3.17.0
6
+ fsspec==2024.12.0
7
+ huggingface-hub==0.28.0
8
+ idna==3.10
9
+ Jinja2==3.1.5
10
+ MarkupSafe==3.0.2
11
+ mpmath==1.3.0
12
+ networkx==3.4.2
13
+ numpy==2.2.2
14
+ omegaconf==2.3.0
15
+ packaging==24.2
16
+ pillow==11.1.0
17
+ PyYAML==6.0.2
18
+ requests==2.32.3
19
+ setuptools==75.8.0
20
+ sympy==1.13.1
21
+ torch==2.5.1
22
+ torchvision==0.20.1
23
+ tqdm==4.67.1
24
+ typing_extensions==4.12.2
25
+ urllib3==2.3.0