Update README.md
Browse files
README.md
CHANGED
@@ -74,44 +74,74 @@ Use the code below to get started with the model for product classification:
|
|
74 |
import torch
|
75 |
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
76 |
|
77 |
-
#
|
78 |
-
model_name
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
model
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
#
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
# Training Details
|
117 |
## Training Data
|
|
|
74 |
import torch
|
75 |
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
76 |
|
77 |
+
# Load the model and tokenizer from the Hugging Face Hub
|
78 |
+
def load_model_and_tokenizer(model_name, num_labels):
|
79 |
+
tokenizer = DistilBertTokenizer.from_pretrained(model_name)
|
80 |
+
model = DistilBertForSequenceClassification.from_pretrained(model_name, num_labels=num_labels)
|
81 |
+
model.eval() # Set the model to evaluation mode
|
82 |
+
return model, tokenizer
|
83 |
+
|
84 |
+
# Predict categories for the provided prompts
|
85 |
+
def predict(model, tokenizer, prompts, category_mapping, device):
|
86 |
+
model.to(device)
|
87 |
+
inputs = tokenizer(prompts, padding=True, truncation=True, return_tensors='pt', max_length=128)
|
88 |
+
|
89 |
+
with torch.no_grad():
|
90 |
+
input_ids = inputs['input_ids'].to(device)
|
91 |
+
attention_mask = inputs['attention_mask'].to(device)
|
92 |
+
|
93 |
+
outputs = model(input_ids, attention_mask=attention_mask)
|
94 |
+
logits = outputs.logits
|
95 |
+
predictions = torch.argmax(logits, dim=1).cpu().numpy()
|
96 |
+
|
97 |
+
predicted_categories = [category_mapping[pred] for pred in predictions]
|
98 |
+
return predicted_categories
|
99 |
+
|
100 |
+
# Main execution block
|
101 |
+
if __name__ == "__main__":
|
102 |
+
# Define some example prompts for prediction
|
103 |
+
prompts = [
|
104 |
+
"Intel Core i7 CPU",
|
105 |
+
"Nikon D3500 Digital Camera",
|
106 |
+
"Bosch Series 6 Dishwasher",
|
107 |
+
"Samsung 32 inch Smart TV",
|
108 |
+
"Apple iPhone 13"
|
109 |
+
]
|
110 |
+
|
111 |
+
# Create the category mapping based on provided comments
|
112 |
+
category_mapping = {
|
113 |
+
0: 'cpus',
|
114 |
+
1: 'digital cameras',
|
115 |
+
2: 'dishwashers',
|
116 |
+
3: 'fridge freezers',
|
117 |
+
4: 'microwaves',
|
118 |
+
5: 'mobile phones',
|
119 |
+
6: 'tvs',
|
120 |
+
7: 'washing machines'
|
121 |
+
}
|
122 |
+
|
123 |
+
model_name = 'Adnan-AI-Labs/DistilBERT-ProductClassifier'
|
124 |
+
|
125 |
+
# Load the model and tokenizer
|
126 |
+
print(f"Loading model and tokenizer from Hugging Face Hub: {model_name}")
|
127 |
+
model, tokenizer = load_model_and_tokenizer(model_name, len(category_mapping))
|
128 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
129 |
+
|
130 |
+
# Make predictions
|
131 |
+
predicted_categories = predict(model, tokenizer, prompts, category_mapping, device)
|
132 |
+
|
133 |
+
# Display the predictions
|
134 |
+
for prompt, category in zip(prompts, predicted_categories):
|
135 |
+
print(f"Prompt: '{prompt}' | Predicted Category: '{category}'")
|
136 |
|
137 |
```
|
138 |
+
## Output
|
139 |
+
Loading model and tokenizer from Hugging Face Hub: Adnan-AI-Labs/DistilBERT-ProductClassifier
|
140 |
+
Prompt: 'Intel Core i7 CPU' | Predicted Category: 'cpus'
|
141 |
+
Prompt: 'Nikon D3500 Digital Camera' | Predicted Category: 'digital cameras'
|
142 |
+
Prompt: 'Bosch Series 6 Dishwasher' | Predicted Category: 'dishwashers'
|
143 |
+
Prompt: 'Samsung 32 inch Smart TV' | Predicted Category: 'tvs'
|
144 |
+
Prompt: 'Apple iPhone 13' | Predicted Category: 'mobile phones'
|
145 |
|
146 |
# Training Details
|
147 |
## Training Data
|