kyrilloswahid commited on
Commit
4191df3
·
verified ·
1 Parent(s): b453465

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import cv2
3
  import numpy as np
4
  import gradio as gr
 
5
  import tensorflow as tf
6
  from tensorflow.keras.models import load_model
7
  from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
@@ -14,31 +15,33 @@ eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", fi
14
  xcp_model = load_model(xcp_path)
15
  eff_model = load_model(eff_path)
16
 
17
- def predict(img):
18
  try:
19
- # Resize input
20
- xcp_img = cv2.resize(img, (299, 299))
21
- eff_img = cv2.resize(img, (224, 224))
 
 
 
22
 
23
- # Preprocess for each model
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
  # Predict
28
  xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
29
  eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
30
-
31
  avg_pred = (xcp_pred + eff_pred) / 2
32
- return "Real" if avg_pred > 0.5 else "Fake"
33
 
 
34
  except Exception as e:
35
- return "Error: " + str(e)
36
 
37
- # ✅ Use literal type-safe components
38
  demo = gr.Interface(
39
  fn=predict,
40
- inputs=gr.Image(type="numpy"),
41
- outputs=gr.Textbox(),
 
 
42
  allow_flagging="never"
43
  )
44
 
 
2
  import cv2
3
  import numpy as np
4
  import gradio as gr
5
+ from PIL import Image
6
  import tensorflow as tf
7
  from tensorflow.keras.models import load_model
8
  from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
 
15
  xcp_model = load_model(xcp_path)
16
  eff_model = load_model(eff_path)
17
 
18
+ def predict(image_pil: Image.Image) -> str:
19
  try:
20
+ # Convert PIL to numpy
21
+ image = np.array(image_pil.convert("RGB"))
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 f"Error: {str(e)}"
38
 
 
39
  demo = gr.Interface(
40
  fn=predict,
41
+ inputs=gr.Image(type="pil", label="Upload Image"), # ✅ Use PIL instead of numpy
42
+ outputs=gr.Textbox(label="Prediction"), # ✅ Safe, schema-compatible
43
+ title="Deepfake Image Detector",
44
+ description="Upload a full image. The model classifies it as real or fake.",
45
  allow_flagging="never"
46
  )
47