Create handler.py
Browse files- handler.py +34 -0
handler.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Dict, List
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class EndpointHandler:
|
| 6 |
+
def __init__(self, path: str = ""):
|
| 7 |
+
self.pipe = pipeline(
|
| 8 |
+
task="token-classification",
|
| 9 |
+
model=path,
|
| 10 |
+
tokenizer=path,
|
| 11 |
+
aggregation_strategy="simple",
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 15 |
+
text = data.get("inputs")
|
| 16 |
+
if not text or not isinstance(text, str):
|
| 17 |
+
return {"error": "Missing required field: inputs"}
|
| 18 |
+
|
| 19 |
+
outputs = self.pipe(text)
|
| 20 |
+
|
| 21 |
+
keywords: List[str] = []
|
| 22 |
+
seen = set()
|
| 23 |
+
|
| 24 |
+
for item in outputs:
|
| 25 |
+
phrase = item.get("word", "").strip()
|
| 26 |
+
if not phrase:
|
| 27 |
+
continue
|
| 28 |
+
|
| 29 |
+
normalized = phrase.lower()
|
| 30 |
+
if normalized not in seen:
|
| 31 |
+
seen.add(normalized)
|
| 32 |
+
keywords.append(phrase)
|
| 33 |
+
|
| 34 |
+
return {"keywords": keywords}
|