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()