Downloads · 30 days
6
15% of all-time downloads
loveucifer/NoNewsLite
NoNewsLite is a machine learning model from loveucifer. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
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 fr…
Downloads · 30 days
6
15% of all-time downloads
All-time downloads
41
Public
Repo size
52.7 MB
Likes
1
Public
Click a slice to open those files.
.bin52.7 MB · 96%
From the Hugging Face model README
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:
The model processes input text through these steps:
The attention mechanism allows the model to explain its decisions by showing which words contributed most to the final prediction.
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)