Nimzi commited on
Commit
a7f66d4
Β·
verified Β·
1 Parent(s): a7c9258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -42
app.py CHANGED
@@ -1,17 +1,28 @@
1
  import streamlit as st
2
- import os
3
- import torch
4
- import torchaudio
5
- import torchvision
6
- import tensorflow as tf
7
  from transformers import pipeline
8
  from PIL import Image
9
- import requests
 
10
  import io
11
 
12
- # Load a fake news detection model from Hugging Face
13
  fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Streamlit UI
16
  st.set_page_config(page_title="Fake News Detector", layout="wide")
17
  st.title("πŸ“° Fake News Detector")
@@ -19,12 +30,6 @@ st.title("πŸ“° Fake News Detector")
19
  # Tabs for Input and Results
20
  tab1, tab2 = st.tabs(["Input", "Results"])
21
 
22
- # Function to fetch real news links based on content
23
- def fetch_real_news_links(news_text):
24
- query = news_text.replace(" ", "+")
25
- search_url = f"https://www.google.com/search?q={query}+site:bbc.com+OR+site:cnn.com+OR+site:reuters.com"
26
- return search_url
27
-
28
  with tab1:
29
  st.sidebar.title("Select Input Type")
30
  option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video Link"])
@@ -36,46 +41,51 @@ with tab1:
36
  st.warning("Please enter some text.")
37
  else:
38
  st.session_state["news_text"] = news_text
39
- st.session_state["analyze"] = True
40
- st.rerun()
41
 
42
  elif option == "Image":
43
- uploaded_file = st.file_uploader("Upload an image of a news article", type=["jpg", "png", "jpeg"])
44
- if uploaded_file is not None:
45
- image = Image.open(uploaded_file)
46
- st.image(image, caption="Uploaded Image", use_column_width=True)
47
- st.info("πŸ” Text extraction from images is under development.")
 
48
 
49
  elif option == "Video Link":
50
- video_url = st.text_input("Enter a video news link to check")
51
  if st.button("Analyze Video"):
52
  if not video_url.strip():
53
- st.warning("Please enter a valid URL.")
54
  else:
55
- st.info("πŸ” Video analysis is under development.")
 
 
56
 
57
  with tab2:
58
- if st.session_state.get("analyze", False):
59
  news_text = st.session_state.get("news_text", "")
60
- with st.spinner("Analyzing..."):
61
- # Check using Hugging Face model
62
- hf_result = fake_news_pipeline(news_text)[0]['label'].lower()
63
 
64
- if hf_result == "fake":
65
  st.error("❌ This news is likely **Fake**!", icon="⚠️")
66
- conclusion = "The analysis suggests that this news might be fabricated or misleading."
67
- elif hf_result == "real":
68
- st.success("βœ… This news is likely **Real**!", icon="βœ…")
69
- conclusion = "The analysis indicates that this news appears to be credible and factual."
70
  else:
71
- st.info("πŸ€” The result is uncertain. Please verify from trusted sources.")
72
- conclusion = "Further verification is recommended."
73
-
74
- # Conclusion Section
75
- st.subheader("πŸ“Œ Conclusion")
76
- st.write(conclusion)
77
 
78
- # Display real news sources
79
- st.subheader("πŸ”— Related News Sources")
80
- real_news_link = fetch_real_news_links(news_text)
81
- st.markdown(f"[πŸ”— Click here to verify]({real_news_link})")
 
 
 
 
 
 
 
 
 
 
 
 
1
  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
  import io
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")
11
 
12
+ def classify_text(news_text):
13
+ result = fake_news_pipeline(news_text)[0]['label'].lower()
14
+ return "Fake" if result == "fake" else "Real"
15
+
16
+ def analyze_image(image):
17
+ # Convert image to tensor (Placeholder: Model should be updated with a real image classifier)
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")
 
30
  # Tabs for Input and Results
31
  tab1, tab2 = st.tabs(["Input", "Results"])
32
 
 
 
 
 
 
 
33
  with tab1:
34
  st.sidebar.title("Select Input Type")
35
  option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video Link"])
 
41
  st.warning("Please enter some text.")
42
  else:
43
  st.session_state["news_text"] = news_text
44
+ st.session_state["analyze_text"] = True
45
+ st.experimental_rerun()
46
 
47
  elif option == "Image":
48
+ uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"])
49
+ if uploaded_image and st.button("Analyze Image"):
50
+ image = Image.open(uploaded_image)
51
+ st.session_state["news_image"] = image
52
+ st.session_state["analyze_image"] = True
53
+ st.experimental_rerun()
54
 
55
  elif option == "Video Link":
56
+ video_url = st.text_input("Enter the video link:")
57
  if st.button("Analyze Video"):
58
  if not video_url.strip():
59
+ st.warning("Please enter a valid video link.")
60
  else:
61
+ st.session_state["video_url"] = video_url
62
+ st.session_state["analyze_video"] = True
63
+ st.experimental_rerun()
64
 
65
  with tab2:
66
+ if st.session_state.get("analyze_text", False):
67
  news_text = st.session_state.get("news_text", "")
68
+ with st.spinner("Analyzing text..."):
69
+ result = classify_text(news_text)
70
+ verification_link = verify_news(news_text)
71
 
72
+ if result == "Fake":
73
  st.error("❌ This news is likely **Fake**!", icon="⚠️")
 
 
 
 
74
  else:
75
+ st.success("βœ… This news is likely **Real**!", icon="βœ…")
 
 
 
 
 
76
 
77
+ st.subheader("πŸ” Verification & Trusted Sources")
78
+ sources = ["https://www.bbc.com/news", "https://www.cnn.com", "https://www.reuters.com"]
79
+ for link in sources:
80
+ st.markdown(f"[πŸ”— {link}]({link})")
81
+ st.markdown(f"[πŸ”Ž Verify on Google]({verification_link})")
82
+
83
+ if st.session_state.get("analyze_image", False):
84
+ image = st.session_state.get("news_image")
85
+ st.image(image, caption="Uploaded Image", use_column_width=True)
86
+ st.info(analyze_image(image))
87
+
88
+ if st.session_state.get("analyze_video", False):
89
+ video_url = st.session_state.get("video_url", "")
90
+ st.video(video_url)
91
+ st.info("Video analysis feature coming soon!")