Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
input_type = "Text"
|
| 15 |
|
|
|
|
| 16 |
if input_type == "URL":
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
article.parse()
|
| 21 |
-
text = article.text
|
| 22 |
-
except Exception as e:
|
| 23 |
-
return f"❌ Failed to extract article: {e}", 0
|
| 24 |
-
else:
|
| 25 |
-
text = input_text
|
| 26 |
-
|
| 27 |
-
if not text:
|
| 28 |
-
return "❌ No text provided", 0
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return verdict,
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
gr.Textbox(lines=6,
|
| 44 |
-
gr.Radio(["Auto Detect", "Text", "URL"],
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import requests
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
# Load model and tokenizer
|
| 7 |
+
model_name = "mrm8488/bert-tiny-finetuned-fake-news-detection"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
def extract_text_from_url(url):
|
| 12 |
+
try:
|
| 13 |
+
response = requests.get(url, timeout=5)
|
| 14 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 15 |
+
paragraphs = soup.find_all("p")
|
| 16 |
+
text = " ".join([p.get_text() for p in paragraphs])
|
| 17 |
+
return text.strip()[:3000] # Limit to 3000 characters
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"Error fetching URL: {e}"
|
|
|
|
| 20 |
|
| 21 |
+
def detect_misinformation(input_text, input_type):
|
| 22 |
if input_type == "URL":
|
| 23 |
+
input_text = extract_text_from_url(input_text)
|
| 24 |
+
if input_text.startswith("Error"):
|
| 25 |
+
return input_text, "Error", 0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(**inputs)
|
| 30 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 31 |
+
score = probs[0][1].item()
|
| 32 |
+
verdict = "Possibly Misinformation" if score > 0.5 else "Authentic"
|
| 33 |
+
return input_text[:1000], verdict, round(score * 100, 2)
|
| 34 |
+
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("## 🧠 Misinformation Detection Dashboard")
|
| 37 |
+
gr.Markdown("Paste article text or a URL. Choose input type and get a verdict.")
|
| 38 |
+
|
| 39 |
+
with gr.Row():
|
| 40 |
+
input_text = gr.Textbox(label="Enter Text or URL", lines=6, placeholder="Paste article text or URL here...")
|
| 41 |
+
input_type = gr.Radio(["Auto Detect", "Text", "URL"], value="Auto Detect", label="Input Type")
|
| 42 |
+
|
| 43 |
+
output_text = gr.Textbox(label="Processed Text", lines=6)
|
| 44 |
+
verdict = gr.Label(label="Verdict")
|
| 45 |
+
score = gr.Label(label="Authenticity Score (%)")
|
| 46 |
+
|
| 47 |
+
btn = gr.Button("Analyze")
|
| 48 |
+
|
| 49 |
+
def handle_input(text, mode):
|
| 50 |
+
if mode == "Auto Detect":
|
| 51 |
+
if text.startswith("http://") or text.startswith("https://"):
|
| 52 |
+
mode = "URL"
|
| 53 |
+
else:
|
| 54 |
+
mode = "Text"
|
| 55 |
+
return detect_misinformation(text, mode)
|
| 56 |
+
|
| 57 |
+
btn.click(fn=handle_input, inputs=[input_text, input_type], outputs=[output_text, verdict, score])
|
| 58 |
+
|
| 59 |
+
demo.launch()
|