Views
No views yet
| Feature | Details |
|---|---|
| Architecture | PyTorch feed-forward neural network (1 hidden layer, ReLU, dropout 0.3) |
| Vectorizer | CountVectorizer (1- & 2-grams, top 20 000 features) |
| Input | Product + brand string (e.g. "Iogurte natural Danone") |
| Output | ECOICOP category name (71 classes) |
| Language | Portuguese (PT-PT) |
| Metric | Value |
|---|---|
| Accuracy | 0.97 |
| Macro F1 | 0.92 |
pip install torch joblib scikit-learn huggingface_hub1import torch, joblib, torch.nn as nn
2from huggingface_hub import hf_hub_download
3
4# ── Same architecture used during training ──
5class SimpleCNN(nn.Module):
6 def __init__(self, input_dim, num_classes):
7 super().__init__()
8 self.fc1 = nn.Linear(input_dim, 512)
9 self.relu = nn.ReLU()
10 self.dropout = nn.Dropout(0.3)
11 self.fc2 = nn.Linear(512, num_classes)
12 def forward(self, x):
13 x = self.fc1(x)
14 x = self.relu(x)
15 x = self.dropout(x)
16 return self.fc2(x)
17
18repo_id = "julisses/ecoicop-pt-cnn"
19
20# Download vectorizer, label encoder and weights
21vectorizer = joblib.load(hf_hub_download(repo_id, "vectorizer.pkl"))
22label_encoder = joblib.load(hf_hub_download(repo_id, "label_encoder.pkl"))
23state_dict = torch.load(hf_hub_download(repo_id, "model.pt"), map_location="cpu")
24
25# Re-create and load the network
26model = SimpleCNN(len(vectorizer.get_feature_names_out()), len(label_encoder.classes_))
27model.load_state_dict(state_dict)
28model.eval()
29
30def predict_ecoicop(text: str) -> str:
31 X = vectorizer.transform([text]).toarray()
32 with torch.no_grad():
33 idx = model(torch.tensor(X, dtype=torch.float32)).argmax(1).item()
34 return label_encoder.inverse_transform([idx])[0]
35
36# Example
37print(predict_ecoicop("Queijo Mini Original, Cheddar e Emmental Babybel"))
38# ➜ Queijo e requeijão