YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
NoNewsLite: Fake News Detection Model
Overview
NoNewsLite is a deep learning model designed to classify news articles as either real or fake. It uses a Bidirectional LSTM with Attention architecture trained on the ISOT Fake News Dataset. The model analyzes text from news titles and content to predict authenticity with high accuracy.
This repository provides:
- A pre-trained model for inference
- Complete code to run predictions locally
- An interactive web interface using Gradio for easy testing
How It Works
The model processes input text through these steps:
- Text Preprocessing: Clean and tokenize input (remove URLs, HTML, stopwords)
- Embedding: Convert words to numerical vectors
- Bi-LSTM: Capture sequential patterns in both directions
- Attention: Highlight important words that influence the prediction
- Classification: Output probability of being real or fake news
The attention mechanism allows the model to explain its decisions by showing which words contributed most to the final prediction.
Requirements
To use this model, install the following packages:
pip install torch nltk gradio scikit-learn pandas numpy
# Run this script to start the demo
import gradio as gr
import torch
import json
import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
# Load vocabulary
with open("vocab.json", "r") as f:
word2idx = json.load(f)
# Define model architecture (see full implementation above)
class BiLSTM_Attention(nn.Module):
# ... (model definition as provided earlier)
pass
# Load model weights
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = BiLSTM_Attention(vocab_size=len(word2idx)).to(device)
model.load_state_dict(torch.load("pytorch_model.bin", map_location=device))
model.eval()
# Text preprocessing functions
def clean_and_preprocess_text(title, text):
combined = f"{title} {text}".lower()
combined = re.sub(r'https?://\S+|www\.\S+', '', combined)
combined = re.sub(r'<.*?>', '', combined)
combined = re.sub(r'[^a-zA-Z\s]', '', combined)
combined = re.sub(r'\s+', ' ', combined).strip()
tokens = word_tokenize(combined)
tokens = [word for word in tokens if word not in stopwords.words('english')]
return ' '.join(tokens)
def text_to_sequence(text, word2idx, max_len=512):
tokens = text.split()
seq = [word2idx.get(word, word2idx["<UNK>"]) for word in tokens]
if len(seq) > max_len:
seq = seq[:max_len]
else:
pad_token = word2idx["<PAD>"]
seq += [pad_token] * (max_len - len(seq))
return torch.tensor(seq, dtype=torch.long).unsqueeze(0)
def predict(title, text):
cleaned = clean_and_preprocess_text(title, text)
seq = text_to_sequence(cleaned, word2idx)
seq = seq.to(device)
with torch.no_grad():
output, _ = model(seq)
prob = output.item()
prediction = "Real News" if prob > 0.5 else "Fake News"
confidence = prob if prob > 0.5 else 1 - prob
return f"**Prediction:** {prediction}\n\n**Confidence:** {confidence:.2%}"
# Launch Gradio interface
demo = gr.Interface(
fn=predict,
inputs=[
gr.Textbox(placeholder="Enter news title...", label="Title"),
gr.Textbox(placeholder="Enter news content...", label="Content", lines=6)
],
outputs=gr.Markdown(label="Result"),
title="๐ NoNewsLite: Fake News Detection",
description="Enter a news article's title and content to detect if it's real or fake using a Bi-LSTM with Attention.",
examples=[
["Donald Trump Dies in Car Crash", "Breaking news: Former President Donald Trump has died in a tragic accident."],
["U.S. Republicans Flip Fiscal Script", "The Republican party has reversed its long-standing opposition to deficit spending."]
],
theme="default"
)
demo.launch(share=True) # Generates a public link
or local inference with
# Example usage
title = "Scientists Confirm Climate Change Is Real"
text = "New study shows global temperatures rising due to greenhouse gas emissions."
result = predict(title, text)
print(result)
- Downloads last month
- 6
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐ Ask for provider support