abuhanzala commited on
Commit
22db8ff
Β·
verified Β·
1 Parent(s): 777a892

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -12
app.py CHANGED
@@ -7,42 +7,37 @@ import tensorflow as tf
7
  interpreter = tf.lite.Interpreter(model_path="model.tflite")
8
  interpreter.allocate_tensors()
9
 
10
- # Get input and output details
11
  input_details = interpreter.get_input_details()
12
  output_details = interpreter.get_output_details()
13
 
14
  # Class labels
15
  class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
16
  CONFIDENCE_THRESHOLD = 0.7
17
- TEMPERATURE = 0.5 # πŸ”‘ Sharpening factor (lower = more confident)
18
 
19
  def preprocess_image(image):
20
- """Resize & normalize image for float32 TFLite model"""
21
  image = image.resize((224, 224)).convert("RGB")
22
  img_array = np.array(image, dtype=np.float32) / 255.0
23
- img_array = np.expand_dims(img_array, axis=0)
24
- return img_array
25
 
26
  def predict_image(image):
27
  try:
28
  img_array = preprocess_image(image)
29
 
30
- # Run inference
31
  interpreter.set_tensor(input_details[0]['index'], img_array)
32
  interpreter.invoke()
33
  output = interpreter.get_tensor(output_details[0]['index'])
34
 
35
- # Apply softmax with temperature scaling (sharper confidence)
36
- probabilities = tf.nn.softmax(output[0] / TEMPERATURE).numpy()
37
-
38
- # Top prediction
39
  class_idx = int(np.argmax(probabilities))
40
  confidence = float(np.max(probabilities))
41
 
42
  if confidence >= CONFIDENCE_THRESHOLD:
43
  return f"βœ… Prediction: {class_names[class_idx]} (Confidence: {confidence:.2f})"
44
  else:
45
- # Show Top-2 predictions if low confidence
46
  top2_idx = np.argsort(probabilities)[-2:][::-1]
47
  suggestion = ", ".join(
48
  [f"{class_names[i]} ({probabilities[i]:.2f})" for i in top2_idx]
@@ -52,7 +47,6 @@ def predict_image(image):
52
  except Exception as e:
53
  return f"❌ Error: {str(e)}"
54
 
55
- # Gradio app
56
  gr.Interface(
57
  fn=predict_image,
58
  inputs=gr.Image(type="pil", label="Upload Cervical Cell Image"),
 
7
  interpreter = tf.lite.Interpreter(model_path="model.tflite")
8
  interpreter.allocate_tensors()
9
 
10
+ # Input / Output details
11
  input_details = interpreter.get_input_details()
12
  output_details = interpreter.get_output_details()
13
 
14
  # Class labels
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]
 
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"),