Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,31 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 4 |
|
| 5 |
MODEL_ID = "DiggaDee/brain-tumor-resnet18"
|
| 6 |
|
| 7 |
processor = AutoImageProcessor.from_pretrained(MODEL_ID)
|
|
|
|
| 8 |
model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
|
|
|
|
| 9 |
model.eval()
|
| 10 |
|
|
|
|
|
|
|
| 11 |
def classify_mri(image):
|
| 12 |
if image is None:
|
| 13 |
-
return
|
| 14 |
|
| 15 |
-
inputs = processor(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
with torch.no_grad():
|
| 18 |
outputs = model(**inputs)
|
|
@@ -23,17 +36,19 @@ def classify_mri(image):
|
|
| 23 |
for i in range(len(probabilities))
|
| 24 |
}
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
result = (
|
| 30 |
f"### Prediction: {prediction}\n\n"
|
| 31 |
f"Confidence: **{confidence:.1%}**\n\n"
|
| 32 |
-
"
|
| 33 |
)
|
| 34 |
|
| 35 |
return scores, result
|
| 36 |
|
|
|
|
| 37 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 38 |
gr.Markdown(
|
| 39 |
"""
|
|
@@ -51,6 +66,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 51 |
type="pil",
|
| 52 |
label="Upload MRI Image"
|
| 53 |
)
|
|
|
|
| 54 |
classify_button = gr.Button(
|
| 55 |
"Classify MRI",
|
| 56 |
variant="primary"
|
|
@@ -61,19 +77,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 61 |
label="Class Probabilities",
|
| 62 |
num_top_classes=4
|
| 63 |
)
|
|
|
|
| 64 |
result_output = gr.Markdown()
|
| 65 |
|
| 66 |
classify_button.click(
|
| 67 |
fn=classify_mri,
|
| 68 |
inputs=image_input,
|
| 69 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
gr.Markdown(
|
| 73 |
"""
|
| 74 |
-
**Disclaimer:** This application is for educational purposes only
|
| 75 |
-
|
| 76 |
"""
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
import torch
|
| 4 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 5 |
|
| 6 |
MODEL_ID = "DiggaDee/brain-tumor-resnet18"
|
| 7 |
|
| 8 |
processor = AutoImageProcessor.from_pretrained(MODEL_ID)
|
| 9 |
+
|
| 10 |
model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
|
| 11 |
+
model.to("cuda")
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
+
|
| 15 |
+
@spaces.GPU
|
| 16 |
def classify_mri(image):
|
| 17 |
if image is None:
|
| 18 |
+
return {}, "Please upload an MRI image."
|
| 19 |
|
| 20 |
+
inputs = processor(
|
| 21 |
+
images=image,
|
| 22 |
+
return_tensors="pt"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
inputs = {
|
| 26 |
+
key: value.to("cuda")
|
| 27 |
+
for key, value in inputs.items()
|
| 28 |
+
}
|
| 29 |
|
| 30 |
with torch.no_grad():
|
| 31 |
outputs = model(**inputs)
|
|
|
|
| 36 |
for i in range(len(probabilities))
|
| 37 |
}
|
| 38 |
|
| 39 |
+
prediction_index = probabilities.argmax().item()
|
| 40 |
+
prediction = model.config.id2label[prediction_index]
|
| 41 |
+
confidence = probabilities[prediction_index].item()
|
| 42 |
|
| 43 |
result = (
|
| 44 |
f"### Prediction: {prediction}\n\n"
|
| 45 |
f"Confidence: **{confidence:.1%}**\n\n"
|
| 46 |
+
"Educational prototype only. Not for medical diagnosis."
|
| 47 |
)
|
| 48 |
|
| 49 |
return scores, result
|
| 50 |
|
| 51 |
+
|
| 52 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 53 |
gr.Markdown(
|
| 54 |
"""
|
|
|
|
| 66 |
type="pil",
|
| 67 |
label="Upload MRI Image"
|
| 68 |
)
|
| 69 |
+
|
| 70 |
classify_button = gr.Button(
|
| 71 |
"Classify MRI",
|
| 72 |
variant="primary"
|
|
|
|
| 77 |
label="Class Probabilities",
|
| 78 |
num_top_classes=4
|
| 79 |
)
|
| 80 |
+
|
| 81 |
result_output = gr.Markdown()
|
| 82 |
|
| 83 |
classify_button.click(
|
| 84 |
fn=classify_mri,
|
| 85 |
inputs=image_input,
|
| 86 |
+
outputs=[
|
| 87 |
+
prediction_output,
|
| 88 |
+
result_output
|
| 89 |
+
]
|
| 90 |
)
|
| 91 |
|
| 92 |
gr.Markdown(
|
| 93 |
"""
|
| 94 |
+
**Disclaimer:** This application is for educational purposes only
|
| 95 |
+
and must not be used for clinical diagnosis.
|
| 96 |
"""
|
| 97 |
)
|
| 98 |
|
| 99 |
+
|
| 100 |
demo.launch()
|