File size: 1,577 Bytes
256edf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

from datasets import load_dataset
from transformers import BertTokenizer, BertForSequenceClassification, Trainer, TrainingArguments
from sklearn.metrics import classification_report
import torch

# Load few-shot dataset
dataset = load_dataset("ai4bharat/sangraha")
train_data = dataset["train"].select(range(30))
test_data = dataset["validation"].select(range(10))

# Tokenizer and preprocessing
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")

def tokenize(example):
    return tokenizer(example["context"], padding="max_length", truncation=True)

def encode_label(example):
    example["label"] = 1 if "bank" in example["context"].lower() else 0
    return example

train_data = train_data.map(tokenize).map(encode_label)
test_data = test_data.map(tokenize).map(encode_label)

train_data.set_format("torch", columns=["input_ids", "attention_mask", "label"])
test_data.set_format("torch", columns=["input_ids", "attention_mask", "label"])

# Model and trainer
model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)

training_args = TrainingArguments(
    output_dir="./results",
    num_train_epochs=3,
    per_device_train_batch_size=4,
    per_device_eval_batch_size=4,
    evaluation_strategy="epoch",
    logging_dir="./logs"
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_data,
    eval_dataset=test_data,
)

trainer.train()
metrics = trainer.evaluate()

predictions = trainer.predict(test_data).predictions.argmax(-1)
print(classification_report(test_data["label"], predictions))