Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4# Load model and tokenizer
5model_name = "naheelkk/fake-news-bert-liar"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
9# Prediction function
10def predict_fake_news(text):
11 inputs = tokenizer(text, truncation=True, max_length=128,
12 padding=True, return_tensors="pt")
13
14 with torch.no_grad():
15 outputs = model(**inputs)
16 logits = outputs.logits
17 probs = torch.softmax(logits, dim=-1)
18
19 prediction = torch.argmax(probs, dim=-1).item()
20 confidence = probs.max().item()
21
22 label = "REAL" if prediction == 1 else "FAKE"
23 return label, confidence
24
25# Example usage
26text = "The Federal Reserve announced an interest rate increase today."
27prediction, confidence = predict_fake_news(text)
28print(f"Prediction: {prediction} (Confidence: {confidence:.3f})")@inproceedings{wang2017liar,
title={LIAR: A benchmark dataset for fake news detection},
author={Wang, William Yang},
booktitle={Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics},
year={2017}
}