Views
No views yet
transformers library. For best results, ensure you follow the same text cleaning used during training.1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3import torch.nn.functional as F
4
5# Repository ID
6repo_id = "aderohmatmaulana98/tweet-classification"
7device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
9# Load Tokenizer & Model
10# It's highly recommended to load the tokenizer from the base model for consistent normalization
11tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base", use_fast=False, normalization=True)
12model = AutoModelForSequenceClassification.from_pretrained(repo_id)
13model.to(device)
14model.eval()
15
16def clean_text(text):
17 """Normalize text to match training conditions"""
18 text = text.replace("{{USERNAME}}", "@USER")
19 text = text.replace("{{URL}}", "HTTPURL")
20 return " ".join(text.split())
21
22# Input text
23text = "The new science breakthrough is amazing!"
24cleaned_text = clean_text(text)
25
26inputs = tokenizer(
27 cleaned_text,
28 return_tensors="pt",
29 truncation=True,
30 max_length=128,
31 padding="max_length"
32)
33inputs = {k: v.to(device) for k, v in inputs.items()}
34
35with torch.no_grad():
36 outputs = model(**inputs)
37 probs = F.softmax(outputs.logits, dim=-1)
38
39pred_id = torch.argmax(probs, dim=-1).item()
40confidence = torch.max(probs).item()
41
42# Label mapping from model config
43label = model.config.id2label[pred_id]
44print(f"Predicted: {label}")
45print(f"Confidence: {confidence*100:.2f}%")