Spaces:
Sleeping
Sleeping
| import os | |
| import cv2 | |
| import numpy as np | |
| import gradio as gr | |
| from PIL import Image | |
| 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 | |
| # Load models once | |
| 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: Image.Image) -> str: | |
| try: | |
| image_np = np.array(image.convert("RGB")) | |
| 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 | |
| return "Real" if avg_pred > 0.5 else "Fake" | |
| except Exception as e: | |
| return "Error: " + str(e) | |
| # ✅ Use Blocks instead of Interface to avoid schema bugs | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| image_input = gr.Image(type="pil", label="Upload Image") | |
| with gr.Row(): | |
| output = gr.Textbox(label="Prediction") | |
| image_input.change(fn=predict, inputs=image_input, outputs=output) | |
| if __name__ == "__main__": | |
| demo.launch() | |