Update app.py
Browse files
app.py
CHANGED
@@ -84,157 +84,58 @@
|
|
84 |
|
85 |
# st.markdown("πΉ **Developed for Fake News & Deepfake Detection Hackathon**")
|
86 |
|
87 |
-
# import streamlit as st
|
88 |
-
# import numpy as np
|
89 |
-
# import cv2
|
90 |
-
# import tempfile
|
91 |
-
# import os
|
92 |
-
# from PIL import Image
|
93 |
-
# import tensorflow as tf
|
94 |
-
# from transformers import pipeline
|
95 |
-
# from tensorflow.keras.applications import Xception, EfficientNetB7
|
96 |
-
# from tensorflow.keras.models import Model
|
97 |
-
# from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
|
98 |
-
# from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
99 |
-
|
100 |
-
# # ---- Page Configuration ----
|
101 |
-
# st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
|
102 |
-
|
103 |
-
# st.title("π° Fake News & Deepfake Detection Tool")
|
104 |
-
# st.write("π Detect Fake News, Deepfake Images, and Videos using AI")
|
105 |
-
|
106 |
-
# # Load Models
|
107 |
-
# fake_news_detector = pipeline("text-classification", model="microsoft/deberta-v3-base")
|
108 |
-
|
109 |
-
# # Load Deepfake Detection Models
|
110 |
-
# base_model_image = Xception(weights="imagenet", include_top=False)
|
111 |
-
# base_model_image.trainable = False # Freeze base layers
|
112 |
-
# x = GlobalAveragePooling2D()(base_model_image.output)
|
113 |
-
# x = Dense(1024, activation="relu")(x)
|
114 |
-
# x = Dense(1, activation="sigmoid")(x) # Sigmoid for probability output
|
115 |
-
# deepfake_image_model = Model(inputs=base_model_image.input, outputs=x)
|
116 |
-
|
117 |
-
# base_model_video = EfficientNetB7(weights="imagenet", include_top=False)
|
118 |
-
# base_model_video.trainable = False
|
119 |
-
# x = GlobalAveragePooling2D()(base_model_video.output)
|
120 |
-
# x = Dense(1024, activation="relu")(x)
|
121 |
-
# x = Dense(1, activation="sigmoid")(x)
|
122 |
-
# deepfake_video_model = Model(inputs=base_model_video.input, outputs=x)
|
123 |
-
|
124 |
-
# # Function to Preprocess Image
|
125 |
-
# def preprocess_image(image_path):
|
126 |
-
# img = load_img(image_path, target_size=(299, 299)) # Xception expects 299x299
|
127 |
-
# img = img_to_array(img)
|
128 |
-
# img = np.expand_dims(img, axis=0)
|
129 |
-
# img /= 255.0 # Normalize pixel values
|
130 |
-
# return img
|
131 |
-
|
132 |
-
# # Function to Detect Deepfake Image
|
133 |
-
# def detect_deepfake_image(image_path):
|
134 |
-
# image = preprocess_image(image_path)
|
135 |
-
# prediction = deepfake_image_model.predict(image)[0][0]
|
136 |
-
# confidence = round(float(prediction), 2)
|
137 |
-
# label = "FAKE" if confidence > 0.5 else "REAL"
|
138 |
-
# return {"label": label, "score": confidence}
|
139 |
-
|
140 |
-
# # ---- Fake News Detection Section ----
|
141 |
-
# st.subheader("π Fake News Detection")
|
142 |
-
# news_input = st.text_area("Enter News Text:", placeholder="Type here...")
|
143 |
-
|
144 |
-
# if st.button("Check News"):
|
145 |
-
# st.write("π Processing...")
|
146 |
-
# prediction = fake_news_detector(news_input)
|
147 |
-
# label = prediction[0]['label']
|
148 |
-
# confidence = prediction[0]['score']
|
149 |
-
|
150 |
-
# if label == "FAKE":
|
151 |
-
# st.error(f"β οΈ Result: This news is FAKE. (Confidence: {confidence:.2f})")
|
152 |
-
# else:
|
153 |
-
# st.success(f"β
Result: This news is REAL. (Confidence: {confidence:.2f})")
|
154 |
-
|
155 |
-
# # ---- Deepfake Image Detection Section ----
|
156 |
-
# st.subheader("πΈ Deepfake Image Detection")
|
157 |
-
# uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
158 |
-
|
159 |
-
# if uploaded_image is not None:
|
160 |
-
# temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
161 |
-
# img = Image.open(uploaded_image).convert("RGB")
|
162 |
-
# img.save(temp_file.name, "JPEG")
|
163 |
-
# st.image(temp_file.name, caption="πΌοΈ Uploaded Image", use_column_width=True)
|
164 |
-
|
165 |
-
# if st.button("Analyze Image"):
|
166 |
-
# st.write("π Processing...")
|
167 |
-
# result = detect_deepfake_image(temp_file.name)
|
168 |
-
|
169 |
-
# if result["label"] == "FAKE":
|
170 |
-
# st.error(f"β οΈ Result: This image is a Deepfake. (Confidence: {result['score']:.2f})")
|
171 |
-
# else:
|
172 |
-
# st.success(f"β
Result: This image is Real. (Confidence: {1 - result['score']:.2f})")
|
173 |
-
|
174 |
-
# # ---- Deepfake Video Detection Section ----
|
175 |
-
# st.subheader("π₯ Deepfake Video Detection")
|
176 |
-
# uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
|
177 |
-
|
178 |
-
# def detect_deepfake_video(video_path):
|
179 |
-
# cap = cv2.VideoCapture(video_path)
|
180 |
-
# frame_scores = []
|
181 |
-
|
182 |
-
# while cap.isOpened():
|
183 |
-
# ret, frame = cap.read()
|
184 |
-
# if not ret:
|
185 |
-
# break
|
186 |
-
|
187 |
-
# frame_path = "temp_frame.jpg"
|
188 |
-
# cv2.imwrite(frame_path, frame)
|
189 |
-
# result = detect_deepfake_image(frame_path)
|
190 |
-
# frame_scores.append(result["score"])
|
191 |
-
# os.remove(frame_path)
|
192 |
-
|
193 |
-
# cap.release()
|
194 |
-
# avg_score = np.mean(frame_scores)
|
195 |
-
# final_label = "FAKE" if avg_score > 0.5 else "REAL"
|
196 |
-
# return {"label": final_label, "score": round(float(avg_score), 2)}
|
197 |
-
|
198 |
-
# if uploaded_video is not None:
|
199 |
-
# st.video(uploaded_video)
|
200 |
-
# temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
201 |
-
# with open(temp_file.name, "wb") as f:
|
202 |
-
# f.write(uploaded_video.read())
|
203 |
-
|
204 |
-
# if st.button("Analyze Video"):
|
205 |
-
# st.write("π Processing...")
|
206 |
-
# result = detect_deepfake_video(temp_file.name)
|
207 |
-
|
208 |
-
# if result["label"] == "FAKE":
|
209 |
-
# st.warning(f"β οΈ Result: This video contains Deepfake elements. (Confidence: {result['score']:.2f})")
|
210 |
-
# else:
|
211 |
-
# st.success(f"β
Result: This video is Real. (Confidence: {1 - result['score']:.2f})")
|
212 |
-
|
213 |
-
# st.markdown("πΉ **Developed for Fake News & Deepfake Detection Hackathon**")
|
214 |
-
|
215 |
import streamlit as st
|
216 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
217 |
from transformers import pipeline
|
218 |
-
import
|
|
|
|
|
|
|
219 |
|
220 |
# ---- Page Configuration ----
|
221 |
-
st.set_page_config(page_title="Fake
|
222 |
|
223 |
st.title("π° Fake News & Deepfake Detection Tool")
|
224 |
st.write("π Detect Fake News, Deepfake Images, and Videos using AI")
|
225 |
|
226 |
-
# Load
|
227 |
-
fake_news_detector = pipeline("text-classification", model="
|
228 |
-
|
229 |
-
#
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
|
239 |
# ---- Fake News Detection Section ----
|
240 |
st.subheader("π Fake News Detection")
|
@@ -242,27 +143,71 @@ news_input = st.text_area("Enter News Text:", placeholder="Type here...")
|
|
242 |
|
243 |
if st.button("Check News"):
|
244 |
st.write("π Processing...")
|
245 |
-
|
246 |
-
# Step 1: AI-Based Classification
|
247 |
prediction = fake_news_detector(news_input)
|
248 |
label = prediction[0]['label']
|
249 |
confidence = prediction[0]['score']
|
250 |
|
251 |
-
# Step 2: Fact Checking via API
|
252 |
-
fact_check_result = fact_check_google(news_input)
|
253 |
-
|
254 |
if label == "FAKE":
|
255 |
st.error(f"β οΈ Result: This news is FAKE. (Confidence: {confidence:.2f})")
|
256 |
else:
|
257 |
st.success(f"β
Result: This news is REAL. (Confidence: {confidence:.2f})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
258 |
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
|
267 |
st.markdown("πΉ **Developed for Fake News & Deepfake Detection Hackathon**")
|
268 |
-
|
|
|
84 |
|
85 |
# st.markdown("πΉ **Developed for Fake News & Deepfake Detection Hackathon**")
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
import streamlit as st
|
88 |
import numpy as np
|
89 |
+
import cv2
|
90 |
+
import tempfile
|
91 |
+
import os
|
92 |
+
from PIL import Image
|
93 |
+
import tensorflow as tf
|
94 |
from transformers import pipeline
|
95 |
+
from tensorflow.keras.applications import Xception, EfficientNetB7
|
96 |
+
from tensorflow.keras.models import Model
|
97 |
+
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
|
98 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
99 |
|
100 |
# ---- Page Configuration ----
|
101 |
+
st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
|
102 |
|
103 |
st.title("π° Fake News & Deepfake Detection Tool")
|
104 |
st.write("π Detect Fake News, Deepfake Images, and Videos using AI")
|
105 |
|
106 |
+
# Load Models
|
107 |
+
fake_news_detector = pipeline("text-classification", model="microsoft/deberta-v3-base")
|
108 |
+
|
109 |
+
# Load Deepfake Detection Models
|
110 |
+
base_model_image = Xception(weights="imagenet", include_top=False)
|
111 |
+
base_model_image.trainable = False # Freeze base layers
|
112 |
+
x = GlobalAveragePooling2D()(base_model_image.output)
|
113 |
+
x = Dense(1024, activation="relu")(x)
|
114 |
+
x = Dense(1, activation="sigmoid")(x) # Sigmoid for probability output
|
115 |
+
deepfake_image_model = Model(inputs=base_model_image.input, outputs=x)
|
116 |
+
|
117 |
+
base_model_video = EfficientNetB7(weights="imagenet", include_top=False)
|
118 |
+
base_model_video.trainable = False
|
119 |
+
x = GlobalAveragePooling2D()(base_model_video.output)
|
120 |
+
x = Dense(1024, activation="relu")(x)
|
121 |
+
x = Dense(1, activation="sigmoid")(x)
|
122 |
+
deepfake_video_model = Model(inputs=base_model_video.input, outputs=x)
|
123 |
+
|
124 |
+
# Function to Preprocess Image
|
125 |
+
def preprocess_image(image_path):
|
126 |
+
img = load_img(image_path, target_size=(299, 299)) # Xception expects 299x299
|
127 |
+
img = img_to_array(img)
|
128 |
+
img = np.expand_dims(img, axis=0)
|
129 |
+
img /= 255.0 # Normalize pixel values
|
130 |
+
return img
|
131 |
+
|
132 |
+
# Function to Detect Deepfake Image
|
133 |
+
def detect_deepfake_image(image_path):
|
134 |
+
image = preprocess_image(image_path)
|
135 |
+
prediction = deepfake_image_model.predict(image)[0][0]
|
136 |
+
confidence = round(float(prediction), 2)
|
137 |
+
label = "FAKE" if confidence > 0.5 else "REAL"
|
138 |
+
return {"label": label, "score": confidence}
|
139 |
|
140 |
# ---- Fake News Detection Section ----
|
141 |
st.subheader("π Fake News Detection")
|
|
|
143 |
|
144 |
if st.button("Check News"):
|
145 |
st.write("π Processing...")
|
|
|
|
|
146 |
prediction = fake_news_detector(news_input)
|
147 |
label = prediction[0]['label']
|
148 |
confidence = prediction[0]['score']
|
149 |
|
|
|
|
|
|
|
150 |
if label == "FAKE":
|
151 |
st.error(f"β οΈ Result: This news is FAKE. (Confidence: {confidence:.2f})")
|
152 |
else:
|
153 |
st.success(f"β
Result: This news is REAL. (Confidence: {confidence:.2f})")
|
154 |
+
|
155 |
+
# ---- Deepfake Image Detection Section ----
|
156 |
+
st.subheader("πΈ Deepfake Image Detection")
|
157 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
158 |
+
|
159 |
+
if uploaded_image is not None:
|
160 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
161 |
+
img = Image.open(uploaded_image).convert("RGB")
|
162 |
+
img.save(temp_file.name, "JPEG")
|
163 |
+
st.image(temp_file.name, caption="πΌοΈ Uploaded Image", use_column_width=True)
|
164 |
|
165 |
+
if st.button("Analyze Image"):
|
166 |
+
st.write("π Processing...")
|
167 |
+
result = detect_deepfake_image(temp_file.name)
|
168 |
+
|
169 |
+
if result["label"] == "FAKE":
|
170 |
+
st.error(f"β οΈ Result: This image is a Deepfake. (Confidence: {result['score']:.2f})")
|
171 |
+
else:
|
172 |
+
st.success(f"β
Result: This image is Real. (Confidence: {1 - result['score']:.2f})")
|
173 |
+
|
174 |
+
# ---- Deepfake Video Detection Section ----
|
175 |
+
st.subheader("π₯ Deepfake Video Detection")
|
176 |
+
uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
|
177 |
+
|
178 |
+
def detect_deepfake_video(video_path):
|
179 |
+
cap = cv2.VideoCapture(video_path)
|
180 |
+
frame_scores = []
|
181 |
+
|
182 |
+
while cap.isOpened():
|
183 |
+
ret, frame = cap.read()
|
184 |
+
if not ret:
|
185 |
+
break
|
186 |
+
|
187 |
+
frame_path = "temp_frame.jpg"
|
188 |
+
cv2.imwrite(frame_path, frame)
|
189 |
+
result = detect_deepfake_image(frame_path)
|
190 |
+
frame_scores.append(result["score"])
|
191 |
+
os.remove(frame_path)
|
192 |
+
|
193 |
+
cap.release()
|
194 |
+
avg_score = np.mean(frame_scores)
|
195 |
+
final_label = "FAKE" if avg_score > 0.5 else "REAL"
|
196 |
+
return {"label": final_label, "score": round(float(avg_score), 2)}
|
197 |
+
|
198 |
+
if uploaded_video is not None:
|
199 |
+
st.video(uploaded_video)
|
200 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
201 |
+
with open(temp_file.name, "wb") as f:
|
202 |
+
f.write(uploaded_video.read())
|
203 |
+
|
204 |
+
if st.button("Analyze Video"):
|
205 |
+
st.write("π Processing...")
|
206 |
+
result = detect_deepfake_video(temp_file.name)
|
207 |
+
|
208 |
+
if result["label"] == "FAKE":
|
209 |
+
st.warning(f"β οΈ Result: This video contains Deepfake elements. (Confidence: {result['score']:.2f})")
|
210 |
+
else:
|
211 |
+
st.success(f"β
Result: This video is Real. (Confidence: {1 - result['score']:.2f})")
|
212 |
|
213 |
st.markdown("πΉ **Developed for Fake News & Deepfake Detection Hackathon**")
|
|