Instructions to use opale-ai/news-classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use opale-ai/news-classifier with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("opale-ai/news-classifier", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
Download utils/embed.py from opale-ai/news-classifier: direct link, hf CLI and curl.
- Browser
- Download file 1 kB
-
https://huggingface.co/opale-ai/news-classifier/resolve/main/utils/embed.py
- Command line
-
hf download hf://opale-ai/news-classifier/utils/embed.py
-
curl -L -o embed.py https://huggingface.co/opale-ai/news-classifier/resolve/main/utils/embed.py
1 kB
| import os | |
| from functools import cache | |
| from itertools import batched | |
| from typing import Generator, Iterator | |
| import numpy as np | |
| from numpy.typing import NDArray | |
| from sentence_transformers import SentenceTransformer | |
| def split(text: str, max_tokens: int = 512) -> Generator[str, None, None]: | |
| # Naive approach - use opale internal chunking techniques (special tokens count) | |
| words = text.split() | |
| if not words: | |
| return | |
| for batch in batched(words, max_tokens // 2): # Assuming 2 tokens per word | |
| yield " ".join(batch) | |
| def get_model(): | |
| return SentenceTransformer( | |
| os.environ["EMBEDDING_MODEL"], revision=os.environ["EMBEDDING_MODEL_REV"] | |
| ) | |
| def embed(texts: Iterator[str], max_tokens: int = 512) -> NDArray: | |
| res: list[NDArray] = [] | |
| for text in texts: | |
| embeddings = get_model().encode( | |
| list(split(text, max_tokens)), show_progress_bar=False | |
| ) | |
| res.append(np.mean(embeddings, axis=0)) | |
| return np.array(res) | |