adnanaman commited on
Commit
6752214
1 Parent(s): 6a840ea

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +66 -36
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
- # Define the model repository name
78
- model_name = "Adnan-AI-Labs/DistilBERT-ProductClassifier"
79
-
80
- # Load the tokenizer and model from the Hugging Face Hub
81
- try:
82
- # Load tokenizer
83
- tokenizer = DistilBertTokenizer.from_pretrained(model_name, use_fast=True)
84
-
85
- # Load model, forcing the download to avoid any cached version
86
- model = DistilBertForSequenceClassification.from_pretrained(model_name)
87
-
88
- print("Model and tokenizer loaded successfully.")
89
-
90
- except Exception as e:
91
- print(f"An error occurred while loading the model: {e}")
92
- exit()
93
-
94
- # Test the model with some sample inputs
95
- sample_texts = [
96
- "estar s20 single uk sim free mobile phone red",
97
- "cello c40227dvbt2 40 full hd black led tv",
98
- ]
99
-
100
- # Prepare the inputs for the model
101
- inputs = tokenizer(sample_texts, padding=True, truncation=True, return_tensors="pt")
102
-
103
- # Make predictions
104
- with torch.no_grad():
105
- outputs = model(**inputs)
106
-
107
- # Get the predicted class indices
108
- predictions = torch.argmax(outputs.logits, dim=1)
109
-
110
- # Print out the predictions
111
- for text, pred in zip(sample_texts, predictions):
112
- print(f"Text: {text} \nPredicted Class: {pred.item()}\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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