-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathtest_validator.py
More file actions
61 lines (47 loc) · 1.65 KB
/
Copy pathtest_validator.py
File metadata and controls
61 lines (47 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Pytest suite for the fake news detection TVD.
Run: pytest test_validator.py -v
Uses hamzab/roberta-fake-news-classification (id2label: 0=FAKE, 1=TRUE).
IMPORTANT: the `<title>TITLE<content>CONTENT<end>` format tokens
(suggested on the model card) degenerate-flag all inputs as FAKE 1.000
regardless of content. Use raw text only.
"""
import json
from pathlib import Path
import pytest
import torch
from transformers import pipeline
MIN_WORDS = 30
FAKE_LABEL = "FAKE"
def _device() -> str:
if torch.backends.mps.is_available():
return "mps"
if torch.cuda.is_available():
return "cuda"
return "cpu"
classifier = pipeline(
"text-classification",
model="hamzab/roberta-fake-news-classification",
device=_device(),
)
with open(Path(__file__).parent / "articles.json") as f:
data = json.load(f)
articles = data["articles"]
class TestFakeNewsDetection:
def test_no_placeholders(self) -> None:
for a in articles:
assert "???" not in a["article_text"], (
f"article {a['id']} has ??? placeholder"
)
def test_minimum_length(self) -> None:
for a in articles:
wc = len(a["article_text"].split())
assert wc >= MIN_WORDS, (
f"article {a['id']}: {wc} words < {MIN_WORDS}"
)
def test_fake_news_detected(self) -> None:
for a in articles:
result = classifier(a["article_text"][:512])[0]
assert result["label"].upper() == FAKE_LABEL, (
f"article {a['id']} classified as {result['label']} "
f"(conf={result['score']:.3f}); need label == {FAKE_LABEL!r}"
)