Spaces:
No application file
No application file
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,63 +0,0 @@
|
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|