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.
Validation metrics document threshold selection and tuning behavior; test metrics remain the primary estimate of out-of-sample performance.
Rows are true labels and columns are predicted labels.
1import torch
2from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
4MODEL_ID = "faodl/agri-utilization-classifier"
5
6texts = [
7 "Rice export prices increased after new procurement rules were announced.",
8 "The finance ministry released its monthly fuel tax bulletin.",
9]
10
11tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, subfolder="transformer")
12model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID, subfolder="transformer")
13threshold = float(getattr(model.config, "threshold", 0.5))
14
15encoded = tokenizer(
16 texts,
17 truncation=True,
18 padding=True,
19 max_length=256,
20 return_tensors="pt",
21)
22
23with torch.no_grad():
24 logits = model(**encoded).logits
25 probabilities = torch.softmax(logits, dim=-1)[:, 1].tolist()
26
27for text, probability in zip(texts, probabilities):
28 label = model.config.id2label[int(probability >= threshold)]
29 print({"text": text, "probability_positive": probability, "label": label})
Available baseline names in this run: "logistic", "xgboost".
1import json
2import joblib
3from huggingface_hub import hf_hub_download
4
5MODEL_ID = "faodl/agri-utilization-classifier"
6BASELINE = "logistic"
7
8texts = [
9 "Maize production forecasts were revised after delayed rains.",
10 "The central bank published new exchange rate statistics.",
11]
12
13model_path = hf_hub_download(
14 repo_id=MODEL_ID,
15 repo_type="model",
16 filename=f"baselines/{BASELINE}/{BASELINE}_tfidf.joblib",
17)
18report_path = hf_hub_download(
19 repo_id=MODEL_ID,
20 repo_type="model",
21 filename="report.json",
22)
23
24pipeline = joblib.load(model_path)
25with open(report_path, encoding="utf-8") as handle:
26 report = json.load(handle)
27
28threshold = next(
29 result["validation_best_threshold"]["threshold"]
30 for result in report["results"]
31 if result["model_type"] == f"{BASELINE}_tfidf"
32)
33
34probabilities = pipeline.predict_proba(texts)[:, 1]
35for text, probability in zip(texts, probabilities):
36 label = "RELEVANT" if probability >= threshold else "NOT_RELEVANT"
37 print({"text": text, "probability_positive": float(probability), "label": label})
Available embedding baseline names in this run: "embedding-logistic", "embedding-svm", "embedding-lightgbm".
1import joblib
2import torch
3from huggingface_hub import hf_hub_download
4from transformers import AutoModel, AutoTokenizer
5
6MODEL_ID = "faodl/agri-utilization-classifier"
7BASELINE = "embedding-logistic"
8
9texts = [
10 "Wheat export inspections rose as demand from importers increased.",
11 "The sports ministry announced a new stadium renovation plan.",
12]
13
14model_path = hf_hub_download(
15 repo_id=MODEL_ID,
16 repo_type="model",
17 filename=f"baselines/{BASELINE}/{BASELINE}.joblib",
18)
19artifact = joblib.load(model_path)
20tokenizer = AutoTokenizer.from_pretrained(artifact["embedding_model_name"])
21encoder = AutoModel.from_pretrained(artifact["embedding_model_name"])
22encoder.eval()
23
24encoded_batches = []
25batch_size = artifact.get("embedding_batch_size", 64)
26for start in range(0, len(texts), batch_size):
27 batch_texts = texts[start : start + batch_size]
28 inputs = tokenizer(
29 batch_texts,
30 padding=True,
31 truncation=True,
32 max_length=artifact.get("embedding_max_length", 256),
33 return_tensors="pt",
34 )
35 with torch.no_grad():
36 outputs = encoder(**inputs)
37 token_embeddings = outputs.last_hidden_state
38 attention_mask = inputs["attention_mask"].unsqueeze(-1).to(token_embeddings.dtype)
39 embeddings = (token_embeddings * attention_mask).sum(dim=1)
40 embeddings = embeddings / attention_mask.sum(dim=1).clamp(min=1e-9)
41 if artifact.get("normalize_embeddings", True):
42 embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1)
43 encoded_batches.append(embeddings)
44embeddings = torch.cat(encoded_batches).numpy()
45probabilities = artifact["classifier"].predict_proba(embeddings)[:, 1]
46threshold = artifact["validation_best_threshold"]["threshold"]
47
48for text, probability in zip(texts, probabilities):
49 label = "RELEVANT" if probability >= threshold else "NOT_RELEVANT"
50 print({"text": text, "probability_positive": float(probability), "label": label})