Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,13 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
|
|
|
|
3 |
from transformers import pipeline
|
4 |
from deepface import DeepFace
|
5 |
from PIL import Image
|
6 |
-
import torch
|
7 |
-
import torchvision.transforms as transforms
|
8 |
import cv2
|
9 |
import numpy as np
|
10 |
from bs4 import BeautifulSoup
|
11 |
-
import re
|
12 |
|
13 |
# Load Fake News Detection Model
|
14 |
fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
|
@@ -16,79 +15,74 @@ fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-fi
|
|
16 |
def classify_text(news_text):
|
17 |
result = fake_news_pipeline(news_text)[0]
|
18 |
label = result['label'].lower()
|
19 |
-
score = result['score'] * 100
|
20 |
return ("Fake" if label == "fake" else "Real"), round(score, 2)
|
21 |
|
22 |
def analyze_image(image):
|
23 |
try:
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
28 |
|
29 |
-
def analyze_video(
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def verify_news(news_text):
|
33 |
search_url = f"https://www.google.com/search?q={'+'.join(news_text.split())}"
|
34 |
return search_url
|
35 |
|
36 |
-
def scrape_verification_links(news_text):
|
37 |
-
sources = [
|
38 |
-
"https://www.bbc.com/news",
|
39 |
-
"https://www.cnn.com",
|
40 |
-
"https://www.reuters.com",
|
41 |
-
"https://factcheck.org",
|
42 |
-
"https://www.snopes.com",
|
43 |
-
"https://www.politifact.com"
|
44 |
-
]
|
45 |
-
verification_links = {}
|
46 |
-
for source in sources:
|
47 |
-
try:
|
48 |
-
response = requests.get(source)
|
49 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
50 |
-
for link in soup.find_all('a', href=True):
|
51 |
-
if re.search(news_text[:5], link.text, re.IGNORECASE):
|
52 |
-
verification_links[link.text] = source + link['href']
|
53 |
-
except:
|
54 |
-
continue
|
55 |
-
return verification_links
|
56 |
-
|
57 |
-
# Streamlit UI
|
58 |
st.set_page_config(page_title="Fake News Detector", layout="wide")
|
59 |
st.title("π° Fake News Detector")
|
60 |
|
61 |
-
# Sidebar for Input Selection
|
62 |
st.sidebar.title("Select Input Type")
|
63 |
option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video Link"])
|
64 |
|
65 |
-
# Input Section
|
66 |
if option == "Text":
|
67 |
news_text = st.text_area("Enter the news content to check:", height=200)
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
st.markdown(f"[π {title}]({link})")
|
77 |
-
st.markdown(f"[π Verify on Google]({verify_news(news_text)})")
|
78 |
|
79 |
elif option == "Image":
|
80 |
uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"])
|
81 |
-
|
82 |
-
if uploaded_image and analyze_image_clicked:
|
83 |
image = Image.open(uploaded_image)
|
|
|
84 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
85 |
-
|
86 |
-
|
|
|
87 |
|
88 |
elif option == "Video Link":
|
89 |
video_url = st.text_input("Enter the video link:")
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
+
import torch
|
4 |
+
import torchvision.transforms as transforms
|
5 |
from transformers import pipeline
|
6 |
from deepface import DeepFace
|
7 |
from PIL import Image
|
|
|
|
|
8 |
import cv2
|
9 |
import numpy as np
|
10 |
from bs4 import BeautifulSoup
|
|
|
11 |
|
12 |
# Load Fake News Detection Model
|
13 |
fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
|
|
|
15 |
def classify_text(news_text):
|
16 |
result = fake_news_pipeline(news_text)[0]
|
17 |
label = result['label'].lower()
|
18 |
+
score = result['score'] * 100
|
19 |
return ("Fake" if label == "fake" else "Real"), round(score, 2)
|
20 |
|
21 |
def analyze_image(image):
|
22 |
try:
|
23 |
+
analysis = DeepFace.analyze(image, actions=["emotion"])
|
24 |
+
dominant_emotion = analysis[0]["dominant_emotion"]
|
25 |
+
return "Fake" if dominant_emotion in ["fear", "surprise"] else "Real"
|
26 |
+
except Exception as e:
|
27 |
+
return "Error: " + str(e)
|
28 |
|
29 |
+
def analyze_video(video_path):
|
30 |
+
try:
|
31 |
+
cap = cv2.VideoCapture(video_path)
|
32 |
+
frame_count = 0
|
33 |
+
results = []
|
34 |
+
while cap.isOpened():
|
35 |
+
ret, frame = cap.read()
|
36 |
+
if not ret or frame_count >= 10:
|
37 |
+
break
|
38 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
39 |
+
result = analyze_image(image)
|
40 |
+
results.append(result)
|
41 |
+
frame_count += 1
|
42 |
+
cap.release()
|
43 |
+
return "Fake" if results.count("Fake") > results.count("Real") else "Real"
|
44 |
+
except Exception as e:
|
45 |
+
return "Error: " + str(e)
|
46 |
|
47 |
def verify_news(news_text):
|
48 |
search_url = f"https://www.google.com/search?q={'+'.join(news_text.split())}"
|
49 |
return search_url
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
st.set_page_config(page_title="Fake News Detector", layout="wide")
|
52 |
st.title("π° Fake News Detector")
|
53 |
|
|
|
54 |
st.sidebar.title("Select Input Type")
|
55 |
option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video Link"])
|
56 |
|
|
|
57 |
if option == "Text":
|
58 |
news_text = st.text_area("Enter the news content to check:", height=200)
|
59 |
+
if st.button("Analyze News"):
|
60 |
+
if news_text.strip():
|
61 |
+
result, accuracy = classify_text(news_text)
|
62 |
+
verification_link = verify_news(news_text)
|
63 |
+
st.write(f"**Result:** {result} (Accuracy: {accuracy}%)")
|
64 |
+
st.markdown(f"[Verify on Google]({verification_link})")
|
65 |
+
else:
|
66 |
+
st.warning("Please enter some text.")
|
|
|
|
|
67 |
|
68 |
elif option == "Image":
|
69 |
uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"])
|
70 |
+
if uploaded_image and st.button("Analyze Image"):
|
|
|
71 |
image = Image.open(uploaded_image)
|
72 |
+
result = analyze_image(image)
|
73 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
74 |
+
st.write(f"**Result:** {result}")
|
75 |
+
verification_link = verify_news("Fake news image verification")
|
76 |
+
st.markdown(f"[Verify on Google]({verification_link})")
|
77 |
|
78 |
elif option == "Video Link":
|
79 |
video_url = st.text_input("Enter the video link:")
|
80 |
+
if st.button("Analyze Video"):
|
81 |
+
if video_url.strip():
|
82 |
+
result = analyze_video(video_url)
|
83 |
+
st.video(video_url)
|
84 |
+
st.write(f"**Result:** {result}")
|
85 |
+
verification_link = verify_news("Fake news video verification")
|
86 |
+
st.markdown(f"[Verify on Google]({verification_link})")
|
87 |
+
else:
|
88 |
+
st.warning("Please enter a valid video link.")
|