Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Load models from HF Hub once
|
| 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")
|
| 20 |
+
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="efficientnet_model.h5")
|
| 21 |
+
xcp_model = load_model(xcp_path)
|
| 22 |
+
eff_model = load_model(eff_path)
|
| 23 |
+
return xcp_model, eff_model
|
| 24 |
+
|
| 25 |
+
xcp_model, eff_model = load_models()
|
| 26 |
+
|
| 27 |
+
# Prediction function
|
| 28 |
+
def predict_deepfake(image_np):
|
| 29 |
+
xcp_img = cv2.resize(image_np, (299, 299))
|
| 30 |
+
eff_img = cv2.resize(image_np, (224, 224))
|
| 31 |
+
|
| 32 |
+
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
| 33 |
+
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
| 34 |
+
|
| 35 |
+
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
| 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 |
+
# File uploader
|
| 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 |
+
st.image(image_rgb, caption="Uploaded Image", use_column_width=True)
|
| 52 |
+
label = predict_deepfake(image_rgb)
|
| 53 |
+
st.success(f"Prediction: {label}")
|