Upload handler.py with huggingface_hub
Browse files- handler.py +20 -0
handler.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class EndpointHandler:
|
| 6 |
+
def __init__(self, path: str = ""):
|
| 7 |
+
self.tokenizer = MarianTokenizer.from_pretrained(path)
|
| 8 |
+
self.model = MarianMTModel.from_pretrained(path)
|
| 9 |
+
|
| 10 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, str]]:
|
| 11 |
+
inputs = data.get("inputs", "")
|
| 12 |
+
|
| 13 |
+
if isinstance(inputs, str):
|
| 14 |
+
inputs = [inputs]
|
| 15 |
+
|
| 16 |
+
encoded = self.tokenizer(inputs, return_tensors="pt", padding=True, truncation=True)
|
| 17 |
+
translated = self.model.generate(**encoded)
|
| 18 |
+
decoded = self.tokenizer.batch_decode(translated, skip_special_tokens=True)
|
| 19 |
+
|
| 20 |
+
return [{"translation_text": text} for text in decoded]
|