Update README.md
Browse files
README.md
CHANGED
|
@@ -5,7 +5,7 @@ metrics:
|
|
| 5 |
---
|
| 6 |
# BERT Text Classification Model
|
| 7 |
|
| 8 |
-
This is a simple
|
| 9 |
|
| 10 |
## Usage
|
| 11 |
|
|
@@ -16,4 +16,22 @@ text = "This is a positive review."
|
|
| 16 |
predicted_class = classify_text(text)
|
| 17 |
print("Predicted class:", predicted_class)
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
---
|
| 6 |
# BERT Text Classification Model
|
| 7 |
|
| 8 |
+
This is a simple model for text classification using BERT.
|
| 9 |
|
| 10 |
## Usage
|
| 11 |
|
|
|
|
| 16 |
predicted_class = classify_text(text)
|
| 17 |
print("Predicted class:", predicted_class)
|
| 18 |
|
| 19 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 20 |
+
|
| 21 |
+
# Load pre-trained BERT tokenizer and model
|
| 22 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
| 23 |
+
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
|
| 24 |
+
|
| 25 |
+
# Define a function to classify text
|
| 26 |
+
def classify_text(text):
|
| 27 |
+
inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True)
|
| 28 |
+
outputs = model(**inputs)
|
| 29 |
+
logits = outputs.logits
|
| 30 |
+
probabilities = logits.softmax(dim=1)
|
| 31 |
+
predicted_class = probabilities.argmax(dim=1).item()
|
| 32 |
+
return predicted_class
|
| 33 |
+
|
| 34 |
+
# Example usage
|
| 35 |
+
text = "This is a positive review."
|
| 36 |
+
predicted_class = classify_text(text)
|
| 37 |
+
print("Predicted class:", predicted_class)
|