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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -19,24 +19,37 @@ def load_and_preprocess_image(img, target_size=(256, 256)):
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
42
  interface = gr.Interface(
 
19
 
20
  # Step 4: Function to make predictions
21
  def predict_image(img):
22
+ print("Image received.") # Confirm function is triggered
23
+
24
+ try:
25
+ img_array = load_and_preprocess_image(img)
26
+ print("Image shape:", img_array.shape)
27
+
28
+ prediction = model.predict(img_array)
29
+ print("Raw prediction:", prediction)
30
+
31
+ # Determine output shape
32
+ if prediction.shape[-1] == 1:
33
+ prob_real = prediction[0][0]
34
+ elif prediction.shape[-1] == 2:
35
+ prob_real = prediction[0][1] # assumes index 1 = "real"
36
+ else:
37
+ print("Unexpected model output shape.")
38
+ return "Error: Unrecognized output shape.", ""
39
+
40
+ real_confidence = prob_real * 100
41
+ fake_confidence = (1 - prob_real) * 100
42
+ result_label = "Real" if real_confidence > fake_confidence else "Fake"
43
+
44
+ result_text = f"The model predicts this image is '{result_label}' with {max(real_confidence, fake_confidence):.2f}% confidence."
45
+ explanation = f"Real Confidence: {real_confidence:.2f}% | Fake Confidence: {fake_confidence:.2f}%"
46
+
47
+ print("Prediction result:", result_text)
48
+ return result_text, explanation
49
+
50
+ except Exception as e:
51
+ print("Error during prediction:", str(e))
52
+ return "An error occurred during prediction.", str(e)
53
 
54
  # Step 5: Define the Gradio interface
55
  interface = gr.Interface(