Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# ---- Page Configuration ----
|
| 9 |
st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
|
|
@@ -11,75 +103,112 @@ st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
|
|
| 11 |
st.title("📰 Fake News & Deepfake Detection Tool")
|
| 12 |
st.write("🚀 Detect Fake News, Deepfake Images, and Videos using AI")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# ---- Fake News Detection Section ----
|
| 15 |
st.subheader("📝 Fake News Detection")
|
| 16 |
news_input = st.text_area("Enter News Text:", "Type here...")
|
| 17 |
|
| 18 |
if st.button("Check News"):
|
| 19 |
st.write("🔍 Processing...")
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# ---- Deepfake Image Detection Section ----
|
| 23 |
st.subheader("📸 Deepfake Image Detection")
|
| 24 |
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
| 25 |
|
| 26 |
-
def compress_image(image, quality=90, max_size=(300, 300)): # ✅ High clarity image
|
| 27 |
-
img = Image.open(image).convert("RGB")
|
| 28 |
-
img.thumbnail(max_size)
|
| 29 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
| 30 |
-
img.save(temp_file.name, "JPEG", quality=quality)
|
| 31 |
-
return temp_file.name
|
| 32 |
-
|
| 33 |
if uploaded_image is not None:
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
if st.button("Analyze Image"):
|
| 37 |
st.write("🔍 Processing...")
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# ---- Deepfake Video Detection Section ----
|
| 41 |
st.subheader("🎥 Deepfake Video Detection")
|
| 42 |
uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
|
| 43 |
|
| 44 |
-
def
|
| 45 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 46 |
-
|
| 47 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
|
| 48 |
-
temp_video.write(video.read())
|
| 49 |
-
video_path = temp_video.name
|
| 50 |
-
|
| 51 |
cap = cv2.VideoCapture(video_path)
|
|
|
|
| 52 |
|
| 53 |
-
if not cap.isOpened():
|
| 54 |
-
st.error("❌ Error: Unable to read video!")
|
| 55 |
-
return None
|
| 56 |
-
|
| 57 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 58 |
-
|
| 59 |
-
# ✅ New Resolution (100x80) & 15 FPS
|
| 60 |
-
frame_width = 50
|
| 61 |
-
frame_height = 80
|
| 62 |
-
out = cv2.VideoWriter(temp_file.name, fourcc, 15.0, (frame_width, frame_height))
|
| 63 |
-
|
| 64 |
while cap.isOpened():
|
| 65 |
ret, frame = cap.read()
|
| 66 |
if not ret:
|
| 67 |
break
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
if uploaded_video is not None:
|
| 77 |
-
st.video(uploaded_video)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
st.markdown("🔹 **Developed for Fake News & Deepfake Detection Hackathon**")
|
|
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# import numpy as np
|
| 3 |
+
# import cv2
|
| 4 |
+
# import tempfile
|
| 5 |
+
# import os
|
| 6 |
+
# from PIL import Image
|
| 7 |
+
|
| 8 |
+
# # ---- Page Configuration ----
|
| 9 |
+
# st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
|
| 10 |
+
|
| 11 |
+
# st.title("📰 Fake News & Deepfake Detection Tool")
|
| 12 |
+
# st.write("🚀 Detect Fake News, Deepfake Images, and Videos using AI")
|
| 13 |
+
|
| 14 |
+
# # ---- Fake News Detection Section ----
|
| 15 |
+
# st.subheader("📝 Fake News Detection")
|
| 16 |
+
# news_input = st.text_area("Enter News Text:", "Type here...")
|
| 17 |
+
|
| 18 |
+
# if st.button("Check News"):
|
| 19 |
+
# st.write("🔍 Processing...")
|
| 20 |
+
# st.success("✅ Result: This news is FAKE.") # Replace with ML Model
|
| 21 |
+
|
| 22 |
+
# # ---- Deepfake Image Detection Section ----
|
| 23 |
+
# st.subheader("📸 Deepfake Image Detection")
|
| 24 |
+
# uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
| 25 |
+
|
| 26 |
+
# def compress_image(image, quality=90, max_size=(300, 300)): # ✅ High clarity image
|
| 27 |
+
# img = Image.open(image).convert("RGB")
|
| 28 |
+
# img.thumbnail(max_size)
|
| 29 |
+
# temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
| 30 |
+
# img.save(temp_file.name, "JPEG", quality=quality)
|
| 31 |
+
# return temp_file.name
|
| 32 |
+
|
| 33 |
+
# if uploaded_image is not None:
|
| 34 |
+
# compressed_image_path = compress_image(uploaded_image)
|
| 35 |
+
# st.image(compressed_image_path, caption="🖼️ Compressed & Clear Image", use_column_width=True)
|
| 36 |
+
# if st.button("Analyze Image"):
|
| 37 |
+
# st.write("🔍 Processing...")
|
| 38 |
+
# st.error("⚠️ Result: This image is a Deepfake.") # Replace with model
|
| 39 |
+
|
| 40 |
+
# # ---- Deepfake Video Detection Section ----
|
| 41 |
+
# st.subheader("🎥 Deepfake Video Detection")
|
| 42 |
+
# uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
|
| 43 |
+
|
| 44 |
+
# def compress_video(video):
|
| 45 |
+
# temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 46 |
+
|
| 47 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
|
| 48 |
+
# temp_video.write(video.read())
|
| 49 |
+
# video_path = temp_video.name
|
| 50 |
+
|
| 51 |
+
# cap = cv2.VideoCapture(video_path)
|
| 52 |
+
|
| 53 |
+
# if not cap.isOpened():
|
| 54 |
+
# st.error("❌ Error: Unable to read video!")
|
| 55 |
+
# return None
|
| 56 |
+
|
| 57 |
+
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 58 |
+
|
| 59 |
+
# # ✅ New Resolution (100x80) & 15 FPS
|
| 60 |
+
# frame_width = 50
|
| 61 |
+
# frame_height = 80
|
| 62 |
+
# out = cv2.VideoWriter(temp_file.name, fourcc, 15.0, (frame_width, frame_height))
|
| 63 |
+
|
| 64 |
+
# while cap.isOpened():
|
| 65 |
+
# ret, frame = cap.read()
|
| 66 |
+
# if not ret:
|
| 67 |
+
# break
|
| 68 |
+
# frame = cv2.resize(frame, (frame_width, frame_height))
|
| 69 |
+
# out.write(frame)
|
| 70 |
+
|
| 71 |
+
# cap.release()
|
| 72 |
+
# out.release()
|
| 73 |
+
|
| 74 |
+
# return temp_file.name
|
| 75 |
+
|
| 76 |
+
# if uploaded_video is not None:
|
| 77 |
+
# st.video(uploaded_video) # ✅ فوراً ویڈیو اپ لوڈ ہونے کے بعد دکھائیں
|
| 78 |
+
# compressed_video_path = compress_video(uploaded_video)
|
| 79 |
+
# if compressed_video_path:
|
| 80 |
+
# st.video(compressed_video_path) # ✅ کمپریسڈ ویڈیو بھی دکھائیں
|
| 81 |
+
# if st.button("Analyze Video"):
|
| 82 |
+
# st.write("🔍 Processing...")
|
| 83 |
+
# st.warning("⚠️ Result: This video contains Deepfake elements.") # Replace with model
|
| 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")
|
|
|
|
| 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:", "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 |
+
|