Update app.py
Browse files
app.py
CHANGED
@@ -84,134 +84,6 @@
|
|
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 |
import cv2
|
@@ -219,7 +91,7 @@ import tempfile
|
|
219 |
import os
|
220 |
from PIL import Image
|
221 |
import tensorflow as tf
|
222 |
-
from transformers import
|
223 |
from tensorflow.keras.applications import Xception, EfficientNetB7
|
224 |
from tensorflow.keras.models import Model
|
225 |
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
|
@@ -228,33 +100,18 @@ from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
|
228 |
# ---- Page Configuration ----
|
229 |
st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
|
230 |
|
231 |
-
st.title("
|
232 |
-
st.write("
|
233 |
|
234 |
-
# Load
|
235 |
-
|
236 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
237 |
-
fake_news_model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
238 |
-
|
239 |
-
def detect_fake_news(text):
|
240 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
241 |
-
outputs = fake_news_model(**inputs)
|
242 |
-
probs = outputs.logits.softmax(dim=1).detach().numpy()[0]
|
243 |
-
|
244 |
-
fake_prob = probs[0]
|
245 |
-
real_prob = probs[1]
|
246 |
-
|
247 |
-
if fake_prob > real_prob:
|
248 |
-
return {"label": "FAKE", "confidence": fake_prob}
|
249 |
-
else:
|
250 |
-
return {"label": "REAL", "confidence": real_prob}
|
251 |
|
252 |
# Load Deepfake Detection Models
|
253 |
base_model_image = Xception(weights="imagenet", include_top=False)
|
254 |
-
base_model_image.trainable = False
|
255 |
x = GlobalAveragePooling2D()(base_model_image.output)
|
256 |
x = Dense(1024, activation="relu")(x)
|
257 |
-
x = Dense(1, activation="sigmoid")(x)
|
258 |
deepfake_image_model = Model(inputs=base_model_image.input, outputs=x)
|
259 |
|
260 |
base_model_video = EfficientNetB7(weights="imagenet", include_top=False)
|
@@ -266,10 +123,10 @@ deepfake_video_model = Model(inputs=base_model_video.input, outputs=x)
|
|
266 |
|
267 |
# Function to Preprocess Image
|
268 |
def preprocess_image(image_path):
|
269 |
-
img = load_img(image_path, target_size=(299, 299))
|
270 |
img = img_to_array(img)
|
271 |
img = np.expand_dims(img, axis=0)
|
272 |
-
img /= 255.0
|
273 |
return img
|
274 |
|
275 |
# Function to Detect Deepfake Image
|
@@ -281,37 +138,41 @@ def detect_deepfake_image(image_path):
|
|
281 |
return {"label": label, "score": confidence}
|
282 |
|
283 |
# ---- Fake News Detection Section ----
|
284 |
-
st.subheader("
|
285 |
news_input = st.text_area("Enter News Text:", placeholder="Type here...")
|
286 |
|
287 |
if st.button("Check News"):
|
288 |
-
st.write("
|
289 |
-
|
290 |
-
|
291 |
-
|
|
|
|
|
|
|
292 |
else:
|
293 |
-
st.success(f"β
Result: This news is REAL. (Confidence: {
|
294 |
|
295 |
# ---- Deepfake Image Detection Section ----
|
296 |
-
st.subheader("
|
297 |
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
298 |
|
299 |
if uploaded_image is not None:
|
300 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
301 |
img = Image.open(uploaded_image).convert("RGB")
|
302 |
img.save(temp_file.name, "JPEG")
|
303 |
-
st.image(temp_file.name, caption="
|
304 |
|
305 |
if st.button("Analyze Image"):
|
306 |
-
st.write("
|
307 |
result = detect_deepfake_image(temp_file.name)
|
|
|
308 |
if result["label"] == "FAKE":
|
309 |
st.error(f"β οΈ Result: This image is a Deepfake. (Confidence: {result['score']:.2f})")
|
310 |
else:
|
311 |
st.success(f"β
Result: This image is Real. (Confidence: {1 - result['score']:.2f})")
|
312 |
|
313 |
# ---- Deepfake Video Detection Section ----
|
314 |
-
st.subheader("
|
315 |
uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
|
316 |
|
317 |
def detect_deepfake_video(video_path):
|
@@ -341,8 +202,9 @@ if uploaded_video is not None:
|
|
341 |
f.write(uploaded_video.read())
|
342 |
|
343 |
if st.button("Analyze Video"):
|
344 |
-
st.write("
|
345 |
result = detect_deepfake_video(temp_file.name)
|
|
|
346 |
if result["label"] == "FAKE":
|
347 |
st.warning(f"β οΈ Result: This video contains Deepfake elements. (Confidence: {result['score']:.2f})")
|
348 |
else:
|
|
|
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
|
|
|
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
|
|
|
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)
|
|
|
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
|
|
|
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):
|
|
|
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:
|