Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,41 +9,36 @@ from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
|
|
| 9 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
|
| 12 |
-
# Load models
|
| 13 |
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="xception_model.h5")
|
| 14 |
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="efficientnet_model.h5")
|
| 15 |
xcp_model = load_model(xcp_path)
|
| 16 |
eff_model = load_model(eff_path)
|
| 17 |
|
| 18 |
-
def predict(
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Resize and preprocess
|
| 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 |
-
# Predict
|
| 31 |
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
| 32 |
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
| 33 |
-
avg_pred = (xcp_pred + eff_pred) / 2
|
| 34 |
|
|
|
|
| 35 |
return "Real" if avg_pred > 0.5 else "Fake"
|
| 36 |
except Exception as e:
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
demo.launch()
|
|
|
|
| 9 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
|
| 12 |
+
# Load models once
|
| 13 |
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="xception_model.h5")
|
| 14 |
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="efficientnet_model.h5")
|
| 15 |
xcp_model = load_model(xcp_path)
|
| 16 |
eff_model = load_model(eff_path)
|
| 17 |
|
| 18 |
+
def predict(image: Image.Image) -> str:
|
| 19 |
try:
|
| 20 |
+
image_np = np.array(image.convert("RGB"))
|
| 21 |
+
xcp_img = cv2.resize(image_np, (299, 299))
|
| 22 |
+
eff_img = cv2.resize(image_np, (224, 224))
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
| 25 |
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
| 26 |
|
|
|
|
| 27 |
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
| 28 |
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
|
|
|
| 29 |
|
| 30 |
+
avg_pred = (xcp_pred + eff_pred) / 2
|
| 31 |
return "Real" if avg_pred > 0.5 else "Fake"
|
| 32 |
except Exception as e:
|
| 33 |
+
return "Error: " + str(e)
|
| 34 |
+
|
| 35 |
+
# ✅ Use Blocks instead of Interface to avoid schema bugs
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
with gr.Row():
|
| 38 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 39 |
+
with gr.Row():
|
| 40 |
+
output = gr.Textbox(label="Prediction")
|
| 41 |
+
image_input.change(fn=predict, inputs=image_input, outputs=output)
|
|
|
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
demo.launch()
|