ShurongSR/pet-food-recall-risk-classifier
Pet Food Recall Risk Classifier
A multi-label text classifier that assigns risk categories to pet food recall and safety-alert records.
Built as an academic assignment for an Information Retrieval course.
Model Description
This model combines a frozen sentence transformer encoder with a trained sklearn classifier head:
Recall text
→ frozen SentenceTransformer (sentence-transformers/all-MiniLM-L6-v2)
→ fixed-size embedding (dim 384)
→ OneVsRestClassifier(LogisticRegression) ← this repository
→ predicted risk labelsThe transformer encoder (sentence-transformers/all-MiniLM-L6-v2) is not fine-tuned and is not included in this repository. It is loaded at inference time via the sentence-transformers library. Only the trained classifier head (classifier.joblib), per-label thresholds (thresholds.json), and label metadata (label_columns.json) are stored here.
Task
Supervised multi-label text classification.
Given a structured text constructed from brand name, product description, and recall reason, the model predicts one or more of three risk categories. A record may carry a single label or multiple labels when several risk types are described.
Input Format
Build the input string using this template before encoding:
Brand: {brand_names}. Product: {product_description}. Recall reason: {recall_reason_description}.Example:
Brand: Example Brand. Product: Dry dog food. Recall reason: May be contaminated with Salmonella.Output Labels
Label order used in all arrays: label_columns.json.
Per-Label Prediction Thresholds
Thresholds were tuned on the validation split and are stored in thresholds.json.
If no label clears its threshold, the label with the highest probability score is returned as a low-confidence fallback.
Usage
import json
import joblib
import numpy as np
from sentence_transformers import SentenceTransformer
# Load artifacts
clf = joblib.load("classifier.joblib")
thresholds = json.load(open("thresholds.json"))["thresholds"]
labels = json.load(open("label_columns.json"))
# Build input text
text = (
"Brand: Example Brand. "
"Product: Dry dog food. "
"Recall reason: May be contaminated with Salmonella."
)
# Encode with frozen transformer
encoder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
embedding = encoder.encode([text]) # shape (1, 384)
# Predict
proba = clf.predict_proba(embedding)[0] # shape (3,)
predicted = [l for i, l in enumerate(labels) if proba[i] >= thresholds[l]]
low_confidence = False
if not predicted:
predicted = [labels[int(np.argmax(proba))]]
low_confidence = True
print(predicted, low_confidence)The src/predict.py script in the source repository wraps this pipeline and outputs clean JSON.
Validation Results (model selection)
Four classifiers were trained and compared on the validation split (15 examples). Logistic Regression was selected because it achieved the best validation macro F1 while also supporting predict_proba and per-label threshold tuning.
Final Test Results
The selected model was evaluated once on the held-out test set (15 examples). No training, threshold tuning, or model selection changes were made after inspecting test results.
Overall
Per-Label
The test set contains only 15 rows, so metrics are sensitive to 1–2 examples. Results should be interpreted as assignment-scale evidence, not robust production performance estimates.
Training Data
103 labeled records from official public pet food recall and safety-alert portals (FDA, EU RASFF, UK FSA, Canada CFIA, openFDA). Labels were assigned using transparent rule-based keyword patterns. 14 uncertain records were excluded rather than forced into a label.
See the companion dataset repository for full split details and source attribution.
Repository Contents
Limitations
- Small training set — 73 training rows; model may not generalise beyond this vocabulary and source mix.
- Small test set — 15 rows; per-label metrics are sensitive to 1–2 examples.
- Rule-based labels — not manually annotated by domain experts.
- Consolidated taxonomy — three labels combine six more specific risk types.
- Source heterogeneity — records from five sources with different terminology.
- Frozen encoder — the transformer is not fine-tuned on recall text.
- Not a safety authority tool — the model predicts text categories, not product safety.
Educational Disclaimer
This model is for educational purposes only.
It does not provide veterinary advice, legal advice, product safety certification, official recall interpretation, or medical or nutritional recommendations.
Always consult official recall notices and qualified professionals for authoritative information about pet food safety.
