Create handler.py

#18
by wensun - opened
Files changed (2) hide show
  1. handler.py +23 -0
  2. requirements.txt +6 -0
handler.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from transformers import AutoTokenizer, AutoModel
3
+ import torch
4
+
5
+ class EndpointHandler:
6
+ def __init__(self, path=""):
7
+ # load model and processor from path
8
+ self.tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
9
+ self.model = AutoModel.from_pretrained(path, trust_remote_code=True).half().cuda()
10
+
11
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
12
+ """
13
+ Args:
14
+ data (:dict:):
15
+ The payload with the text prompt and generation parameters.
16
+ """
17
+ # process input
18
+ inputs = data.pop("inputs", data)
19
+ history = data.pop("history", None)
20
+
21
+ response, new_history = self.model.chat(self.tokenizer, inputs, history)
22
+
23
+ return [{"generated_text": response, "history": new_history}]
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ protobuf>=3.19.5,<3.20.1
2
+ transformers==4.26.1
3
+ icetk
4
+ cpm_kernels
5
+ torch>=1.10
6
+ gradio