MyParalegal commited on
Commit
0f230c6
·
1 Parent(s): 5d0b10d

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +28 -0
handler.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Dict
2
+ import torch
3
+ import transformers
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ from langchain.llms import HuggingFacePipeline
6
+
7
+ dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16
8
+
9
+ class EndpointHandler:
10
+ def __init__(self, path=""):
11
+ tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
12
+ model = AutoModelForCausalLM.from_pretrained(
13
+ path,
14
+ return_dict=True,
15
+ device_map="auto",
16
+ # load_in_8bit=True,
17
+ torch_dtype=dtype,
18
+ trust_remote_code=True
19
+ )
20
+
21
+ self.pipeline = transformers.pipeline(
22
+ "text-generation", model=model, tokenizer=tokenizer, repetition_penalty=1.2, temperature=0.5, max_new_tokens = 2000
23
+ )
24
+
25
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
26
+ prompt = data.pop("inputs", data)
27
+ llm = HuggingFacePipeline(pipeline=self.pipeline)
28
+ return llm(prompt)