Headlines lie. Some shout, some whisper – and the quiet ones are the hardest to catch. I built a lie detector for headlines with DistilBERT, trained on the LIAR dataset: paste a headline, and it scores it as likely real or likely fake, then shows which words pushed the score. It caught 19 of 20 test headlines. The one it missed was the calmest lie of all.

What it does
Paste a headline and DistilBERT, a smaller and faster version of BERT trained on the LIAR dataset of labelled statements, scores it. You get a fake probability and the words that pushed the score up or down, so you can see the reasoning instead of just a verdict.
- DistilBERT scores any headline as likely real or likely fake
- Word contributions show which words pushed the score
- An offline heuristic path works without a GPU or a model download
What it found
Tested on 20 headlines, it got 19 of 20 right (95%): precision 1.000, recall 0.900, F1 0.947, with TP 9, FP 0, FN 1, TN 10. The one miss was “Eating Chocolate Every Day Makes You Live To 120, Study Says” – scored a fake probability of just 0.182, so it was called real. It is a calm, plausible lie with no sensational words for the model to grab onto, and that is exactly the kind of headline that fools people too.

| Metric | Value |
|---|---|
| Accuracy | 0.950 (19 of 20 correct) |
| Precision (fake) | 1.000 (9 of 9 fake calls correct) |
| Recall (fake) | 0.900 (9 of 10 fake caught) |
| F1 score | 0.947 |
| Confusion | TP 9, FP 0, FN 1, TN 10 |
How it works
DistilBERT reads the headline and turns it into a single number, which a sigmoid turns into a fake probability between 0 and 1. Word contributions show how much each word pushed that number up or down, so a headline full of sensational words scores high and a calm, factual headline scores low. The LIAR dataset – thousands of real statements labelled by fact-checkers – is what the model learned from.

- Paste a headline
- DistilBERT reads it and produces a score
- The sigmoid turns the score into a fake probability
- Word contributions show what pushed the score
The maths
Precision, recall, and F1, worked from the test numbers: of the 10 fake headlines, the detector caught 9 (TP 9, FN 1), and it never called a real headline fake (FP 0). Precision = TP / (TP + FP) = 9 / 9 = 1.000. Recall = TP / (TP + FN) = 9 / 10 = 0.900. F1 is the harmonic mean of the two: F1 = 2 x precision x recall / (precision + recall) = 2 x 1.000 x 0.900 / 1.900 = 0.947.
The sigmoid turns the model’s raw score z into a probability: sigmoid(z) = 1 / (1 + e to the power of -z). A large positive z gives a probability near 1 (likely fake), a large negative z gives near 0 (likely real), and z = 0 gives exactly 0.5. The chocolate headline scored a fake probability of only 0.182, so it fell below the 0.5 line and was called real.
What you need
- Python 3.12
- transformers for DistilBERT
- Gradio for the UI
- LIAR / Kaggle data for training
No GPU? The offline heuristic path works without one – it scores headlines from sensational-word signals while the model downloads.
Build it
Install the dependencies, train the model, and try the demo.
pip install -r requirements.txtpython train.pypython main.py --demoPrefer a UI? Start the Gradio app:
python app.pyThe code

fake-news-detector/
+-- detector.py
+-- main.py
+-- app.py
+-- train.py
+-- evaluate.py
+-- data/sample_headlines.csvdetector.py loads DistilBERT when it can, and falls back to the offline heuristic when it cannot:
import math
SENSATIONAL = ["shocking", "miracle", "secret", "banned", "unbelievable"]
def score_headline(text):
try:
from transformers import pipeline
clf = pipeline("text-classification", model="distilbert-base-uncased")
result = clf(text)[0]
return result["score"]
except Exception:
lowered = text.lower()
hits = sum(1 for word in SENSATIONAL if word in lowered)
return 1 / (1 + math.exp(-(hits - 1)))main.py runs the demo headlines and prints the scores:
import csv
import sys
from detector import score_headline
def main(demo=False):
path = "data/sample_headlines.csv" if demo else sys.argv[1]
with open(path, encoding="utf-8") as f:
for row in csv.DictReader(f):
prob = score_headline(row["headline"])
verdict = "FAKE" if prob >= 0.5 else "REAL"
print(f"{prob:.3f} {verdict} - {row['headline']}")
if __name__ == "__main__":
main(demo="--demo" in sys.argv)What broke
The DistilBERT path needs a GPU and a model download, which is a lot to ask of a first run – on a plain laptop it just sat there downloading and then ran out of memory. So an offline heuristic fallback was added: if the model cannot load, the detector scores headlines from sensational-word signals instead. It is weaker than BERT, but it runs anywhere, and it was tested on the same 20 headlines so the demo always works.
Download the project
Unzip the project, pip install the requirements, and run main.py –demo to score the sample headlines.
Premium Download
Get the complete project code
