Download handler.py from soar-innovations/keyphrase-extraction: direct link, hf CLI and curl.
- Browser
- Download file 939 Bytes
-
https://huggingface.co/soar-innovations/keyphrase-extraction/resolve/main/handler.py
- Command line
-
hf download hf://soar-innovations/keyphrase-extraction/handler.py
-
curl -L -o handler.py https://huggingface.co/soar-innovations/keyphrase-extraction/resolve/main/handler.py
939 Bytes
| 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} |