Nimzi commited on
Commit
2ee8dae
Β·
verified Β·
1 Parent(s): a1b2fed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -13
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 provides a result."""
21
  try:
22
  analysis = DeepFace.analyze(image, actions=['emotion'])
23
  dominant_emotion = analysis[0]['dominant_emotion']
24
- return f"Analysis Complete - Dominant Emotion: {dominant_emotion}", 90.0 # Dummy Accuracy
 
25
  except Exception as e:
26
- return f"Error: {str(e)}", 0.0
 
 
 
 
 
 
 
27
 
28
  def verify_news(news_text):
29
- """Generates a Google search link for verification."""
30
- search_url = f"https://www.google.com/search?q={'+'.join(news_text.split())}"
31
- return search_url
 
 
 
 
 
 
 
 
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
- return result, accuracy
60
- return "Unknown", 0.0
 
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
- verification_link = verify_news(news_text)
127
- st.markdown(f"[πŸ”Ž Verify on Google]({verification_link})")
 
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})")