File size: 1,521 Bytes
c7abe91
0973c8d
c7abe91
 
f3efed7
b4d39e8
f3efed7
 
b4d39e8
f3efed7
 
 
 
b4d39e8
 
5de0c02
c7abe91
 
 
 
 
 
b4d39e8
 
c7abe91
 
 
 
 
 
 
 
 
 
 
b4d39e8
 
 
 
 
 
c7abe91
 
 
 
 
b4d39e8
c7abe91
0973c8d
6df4cb4
c7abe91
b4d39e8
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image, ImageOps
import socket

def find_free_port():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(('', 0))
        return s.getsockname()[1]

free_port = find_free_port()
print(f"Launching Gradio on free port: {free_port}")

# Make sure this filename is correct!
model = tf.keras.models.load_model('best_cnnmodelf&n.h5')

class_names = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']

def preprocess(image):
    size = (150, 150)
    image = ImageOps.fit(image, size, Image.ANTIALIAS)
    image = np.array(image) / 255.0
    image = np.expand_dims(image, axis=0)
    return image

def classify(image):
    img = preprocess(image)
    preds = model.predict(img)
    scores = tf.nn.softmax(preds[0]).numpy()
    top_idx = np.argmax(scores)
    predicted_class = class_names[top_idx]
    confidence = scores[top_idx] * 100
    return f"Prediction: {predicted_class}", f"Confidence: {confidence:.2f}%"

iface = gr.Interface(
    fn=classify,
    inputs=gr.Image(type="pil", label="Upload MRI scan"),
    outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Confidence")],
    title="Brain Tumor Classification",
    description="""
Upload a brain MRI scan image (jpg or png). The model will classify the tumor type as:
- Glioma Tumor
- Meningioma Tumor
- No Tumor
- Pituitary Tumor
""",
    allow_flagging="never",
)

if __name__ == "__main__":
    iface.launch(server_port=free_port)