File size: 1,135 Bytes
a7c737f
 
 
 
 
 
 
 
 
 
7e76094
 
a7c737f
 
 
 
 
 
 
 
 
 
74f762c
 
 
 
a7c737f
 
 
 
 
 
7e76094
a7c737f
 
 
 
 
 
 
 
 
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
import gradio as gr
from fastai.vision.all import *
from huggingface_hub import from_pretrained_fastai

repo_id = "hugginglearners/brain-tumor-detection-mri"
learn = from_pretrained_fastai(repo_id)
labels = learn.dls.vocab

def predict(img):
    img = PILImage.create(img)
    size = (224, 224)
    img = img.thumbnail(size, Image.ANTIALIAS)
    _pred, _pred_w_idx, probs = learn.predict(img)
    # gradio doesn't support tensors, so converting to float
    labels_probs = {labels[i]: float(probs[i]) for i, _ in enumerate(labels)}
    return labels_probs

interface_options = {
    "title": "Brain tumor detection for MRI images",
    "description": "For reference only. Should **not** be used for medical diagnosis",
    "interpretation": "default",
    "layout": "horizontal",
    "examples": [
        "no 89.jpg",
        "Y22.jpg"
    ],
    "allow_flagging": "never",
}

demo = gr.Interface(
    fn=predict,
    inputs=gr.inputs.Image(shape=(480, 480)),
    outputs=gr.outputs.Label(num_top_classes=2),
    **interface_options,
)

launch_options = {
    "enable_queue": True,
    "share": False,
}

demo.launch(**launch_options)