Views
No views yet
vinai/bertweet-large serving as the base model.1import torch
2from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
3from tqdm import tqdm
4import pandas as pd
5def classify_tweets(df, text_col, model, tokenizer):
6 df[text_col]=df[text_col].astype(str)
7 device = 0 if torch.cuda.is_available() else -1 # Use GPU if available
8 classifier = pipeline(
9 "text-classification",
10 model=model,
11 tokenizer=tokenizer,
12 device=device,
13 truncation=True, # Ensures inputs don't exceed max length
14 max_length=512, # Manually set to avoid exceeding model's limit
15 padding="max_length" # Ensures all inputs have the same length
16 )
17
18 outcomes, probs, pred_labels = [], [], []
19 for text in tqdm(df[text_col]): # Fixed tqdm syntax
20 preds = classifier(text, return_all_scores=True)
21 outcomes.append(preds)
22
23 # Extract probabilities and predicted label
24 label_scores = {entry['label']: entry['score'] for entry in preds[0]}
25 probs.append(list(label_scores.values()))
26 pred_labels.append(max(label_scores, key=label_scores.get))
27 df["predicted_label"] = pred_labels
28 return df
29model_name="cja5553/covid-vaccine-sentiments-BERTweet-large"
30label2id={1:"Negative",2:"Neutral",3:"Positive"}
31id2label = {v: k for k, v in label2id.items()}
32model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=len(label2id),
33 label2id=label2id,id2label=id2label,
34 use_auth_token=False).to("cuda")
35tokenizer = AutoTokenizer.from_pretrained(model_name,use_auth_token=False)
36text_col="text" # change text column accordingly
37df_with_classification=classify_tweets(df, text_col, model, tokenizer)@inproceedings{
author={Charles Alba, Benjamin C Warner, Akshar Saxena, Jiaxin Huang, Ruopeng An},
title={Towards Robust Sentiment Analysis of Temporally-Sensitive Policy-Related Online Text},
year={2025},
booktitle={Proceedings of the 63rd Annual Meeting of the Association for Computational Linguistics (ACL), Volume 4: Student Research Workshop.},
url={https://aclanthology.org/2025.acl-srw.70/}}