Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
9 |
-
fake_news_pipeline = pipeline("text-classification", model="
|
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 |
-
#
|
|
|
|
|
|
|
|
|
31 |
col1, col2, col3 = st.columns(3)
|
32 |
|
33 |
-
# Text
|
34 |
with col1:
|
35 |
-
st.subheader("π
|
36 |
-
news_text = st.text_area("Enter the news content
|
37 |
-
|
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 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
# Image Upload Section
|
48 |
with col2:
|
49 |
-
st.subheader("πΌοΈ
|
50 |
uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"])
|
51 |
-
|
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
|
59 |
with col3:
|
60 |
-
st.subheader("π₯
|
61 |
video_url = st.text_input("Enter the video link:")
|
62 |
-
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|