Download core/classify.py from dmth10/tell: direct link, hf CLI and curl.
- Browser
- Download file 980 Bytes
-
https://huggingface.co/spaces/dmth10/tell/resolve/main/core/classify.py
- Command line
-
hf download hf://spaces/dmth10/tell/core/classify.py
-
curl -L -o classify.py https://huggingface.co/spaces/dmth10/tell/resolve/main/core/classify.py
980 Bytes
| import logging | |
| log = logging.getLogger("tell") | |
| # CLIP zero-shot prompts. Wording matters a lot here, so test changes on real uploads. | |
| LABELS = { | |
| "chart": "a bar chart or line graph with axes", | |
| "screenshot": "a screenshot of a social media post or a text message conversation", | |
| "photo": "a photo, painting, meme, or AI-generated picture", | |
| } | |
| _clf = None | |
| def classify_image(image, threshold: float = 0.5) -> tuple[str, float]: | |
| global _clf | |
| try: | |
| if _clf is None: | |
| from transformers import pipeline | |
| _clf = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32") | |
| top = _clf(image, candidate_labels=list(LABELS.values()))[0] | |
| except Exception as e: | |
| log.warning("Classifier failed: %s", e) | |
| return "unknown", 0.0 | |
| if top["score"] < threshold: | |
| return "unknown", top["score"] | |
| kind = next(k for k, v in LABELS.items() if v == top["label"]) | |
| return kind, top["score"] |