abashar commited on
Commit
de6e79a
·
verified ·
1 Parent(s): df8cb5d

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +38 -0
handler.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from typing import Dict, List, Any
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
4
+
5
+ # check for GPU
6
+ device = 0 if torch.cuda.is_available() else -1
7
+
8
+ # multi-model list
9
+ multi_model_list = [
10
+ {"model_id": "omarabb315/gemma-2B-2nd_filtered_3_full", "task": "text-generation"},
11
+ {"model_id": "omarabb315/gemma-2B-2nd_filtered_3_16bit", "task": "text-generation"},
12
+ {"model_id": "omarabb315/Gemma-2-9B-filtered_3_4bits", "task": "text-generation"},
13
+ ]
14
+
15
+ class EndpointHandler():
16
+ def __init__(self, path=""):
17
+ self.multi_model={}
18
+ # load all the models onto device
19
+ for model in multi_model_list:
20
+ self.multi_model[model["model_id"]] = pipeline(model["task"], model=model["model_id"], device=device)
21
+
22
+ def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
23
+ # deserialize incomin request
24
+ inputs = data.pop("inputs", data)
25
+ parameters = data.pop("parameters", None)
26
+ model_id = data.pop("model_id", None)
27
+
28
+ # check if model_id is in the list of models
29
+ if model_id is None or model_id not in self.multi_model:
30
+ raise ValueError(f"model_id: {model_id} is not valid. Available models are: {list(self.multi_model.keys())}")
31
+
32
+ # pass inputs with all kwargs in data
33
+ if parameters is not None:
34
+ prediction = self.multi_model[model_id](inputs, **parameters)
35
+ else:
36
+ prediction = self.multi_model[model_id](inputs)
37
+ # postprocess the prediction
38
+ return prediction