Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,28 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
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
|
|
|
10 |
import io
|
11 |
|
12 |
-
# Load
|
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["
|
40 |
-
st.
|
41 |
|
42 |
elif option == "Image":
|
43 |
-
|
44 |
-
if
|
45 |
-
image = Image.open(
|
46 |
-
st.
|
47 |
-
st.
|
|
|
48 |
|
49 |
elif option == "Video Link":
|
50 |
-
video_url = st.text_input("Enter
|
51 |
if st.button("Analyze Video"):
|
52 |
if not video_url.strip():
|
53 |
-
st.warning("Please enter a valid
|
54 |
else:
|
55 |
-
st.
|
|
|
|
|
56 |
|
57 |
with tab2:
|
58 |
-
if st.session_state.get("
|
59 |
news_text = st.session_state.get("news_text", "")
|
60 |
-
with st.spinner("Analyzing..."):
|
61 |
-
|
62 |
-
|
63 |
|
64 |
-
if
|
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.
|
72 |
-
conclusion = "Further verification is recommended."
|
73 |
-
|
74 |
-
# Conclusion Section
|
75 |
-
st.subheader("π Conclusion")
|
76 |
-
st.write(conclusion)
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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!")
|