Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|