DiggaDee commited on
Commit
7b9d96d
路
verified 路
1 Parent(s): d9601d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -8
app.py CHANGED
@@ -9,23 +9,71 @@ model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
9
  model.eval()
10
 
11
  def classify_mri(image):
 
 
 
12
  inputs = processor(images=image, return_tensors="pt")
13
 
14
  with torch.no_grad():
15
  outputs = model(**inputs)
16
  probabilities = torch.softmax(outputs.logits, dim=-1)[0]
17
 
18
- return {
19
  model.config.id2label[i]: float(probabilities[i])
20
  for i in range(len(probabilities))
21
  }
22
 
23
- demo = gr.Interface(
24
- fn=classify_mri,
25
- inputs=gr.Image(type="pil", label="Upload brain MRI"),
26
- outputs=gr.Label(num_top_classes=4),
27
- title="Brain MRI Tumour Classification",
28
- description="Educational prototype only. Not for medical diagnosis."
29
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  demo.launch()
 
9
  model.eval()
10
 
11
  def classify_mri(image):
12
+ if image is None:
13
+ return None, "Please upload an MRI image."
14
+
15
  inputs = processor(images=image, return_tensors="pt")
16
 
17
  with torch.no_grad():
18
  outputs = model(**inputs)
19
  probabilities = torch.softmax(outputs.logits, dim=-1)[0]
20
 
21
+ scores = {
22
  model.config.id2label[i]: float(probabilities[i])
23
  for i in range(len(probabilities))
24
  }
25
 
26
+ prediction = max(scores, key=scores.get)
27
+ confidence = scores[prediction]
28
+
29
+ result = (
30
+ f"### Prediction: {prediction}\n\n"
31
+ f"Confidence: **{confidence:.1%}**\n\n"
32
+ "This is an educational prototype and not a medical diagnosis."
33
+ )
34
+
35
+ return scores, result
36
+
37
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
38
+ gr.Markdown(
39
+ """
40
+ # Brain MRI Tumour Classification
41
+
42
+ Upload a brain MRI image to classify it as:
43
+
44
+ **Glioma 路 Meningioma 路 No Tumour 路 Pituitary**
45
+ """
46
+ )
47
+
48
+ with gr.Row():
49
+ with gr.Column():
50
+ image_input = gr.Image(
51
+ type="pil",
52
+ label="Upload MRI Image"
53
+ )
54
+ classify_button = gr.Button(
55
+ "Classify MRI",
56
+ variant="primary"
57
+ )
58
+
59
+ with gr.Column():
60
+ prediction_output = gr.Label(
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=[prediction_output, result_output]
70
+ )
71
+
72
+ gr.Markdown(
73
+ """
74
+ **Disclaimer:** This application is for educational purposes only.
75
+ It must not be used for clinical diagnosis or treatment decisions.
76
+ """
77
+ )
78
 
79
  demo.launch()