Image-Verifier / app.py
Zeyadd-Mostaffa's picture
Update app.py
abc9bfa verified
raw
history blame
1.85 kB
import os
import cv2
import numpy as np
import gradio as gr
import tensorflow as tf
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
# Download and load models
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="xception_model.h5")
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="efficientnet_model.h5")
xcp_model = load_model(xcp_path)
eff_model = load_model(eff_path)
def predict(image_path):
# Read the image from file path
image = cv2.imread(image_path)
# Check if loading failed
if image is None:
raise ValueError("Failed to load image. Make sure the input is an image file.")
# Convert BGR to RGB (OpenCV loads images in BGR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Resize for each model
xcp_img = cv2.resize(image, (299, 299))
eff_img = cv2.resize(image, (224, 224))
# Preprocess
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
# Predict
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": label,
"average": round(avg_pred, 3),
"xception": round(xcp_pred, 3),
"efficientnet": round(eff_pred, 3)
}
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type="filepath"),
outputs=gr.JSON(), # βœ… Now it actually returns a dict
live=False
)
iface.launch()