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90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python
# coding: utf-8

# 
# # Exercise 3: Fine-Tuning Pretrained Transformer on Text Classification Task
# 
# In this lab, you will apply the concepts learned by fine-tuning a pre-trained Transformer model on a text classification task using the Hugging Face `transformers` library.
# 
# ## Objectives:
# - Learn to load a pre-trained model from Hugging Face.
# - Fine-tune the model on a text classification dataset.
# - Evaluate and save the fine-tuned model.
# 

# ## Installing Necessary Libraries
# 
# In this lab, we will use the following Python libraries:
# 
# - **Transformers**: Hugging Face's `transformers` library provides pre-trained models for various Natural Language Processing (NLP) tasks. We will use this to load and fine-tune a pre-trained transformer model.
#   
# - **Datasets**: Hugging Face's `datasets` library allows easy access to a wide range of datasets and provides tools for efficient data processing.
#   
# - **Scikit-learn**: A widely-used library for machine learning, which includes tools for metrics, evaluation, and preprocessing. In this lab, we'll use it to calculate evaluation metrics such as accuracy, precision, recall, and F1 score.
# 

# In[1]:


# Install necessary libraries


# # Setup and Import Libraries
# 
# In this section, we import all the necessary libraries for our IMDb sentiment analysis lab.
# 
# - **os, random, numpy, torch**: For system operations, reproducibility, and PyTorch support.
# - **datasets**: To load the IMDb dataset from Hugging Face.
# - **transformers**: Includes tokenizer, model, training utilities, data collator, and callbacks for early stopping.
# 

# In[2]:


# Set random seeds for reproducibility
import os
import random
import numpy as np
import torch

SEED = 42
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
if torch.cuda.is_available():
    torch.cuda.manual_seed_all(SEED)

# Import dataset and transformer utilities
from datasets import load_dataset
from transformers import (
    AutoTokenizer,
    AutoModelForSequenceClassification,
    DataCollatorWithPadding,
    TrainingArguments,
    Trainer,
    EarlyStoppingCallback
)


USE_MPS = torch.backends.mps.is_available()
device = torch.device("mps" if USE_MPS else "cpu")
print("Using device:", device)


# # Load IMDb Dataset
# 
# We load the IMDb movie review dataset from Hugging Face:
# 
# - The dataset contains **25,000 training examples** and **25,000 test examples**.
# - Each example has a `text` field (the movie review) and a `label` field (`0` = negative, `1` = positive).
# - We print the dataset to verify its structure.
# 

# In[3]:


# Load the ag_news dataset
raw = load_dataset("SetFit/ag_news")
print(raw)


# # Tokenization and Dataset Preparation
# 
# In this section, we:
# 
# 1. Load the pre-trained BERT tokenizer (`bert-base-uncased`).
# 2. Tokenize the IMDb text data with truncation to a maximum sequence length of 128 tokens.
# 3. Remove the original text column to ensure Trainer only works with tensor inputs.
# 4. Set the dataset format to PyTorch tensors for compatibility with the Trainer.
# 5. Split the training set to create a validation set (5,000 examples) for monitoring validation loss during fine-tuning.
# 

# In[4]:


# Load BERT tokenizer
MODEL_NAME = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)

# Tokenization function
def tokenize_fn(examples):
    return tokenizer(examples["text"], truncation=True, max_length=128)

cols_to_remove = [c for c in raw["train"].column_names if c not in ("label",)]

# Apply tokenization to the dataset
tokenized = raw.map(tokenize_fn, batched=True, remove_columns=cols_to_remove)


# Remove original text column to avoid issues during batching
if "text" in tokenized["train"].column_names:
    tokenized = tokenized.remove_columns(["text"])

# Set dataset format to PyTorch tensors
tokenized.set_format("torch")

# Shuffle and split the training dataset to create a validation set
train_dataset = tokenized["train"].shuffle(seed=SEED)
val_split = train_dataset.train_test_split(test_size=5000, seed=SEED)
train_dataset = val_split["train"]
eval_dataset = val_split["test"]


# After tokenization, column removal, and setting the dataset format to PyTorch tensors, the training dataset looks like this:
# 
# - **Number of examples:** 20,000 (after splitting 5,000 examples for validation)
# - **Features:**
#   - `label` – the target sentiment (0 = negative, 1 = positive)
#   - `input_ids` – numerical token IDs representing the words/subwords of each sentence
#   - `token_type_ids` – segment IDs used by BERT to distinguish sentences (for single-sentence tasks, usually all zeros)
#   - `attention_mask` – indicates which tokens are real (1) and which are padding (0), allowing the model to ignore padding during self-attention
# 

# In[5]:


print(train_dataset)


# # Model Initialization and Data Collator
# 
# In this section, we:
# 
# 1. Load a pre-trained BERT model (`bert-base-uncased`) for sequence classification with 2 output labels (positive and negative sentiment).
# 2. Set up a `DataCollatorWithPadding` to dynamically pad batches during training and evaluation.
# 

# In[6]:


# Load pre-trained BERT model for sequence classification
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=4)

# Create a data collator that dynamically pads input sequences in each batch
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)


# # Metrics Calculation Using Scikit-Learn
# 
# In this section, we define a function to compute evaluation metrics for the model using **scikit-learn**:
# 
# - **Accuracy**: Fraction of correctly predicted examples.
# - **F1 Score**: Harmonic mean of precision and recall for binary classification.
# 
# Using scikit-learn simplifies metric computation and ensures correctness.
# 

# In[7]:


from sklearn.metrics import accuracy_score, f1_score
import numpy as np

# Define a metrics computation function using scikit-learn
def compute_metrics(eval_pred):
    logits, labels = eval_pred
    # Convert logits to predicted class indices
    preds = np.argmax(logits, axis=-1)

    # Compute accuracy and F1 score using scikit-learn
    acc = accuracy_score(labels, preds)
    f1 = f1_score(labels, preds, average='macro')

    return {"accuracy": acc, "f1_macro": f1}


# # Training Setup with Trainer and Early Stopping
# 
# In this section, we configure the training process:
# 
# 1. **TrainingArguments**:
#    - Sets output directory, batch sizes, number of epochs, learning rate, weight decay, warmup steps, and other training hyperparameters.
#    - Enables evaluation and checkpoint saving at the end of each epoch.
#    - Loads the best model at the end based on evaluation loss.
#    - Uses mixed-precision (fp16) if a GPU is available.
# 
# 2. **Trainer**:
#    - Combines the model, datasets, tokenizer, data collator, and metrics function.
#    - Includes `EarlyStoppingCallback` to stop training if validation loss does not improve for 2 evaluation steps (epochs in this case).
# 
# This setup makes fine-tuning BERT efficient and helps prevent overfitting.
# 

# In[8]:


from transformers import TrainingArguments, Trainer, EarlyStoppingCallback

# Define training arguments
training_args = TrainingArguments(
    output_dir="./results",
    eval_strategy="epoch",
    save_strategy="epoch",
    logging_strategy="epoch",
    #report_to=[],  # <- disable all integrations (no wandb, no tensorboard)
    per_device_train_batch_size=8,
    per_device_eval_batch_size=8,
    num_train_epochs=3,
    learning_rate=2e-5,
    weight_decay=0.1,
    warmup_steps=100,
    load_best_model_at_end=True,
    metric_for_best_model="eval_loss",
    greater_is_better=False,
    save_total_limit=3,
    fp16=torch.cuda.is_available(),
    dataloader_drop_last=False,
    gradient_accumulation_steps=1,
    seed=SEED,
)

# Create Trainer instance with early stopping
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    tokenizer=tokenizer,
    data_collator=data_collator,
    compute_metrics=compute_metrics,
    callbacks=[EarlyStoppingCallback(early_stopping_patience=2)],
)


# # Start Training
# 
# Now we begin fine-tuning the BERT model on the IMDb dataset using the Hugging Face Trainer.
# 
# - The training process will:
#   1. Iterate over the training dataset for the specified number of epochs.
#   2. Evaluate on the validation set at the end of each epoch.
#   3. Save checkpoints for the best model based on validation loss.
#   4. Stop early if the validation loss does not improve for 2 consecutive evaluation steps (early stopping).
# - Training progress, loss, accuracy, and F1 score will be displayed in real time.
# 

# In[9]:


# Start model training
trainer.train()


# # Save the Fine-Tuned Model and Tokenizer
# 
# After training, we save the fine-tuned BERT model and its tokenizer so that they can be easily reloaded later for inference or further fine-tuning.
# 
# - The model weights and configuration will be saved in the folder `my-fine-tuned-bert`.
# - The tokenizer files are also saved in the same directory.
# 

# In[10]:


# Save the fine-tuned model
trainer.save_model('my-fine-tuned-bert')

# Save the tokenizer
tokenizer.save_pretrained('my-fine-tuned-bert')


# # Inference with the Fine-Tuned Model
# 
# In this section, we demonstrate how to load the fine-tuned BERT model and tokenizer, and perform sentiment prediction on new text.
# 
# - We use Hugging Face's `TextClassificationPipeline` for convenient text classification.
# - The model predicts either `LABEL_0` (negative) or `LABEL_1` (positive).
# - We map these labels to more meaningful sentiment labels for clarity.
# - Finally, we test the model on a sample sentence.
# 

# In[11]:


from transformers import AutoModelForSequenceClassification, AutoTokenizer, TextClassificationPipeline

# Load the fine-tuned model and tokenizer
new_model = AutoModelForSequenceClassification.from_pretrained('my-fine-tuned-bert')
new_tokenizer = AutoTokenizer.from_pretrained('my-fine-tuned-bert')

# Create a text classification pipeline
classifier = TextClassificationPipeline(
    model=new_model, 
    tokenizer=new_tokenizer,     )

# Define label mapping
label_mapping = {
    0: 'World',
    1: 'Sports',
    2: 'Business',
    3: 'Sci/Tech'
}

# Test the classifier on a sample sentence
sample_text = "This movie was good"
result = classifier(sample_text)

# Map the predicted label to a meaningful sentiment
mapped_result = {
    'label': label_mapping[int(result[0]['label'].split('_')[1])],
    'score': result[0]['score']
}

print(mapped_result)


# In[12]:


from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer
import numpy as np
from sklearn.metrics import accuracy_score, f1_score, classification_report, confusion_matrix

MODEL_DIR = "my-fine-tuned-bert"
MODEL_NAME = "bert-base-uncased"

raw = load_dataset("SetFit/ag_news")
tok = AutoTokenizer.from_pretrained(MODEL_NAME)

def tokenize_fn(ex):
    return tok(ex["text"], truncation=True, max_length=128)

test_tok = raw["test"].map(tokenize_fn, batched=True)
test_tok = test_tok.rename_column("label", "labels")
cols = ["input_ids", "attention_mask", "labels"]
if "token_type_ids" in test_tok.column_names:
    cols.append("token_type_ids")
test_tok.set_format(type="torch", columns=cols)

model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)

trainer = Trainer(model=model, tokenizer=tok)
pred_out = trainer.predict(test_tok)

y_prob = pred_out.predictions
y_pred = np.argmax(y_prob, axis=1)
y_true = pred_out.label_ids

acc = accuracy_score(y_true, y_pred)
f1m = f1_score(y_true, y_pred, average="macro")
print({"test_accuracy": acc, "test_f1_macro": f1m})
print(classification_report(y_true, y_pred, digits=4))

from sklearn.metrics import ConfusionMatrixDisplay
import matplotlib.pyplot as plt
ConfusionMatrixDisplay.from_predictions(y_true, y_pred)
plt.title("AG News – Confusion Matrix (Test)")
plt.show()


# In[13]:


import torch, gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline

MODEL_ID = "my-fine-tuned-bert"

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)

label_names = {0: "World", 1: "Sports", 2: "Business", 3: "Sci/Tech"}

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
clf = pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=None, device=device)

def predict(text):
    text = text.strip()
    out = clf(text, truncation=True)
    out = out[0] if isinstance(out[0], list) else out
    results = {}
    for o in sorted(out, key=lambda x: -x["score"]):
        idx = int(o["label"].split("_")[1])
        results[label_names[idx]] = o["score"]
    return results

demo = gr.Interface(
    fn=predict,
    inputs=gr.Textbox(lines=3, label="Enter news headline"),
    outputs=gr.Label(num_top_classes=4, label="Predicted topic"),
    title="AG News Topic Classifier (BERT-base)"
)

demo.launch(share=True)