Nimzi commited on
Commit
a906516
Β·
verified Β·
1 Parent(s): ea6e24c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -3,11 +3,14 @@ import requests
3
  from transformers import pipeline
4
  from PIL import Image
5
  import torchvision.transforms as transforms
 
 
 
6
 
7
- # Load a more accurate fake news detection model
8
  fake_news_pipeline = pipeline("text-classification", model="roberta-base-openai-detector")
9
 
10
- # Function to classify news text
11
  def classify_text(news_text):
12
  result = fake_news_pipeline(news_text)[0]
13
  label = result['label'].lower()
@@ -19,11 +22,35 @@ def verify_news(news_text):
19
  search_url = f"https://toolbox.google.com/factcheck/explorer/search/{'+'.join(news_text.split())}"
20
  return search_url
21
 
22
- # Function to analyze images (Dummy function for now)
23
  def analyze_image(image):
24
  transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])
25
  image_tensor = transform(image).unsqueeze(0)
26
- return "Image analysis feature coming soon!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Streamlit UI
29
  st.set_page_config(page_title="Fake News Detector", layout="wide")
@@ -61,7 +88,8 @@ with col2:
61
  if uploaded_image and st.button("Analyze Image"):
62
  image = Image.open(uploaded_image)
63
  st.image(image, caption="Uploaded Image", use_column_width=True)
64
- st.info(analyze_image(image))
 
65
 
66
  # Video Link Section
67
  with col3:
@@ -72,5 +100,5 @@ with col3:
72
  st.warning("Please enter a valid video link.")
73
  else:
74
  st.video(video_url)
75
- st.info("Video analysis feature coming soon!")
76
-
 
3
  from transformers import pipeline
4
  from PIL import Image
5
  import torchvision.transforms as transforms
6
+ import torch
7
+ import cv2
8
+ import numpy as np
9
 
10
+ # Load Fake News Detection Model
11
  fake_news_pipeline = pipeline("text-classification", model="roberta-base-openai-detector")
12
 
13
+ # Function to classify text news
14
  def classify_text(news_text):
15
  result = fake_news_pipeline(news_text)[0]
16
  label = result['label'].lower()
 
22
  search_url = f"https://toolbox.google.com/factcheck/explorer/search/{'+'.join(news_text.split())}"
23
  return search_url
24
 
25
+ # Function to analyze images for fake news
26
  def analyze_image(image):
27
  transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])
28
  image_tensor = transform(image).unsqueeze(0)
29
+
30
+ # Convert to OpenCV format
31
+ image_cv = np.array(image)
32
+ image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
33
+
34
+ # Apply Fake Image Detection (Basic Example)
35
+ edges = cv2.Canny(image_cv, 100, 200)
36
+ edge_percentage = np.sum(edges) / (image_cv.shape[0] * image_cv.shape[1])
37
+
38
+ if edge_percentage > 0.1:
39
+ return "❌ This image might be **manipulated** or **deepfake**!"
40
+ else:
41
+ return "βœ… This image appears to be **authentic**."
42
+
43
+ # Function to analyze video metadata
44
+ def analyze_video(video_url):
45
+ api_url = f"https://noembed.com/embed?url={video_url}"
46
+ response = requests.get(api_url).json()
47
+
48
+ if "title" in response:
49
+ title = response["title"]
50
+ source = response["provider_name"]
51
+ return f"πŸŽ₯ Video Title: {title}\nπŸ”— Source: {source}"
52
+ else:
53
+ return "❌ Unable to fetch video details! Check if the link is correct."
54
 
55
  # Streamlit UI
56
  st.set_page_config(page_title="Fake News Detector", layout="wide")
 
88
  if uploaded_image and st.button("Analyze Image"):
89
  image = Image.open(uploaded_image)
90
  st.image(image, caption="Uploaded Image", use_column_width=True)
91
+ result = analyze_image(image)
92
+ st.info(result)
93
 
94
  # Video Link Section
95
  with col3:
 
100
  st.warning("Please enter a valid video link.")
101
  else:
102
  st.video(video_url)
103
+ result = analyze_video(video_url)
104
+ st.info(result)