Nimzi commited on
Commit
5bc427c
Β·
verified Β·
1 Parent(s): 92ff348

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -70
app.py CHANGED
@@ -10,11 +10,12 @@ import io
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!"
@@ -27,76 +28,72 @@ def verify_news(news_text):
27
  st.set_page_config(page_title="Fake News Detector", layout="wide")
28
  st.title("πŸ“° Fake News Detector")
29
 
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"])
 
 
 
 
 
 
 
 
 
36
 
37
- if option == "Text":
38
- st.subheader("πŸ“ Enter News Text")
39
- news_text = st.text_area("Enter the news content to check:", height=200)
40
- if st.button("Analyze News", key="text_analyze", help="Click to analyze the text input"):
41
- if not news_text.strip():
42
- st.warning("⚠️ Please enter some text.")
43
- else:
44
- st.session_state["news_text"] = news_text
45
- st.session_state["analyze_text"] = True
46
- st.rerun()
47
 
48
- elif option == "Image":
49
- st.subheader("πŸ–Ό Upload News Image")
50
- uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"], key="image_upload")
51
- if uploaded_image and st.button("Analyze Image", key="image_analyze", help="Click to analyze the uploaded image"):
52
- image = Image.open(uploaded_image)
53
- st.session_state["news_image"] = image
54
- st.session_state["analyze_image"] = True
55
- st.rerun()
56
 
57
- elif option == "Video Link":
58
- st.subheader("πŸŽ₯ Enter Video Link")
59
- video_url = st.text_input("Enter the video link:", key="video_input")
60
- if st.button("Analyze Video", key="video_analyze", help="Click to analyze the video link"):
61
- if not video_url.strip():
62
- st.warning("⚠️ Please enter a valid video link.")
63
- else:
64
- st.session_state["video_url"] = video_url
65
- st.session_state["analyze_video"] = True
66
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- with tab2:
69
- if st.session_state.get("analyze_text", False):
70
- news_text = st.session_state.get("news_text", "")
71
- with st.spinner("Analyzing text..."):
72
- result = classify_text(news_text)
73
- verification_link = verify_news(news_text)
74
-
75
- if result == "Fake":
76
- st.error("❌ This news is likely **Fake**!", icon="⚠️")
77
- st.markdown("<style>div.stAlert {background-color: #FFCCCC;}</style>", unsafe_allow_html=True)
78
- else:
79
- st.success("βœ… This news is likely **Real**!", icon="βœ…")
80
- st.markdown("<style>div.stAlert {background-color: #CCFFCC;}</style>", unsafe_allow_html=True)
81
-
82
- st.subheader("πŸ” Verification & Trusted Sources")
83
- sources = [
84
- "https://www.bbc.com/news",
85
- "https://www.cnn.com",
86
- "https://www.reuters.com",
87
- "https://huggingface.co/datasets",
88
- "https://wildfireai.com/deepfake-detection"
89
- ]
90
- for link in sources:
91
- st.markdown(f"[πŸ”— {link}]({link})")
92
- st.markdown(f"[πŸ”Ž Verify on Google]({verification_link})")
93
 
94
- if st.session_state.get("analyze_image", False):
95
- image = st.session_state.get("news_image")
96
- st.image(image, caption="Uploaded Image", use_column_width=True)
97
- st.info(analyze_image(image))
98
-
99
- if st.session_state.get("analyze_video", False):
100
- video_url = st.session_state.get("video_url", "")
101
- st.video(video_url)
102
- st.info("Video analysis feature coming soon!")
 
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]
14
+ label = result['label'].lower()
15
+ score = result['score'] * 100 # Convert to percentage
16
+ return ("Fake" if label == "fake" else "Real"), round(score, 2)
17
 
18
  def analyze_image(image):
 
19
  transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])
20
  image_tensor = transform(image).unsqueeze(0)
21
  return "Image analysis feature coming soon!"
 
28
  st.set_page_config(page_title="Fake News Detector", layout="wide")
29
  st.title("πŸ“° Fake News Detector")
30
 
31
+ # Sidebar for Input Selection
32
+ st.sidebar.title("Select Input Type")
33
+ option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video Link"])
34
 
35
+ # Input Section
36
+ if option == "Text":
37
+ news_text = st.text_area("Enter the news content to check:", height=200)
38
+ if st.button("Analyze News", key="analyze_text"):
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["analyze_text"] = True
45
+ st.session_state["result_text"] = result
46
+ st.session_state["accuracy_text"] = accuracy
47
 
48
+ elif option == "Image":
49
+ uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"])
50
+ if uploaded_image and st.button("Analyze Image", key="analyze_image"):
51
+ image = Image.open(uploaded_image)
52
+ st.session_state["news_image"] = image
53
+ st.session_state["analyze_image"] = True
 
 
 
 
54
 
55
+ elif option == "Video Link":
56
+ video_url = st.text_input("Enter the video link:")
57
+ if st.button("Analyze Video", key="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
 
64
+ # Results Section
65
+ st.subheader("πŸ“Š Analysis Results")
66
+ if st.session_state.get("analyze_text", False):
67
+ result = st.session_state.get("result_text")
68
+ accuracy = st.session_state.get("accuracy_text")
69
+ verification_link = verify_news(st.session_state.get("news_text"))
70
+
71
+ if result == "Fake":
72
+ st.error(f"❌ This news is likely **Fake**! (Accuracy: {accuracy}%)", icon="⚠️")
73
+ else:
74
+ st.success(f"βœ… This news is likely **Real**! (Accuracy: {accuracy}%)", icon="βœ…")
75
+
76
+ st.subheader("πŸ” Verification & Trusted Sources")
77
+ sources = [
78
+ "https://www.bbc.com/news",
79
+ "https://www.cnn.com",
80
+ "https://www.reuters.com",
81
+ "https://factcheck.org",
82
+ "https://www.snopes.com",
83
+ "https://www.politifact.com",
84
+ "https://deepfake-o-meter.ai",
85
+ "https://huggingface.co/models?pipeline_tag=text-classification"
86
+ ]
87
+ for link in sources:
88
+ st.markdown(f"[οΏ½οΏ½οΏ½οΏ½ {link}]({link})")
89
+ st.markdown(f"[πŸ”Ž Verify on Google]({verification_link})")
90
 
91
+ if st.session_state.get("analyze_image", False):
92
+ image = st.session_state.get("news_image")
93
+ st.image(image, caption="Uploaded Image", use_column_width=True)
94
+ st.info(analyze_image(image))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ if st.session_state.get("analyze_video", False):
97
+ video_url = st.session_state.get("video_url", "")
98
+ st.video(video_url)
99
+ st.info("Video analysis feature coming soon!")