Alexionby commited on
Commit
a1374ed
·
1 Parent(s): 427e8fe

model added

Browse files
__pycache__/handler.cpython-310.pyc ADDED
Binary file (1.34 kB). View file
 
handler.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from transformers import AutoModelForImageClassification, AutoFeatureExtractor
3
+ import torch
4
+ from PIL import Image
5
+ import io
6
+
7
+ class EndpointHandler:
8
+ def __init__(self):
9
+ # Initialize model and feature extractor
10
+ model_id = "alexionby/ainoai"
11
+ self.model = AutoModelForImageClassification.from_pretrained(model_id)
12
+ self.feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
13
+
14
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
15
+ # Convert bytes to PIL Image
16
+ image = data.pop('image', data)
17
+ image = Image.open(io.BytesIO(image))
18
+
19
+ # Preprocess the image
20
+ inputs = self.feature_extractor(images=image, return_tensors="pt")
21
+
22
+ # Run the model
23
+ with torch.no_grad():
24
+ outputs = self.model(**inputs)
25
+
26
+ # Post-process the model outputs as needed
27
+ logits = outputs.logits
28
+ probabilities = torch.nn.functional.softmax(logits, dim=-1)
29
+ predictions = probabilities.argmax(-1)
30
+
31
+ # Convert predictions to JSON-serializable format
32
+ return {"label": str(predictions.item())}
33
+
34
+
35
+ # import torch
36
+ # from PIL import Image
37
+ # import io
38
+
39
+ # class EndpointHandler():
40
+ # def __init__(self, path=""):
41
+ # # Preload all the elements you are going to need at inference.
42
+ # # pseudo:
43
+ # self.model= load_model(path)
44
+
45
+ # def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
46
+ # """
47
+ # data args:
48
+ # inputs (:obj: `str` | `PIL.Image` | `np.array`)
49
+ # kwargs
50
+ # Return:
51
+ # A :obj:`list` | `dict`: will be serialized and returned
52
+ # """
53
+
54
+ # # pseudo
55
+ # # self.model(input)
imgs/optimus.jpg ADDED
requirements.txt ADDED
File without changes
test.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from handler import EndpointHandler
2
+
3
+ # init handler
4
+ my_handler = EndpointHandler()
5
+
6
+ from PIL import Image
7
+ image_bytes = image.open("imgs/optimus.jpg")
8
+
9
+ print(my_handler(image_bytes))