File size: 1,425 Bytes
1602826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67c0392
1f09592
42a35e2
 
 
 
d76f11a
e5058b0
1f09592
1602826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d7a534
1602826
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68

import gradio as gr
import os
import torch

from model import create_efficientb2_model
from timeit import default_timer as timer

class_names = [
    "glioma",
    "meningioma",
    "notumor",
    "pituitary"
]

efficientb2, transforms = create_efficientb2_model(num_classes=4)

# Load the entire model directly from the file
my_model_weight = torch.load(
    f="efficientnet_mri_model.pth",
    map_location=torch.device("cpu"),
    weights_only=False
)

efficientb2.load_state_dict(my_model_weight())

def predict_img(img):

  start_time = timer()

  img = transforms(img).unsqueeze(0)

  efficientb2.eval()

  with torch.inference_mode():

    pred_probs = torch.softmax(efficientb2(img), dim=1)

    pred_labels_and_probs = {
        class_names[i] : float(pred_probs[0][i]) for i in range(len(class_names))
    }

    pred_time = round(timer() - start_time(),5)

    return pred_labels_and_probs, pred_time

title = "MRI Result Finder"
description = "Efficientnet b2 model to classify MRI images"
article = "Created at 2026"

example_list = [["examples/" + example] for example in os.listdir("examples")]

demo = gr.Interface(
    fn=predict_img,
    inputs=gr.Image(type="pil"),
    outputs=[
        gr.Label(num_top_classes=4,label="Predictions"),
        gr.Number(label="Prediction Time")
    ],
    examples = example_list,
    title = title,
    description = description,
    article = article
)


demo.lunch()