lgsilvaesilva's picture
Upload folder using huggingface_hub
8c950ad verified
|
Raw
History Blame Contribute Delete
11.7 kB
---
library_name: transformers
pipeline_tag: text-classification
base_model: FacebookAI/xlm-roberta-base
tags:
- text-classification
- binary-classification
- amis
- agriculture
language: multilingual
---
# AMIS Commodity Classifier
This model repository contains artifacts from an AMIS commodity relevance classifier training run.
It includes the Transformer model, any configured TF-IDF or sentence-embedding baselines, prediction files, and the training report.
- Dataset: `faodl/amis-agri-rice`
- Dataset subset: ``
- Dataset revision: `main`
- Text column: `chunk_text`
- Label column: `label`
- Transformer: `FacebookAI/xlm-roberta-base`
- Generated at: `2026-06-08T17:42:20.320378+00:00`
## Dataset Summary
| Split | Rows | Label 0 | Label 1 | Unique groups | Mean text length |
| --- | ---: | ---: | ---: | ---: | ---: |
| train | 4823 | 3873 | 950 | 2128 | 711.6 |
| validation | 1071 | 846 | 225 | 456 | 702.2 |
| test | 954 | 772 | 182 | 457 | 694.3 |
## Threshold Comparison on Validation Split
Validation metrics document threshold selection and tuning behavior; test metrics remain the primary estimate of out-of-sample performance.
| Model | Threshold | Accuracy | Precision | Recall | F1 | ROC AUC | Average precision |
| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| logistic_tfidf | 0.500 | 0.948 | 0.882 | 0.867 | 0.874 | 0.958 | 0.926 |
| logistic_tfidf | 0.517 | 0.951 | 0.899 | 0.867 | 0.882 | 0.958 | 0.926 |
| xgboost_tfidf | 0.500 | 0.957 | 0.891 | 0.907 | 0.899 | 0.975 | 0.947 |
| xgboost_tfidf | 0.522 | 0.957 | 0.891 | 0.907 | 0.899 | 0.975 | 0.947 |
| embedding-logistic_sentence_embeddings | 0.500 | 0.945 | 0.827 | 0.933 | 0.877 | 0.978 | 0.910 |
| embedding-logistic_sentence_embeddings | 0.617 | 0.954 | 0.879 | 0.907 | 0.893 | 0.978 | 0.910 |
| embedding-svm_sentence_embeddings | 0.500 | 0.952 | 0.914 | 0.853 | 0.883 | 0.977 | 0.908 |
| embedding-svm_sentence_embeddings | 0.496 | 0.953 | 0.915 | 0.858 | 0.885 | 0.977 | 0.908 |
| embedding-lightgbm_sentence_embeddings | 0.500 | 0.947 | 0.900 | 0.840 | 0.869 | 0.979 | 0.922 |
| embedding-lightgbm_sentence_embeddings | 0.037 | 0.953 | 0.866 | 0.920 | 0.892 | 0.979 | 0.922 |
| transformer | 0.500 | 0.960 | 0.889 | 0.924 | 0.906 | 0.977 | 0.918 |
| transformer | 0.966 | 0.962 | 0.904 | 0.916 | 0.909 | 0.977 | 0.918 |
## Threshold Comparison on Test Split
| Model | Threshold | Accuracy | Precision | Recall | F1 | ROC AUC | Average precision |
| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| logistic_tfidf | 0.500 | 0.942 | 0.829 | 0.879 | 0.853 | 0.963 | 0.893 |
| logistic_tfidf | 0.517 | 0.942 | 0.832 | 0.874 | 0.853 | 0.963 | 0.893 |
| xgboost_tfidf | 0.500 | 0.950 | 0.872 | 0.863 | 0.867 | 0.980 | 0.936 |
| xgboost_tfidf | 0.522 | 0.951 | 0.881 | 0.857 | 0.869 | 0.980 | 0.936 |
| embedding-logistic_sentence_embeddings | 0.500 | 0.954 | 0.832 | 0.951 | 0.887 | 0.982 | 0.930 |
| embedding-logistic_sentence_embeddings | 0.617 | 0.954 | 0.845 | 0.929 | 0.885 | 0.982 | 0.930 |
| embedding-svm_sentence_embeddings | 0.500 | 0.950 | 0.872 | 0.863 | 0.867 | 0.983 | 0.938 |
| embedding-svm_sentence_embeddings | 0.496 | 0.950 | 0.872 | 0.863 | 0.867 | 0.983 | 0.938 |
| embedding-lightgbm_sentence_embeddings | 0.500 | 0.953 | 0.878 | 0.874 | 0.876 | 0.980 | 0.935 |
| embedding-lightgbm_sentence_embeddings | 0.037 | 0.948 | 0.814 | 0.940 | 0.872 | 0.980 | 0.935 |
| transformer | 0.500 | 0.962 | 0.876 | 0.934 | 0.904 | 0.990 | 0.972 |
| transformer | 0.966 | 0.961 | 0.901 | 0.896 | 0.898 | 0.990 | 0.972 |
## Confusion Matrices on Test Split
Rows are true labels and columns are predicted labels.
### logistic_tfidf at threshold 0.500
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 739 | 33 |
| RELEVANT | 22 | 160 |
### logistic_tfidf at threshold 0.517
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 740 | 32 |
| RELEVANT | 23 | 159 |
### xgboost_tfidf at threshold 0.500
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 749 | 23 |
| RELEVANT | 25 | 157 |
### xgboost_tfidf at threshold 0.522
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 751 | 21 |
| RELEVANT | 26 | 156 |
### embedding-logistic_sentence_embeddings at threshold 0.500
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 737 | 35 |
| RELEVANT | 9 | 173 |
### embedding-logistic_sentence_embeddings at threshold 0.617
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 741 | 31 |
| RELEVANT | 13 | 169 |
### embedding-svm_sentence_embeddings at threshold 0.500
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 749 | 23 |
| RELEVANT | 25 | 157 |
### embedding-svm_sentence_embeddings at threshold 0.496
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 749 | 23 |
| RELEVANT | 25 | 157 |
### embedding-lightgbm_sentence_embeddings at threshold 0.500
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 750 | 22 |
| RELEVANT | 23 | 159 |
### embedding-lightgbm_sentence_embeddings at threshold 0.037
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 733 | 39 |
| RELEVANT | 11 | 171 |
### transformer at threshold 0.500
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 748 | 24 |
| RELEVANT | 12 | 170 |
### transformer at threshold 0.966
| True / Predicted | NOT_RELEVANT | RELEVANT |
| --- | ---: | ---: |
| NOT_RELEVANT | 754 | 18 |
| RELEVANT | 19 | 163 |
## Validation-Tuned Thresholds
- `logistic_tfidf`: threshold `0.517` (validation F1 `0.882`); test F1 change vs 0.5: `-0.001`.
- `xgboost_tfidf`: threshold `0.522` (validation F1 `0.899`); test F1 change vs 0.5: `+0.002`.
- `embedding-logistic_sentence_embeddings`: threshold `0.617` (validation F1 `0.893`); test F1 change vs 0.5: `-0.002`.
- `embedding-svm_sentence_embeddings`: threshold `0.496` (validation F1 `0.885`); test F1 change vs 0.5: `+0.000`.
- `embedding-lightgbm_sentence_embeddings`: threshold `0.037` (validation F1 `0.892`); test F1 change vs 0.5: `-0.004`.
- `transformer`: threshold `0.966` (validation F1 `0.909`); test F1 change vs 0.5: `-0.006`.
## Artifacts
- `logistic_tfidf`: `/content/agri-rice-classifier/baselines/logistic`
- `xgboost_tfidf`: `/content/agri-rice-classifier/baselines/xgboost`
- `embedding-logistic_sentence_embeddings`: `/content/agri-rice-classifier/baselines/embedding-logistic`
- `embedding-svm_sentence_embeddings`: `/content/agri-rice-classifier/baselines/embedding-svm`
- `embedding-lightgbm_sentence_embeddings`: `/content/agri-rice-classifier/baselines/embedding-lightgbm`
- `transformer`: `/content/agri-rice-classifier/transformer`
## Inference
Install the runtime dependencies:
```bash
pip install transformers torch huggingface_hub pandas joblib scikit-learn xgboost lightgbm
```
### Transformer
```python
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
MODEL_ID = "faodl/agri-rice-classifier"
texts = [
"Rice export prices increased after new procurement rules were announced.",
"The finance ministry released its monthly fuel tax bulletin.",
]
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, subfolder="transformer")
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID, subfolder="transformer")
threshold = float(getattr(model.config, "threshold", 0.5))
encoded = tokenizer(
texts,
truncation=True,
padding=True,
max_length=256,
return_tensors="pt",
)
with torch.no_grad():
logits = model(**encoded).logits
probabilities = torch.softmax(logits, dim=-1)[:, 1].tolist()
for text, probability in zip(texts, probabilities):
label = model.config.id2label[int(probability >= threshold)]
print({"text": text, "probability_positive": probability, "label": label})
```
### TF-IDF Baselines
Available baseline names in this run: "logistic", "xgboost".
```python
import json
import joblib
from huggingface_hub import hf_hub_download
MODEL_ID = "faodl/agri-rice-classifier"
BASELINE = "logistic"
texts = [
"Maize production forecasts were revised after delayed rains.",
"The central bank published new exchange rate statistics.",
]
model_path = hf_hub_download(
repo_id=MODEL_ID,
repo_type="model",
filename=f"baselines/{BASELINE}/{BASELINE}_tfidf.joblib",
)
report_path = hf_hub_download(
repo_id=MODEL_ID,
repo_type="model",
filename="report.json",
)
pipeline = joblib.load(model_path)
with open(report_path, encoding="utf-8") as handle:
report = json.load(handle)
threshold = next(
result["validation_best_threshold"]["threshold"]
for result in report["results"]
if result["model_type"] == f"{BASELINE}_tfidf"
)
probabilities = pipeline.predict_proba(texts)[:, 1]
for text, probability in zip(texts, probabilities):
label = "RELEVANT" if probability >= threshold else "NOT_RELEVANT"
print({"text": text, "probability_positive": float(probability), "label": label})
```
### Sentence-Embedding Baselines
Available embedding baseline names in this run: "embedding-logistic", "embedding-svm", "embedding-lightgbm".
```python
import joblib
import torch
from huggingface_hub import hf_hub_download
from transformers import AutoModel, AutoTokenizer
MODEL_ID = "faodl/agri-rice-classifier"
BASELINE = "embedding-logistic"
texts = [
"Wheat export inspections rose as demand from importers increased.",
"The sports ministry announced a new stadium renovation plan.",
]
model_path = hf_hub_download(
repo_id=MODEL_ID,
repo_type="model",
filename=f"baselines/{BASELINE}/{BASELINE}.joblib",
)
artifact = joblib.load(model_path)
tokenizer = AutoTokenizer.from_pretrained(artifact["embedding_model_name"])
encoder = AutoModel.from_pretrained(artifact["embedding_model_name"])
encoder.eval()
encoded_batches = []
batch_size = artifact.get("embedding_batch_size", 64)
for start in range(0, len(texts), batch_size):
batch_texts = texts[start : start + batch_size]
inputs = tokenizer(
batch_texts,
padding=True,
truncation=True,
max_length=artifact.get("embedding_max_length", 256),
return_tensors="pt",
)
with torch.no_grad():
outputs = encoder(**inputs)
token_embeddings = outputs.last_hidden_state
attention_mask = inputs["attention_mask"].unsqueeze(-1).to(token_embeddings.dtype)
embeddings = (token_embeddings * attention_mask).sum(dim=1)
embeddings = embeddings / attention_mask.sum(dim=1).clamp(min=1e-9)
if artifact.get("normalize_embeddings", True):
embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1)
encoded_batches.append(embeddings)
embeddings = torch.cat(encoded_batches).numpy()
probabilities = artifact["classifier"].predict_proba(embeddings)[:, 1]
threshold = artifact["validation_best_threshold"]["threshold"]
for text, probability in zip(texts, probabilities):
label = "RELEVANT" if probability >= threshold else "NOT_RELEVANT"
print({"text": text, "probability_positive": float(probability), "label": label})
```
## Files
- `REPORT.md`: Markdown report for this training run.
- `report.json`: Machine-readable report containing metrics and thresholds.
- `transformer/`: Fine-tuned Transformer artifacts, when Transformer training is enabled.
- `baselines/`: TF-IDF and sentence-embedding baseline artifacts, when baseline training is enabled.
- `*/validation_predictions.csv` and `*/test_predictions.csv`: Split-level predictions.