File size: 1,951 Bytes
d45efd4
 
05e9c1c
d45efd4
 
 
06e5717
d45efd4
 
22db8ff
d45efd4
 
 
777a892
d45efd4
1ef3fee
011b561
d45efd4
 
5ab8f46
b002185
f72b2f3
 
d45efd4
5ab8f46
d45efd4
 
05e9c1c
d45efd4
05e9c1c
5ab8f46
 
05e9c1c
 
 
 
 
 
 
 
 
 
465e140
05e9c1c
 
 
 
5ab8f46
d45efd4
05e9c1c
d45efd4
5ab8f46
d45efd4
 
f72b2f3
05e9c1c
 
 
d45efd4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
import numpy as np
from PIL import Image
import tensorflow as tf

# Load TFLite model
interpreter = tf.lite.Interpreter(model_path="modelAZL.tflite")
interpreter.allocate_tensors()

# Input / Output details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Class labels
class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
CONFIDENCE_THRESHOLD = 0.25

def predict_image(image):
    try:
        # Preprocess
        image = image.resize((224, 224)).convert("RGB")
        img_array = np.array(image, dtype=np.float32) / 255.0
        img_array = np.expand_dims(img_array, axis=0)

        # Run inference
        interpreter.set_tensor(input_details[0]['index'], img_array)
        interpreter.invoke()
        output = interpreter.get_tensor(output_details[0]['index'])[0]  # shape (num_classes,)

        # Normalize if needed (sometimes TFLite outputs logits)
        probs = tf.nn.softmax(output).numpy()

        # Get predicted class
        class_idx = int(np.argmax(probs))
        confidence = float(np.max(probs))

        # Format output (show every class probability)
        results = []
        for i, prob in enumerate(probs):
            results.append(f"{class_names[i]}: {prob*100:.2f}%")

        results_text = "\n".join(results)

        if confidence < CONFIDENCE_THRESHOLD:
            return f"⚠️ Low confidence ({confidence:.2f}). The model is unsure.\n\nProbabilities:\n{results_text}"
        else:
            return f"✅ Prediction: {class_names[class_idx]} ({confidence*100:.2f}%)\n\nProbabilities:\n{results_text}"

    except Exception as e:
        return f"Error: {str(e)}"

# Gradio UI
gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="Muscle Disease Detection",
    description="Upload an MRI image to detect muscle conditions."
).launch()