jduhmbd commited on
Commit
1f091e0
1 Parent(s): 6d0573a

Upload handler.py

Browse files
Files changed (1) hide show
  1. handler.py +46 -0
handler.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ import torch
3
+ from transformers import pipeline
4
+
5
+ class EndpointHandler:
6
+ def __init__(self, path=""):
7
+ self.pipeline = pipeline(
8
+ "text-generation",
9
+ model="mistralai/Mixtral-8x7B-Instruct-v0.1",
10
+ device_map='auto',
11
+ #trust_remote_code=True,
12
+ model_kwargs={
13
+ "load_in_4bit": True
14
+ },
15
+ # batch_size=1,
16
+ )
17
+ # model.generation_config
18
+
19
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
20
+ """
21
+ data args:
22
+ inputs (:obj: `str`)
23
+ parameters (:obj: `Dict`)
24
+ Return:
25
+ A :obj:`list` | `dict`: will be serialized and returned
26
+ """
27
+ # get inputs
28
+ inputs = data.pop("inputs", "")
29
+ # get additional date field
30
+ params = data.pop("parameters", ())
31
+ if not params:
32
+ params = dict()
33
+ # run normal prediction
34
+ generation = self.pipeline(inputs, **params)
35
+ # **generate_kwargs https://huggingface.co/docs/transformers/generation_strategies#customize-text-generation,
36
+ # https://huggingface.co/docs/transformers/generation_strategies#customize-text-generation
37
+ return generation
38
+
39
+ # Returns
40
+
41
+ # A list or a list of list of dict
42
+
43
+ # Returns one of the following dictionaries (cannot return a combination of both generated_text and generated_token_ids):
44
+
45
+ # generated_text (str, present when return_text=True) — The generated text.
46
+ # generated_token_ids (torch.Tensor or tf.Tensor, present when return_tensors=True) — The token ids of the generated text.