Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,51 +8,37 @@ from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
|
|
| 8 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-
|
| 13 |
-
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-
|
| 14 |
xcp_model = load_model(xcp_path)
|
| 15 |
eff_model = load_model(eff_path)
|
| 16 |
|
| 17 |
-
def predict(image_path):
|
| 18 |
-
# Read the image from file path
|
| 19 |
image = cv2.imread(image_path)
|
| 20 |
-
|
| 21 |
-
# Check if loading failed
|
| 22 |
if image is None:
|
| 23 |
-
|
| 24 |
|
| 25 |
-
# Convert BGR to RGB (OpenCV loads images in BGR)
|
| 26 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 27 |
|
| 28 |
-
# Resize for each model
|
| 29 |
xcp_img = cv2.resize(image, (299, 299))
|
| 30 |
eff_img = cv2.resize(image, (224, 224))
|
| 31 |
|
| 32 |
-
# Preprocess
|
| 33 |
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
| 34 |
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
| 35 |
|
| 36 |
-
# Predict
|
| 37 |
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
| 38 |
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
| 39 |
-
avg_pred = (xcp_pred + eff_pred) / 2
|
| 40 |
|
|
|
|
| 41 |
label = "Real" if avg_pred > 0.5 else "Fake"
|
| 42 |
-
|
| 43 |
-
return {"result": {
|
| 44 |
-
"label": label,
|
| 45 |
-
"average": round(avg_pred, 3),
|
| 46 |
-
"xception": round(xcp_pred, 3),
|
| 47 |
-
"efficientnet": round(eff_pred, 3)
|
| 48 |
-
}}
|
| 49 |
-
|
| 50 |
|
| 51 |
iface = gr.Interface(
|
| 52 |
fn=predict,
|
| 53 |
-
inputs=gr.Image(type="filepath"),
|
| 54 |
-
outputs=
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
iface.launch()
|
|
|
|
| 8 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
|
| 11 |
+
# Load models from Hugging Face Hub
|
| 12 |
+
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="xception_model.h5")
|
| 13 |
+
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="efficientnet_model.h5")
|
| 14 |
xcp_model = load_model(xcp_path)
|
| 15 |
eff_model = load_model(eff_path)
|
| 16 |
|
| 17 |
+
def predict(image_path): # receives file path (not array)
|
|
|
|
| 18 |
image = cv2.imread(image_path)
|
|
|
|
|
|
|
| 19 |
if image is None:
|
| 20 |
+
return "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) / 2
|
| 34 |
label = "Real" if avg_pred > 0.5 else "Fake"
|
| 35 |
+
return label
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
iface = gr.Interface(
|
| 38 |
fn=predict,
|
| 39 |
+
inputs=gr.Image(type="filepath", label="image_path"), # <- This must match backend call
|
| 40 |
+
outputs="text",
|
| 41 |
+
allow_flagging="never"
|
| 42 |
)
|
| 43 |
|
| 44 |
+
iface.launch()
|
|
|