Zeyadd-Mostaffa commited on
Commit
4aae750
·
verified ·
1 Parent(s): ecd8bf8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import numpy as np
3
  from tensorflow.keras.models import load_model
4
  from tensorflow.keras.preprocessing.image import img_to_array
 
5
  from PIL import Image
6
  from huggingface_hub import hf_hub_download
7
 
@@ -11,20 +12,16 @@ model = load_model(model_path)
11
 
12
  # Inference function
13
  def predict(image):
14
- # === Short-Term Workaround for Inference ===
15
- image = image.convert("RGB") # Ensure 3 color channels
16
- image = image.resize((299, 299)) # Resize to match model input
17
- image = img_to_array(image).astype("float32") # Convert to array + ensure float32
18
- image = np.expand_dims(image, axis=0) # Add batch dimension
19
- image /= 255.0 # Normalize to [0, 1] like during training
20
-
21
- # Model prediction
22
- prob = model.predict(image)[0][0]
23
 
24
- # 0 = Fake, 1 = Real
25
- label = "Fake" if prob < 0.3 else "Real"
26
- confidence = round(float(1 - prob if label == "Fake" else prob), 3)
27
 
 
 
 
28
 
29
  return f"{label} ({confidence * 100:.1f}%)"
30
 
@@ -38,4 +35,3 @@ iface = gr.Interface(
38
 
39
  iface.launch()
40
 
41
-
 
2
  import numpy as np
3
  from tensorflow.keras.models import load_model
4
  from tensorflow.keras.preprocessing.image import img_to_array
5
+ from tensorflow.keras.applications.xception import preprocess_input
6
  from PIL import Image
7
  from huggingface_hub import hf_hub_download
8
 
 
12
 
13
  # Inference function
14
  def predict(image):
15
+ image = image.resize((299, 299)) # Resize to match model input
16
+ image = img_to_array(image) # Convert to numpy array
17
+ image = np.expand_dims(image, axis=0) # Add batch dimension
18
+ image = preprocess_input(image) # Apply Xception preprocessing (important fix!)
 
 
 
 
 
19
 
20
+ prob = model.predict(image)[0][0]
 
 
21
 
22
+ # Based on training: label 0 = Fake, label 1 = Real
23
+ label = "Real" if prob > 0.5 else "Fake"
24
+ confidence = round(float(prob if prob > 0.5 else 1 - prob), 3)
25
 
26
  return f"{label} ({confidence * 100:.1f}%)"
27
 
 
35
 
36
  iface.launch()
37