| 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} |