TLSIM / streamlit_app.py
kyrilloswahid's picture
Update streamlit_app.py
d042236 verified
raw
history blame
2.23 kB
import streamlit as st
import numpy as np
import tensorflow as tf
import cv2
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
from huggingface_hub import hf_hub_download
# Set Streamlit page configuration
st.set_page_config(page_title="Deepfake Image Verifier", layout="centered")
st.title("πŸ” Deepfake Image Verifier")
st.markdown("Upload an image to classify it as **Real** or **Fake** using an ensemble of Xception and EfficientNet models.")
# Load models only once and cache them
@st.cache_resource
def load_models():
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="xception_model.h5")
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="efficientnet_model.h5")
xcp_model = load_model(xcp_path)
eff_model = load_model(eff_path)
return xcp_model, eff_model
xcp_model, eff_model = load_models()
# Prediction function
def predict(image_np):
xcp_img = cv2.resize(image_np, (299, 299))
eff_img = cv2.resize(image_np, (224, 224))
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
avg_pred = (xcp_pred + eff_pred) / 2
label = "🟒 Real" if avg_pred > 0.5 else "πŸ”΄ Fake"
return label
# Upload image
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded_file:
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
if image is None:
st.error("Failed to decode the image. Please try another file.")
else:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
st.image(image_rgb, caption="Uploaded Image", use_column_width=True)
with st.spinner("Analyzing..."):
label = predict(image_rgb)
st.success(f"Prediction: **{label}**")