Spaces:
Sleeping
Sleeping
Update prediction.py
Browse files- app/prediction.py +11 -3
app/prediction.py
CHANGED
|
@@ -57,7 +57,6 @@ class PredictionPipeline:
|
|
| 57 |
if not all_logits:
|
| 58 |
return {"error": "All images were invalid."}
|
| 59 |
|
| 60 |
-
# --- Aggregate Prediction (same as before) ---
|
| 61 |
avg_logits = torch.mean(torch.stack(all_logits), dim=0)
|
| 62 |
probabilities = torch.nn.functional.softmax(avg_logits, dim=-1)
|
| 63 |
confidence_score, predicted_class_idx = torch.max(probabilities, dim=-1)
|
|
@@ -65,7 +64,15 @@ class PredictionPipeline:
|
|
| 65 |
final_prediction = self.id2label[predicted_class_idx.item()]
|
| 66 |
final_confidence = confidence_score.item()
|
| 67 |
|
| 68 |
-
# --- NEW:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
watermarked_images = [
|
| 70 |
add_watermark(img_np, res["prediction"], res["confidence"])
|
| 71 |
for img_np, res in zip(valid_images_as_np, individual_results)
|
|
@@ -77,4 +84,5 @@ class PredictionPipeline:
|
|
| 77 |
"final_confidence": final_confidence,
|
| 78 |
"individual_results": individual_results,
|
| 79 |
"watermarked_images": watermarked_images
|
| 80 |
-
}
|
|
|
|
|
|
| 57 |
if not all_logits:
|
| 58 |
return {"error": "All images were invalid."}
|
| 59 |
|
|
|
|
| 60 |
avg_logits = torch.mean(torch.stack(all_logits), dim=0)
|
| 61 |
probabilities = torch.nn.functional.softmax(avg_logits, dim=-1)
|
| 62 |
confidence_score, predicted_class_idx = torch.max(probabilities, dim=-1)
|
|
|
|
| 64 |
final_prediction = self.id2label[predicted_class_idx.item()]
|
| 65 |
final_confidence = confidence_score.item()
|
| 66 |
|
| 67 |
+
# --- NEW: Add confidence check ---
|
| 68 |
+
if final_confidence < 0.60:
|
| 69 |
+
return {
|
| 70 |
+
"error": "Low Confidence Prediction",
|
| 71 |
+
"details": f"The model's confidence of {final_confidence:.1%} is too low. "
|
| 72 |
+
"Please ensure the uploaded image is a clear, frontal chest X-ray."
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
# --- Watermarking (same as before) ---
|
| 76 |
watermarked_images = [
|
| 77 |
add_watermark(img_np, res["prediction"], res["confidence"])
|
| 78 |
for img_np, res in zip(valid_images_as_np, individual_results)
|
|
|
|
| 84 |
"final_confidence": final_confidence,
|
| 85 |
"individual_results": individual_results,
|
| 86 |
"watermarked_images": watermarked_images
|
| 87 |
+
}
|
| 88 |
+
|