- Find the notebook here: https://colab.research.google.com/drive/1pyQB5Olz5E24-wcfPvYDcOUmV3iUcYBW#scrollTo=6hvjXFJtahN8
labels = df_combined['label_text'].unique().tolist()
labels = [s.strip() for s in labels ]
NUM_LABELS= len(labels)
id2label={id:label for id,label in enumerate(labels)}
label2id={label:id for id,label in enumerate(labels)}
tokenizer = BertTokenizerFast.from_pretrained("bert-base-uncased", max_length=512)
model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=NUM_LABELS, id2label=id2label, label2id=label2id)
def predict(text):
"""
Predicts the class label for a given input text
Args:
text (str): The input text for which the class label needs to be predicted.
Returns:
probs (torch.Tensor): Class probabilities for the input text.
pred_label_idx (torch.Tensor): The index of the predicted class label.
pred_label (str): The predicted class label.
"""
# Tokenize the input text and move tensors to the GPU if available
inputs = tokenizer(text, padding=True, truncation=True, max_length=512, return_tensors="pt")
# Get model output (logits)
outputs = model(**inputs)
probs = outputs[0].softmax(1)
""" Explanation outputs: The BERT model returns a tuple containing the output logits (and possibly other elements depending on the model configuration). In this case, the output logits are the first element in the tuple, which is why we access it using outputs[0].
outputs[0]: This is a tensor containing the raw output logits for each class. The shape of the tensor is (batch_size, num_classes) where batch_size is the number of input samples (in this case, 1, as we are predicting for a single input text) and num_classes is the number of target classes.
softmax(1): The softmax function is applied along dimension 1 (the class dimension) to convert the raw logits into class probabilities. Softmax normalizes the logits so that they sum to 1, making them interpretable as probabilities. """
# Get the index of the class with the highest probability
# argmax() finds the index of the maximum value in the tensor along a specified dimension.
# By default, if no dimension is specified, it returns the index of the maximum value in the flattened tensor.
pred_label_idx = probs.argmax()
# print(probs)
# Now map the predicted class index to the actual class label
# Since pred_label_idx is a tensor containing a single value (the predicted class index),
# the .item() method is used to extract the value as a scalar
pred_label = model.config.id2label[pred_label_idx.item()]
return probs, pred_label_idx, pred_label
- How to infer the model in huggingface ?
model_path = "KN123/PromptClassifier-v1.0"
model = BertForSequenceClassification.from_pretrained(model_path)
tokenizer= BertTokenizerFast.from_pretrained(model_path)
nlp= pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
print(predict("set the color Red to items F-3333"))
(tensor([[0.9979, 0.0021]], grad_fn=<SoftmaxBackward0>), tensor(0), 'instruction_prompt')
nlp("Calculate the average attendance at each sports stadium.")
(tensor([[0.0038, 0.9962]], grad_fn=<SoftmaxBackward0>), tensor(1), 'sql_prompt')
- Downloads last month
- 5
This model does not have enough activity to be deployed to Inference API (serverless) yet. Increase its social
visibility and check back later, or deploy to Inference Endpoints (dedicated)
instead.