Zeyadd-Mostaffa commited on
Commit
1fc17bf
·
verified ·
1 Parent(s): c4aa679

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -20
app.py CHANGED
@@ -12,36 +12,30 @@ model = tf.keras.models.load_model(model_path)
12
 
13
  # Step 3: Function to preprocess the input image
14
  def load_and_preprocess_image(img, target_size=(256, 256)):
15
- # Resize the image to the model's expected input size
16
  img = img.resize(target_size)
17
-
18
- # Convert to array and normalize
19
  img_array = np.array(img) / 255.0
20
-
21
- # Expand dimensions to match the input shape of the model
22
  img_array = np.expand_dims(img_array, axis=0)
23
-
24
  return img_array
25
 
26
  # Step 4: Function to make predictions
27
  def predict_image(img):
28
- # Preprocess the image
29
  img_array = load_and_preprocess_image(img)
30
-
31
- # Make a prediction
32
- prediction = model.predict(img_array)[0][0]
33
-
34
- # Confidence scores
35
- real_confidence = prediction * 100
36
- fake_confidence = (1 - prediction) * 100
37
-
38
- # Determine label
 
 
 
39
  result_label = "Real" if real_confidence > fake_confidence else "Fake"
40
-
41
- # Return results
42
  result_text = f"The model predicts this image is '{result_label}' with {max(real_confidence, fake_confidence):.2f}% confidence."
43
  explanation = f"Real Confidence: {real_confidence:.2f}% | Fake Confidence: {fake_confidence:.2f}%"
44
-
45
  return result_text, explanation
46
 
47
  # Step 5: Define the Gradio interface
@@ -49,7 +43,8 @@ interface = gr.Interface(
49
  fn=predict_image,
50
  inputs=gr.Image(type="pil", label="Upload an Image"),
51
  outputs=[
52
- gr.Textbox(label="Prediction Result")
 
53
  ],
54
  title="Deepfake Image Detector",
55
  description="Upload an image, and the model will classify whether it is a 'real' or 'fake' image using deep learning."
@@ -58,3 +53,4 @@ interface = gr.Interface(
58
  # Step 6: Launch the app
59
  if __name__ == "__main__":
60
  interface.launch()
 
 
12
 
13
  # Step 3: Function to preprocess the input image
14
  def load_and_preprocess_image(img, target_size=(256, 256)):
 
15
  img = img.resize(target_size)
 
 
16
  img_array = np.array(img) / 255.0
 
 
17
  img_array = np.expand_dims(img_array, axis=0)
 
18
  return img_array
19
 
20
  # Step 4: Function to make predictions
21
  def predict_image(img):
 
22
  img_array = load_and_preprocess_image(img)
23
+ prediction = model.predict(img_array)
24
+
25
+ # Determine output shape and extract confidence
26
+ if prediction.shape[-1] == 1:
27
+ prob_real = prediction[0][0]
28
+ elif prediction.shape[-1] == 2:
29
+ prob_real = prediction[0][1] # Assuming second index = "real"
30
+ else:
31
+ return "Model output shape not recognized.", "Cannot compute prediction."
32
+
33
+ real_confidence = prob_real * 100
34
+ fake_confidence = (1 - prob_real) * 100
35
  result_label = "Real" if real_confidence > fake_confidence else "Fake"
36
+
 
37
  result_text = f"The model predicts this image is '{result_label}' with {max(real_confidence, fake_confidence):.2f}% confidence."
38
  explanation = f"Real Confidence: {real_confidence:.2f}% | Fake Confidence: {fake_confidence:.2f}%"
 
39
  return result_text, explanation
40
 
41
  # Step 5: Define the Gradio interface
 
43
  fn=predict_image,
44
  inputs=gr.Image(type="pil", label="Upload an Image"),
45
  outputs=[
46
+ gr.Textbox(label="Prediction Result"),
47
+ gr.Textbox(label="Confidence Scores")
48
  ],
49
  title="Deepfake Image Detector",
50
  description="Upload an image, and the model will classify whether it is a 'real' or 'fake' image using deep learning."
 
53
  # Step 6: Launch the app
54
  if __name__ == "__main__":
55
  interface.launch()
56
+