ikeno-ada commited on
Commit
35ba4de
·
verified ·
1 Parent(s): 6477da9

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +28 -0
handler.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline, M2M100ForConditionalGeneration, M2M100Tokenizer,QuantoConfig
2
+ from typing import Dict, List, Any
3
+
4
+ class EndpointHandler():
5
+ def __init__(self, path=""):
6
+ # load the optimized model
7
+ model = M2M100ForConditionalGeneration.from_pretrained(path)
8
+ tokenizer = M2M100Tokenizer.from_pretrained(path)
9
+ # create inference pipeline
10
+ self.pipeline = pipeline("translation", model=model, tokenizer=tokenizer)
11
+
12
+
13
+ def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
14
+ """
15
+ Args:
16
+ data (:obj:):
17
+ includes the input data and the parameters for the inference.
18
+ Return:
19
+ A :obj:`list`:. The object returned should be a list of one list like [[{"label": 0.9939950108528137}]] containing :
20
+ - "label": A string representing what the label/class is. There can be multiple labels.
21
+ - "score": A score between 0 and 1 describing how confident the model is for this label/class.
22
+ """
23
+ text = data.get("text", data)
24
+ lang = data.get("langId",data)
25
+ encoded = tokenizer(text, return_tensors="pt")
26
+ generated_tokens = model.generate(**encoded, forced_bos_token_id=tokenizer.get_lang_id(lang))
27
+ result = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
28
+ return {'transdlated':result}