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