Update app.py
Browse files
app.py
CHANGED
@@ -219,7 +219,7 @@ import tempfile
|
|
219 |
import os
|
220 |
from PIL import Image
|
221 |
import tensorflow as tf
|
222 |
-
from transformers import pipeline
|
223 |
from tensorflow.keras.applications import Xception, EfficientNetB7
|
224 |
from tensorflow.keras.models import Model
|
225 |
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
|
@@ -231,15 +231,30 @@ st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
|
|
231 |
st.title("\U0001F4F0 Fake News & Deepfake Detection Tool")
|
232 |
st.write("\U0001F680 Detect Fake News, Deepfake Images, and Videos using AI")
|
233 |
|
234 |
-
# Load
|
235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
|
237 |
# Load Deepfake Detection Models
|
238 |
base_model_image = Xception(weights="imagenet", include_top=False)
|
239 |
-
base_model_image.trainable = False
|
240 |
x = GlobalAveragePooling2D()(base_model_image.output)
|
241 |
x = Dense(1024, activation="relu")(x)
|
242 |
-
x = Dense(1, activation="sigmoid")(x)
|
243 |
deepfake_image_model = Model(inputs=base_model_image.input, outputs=x)
|
244 |
|
245 |
base_model_video = EfficientNetB7(weights="imagenet", include_top=False)
|
@@ -251,10 +266,10 @@ deepfake_video_model = Model(inputs=base_model_video.input, outputs=x)
|
|
251 |
|
252 |
# Function to Preprocess Image
|
253 |
def preprocess_image(image_path):
|
254 |
-
img = load_img(image_path, target_size=(299, 299))
|
255 |
img = img_to_array(img)
|
256 |
img = np.expand_dims(img, axis=0)
|
257 |
-
img /= 255.0
|
258 |
return img
|
259 |
|
260 |
# Function to Detect Deepfake Image
|
@@ -271,17 +286,11 @@ news_input = st.text_area("Enter News Text:", placeholder="Type here...")
|
|
271 |
|
272 |
if st.button("Check News"):
|
273 |
st.write("\U0001F50D Processing...")
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
label = prediction["labels"][0] # Highest confidence label
|
279 |
-
confidence = prediction["scores"][0] # Confidence of highest label
|
280 |
-
|
281 |
-
if label == "FAKE":
|
282 |
-
st.error(f"⚠️ Result: This news is FAKE. (Confidence: {confidence:.2f})")
|
283 |
else:
|
284 |
-
st.success(f"✅ Result: This news is REAL. (Confidence: {confidence:.2f})")
|
285 |
|
286 |
# ---- Deepfake Image Detection Section ----
|
287 |
st.subheader("\U0001F4F8 Deepfake Image Detection")
|
@@ -296,7 +305,6 @@ if uploaded_image is not None:
|
|
296 |
if st.button("Analyze Image"):
|
297 |
st.write("\U0001F50D Processing...")
|
298 |
result = detect_deepfake_image(temp_file.name)
|
299 |
-
|
300 |
if result["label"] == "FAKE":
|
301 |
st.error(f"⚠️ Result: This image is a Deepfake. (Confidence: {result['score']:.2f})")
|
302 |
else:
|
@@ -335,10 +343,10 @@ if uploaded_video is not None:
|
|
335 |
if st.button("Analyze Video"):
|
336 |
st.write("\U0001F50D Processing...")
|
337 |
result = detect_deepfake_video(temp_file.name)
|
338 |
-
|
339 |
if result["label"] == "FAKE":
|
340 |
st.warning(f"⚠️ Result: This video contains Deepfake elements. (Confidence: {result['score']:.2f})")
|
341 |
else:
|
342 |
st.success(f"✅ Result: This video is Real. (Confidence: {1 - result['score']:.2f})")
|
343 |
|
344 |
st.markdown("🔹 **Developed for Fake News & Deepfake Detection Hackathon**")
|
|
|
|
219 |
import os
|
220 |
from PIL import Image
|
221 |
import tensorflow as tf
|
222 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
223 |
from tensorflow.keras.applications import Xception, EfficientNetB7
|
224 |
from tensorflow.keras.models import Model
|
225 |
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
|
|
|
231 |
st.title("\U0001F4F0 Fake News & Deepfake Detection Tool")
|
232 |
st.write("\U0001F680 Detect Fake News, Deepfake Images, and Videos using AI")
|
233 |
|
234 |
+
# Load Fake News Detection Model
|
235 |
+
model_name = "mrm8488/bert-mini-fake-news"
|
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 |
|
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
|
|
|
286 |
|
287 |
if st.button("Check News"):
|
288 |
st.write("\U0001F50D Processing...")
|
289 |
+
result = detect_fake_news(news_input)
|
290 |
+
if result["label"] == "FAKE":
|
291 |
+
st.error(f"⚠️ Result: This news is FAKE. (Confidence: {result['confidence']:.2f})")
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
else:
|
293 |
+
st.success(f"✅ Result: This news is REAL. (Confidence: {result['confidence']:.2f})")
|
294 |
|
295 |
# ---- Deepfake Image Detection Section ----
|
296 |
st.subheader("\U0001F4F8 Deepfake Image Detection")
|
|
|
305 |
if st.button("Analyze Image"):
|
306 |
st.write("\U0001F50D Processing...")
|
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:
|
|
|
343 |
if st.button("Analyze Video"):
|
344 |
st.write("\U0001F50D Processing...")
|
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:
|
349 |
st.success(f"✅ Result: This video is Real. (Confidence: {1 - result['score']:.2f})")
|
350 |
|
351 |
st.markdown("🔹 **Developed for Fake News & Deepfake Detection Hackathon**")
|
352 |
+
|