pravjet commited on
Commit
3868de3
·
verified ·
1 Parent(s): 4f7a780

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -14
app.py CHANGED
@@ -1,24 +1,109 @@
 
 
 
1
  import requests
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  def fact_check_google_api(query, api_key):
 
 
4
  url = "https://factchecktools.googleapis.com/v1alpha1/claims:search"
5
  params = {
6
  "query": query,
7
  "languageCode": "en-US",
8
  "key": api_key
9
  }
10
- response = requests.get(url, params=params)
11
- if response.status_code == 200:
12
- data = response.json()
13
- if "claims" in data:
14
- results = []
15
- for claim in data["claims"]:
16
- text = claim.get("text", "No claim text")
17
- rating = claim.get("claimReview", [{}])[0].get("textualRating", "No rating")
18
- publisher = claim.get("claimReview", [{}])[0].get("publisher", {}).get("name", "Unknown")
19
- results.append(f"Claim: {text}\nRating: {rating}\nSource: {publisher}")
20
- return "\n\n".join(results)
 
 
 
 
21
  else:
22
- return "No fact-checks found for this query."
23
- else:
24
- return f"Error: {response.status_code} - {response.text}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
  import requests
5
+ from bs4 import BeautifulSoup
6
+ import matplotlib.pyplot as plt
7
+ import os
8
+
9
+ # Load model and tokenizer
10
+ model_name = "mrm8488/bert-tiny-finetuned-fake-news-detection"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
13
+
14
+ # Initialize verdict counters
15
+ verdict_counts = {"Authentic": 0, "Possibly Misinformation": 0}
16
+
17
+ # Read API key from environment variable
18
+ FACT_CHECK_API_KEY = os.getenv("FACT_CHECK_API_KEY")
19
+
20
+ def extract_text_from_url(url):
21
+ try:
22
+ response = requests.get(url, timeout=5)
23
+ soup = BeautifulSoup(response.text, "html.parser")
24
+ paragraphs = soup.find_all("p")
25
+ text = " ".join([p.get_text() for p in paragraphs])
26
+ return text.strip()[:3000]
27
+ except Exception as e:
28
+ return f"Error fetching URL: {e}"
29
+
30
+ def update_chart():
31
+ labels = list(verdict_counts.keys())
32
+ sizes = list(verdict_counts.values())
33
+ fig, ax = plt.subplots()
34
+ ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
35
+ ax.set_title("Verdict Distribution")
36
+ return fig
37
 
38
  def fact_check_google_api(query, api_key):
39
+ if not api_key:
40
+ return "API key not found. Please set FACT_CHECK_API_KEY in environment."
41
  url = "https://factchecktools.googleapis.com/v1alpha1/claims:search"
42
  params = {
43
  "query": query,
44
  "languageCode": "en-US",
45
  "key": api_key
46
  }
47
+ try:
48
+ response = requests.get(url, params=params)
49
+ if response.status_code == 200:
50
+ data = response.json()
51
+ if "claims" in data:
52
+ results = []
53
+ for claim in data["claims"]:
54
+ text = claim.get("text", "No claim text")
55
+ review = claim.get("claimReview", [{}])[0]
56
+ rating = review.get("textualRating", "No rating")
57
+ publisher = review.get("publisher", {}).get("name", "Unknown")
58
+ results.append(f"Claim: {text}\nRating: {rating}\nSource: {publisher}")
59
+ return "\n\n".join(results)
60
+ else:
61
+ return "No fact-checks found for this query."
62
  else:
63
+ return f"Error: {response.status_code} - {response.text}"
64
+ except Exception as e:
65
+ return f"Error calling Fact Check API: {e}"
66
+
67
+ def detect_misinformation(input_text, input_type):
68
+ if input_type == "URL":
69
+ input_text = extract_text_from_url(input_text)
70
+ if input_text.startswith("Error"):
71
+ return input_text, "Error", 0.0, update_chart(), "URL extraction failed."
72
+
73
+ inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
74
+ with torch.no_grad():
75
+ outputs = model(**inputs)
76
+ probs = torch.nn.functional.softmax(outputs.logits, dim=1)
77
+ score = probs[0][1].item()
78
+ verdict = "Possibly Misinformation" if score > 0.5 else "Authentic"
79
+ verdict_counts[verdict] += 1
80
+ fact_check_result = fact_check_google_api(input_text, FACT_CHECK_API_KEY)
81
+ return input_text[:1000], verdict, round(score * 100, 2), update_chart(), fact_check_result
82
+
83
+ with gr.Blocks() as demo:
84
+ gr.Markdown("## 🧠 Misinformation Detection Dashboard")
85
+ gr.Markdown("Paste article text or a URL. Choose input type and get a verdict.")
86
+
87
+ with gr.Row():
88
+ input_text = gr.Textbox(label="Enter Text or URL", lines=6, placeholder="Paste article text or URL here...")
89
+ input_type = gr.Radio(["Auto Detect", "Text", "URL"], value="Auto Detect", label="Input Type")
90
+
91
+ output_text = gr.Textbox(label="Processed Text", lines=6)
92
+ verdict = gr.Label(label="Verdict")
93
+ score = gr.Label(label="Authenticity Score (%)")
94
+ chart = gr.Plot(label="Analytics Dashboard")
95
+ fact_check = gr.Textbox(label="Fact Check Results", lines=6)
96
+
97
+ btn = gr.Button("Analyze")
98
+
99
+ def handle_input(text, mode):
100
+ if mode == "Auto Detect":
101
+ if text.startswith("http://") or text.startswith("https://"):
102
+ mode = "URL"
103
+ else:
104
+ mode = "Text"
105
+ return detect_misinformation(text, mode)
106
+
107
+ btn.click(fn=handle_input, inputs=[input_text, input_type], outputs=[output_text, verdict, score, chart, fact_check])
108
+
109
+ demo.launch()