T-LENS/Image-Verifier
0
1import os2import cv23import numpy as np4import gradio as gr5import tensorflow as tf6from tensorflow.keras.models import load_model7from tensorflow.keras.applications.xception import preprocess_input as xcp_pre8from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre9from huggingface_hub import hf_hub_download10 11# Load models12xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="xception_model.h5")13eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="efficientnet_model.h5")14xcp_model = load_model(xcp_path)15eff_model = load_model(eff_path)16 17def predict(image_path):18 image = cv2.imread(image_path)19 if image is None:20 return {"label": "Invalid image"}21 22 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)23 24 xcp_img = cv2.resize(image, (299, 299))25 eff_img = cv2.resize(image, (224, 224))26 27 xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]28 eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]29 30 xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]31 eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]32 33 avg_pred = (xcp_pred + eff_pred) / 234 label = "Real" if avg_pred > 0.5 else "Fake"35 36 return {"label": label}37 38iface = gr.Interface(39 fn=predict,40 inputs=gr.Image(type="filepath", label="image_path"),41 outputs=gr.JSON(label="output"),42 allow_flagging="never"43)44 45iface.launch()46 