Update app.py
Browse files
app.py
CHANGED
|
@@ -27,33 +27,22 @@ def predict_image(image):
|
|
| 27 |
interpreter.invoke()
|
| 28 |
output = interpreter.get_tensor(output_details[0]['index'])[0] # shape (num_classes,)
|
| 29 |
|
| 30 |
-
# Normalize if needed
|
| 31 |
probs = tf.nn.softmax(output).numpy()
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
confidence = float(np.max(probs))
|
| 36 |
|
| 37 |
-
|
| 38 |
-
results = []
|
| 39 |
-
for i, prob in enumerate(probs):
|
| 40 |
-
results.append(f"{class_names[i]}: {prob*100:.2f}%")
|
| 41 |
-
|
| 42 |
-
results_text = "\n".join(results)
|
| 43 |
-
|
| 44 |
-
if confidence < CONFIDENCE_THRESHOLD:
|
| 45 |
-
return f"⚠️ Low confidence ({confidence:.2f}). The model is unsure.\n\nProbabilities:\n{results_text}"
|
| 46 |
-
else:
|
| 47 |
-
return f"✅ Prediction: {class_names[class_idx]} ({confidence*100:.2f}%)\n\nProbabilities:\n{results_text}"
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
-
return
|
| 51 |
|
| 52 |
# Gradio UI
|
| 53 |
gr.Interface(
|
| 54 |
fn=predict_image,
|
| 55 |
inputs=gr.Image(type="pil"),
|
| 56 |
-
outputs=
|
| 57 |
-
title="
|
| 58 |
-
description="Upload an
|
| 59 |
).launch()
|
|
|
|
| 27 |
interpreter.invoke()
|
| 28 |
output = interpreter.get_tensor(output_details[0]['index'])[0] # shape (num_classes,)
|
| 29 |
|
| 30 |
+
# Normalize if needed
|
| 31 |
probs = tf.nn.softmax(output).numpy()
|
| 32 |
|
| 33 |
+
# Convert to dict for Gradio Label
|
| 34 |
+
probs_dict = {class_names[i]: float(probs[i]) for i in range(len(class_names))}
|
|
|
|
| 35 |
|
| 36 |
+
return probs_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
+
return {"Error": 1.0} # dummy error output
|
| 40 |
|
| 41 |
# Gradio UI
|
| 42 |
gr.Interface(
|
| 43 |
fn=predict_image,
|
| 44 |
inputs=gr.Image(type="pil"),
|
| 45 |
+
outputs=gr.Label(num_top_classes=len(class_names)), # shows all classes with bars
|
| 46 |
+
title="Cervical Cancer Classification",
|
| 47 |
+
description="Upload an image. The model shows probabilities for each class."
|
| 48 |
).launch()
|