Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,37 +19,14 @@ 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 |
-
# 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(
|
|
@@ -66,4 +43,3 @@ interface = gr.Interface(
|
|
| 66 |
# Step 6: Launch the app
|
| 67 |
if __name__ == "__main__":
|
| 68 |
interface.launch()
|
| 69 |
-
|
|
|
|
| 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)[0][0]
|
| 24 |
+
real_confidence = prediction * 100
|
| 25 |
+
fake_confidence = (1 - prediction) * 100
|
| 26 |
+
result_label = "Real" if real_confidence > fake_confidence else "Fake"
|
| 27 |
+
result_text = f"The model predicts this image is '{result_label}' with {max(real_confidence, fake_confidence):.2f}% confidence."
|
| 28 |
+
explanation = f"Real Confidence: {real_confidence:.2f}% | Fake Confidence: {fake_confidence:.2f}%"
|
| 29 |
+
return result_text, explanation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Step 5: Define the Gradio interface
|
| 32 |
interface = gr.Interface(
|
|
|
|
| 43 |
# Step 6: Launch the app
|
| 44 |
if __name__ == "__main__":
|
| 45 |
interface.launch()
|
|
|