Update streamlit_app.py
Browse files- streamlit_app.py +18 -13
streamlit_app.py
CHANGED
|
@@ -1,19 +1,19 @@
|
|
| 1 |
-
# streamlit_app.py
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
import tensorflow as tf
|
| 5 |
import cv2
|
| 6 |
-
import os
|
| 7 |
from tensorflow.keras.models import load_model
|
| 8 |
from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
|
| 9 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
|
|
|
|
| 12 |
st.set_page_config(page_title="Deepfake Image Verifier", layout="centered")
|
| 13 |
-
st.title("Deepfake Image Verifier")
|
| 14 |
-
st.markdown("Upload a face image to classify it as Real or Fake using Xception and EfficientNet.")
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
@st.cache_resource
|
| 18 |
def load_models():
|
| 19 |
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="xception_model.h5")
|
|
@@ -25,7 +25,7 @@ def load_models():
|
|
| 25 |
xcp_model, eff_model = load_models()
|
| 26 |
|
| 27 |
# Prediction function
|
| 28 |
-
def
|
| 29 |
xcp_img = cv2.resize(image_np, (299, 299))
|
| 30 |
eff_img = cv2.resize(image_np, (224, 224))
|
| 31 |
|
|
@@ -36,18 +36,23 @@ def predict_deepfake(image_np):
|
|
| 36 |
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
| 37 |
|
| 38 |
avg_pred = (xcp_pred + eff_pred) / 2
|
| 39 |
-
label = "Real" if avg_pred > 0.5 else "Fake"
|
| 40 |
return label
|
| 41 |
|
| 42 |
-
#
|
| 43 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 44 |
|
| 45 |
if uploaded_file:
|
| 46 |
-
# Read and preprocess
|
| 47 |
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 48 |
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 49 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
import cv2
|
|
|
|
| 5 |
from tensorflow.keras.models import load_model
|
| 6 |
from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
|
| 7 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
+
# Set Streamlit page configuration
|
| 11 |
st.set_page_config(page_title="Deepfake Image Verifier", layout="centered")
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
st.title("π Deepfake Image Verifier")
|
| 14 |
+
st.markdown("Upload an image to classify it as **Real** or **Fake** using an ensemble of Xception and EfficientNet models.")
|
| 15 |
+
|
| 16 |
+
# Load models only once and cache them
|
| 17 |
@st.cache_resource
|
| 18 |
def load_models():
|
| 19 |
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="xception_model.h5")
|
|
|
|
| 25 |
xcp_model, eff_model = load_models()
|
| 26 |
|
| 27 |
# Prediction function
|
| 28 |
+
def predict(image_np):
|
| 29 |
xcp_img = cv2.resize(image_np, (299, 299))
|
| 30 |
eff_img = cv2.resize(image_np, (224, 224))
|
| 31 |
|
|
|
|
| 36 |
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
| 37 |
|
| 38 |
avg_pred = (xcp_pred + eff_pred) / 2
|
| 39 |
+
label = "π’ Real" if avg_pred > 0.5 else "π΄ Fake"
|
| 40 |
return label
|
| 41 |
|
| 42 |
+
# Upload image
|
| 43 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 44 |
|
| 45 |
if uploaded_file:
|
|
|
|
| 46 |
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 47 |
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
|
|
|
| 48 |
|
| 49 |
+
if image is None:
|
| 50 |
+
st.error("Failed to decode the image. Please try another file.")
|
| 51 |
+
else:
|
| 52 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 53 |
+
st.image(image_rgb, caption="Uploaded Image", use_column_width=True)
|
| 54 |
+
|
| 55 |
+
with st.spinner("Analyzing..."):
|
| 56 |
+
label = predict(image_rgb)
|
| 57 |
+
|
| 58 |
+
st.success(f"Prediction: **{label}**")
|