File size: 1,073 Bytes
a8be84c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf 
import numpy as np
from PIL import Image

interpreter = tf.lite.Interpreter(model_path = "cnn.tflite")
interpreter.allocate_tensors()
input_details  = interpreter.get_input_details()
output_details = interpreter.get_output_details()

CLASSES = ["Glioma", "Meningioma", "No Tumor", "Pituitary"]
IMG_SIZE = (224, 224)

def preprocess(img: Image.Image) -> np.ndarray:
    img = img.resize(IMG_SIZE)
    arr = np.asarray(img, dtype=np.float32) / 255.0
    return np.expand_dims(arr, 0)      

def predict(image):
    x = preprocess(image)
    interpreter.set_tensor(input_details[0]["index"], x)
    interpreter.invoke()
    probs = interpreter.get_tensor(output_details[0]["index"])
    return CLASSES[int(np.argmax(probs, axis=1)[0])]

demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Brain MRI"),
    outputs=gr.Label(num_top_classes=4),
    title="Brain‑Tumor Classifier (.tflite)",
    description="Returns: Glioma, Meningioma, No Tumor, Pituitary"
)

if __name__ == "__main__":
    demo.launch()