pravjet commited on
Commit
c03eefd
·
verified ·
1 Parent(s): ed4d2e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -49
app.py CHANGED
@@ -1,54 +1,59 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- from newspaper import Article
 
 
 
 
 
 
4
 
5
- # Load the model
6
- model = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
7
-
8
- def analyze(input_text, input_type):
9
- # Auto-detect if input is URL or text
10
- if input_type == "Auto Detect":
11
- if input_text.startswith("http://") or input_text.startswith("https://"):
12
- input_type = "URL"
13
- else:
14
- input_type = "Text"
15
 
 
16
  if input_type == "URL":
17
- try:
18
- article = Article(input_text)
19
- article.download()
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
- try:
31
- result = model(text)[0]
32
- label = result["label"]
33
- score = result["score"]
34
- verdict = "Authentic" if label == "REAL" else "Possibly Misinformation"
35
- authenticity_score = round(score * 100, 2)
36
- return verdict, authenticity_score
37
- except Exception as e:
38
- return f"❌ Model inference failed: {e}", 0
39
-
40
- interface = gr.Interface(
41
- fn=analyze,
42
- inputs=[
43
- gr.Textbox(lines=6, label="Paste article text or URL here"),
44
- gr.Radio(["Auto Detect", "Text", "URL"], label="Input Type", value="Auto Detect")
45
- ],
46
- outputs=[
47
- gr.Textbox(label="Verdict"),
48
- gr.Number(label="Authenticity Score (%)")
49
- ],
50
- title="Misinformation Detection Dashboard",
51
- description="Enter article text or a URL to detect whether the content is authentic or possibly misinformation."
52
- )
53
-
54
- interface.launch()
 
 
 
 
 
 
 
 
 
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()