Update README.md
Browse fileshow to call model
README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
---
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# Load model and tokenizer from Hugging Face Hub
|
| 9 |
+
repo_id = "amelkhoadry/optimized-bert-model"
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(repo_id)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 12 |
+
|
| 13 |
+
# Load your test dataset (replace 'your_dataset'/'test' with your actual dataset and split)
|
| 14 |
+
# Example uses the 'imdb' dataset as a placeholder. Replace as needed.
|
| 15 |
+
test_dataset = load_dataset("imdb", split="test")
|
| 16 |
+
|
| 17 |
+
# Preprocess test data using the tokenizer
|
| 18 |
+
def preprocess(example):
|
| 19 |
+
return tokenizer(
|
| 20 |
+
example["text"],
|
| 21 |
+
truncation=True,
|
| 22 |
+
padding='max_length',
|
| 23 |
+
max_length=128
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
test_dataset = test_dataset.map(preprocess, batched=True)
|
| 27 |
+
test_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'label'])
|
| 28 |
+
|
| 29 |
+
# Predict on test dataset
|
| 30 |
+
model.eval()
|
| 31 |
+
predictions = []
|
| 32 |
+
labels = []
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
for batch in torch.utils.data.DataLoader(test_dataset, batch_size=32):
|
| 35 |
+
input_ids = batch["input_ids"]
|
| 36 |
+
attention_mask = batch["attention_mask"]
|
| 37 |
+
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 38 |
+
logits = outputs.logits
|
| 39 |
+
preds = torch.argmax(logits, dim=1)
|
| 40 |
+
predictions.extend(preds.tolist())
|
| 41 |
+
labels.extend(batch["label"].tolist())
|
| 42 |
+
|
| 43 |
+
# Example: print the first 10 predictions and labels
|
| 44 |
+
print("First 10 predictions:", predictions[:10])
|
| 45 |
+
print("First 10 actual labels:", labels[:10])
|
| 46 |
+
|
| 47 |
+
# If you want to compute accuracy:
|
| 48 |
+
accuracy = sum([int(p == l) for p, l in zip(predictions, labels)]) / len(labels)
|
| 49 |
+
print(f"Test Accuracy: {accuracy:.4f}")
|