Spaces:
Sleeping
Sleeping
import gradio as gr | |
import numpy as np | |
from tensorflow.keras.models import load_model | |
from tensorflow.keras.preprocessing.image import img_to_array | |
from tensorflow.keras.applications.xception import preprocess_input | |
from PIL import Image | |
from huggingface_hub import hf_hub_download | |
# Download and load the model | |
model_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/cv_GP", filename="xception_model.h5") | |
model = load_model(model_path) | |
# Inference function | |
def predict(image): | |
image = image.resize((299, 299)) # Resize to match model input | |
image = img_to_array(image) # Convert to numpy array | |
image = np.expand_dims(image, axis=0) # Add batch dimension | |
image = preprocess_input(image) # Apply Xception preprocessing (important fix!) | |
prob = model.predict(image)[0][0] | |
# Based on training: label 0 = Fake, label 1 = Real | |
label = "Real" if prob > 0.5 else "Fake" | |
confidence = round(float(prob if prob > 0.5 else 1 - prob), 3) | |
return f"{label} ({confidence * 100:.1f}%)" | |
# Gradio interface | |
iface = gr.Interface( | |
fn=predict, | |
inputs=gr.Image(type="pil"), | |
outputs=gr.Text(), | |
title="Deepfake Detection (Xception Model)" | |
) | |
iface.launch() | |