Nimzi commited on
Commit
a1b2fed
·
verified ·
1 Parent(s): a346939

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -12
app.py CHANGED
@@ -4,6 +4,7 @@ from transformers import pipeline
4
  from deepface import DeepFace
5
  from PIL import Image
6
  import io
 
7
 
8
  # Load Fake News Detection Model from Hugging Face
9
  fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
@@ -29,18 +30,34 @@ def verify_news(news_text):
29
  search_url = f"https://www.google.com/search?q={'+'.join(news_text.split())}"
30
  return search_url
31
 
32
- def check_video_fake_news(video_url):
33
- """Uses Google Fact-Check API for video verification."""
34
- api_url = f"https://factchecktools.googleapis.com/v1alpha1/claims:search?query={video_url}&key=YOUR_API_KEY"
35
- response = requests.get(api_url)
 
 
 
 
 
 
 
 
 
 
36
 
 
37
  if response.status_code == 200:
38
  data = response.json()
39
- if 'claims' in data:
40
- return "Fake", 85.0 # Fake News Detected (Dummy Value)
41
- else:
42
- return "Real", 92.0 # Seems Real (Dummy Value)
43
- return "Unknown", 0.0 # Could Not Verify
 
 
 
 
 
44
 
45
  # Streamlit UI
46
  st.set_page_config(page_title="Fake News Detector", layout="wide")
@@ -88,7 +105,7 @@ with col3:
88
  if not video_url.strip():
89
  st.warning("Please enter a valid video link.")
90
  else:
91
- result, accuracy = check_video_fake_news(video_url)
92
  st.session_state["video_result"] = result
93
  st.session_state["video_accuracy"] = accuracy
94
  st.session_state["video_url"] = video_url # Store Video URL for Display
@@ -117,5 +134,9 @@ if "image_result" in st.session_state:
117
  # 🔹 Video Result
118
  if "video_result" in st.session_state:
119
  st.video(st.session_state["video_url"])
120
- st.info(f"🎥 **Video Analysis Result:** {st.session_state['video_result']} (Accuracy: {st.session_state['video_accuracy']}%)")
121
-
 
 
 
 
 
4
  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")
 
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."""
35
+ pattern = r"(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
36
+ match = re.search(pattern, video_url)
37
+ return match.group(1) if match else None
38
+
39
+ 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}"
47
 
48
+ response = requests.get(metadata_url)
49
  if response.status_code == 200:
50
  data = response.json()
51
+ if "items" in data and len(data["items"]) > 0:
52
+ video_details = data["items"][0]["snippet"]
53
+ video_title = video_details["title"]
54
+ video_description = video_details["description"]
55
+ combined_text = video_title + " " + video_description
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")
 
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
 
134
  # 🔹 Video Result
135
  if "video_result" in st.session_state:
136
  st.video(st.session_state["video_url"])
137
+ if st.session_state["video_result"] == "Fake":
138
+ st.error(f"❌ **This video is Fake!** (Accuracy: {st.session_state['video_accuracy']}%)")
139
+ elif st.session_state["video_result"] == "Real":
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.")