Spaces:
Sleeping
Sleeping
Download api/content_moderator.py from mothy-08/content-moderator-api: direct link, hf CLI and curl.
- Browser
- Download file 575 Bytes
-
https://huggingface.co/spaces/mothy-08/content-moderator-api/resolve/main/api/content_moderator.py
- Command line
-
hf download hf://spaces/mothy-08/content-moderator-api/api/content_moderator.py
-
curl -L -o content_moderator.py https://huggingface.co/spaces/mothy-08/content-moderator-api/resolve/main/api/content_moderator.py
575 Bytes
| import unicodedata | |
| class ContentModerator: | |
| def __init__(self, classifier): | |
| self.classifier = classifier | |
| def predict_text(self, text: str) -> dict: | |
| text = self._preprocess_text(text) | |
| result = self.classifier(text)[0] | |
| return { | |
| "label": result["label"], | |
| "score": result["score"], | |
| } | |
| def _preprocess_text(self, text: str) -> str: | |
| text = unicodedata.normalize("NFKC", text) | |
| text = "".join(ch for ch in text if unicodedata.category(ch)[0] != "C") | |
| return " ".join(text.split()) | |