Spaces:
No application file
No application file
Upload 3 files
Browse files- README.md +28 -13
- app.py +63 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,13 +1,28 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Misinformation Detection Dashboard (Gradio)
|
| 2 |
+
|
| 3 |
+
This Gradio app detects misinformation in news articles using a fine-tuned DistilBERT model.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- Paste a news article URL
|
| 8 |
+
- Automatically extracts and detects language
|
| 9 |
+
- Translates Russian articles to English using Facebook's FSMT
|
| 10 |
+
- Predicts fakeness score using DistilBERT
|
| 11 |
+
- Displays trust verdict based on score
|
| 12 |
+
|
| 13 |
+
## Files
|
| 14 |
+
|
| 15 |
+
- `app.py`: Main Gradio interface and model logic
|
| 16 |
+
- `requirements.txt`: Python dependencies
|
| 17 |
+
- `README.md`: Project overview
|
| 18 |
+
|
| 19 |
+
## Deployment (Hugging Face Spaces)
|
| 20 |
+
|
| 21 |
+
1. Create a new Space using Gradio SDK
|
| 22 |
+
2. Upload `app.py`, `requirements.txt`, and `README.md`
|
| 23 |
+
3. Hugging Face will auto-launch the app
|
| 24 |
+
|
| 25 |
+
## Requirements
|
| 26 |
+
|
| 27 |
+
- Python 3.8+
|
| 28 |
+
- Internet access for model downloads
|
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, FSMTForConditionalGeneration, FSMTTokenizer
|
| 3 |
+
from newspaper import Article
|
| 4 |
+
from langdetect import detect
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Load models
|
| 8 |
+
classification_model_name = 'distilbert-base-uncased-finetuned-sst-2-english'
|
| 9 |
+
translation_model_name = 'facebook/wmt19-ru-en'
|
| 10 |
+
|
| 11 |
+
classification_model = AutoModelForSequenceClassification.from_pretrained(classification_model_name)
|
| 12 |
+
classification_tokenizer = AutoTokenizer.from_pretrained(classification_model_name)
|
| 13 |
+
|
| 14 |
+
translation_model = FSMTForConditionalGeneration.from_pretrained(translation_model_name)
|
| 15 |
+
translation_tokenizer = FSMTTokenizer.from_pretrained(translation_model_name)
|
| 16 |
+
|
| 17 |
+
def analyze_article(url):
|
| 18 |
+
try:
|
| 19 |
+
article = Article(url)
|
| 20 |
+
article.download()
|
| 21 |
+
article.parse()
|
| 22 |
+
text = article.title + '. ' + article.text
|
| 23 |
+
lang = detect(text)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error: {e}", "", "", 0, "error"
|
| 26 |
+
|
| 27 |
+
translated_text = ""
|
| 28 |
+
if lang == 'ru':
|
| 29 |
+
input_ids = translation_tokenizer.encode(text, return_tensors="pt", max_length=512, truncation=True)
|
| 30 |
+
outputs = translation_model.generate(input_ids)
|
| 31 |
+
translated_text = translation_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 32 |
+
text = translated_text
|
| 33 |
+
|
| 34 |
+
tokens = classification_tokenizer(text, truncation=True, return_tensors="pt")
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
outputs = classification_model(**tokens)
|
| 37 |
+
score = torch.nn.functional.softmax(outputs.logits[0], dim=0)[1].item()
|
| 38 |
+
percentage = int(score * 100)
|
| 39 |
+
|
| 40 |
+
if percentage > 70:
|
| 41 |
+
status = "We would not trust this text!"
|
| 42 |
+
elif percentage > 40:
|
| 43 |
+
status = "We are not sure about this text!"
|
| 44 |
+
else:
|
| 45 |
+
status = "We would trust this text!"
|
| 46 |
+
|
| 47 |
+
return text[:1000], lang.upper(), translated_text[:1000] if translated_text else "Not required", percentage, status
|
| 48 |
+
|
| 49 |
+
demo = gr.Interface(
|
| 50 |
+
fn=analyze_article,
|
| 51 |
+
inputs=gr.Textbox(label="Enter Article URL"),
|
| 52 |
+
outputs=[
|
| 53 |
+
gr.Textbox(label="Extracted or Translated Text"),
|
| 54 |
+
gr.Textbox(label="Detected Language"),
|
| 55 |
+
gr.Textbox(label="Translated Text (if Russian)"),
|
| 56 |
+
gr.Number(label="Fakeness Score (%)"),
|
| 57 |
+
gr.Textbox(label="Trust Verdict")
|
| 58 |
+
],
|
| 59 |
+
title="🧠 Misinformation Detection Dashboard",
|
| 60 |
+
description="Paste a news article URL to detect language, translate if needed, and predict fakeness using a fine-tuned DistilBERT model."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
newspaper3k
|
| 5 |
+
langdetect
|