yovelcohen1 commited on
Commit
e31682d
·
1 Parent(s): f145981

Upload 2 files

Browse files
Files changed (2) hide show
  1. hgf.py +23 -0
  2. requirements.txt +1 -0
hgf.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+
3
+ from transformers import MBart50TokenizerFast, MBartForConditionalGeneration
4
+
5
+
6
+ class EndpointHandler:
7
+ def __init__(self, path=""):
8
+ self.tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
9
+ self.model = MBartForConditionalGeneration.from_pretrained(path)
10
+
11
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
12
+ inputs = data.pop("inputs", data)
13
+ source_lang = data.pop("src_lang", None)
14
+ target_lang = data.pop("target_lang", None)
15
+
16
+ self.tokenizer.src_lang = source_lang
17
+
18
+ encoded = self.tokenizer(inputs, return_tensors='pt')
19
+ generated_tokens = self.model.generate(
20
+ **encoded, forced_bos_token_id=self.tokenizer.lang_code_to_id[target_lang]
21
+ )
22
+ d = self.tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
23
+ return d
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ transformers