File size: 2,335 Bytes
d9601d5 c4bc877 d9601d5 fec1bc5 d9601d5 c4bc877 d9601d5 c4bc877 d9601d5 c4bc877 d9601d5 7b9d96d c4bc877 7b9d96d c4bc877 d9601d5 7b9d96d d9601d5 c4bc877 7b9d96d c4bc877 7b9d96d c4bc877 7b9d96d c4bc877 7b9d96d c4bc877 7b9d96d c4bc877 7b9d96d c4bc877 7b9d96d d9601d5 c4bc877 d9601d5 | 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | import gradio as gr
import spaces
import torch
from transformers import AutoImageProcessor, AutoModelForImageClassification
MODEL_ID = "DiggaDee/brain-tumor-resnet18"
processor = AutoImageProcessor.from_pretrained(MODEL_ID)
model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
model.to("cuda")
model.eval()
@spaces.GPU
def classify_mri(image):
if image is None:
return {}, "Please upload an MRI image."
inputs = processor(
images=image,
return_tensors="pt"
)
inputs = {
key: value.to("cuda")
for key, value in inputs.items()
}
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.softmax(outputs.logits, dim=-1)[0]
scores = {
model.config.id2label[i]: float(probabilities[i])
for i in range(len(probabilities))
}
prediction_index = probabilities.argmax().item()
prediction = model.config.id2label[prediction_index]
confidence = probabilities[prediction_index].item()
result = (
f"### Prediction: {prediction}\n\n"
f"Confidence: **{confidence:.1%}**\n\n"
"Educational prototype only. Not for medical diagnosis."
)
return scores, result
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# Brain MRI Tumour Classification
Upload a brain MRI image to classify it as:
**Glioma 路 Meningioma 路 No Tumour 路 Pituitary**
"""
)
with gr.Row():
with gr.Column():
image_input = gr.Image(
type="pil",
label="Upload MRI Image"
)
classify_button = gr.Button(
"Classify MRI",
variant="primary"
)
with gr.Column():
prediction_output = gr.Label(
label="Class Probabilities",
num_top_classes=4
)
result_output = gr.Markdown()
classify_button.click(
fn=classify_mri,
inputs=image_input,
outputs=[
prediction_output,
result_output
]
)
gr.Markdown(
"""
**Disclaimer:** This application is for educational purposes only
and must not be used for clinical diagnosis.
"""
)
demo.launch() |