abuhanzala commited on
Commit
f72b2f3
·
verified ·
1 Parent(s): 22db8ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -24
app.py CHANGED
@@ -15,42 +15,30 @@ output_details = interpreter.get_output_details()
15
  class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
16
  CONFIDENCE_THRESHOLD = 0.7
17
 
18
- def preprocess_image(image):
19
- image = image.resize((224, 224)).convert("RGB")
20
- img_array = np.array(image, dtype=np.float32) / 255.0
21
- return np.expand_dims(img_array, axis=0)
22
-
23
  def predict_image(image):
24
  try:
25
- img_array = preprocess_image(image)
 
 
26
 
27
- # Inference
28
  interpreter.set_tensor(input_details[0]['index'], img_array)
29
  interpreter.invoke()
30
  output = interpreter.get_tensor(output_details[0]['index'])
31
 
32
- # Normal softmax
33
- probabilities = tf.nn.softmax(output[0]).numpy()
34
- class_idx = int(np.argmax(probabilities))
35
- confidence = float(np.max(probabilities))
36
 
37
- if confidence >= CONFIDENCE_THRESHOLD:
38
- return f" Prediction: {class_names[class_idx]} (Confidence: {confidence:.2f})"
39
  else:
40
- # Show top-2 if low confidence
41
- top2_idx = np.argsort(probabilities)[-2:][::-1]
42
- suggestion = ", ".join(
43
- [f"{class_names[i]} ({probabilities[i]:.2f})" for i in top2_idx]
44
- )
45
- return f"⚠️ Low confidence ({confidence:.2f}).\nPossible classes: {suggestion}"
46
-
47
  except Exception as e:
48
- return f"Error: {str(e)}"
49
 
50
  gr.Interface(
51
  fn=predict_image,
52
- inputs=gr.Image(type="pil", label="Upload Cervical Cell Image"),
53
  outputs="text",
54
- title="Cervical Cancer Cell Classification",
55
- description="Upload a cervical cell image. The model predicts the cell type with confidence scores."
56
  ).launch()
 
15
  class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
16
  CONFIDENCE_THRESHOLD = 0.7
17
 
 
 
 
 
 
18
  def predict_image(image):
19
  try:
20
+ image = image.resize((128, 128)).convert("RGB")
21
+ img_array = np.array(image, dtype=np.float32) / 255.0
22
+ img_array = np.expand_dims(img_array, axis=0)
23
 
 
24
  interpreter.set_tensor(input_details[0]['index'], img_array)
25
  interpreter.invoke()
26
  output = interpreter.get_tensor(output_details[0]['index'])
27
 
28
+ class_idx = int(np.argmax(output))
29
+ confidence = float(np.max(output))
 
 
30
 
31
+ if confidence < CONFIDENCE_THRESHOLD:
32
+ return f"⚠️ Low confidence ({confidence:.2f}). The model is unsure. Please try a clearer image."
33
  else:
34
+ return f"✅ Prediction: {class_names[class_idx]} (Confidence: {confidence:.2f})"
 
 
 
 
 
 
35
  except Exception as e:
36
+ return f"Error: {str(e)}"
37
 
38
  gr.Interface(
39
  fn=predict_image,
40
+ inputs=gr.Image(type="pil"),
41
  outputs="text",
42
+ title="Muscle Disease Detection",
43
+ description="Upload an MRI image to detect muscle conditions."
44
  ).launch()