Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from deepface import DeepFace
|
|
5 |
from PIL import Image
|
6 |
import io
|
7 |
import re
|
|
|
8 |
|
9 |
# Load Fake News Detection Model from Hugging Face
|
10 |
fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
|
@@ -17,18 +18,34 @@ def classify_text(news_text):
|
|
17 |
return ("Fake" if label == "fake" else "Real"), round(score, 2)
|
18 |
|
19 |
def analyze_image(image):
|
20 |
-
"""Analyzes image using DeepFace and
|
21 |
try:
|
22 |
analysis = DeepFace.analyze(image, actions=['emotion'])
|
23 |
dominant_emotion = analysis[0]['dominant_emotion']
|
24 |
-
|
|
|
25 |
except Exception as e:
|
26 |
-
return f"Error: {str(e)}", 0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def verify_news(news_text):
|
29 |
-
"""
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def extract_video_id(video_url):
|
34 |
"""Extracts the video ID from a YouTube URL."""
|
@@ -40,7 +57,7 @@ def fetch_video_metadata(video_url):
|
|
40 |
"""Fetches video metadata and runs Fake News detection on it."""
|
41 |
video_id = extract_video_id(video_url)
|
42 |
if not video_id:
|
43 |
-
return "Invalid Video URL", 0.0
|
44 |
|
45 |
api_key = "YOUR_YOUTUBE_API_KEY" # Replace with a valid YouTube API Key
|
46 |
metadata_url = f"https://www.googleapis.com/youtube/v3/videos?id={video_id}&part=snippet&key={api_key}"
|
@@ -56,8 +73,9 @@ def fetch_video_metadata(video_url):
|
|
56 |
|
57 |
# Classify the video metadata text
|
58 |
result, accuracy = classify_text(combined_text)
|
59 |
-
|
60 |
-
|
|
|
61 |
|
62 |
# Streamlit UI
|
63 |
st.set_page_config(page_title="Fake News Detector", layout="wide")
|
@@ -79,8 +97,10 @@ with col1:
|
|
79 |
st.warning("Please enter some text.")
|
80 |
else:
|
81 |
result, accuracy = classify_text(news_text)
|
|
|
82 |
st.session_state["text_result"] = result
|
83 |
st.session_state["text_accuracy"] = accuracy
|
|
|
84 |
|
85 |
# πΉ Image Upload Section
|
86 |
with col2:
|
@@ -90,9 +110,10 @@ with col2:
|
|
90 |
|
91 |
if uploaded_image and analyze_image_clicked:
|
92 |
image = Image.open(uploaded_image)
|
93 |
-
result, accuracy = analyze_image(image)
|
94 |
st.session_state["image_result"] = result
|
95 |
st.session_state["image_accuracy"] = accuracy
|
|
|
96 |
st.session_state["news_image"] = image # Store Image for Display
|
97 |
|
98 |
# πΉ Video Link Section
|
@@ -105,9 +126,10 @@ with col3:
|
|
105 |
if not video_url.strip():
|
106 |
st.warning("Please enter a valid video link.")
|
107 |
else:
|
108 |
-
result, accuracy = fetch_video_metadata(video_url)
|
109 |
st.session_state["video_result"] = result
|
110 |
st.session_state["video_accuracy"] = accuracy
|
|
|
111 |
st.session_state["video_url"] = video_url # Store Video URL for Display
|
112 |
|
113 |
# πΉ Results Section
|
@@ -123,13 +145,15 @@ if "text_result" in st.session_state:
|
|
123 |
else:
|
124 |
st.success(f"β
This news is **Real**! (Accuracy: {accuracy}%)", icon="β
")
|
125 |
|
126 |
-
|
127 |
-
st.
|
|
|
128 |
|
129 |
# πΉ Image Result
|
130 |
if "image_result" in st.session_state:
|
131 |
st.image(st.session_state["news_image"], caption="Uploaded Image", use_column_width=True)
|
132 |
st.info(f"πΌοΈ **Image Analysis Result:** {st.session_state['image_result']} (Accuracy: {st.session_state['image_accuracy']}%)")
|
|
|
133 |
|
134 |
# πΉ Video Result
|
135 |
if "video_result" in st.session_state:
|
@@ -140,3 +164,7 @@ if "video_result" in st.session_state:
|
|
140 |
st.success(f"β
**This video is Real!** (Accuracy: {st.session_state['video_accuracy']}%)")
|
141 |
else:
|
142 |
st.warning("β οΈ Unable to verify the authenticity of this video.")
|
|
|
|
|
|
|
|
|
|
5 |
from PIL import Image
|
6 |
import io
|
7 |
import re
|
8 |
+
import base64
|
9 |
|
10 |
# Load Fake News Detection Model from Hugging Face
|
11 |
fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
|
|
|
18 |
return ("Fake" if label == "fake" else "Real"), round(score, 2)
|
19 |
|
20 |
def analyze_image(image):
|
21 |
+
"""Analyzes image using DeepFace and Google Reverse Image Search."""
|
22 |
try:
|
23 |
analysis = DeepFace.analyze(image, actions=['emotion'])
|
24 |
dominant_emotion = analysis[0]['dominant_emotion']
|
25 |
+
reverse_search_url = reverse_image_search(image)
|
26 |
+
return f"Analysis: {dominant_emotion}", 90.0, reverse_search_url # Dummy Accuracy
|
27 |
except Exception as e:
|
28 |
+
return f"Error: {str(e)}", 0.0, None
|
29 |
+
|
30 |
+
def reverse_image_search(image):
|
31 |
+
"""Creates a Google Reverse Image Search link for verification."""
|
32 |
+
buffered = io.BytesIO()
|
33 |
+
image.save(buffered, format="PNG")
|
34 |
+
encoded_img = base64.b64encode(buffered.getvalue()).decode()
|
35 |
+
return f"https://www.google.com/searchbyimage?image_url=data:image/png;base64,{encoded_img}"
|
36 |
|
37 |
def verify_news(news_text):
|
38 |
+
"""Searches trusted fact-checking websites for news verification."""
|
39 |
+
sources = [
|
40 |
+
("BBC News", "https://www.bbc.com/news"),
|
41 |
+
("CNN", "https://www.cnn.com"),
|
42 |
+
("Reuters", "https://www.reuters.com"),
|
43 |
+
("FactCheck.org", "https://www.factcheck.org"),
|
44 |
+
("Snopes", "https://www.snopes.com"),
|
45 |
+
("PolitiFact", "https://www.politifact.com"),
|
46 |
+
("Google Search", f"https://www.google.com/search?q={'+'.join(news_text.split())}")
|
47 |
+
]
|
48 |
+
return sources
|
49 |
|
50 |
def extract_video_id(video_url):
|
51 |
"""Extracts the video ID from a YouTube URL."""
|
|
|
57 |
"""Fetches video metadata and runs Fake News detection on it."""
|
58 |
video_id = extract_video_id(video_url)
|
59 |
if not video_id:
|
60 |
+
return "Invalid Video URL", 0.0, None
|
61 |
|
62 |
api_key = "YOUR_YOUTUBE_API_KEY" # Replace with a valid YouTube API Key
|
63 |
metadata_url = f"https://www.googleapis.com/youtube/v3/videos?id={video_id}&part=snippet&key={api_key}"
|
|
|
73 |
|
74 |
# Classify the video metadata text
|
75 |
result, accuracy = classify_text(combined_text)
|
76 |
+
verification_links = verify_news(video_title)
|
77 |
+
return result, accuracy, verification_links
|
78 |
+
return "Unknown", 0.0, None
|
79 |
|
80 |
# Streamlit UI
|
81 |
st.set_page_config(page_title="Fake News Detector", layout="wide")
|
|
|
97 |
st.warning("Please enter some text.")
|
98 |
else:
|
99 |
result, accuracy = classify_text(news_text)
|
100 |
+
verification_links = verify_news(news_text)
|
101 |
st.session_state["text_result"] = result
|
102 |
st.session_state["text_accuracy"] = accuracy
|
103 |
+
st.session_state["text_verification"] = verification_links
|
104 |
|
105 |
# πΉ Image Upload Section
|
106 |
with col2:
|
|
|
110 |
|
111 |
if uploaded_image and analyze_image_clicked:
|
112 |
image = Image.open(uploaded_image)
|
113 |
+
result, accuracy, reverse_search_url = analyze_image(image)
|
114 |
st.session_state["image_result"] = result
|
115 |
st.session_state["image_accuracy"] = accuracy
|
116 |
+
st.session_state["image_search_url"] = reverse_search_url
|
117 |
st.session_state["news_image"] = image # Store Image for Display
|
118 |
|
119 |
# πΉ Video Link Section
|
|
|
126 |
if not video_url.strip():
|
127 |
st.warning("Please enter a valid video link.")
|
128 |
else:
|
129 |
+
result, accuracy, verification_links = fetch_video_metadata(video_url)
|
130 |
st.session_state["video_result"] = result
|
131 |
st.session_state["video_accuracy"] = accuracy
|
132 |
+
st.session_state["video_verification"] = verification_links
|
133 |
st.session_state["video_url"] = video_url # Store Video URL for Display
|
134 |
|
135 |
# πΉ Results Section
|
|
|
145 |
else:
|
146 |
st.success(f"β
This news is **Real**! (Accuracy: {accuracy}%)", icon="β
")
|
147 |
|
148 |
+
st.subheader("π Trusted Fact-Checking Sources")
|
149 |
+
for name, link in st.session_state["text_verification"]:
|
150 |
+
st.markdown(f"[π {name}]({link})")
|
151 |
|
152 |
# πΉ Image Result
|
153 |
if "image_result" in st.session_state:
|
154 |
st.image(st.session_state["news_image"], caption="Uploaded Image", use_column_width=True)
|
155 |
st.info(f"πΌοΈ **Image Analysis Result:** {st.session_state['image_result']} (Accuracy: {st.session_state['image_accuracy']}%)")
|
156 |
+
st.markdown(f"[π Google Reverse Image Search]({st.session_state['image_search_url']})")
|
157 |
|
158 |
# πΉ Video Result
|
159 |
if "video_result" in st.session_state:
|
|
|
164 |
st.success(f"β
**This video is Real!** (Accuracy: {st.session_state['video_accuracy']}%)")
|
165 |
else:
|
166 |
st.warning("β οΈ Unable to verify the authenticity of this video.")
|
167 |
+
|
168 |
+
st.subheader("π Trusted Fact-Checking Sources")
|
169 |
+
for name, link in st.session_state["video_verification"]:
|
170 |
+
st.markdown(f"[π {name}]({link})")
|