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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -51
app.py CHANGED
@@ -2,95 +2,75 @@ import streamlit as st
2
  import requests
3
  from transformers import pipeline
4
  from PIL import Image
5
- import torch
6
  import torchvision.transforms as transforms
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")
10
 
 
11
  def classify_text(news_text):
12
  result = fake_news_pipeline(news_text)[0]
13
  label = result['label'].lower()
14
  score = result['score'] * 100 # Convert to percentage
15
  return ("Fake" if label == "fake" else "Real"), round(score, 2)
16
 
 
 
 
 
 
 
17
  def analyze_image(image):
18
  transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])
19
  image_tensor = transform(image).unsqueeze(0)
20
  return "Image analysis feature coming soon!"
21
 
22
- def verify_news(news_text):
23
- search_url = f"https://www.google.com/search?q={'+'.join(news_text.split())}"
24
- return search_url
25
-
26
  # Streamlit UI
27
  st.set_page_config(page_title="Fake News Detector", layout="wide")
28
  st.title("πŸ“° Fake News Detector")
29
 
30
- # Dividing Input Section
 
 
 
 
31
  col1, col2, col3 = st.columns(3)
32
 
33
- # Text Input Section
34
  with col1:
35
- st.subheader("πŸ“„ Enter Text")
36
- news_text = st.text_area("Enter the news content to check:", height=150)
37
- analyze_text_clicked = st.button("Analyze News")
38
- if analyze_text_clicked:
39
  if not news_text.strip():
40
  st.warning("Please enter some text.")
41
  else:
42
  result, accuracy = classify_text(news_text)
43
- st.session_state["news_text"] = news_text
44
- st.session_state["result_text"] = result
45
- st.session_state["accuracy_text"] = accuracy
 
 
 
 
 
46
 
47
  # Image Upload Section
48
  with col2:
49
- st.subheader("πŸ–ΌοΈ Upload Image")
50
  uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"])
51
- analyze_image_clicked = st.button("Analyze Image")
52
- if uploaded_image and analyze_image_clicked:
53
  image = Image.open(uploaded_image)
54
- st.session_state["news_image"] = image
55
  st.image(image, caption="Uploaded Image", use_column_width=True)
56
  st.info(analyze_image(image))
57
 
58
- # Video Link Input Section
59
  with col3:
60
- st.subheader("πŸŽ₯ Enter Video Link")
61
  video_url = st.text_input("Enter the video link:")
62
- analyze_video_clicked = st.button("Analyze Video")
63
- if analyze_video_clicked:
64
  if not video_url.strip():
65
  st.warning("Please enter a valid video link.")
66
  else:
67
- st.session_state["video_url"] = video_url
68
  st.video(video_url)
69
  st.info("Video analysis feature coming soon!")
70
 
71
- # Results Section
72
- st.subheader("πŸ“Š Analysis Results")
73
- if "result_text" in st.session_state:
74
- result = st.session_state.get("result_text")
75
- accuracy = st.session_state.get("accuracy_text")
76
- verification_link = verify_news(st.session_state.get("news_text"))
77
-
78
- if result == "Fake":
79
- st.error(f"❌ This news is likely **Fake**! (Accuracy: {accuracy}%)", icon="⚠️")
80
- else:
81
- st.success(f"βœ… This news is likely **Real**! (Accuracy: {accuracy}%)", icon="βœ…")
82
-
83
- st.subheader("πŸ” Verification & Trusted Sources")
84
- sources = [
85
- "https://www.bbc.com/news",
86
- "https://www.cnn.com",
87
- "https://www.reuters.com",
88
- "https://factcheck.org",
89
- "https://www.snopes.com",
90
- "https://www.politifact.com",
91
- "https://deepfake-o-meter.ai",
92
- "https://huggingface.co/models?pipeline_tag=text-classification"
93
- ]
94
- for link in sources:
95
- st.markdown(f"[πŸ”— {link}]({link})")
96
- st.markdown(f"[πŸ”Ž Verify on Google]({verification_link})")
 
2
  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()
14
  score = result['score'] * 100 # Convert to percentage
15
  return ("Fake" if label == "fake" else "Real"), round(score, 2)
16
 
17
+ # Function to verify news with Google Fact Check API
18
+ 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")
30
  st.title("πŸ“° Fake News Detector")
31
 
32
+ # Sidebar Navigation
33
+ st.sidebar.title("Select Input Type")
34
+ option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video Link"])
35
+
36
+ # Input Sections
37
  col1, col2, col3 = st.columns(3)
38
 
39
+ # Text Analysis Section
40
  with col1:
41
+ st.subheader("πŸ“„ Text News Check")
42
+ news_text = st.text_area("Enter the news content:", height=200)
43
+ if st.button("Analyze News"):
 
44
  if not news_text.strip():
45
  st.warning("Please enter some text.")
46
  else:
47
  result, accuracy = classify_text(news_text)
48
+ verification_link = verify_news(news_text)
49
+
50
+ if result == "Fake":
51
+ st.error(f"❌ Likely **Fake News**! (Confidence: {accuracy}%)")
52
+ else:
53
+ st.success(f"βœ… Likely **Real News**! (Confidence: {accuracy}%)")
54
+
55
+ st.markdown(f"[πŸ”Ž Verify on Google Fact Check]({verification_link})")
56
 
57
  # Image Upload Section
58
  with col2:
59
+ st.subheader("πŸ–ΌοΈ Image News Check")
60
  uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"])
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:
68
+ st.subheader("πŸŽ₯ Video News Check")
69
  video_url = st.text_input("Enter the video link:")
70
+ if st.button("Analyze Video"):
 
71
  if not video_url.strip():
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