Rihan1729 commited on
Commit
c47f58c
·
verified ·
1 Parent(s): bdf5fa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -20
app.py CHANGED
@@ -1,20 +1,121 @@
1
- ---
2
- title: 🧠 NEURO-SCAN AI
3
- emoji:
4
- colorFrom: blue
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 6.6.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- short_description: Advanced ViT-based Brain Tumor Detection & Overlay
12
- ---
13
-
14
- # Neuro-Scan AI
15
- An advanced computer vision application using **Vision Transformers (ViT)** to classify MRI scans into four categories with high precision.
16
-
17
- ### Key Features:
18
- - **Real-time Visualization:** Automated red-zone highlighting for detected masses.
19
- - **Deep Insights:** Full probability distribution for clinical transparency.
20
- - **Dark Mode UI:** Optimized for medical review environments.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ import torch
5
+ import numpy as np
6
+
7
+ # --- Model Setup (Unchanged) ---
8
+ model_name = "Hemgg/brain-tumor-classification"
9
+ processor = AutoImageProcessor.from_pretrained(model_name)
10
+ model = AutoModelForImageClassification.from_pretrained(model_name)
11
+ class_names = ["🧠 Glioma", "🎯 Meningioma", "✅ No Tumor", "⚡ Pituitary"]
12
+
13
+ # --- Custom CSS for Advanced Look ---
14
+ custom_css = """
15
+ body, .gradio-container {
16
+ background-color: #0b0f19 !important;
17
+ color: #ffffff !important;
18
+ }
19
+ .md, .md p, .md h1, .md h2, .md h3 {
20
+ color: white !important;
21
+ }
22
+ #custom-card {
23
+ background: rgba(255, 255, 255, 0.05);
24
+ border-radius: 15px;
25
+ padding: 20px;
26
+ border: 1px solid rgba(255, 255, 255, 0.1);
27
+ }
28
+ .gr-button-primary {
29
+ background: linear-gradient(90deg, #2563eb, #7c3aed) !important;
30
+ border: none !important;
31
+ }
32
+ footer {display: none !important;}
33
+ """
34
+
35
+ def classify_tumor(image):
36
+ if image is None:
37
+ return "Please upload a brain MRI", None
38
+
39
+ inputs = processor(images=image, return_tensors="pt")
40
+ with torch.no_grad():
41
+ outputs = model(**inputs)
42
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
43
+ pred_idx = probs.argmax(-1).item()
44
+ confidence = probs[0][pred_idx].item()
45
+ tumor_type = class_names[pred_idx]
46
+
47
+ overlay_img = create_overlay(image, pred_idx, confidence)
48
+
49
+ # Advanced Result Formatting
50
+ result = f"""
51
+ <div style="background: rgba(255,255,255,0.1); padding: 20px; border-radius: 10px; border-left: 5px solid #3b82f6;">
52
+ <h2 style="margin:0; color: white;">Analysis: {tumor_type}</h2>
53
+ <h3 style="margin:5px 0; color: #93c5fd;">Confidence Score: {confidence:.1%}</h3>
54
+ <hr style="opacity: 0.2; margin: 15px 0;">
55
+ <p style="color: #cbd5e1;"><b>Probability Distribution:</b></p>
56
+ {"".join([f"<div style='margin-bottom:5px;'>{class_names[i]}: <span style='float:right;'>{probs[0][i]:.1%}</span></div>" for i in range(4)])}
57
+ </div>
58
+ """
59
+ return result, overlay_img
60
+
61
+ def create_overlay(image, tumor_class, confidence):
62
+ overlay = image.copy().convert("RGBA")
63
+ draw = ImageDraw.Draw(overlay)
64
+ w, h = overlay.size
65
+
66
+ if tumor_class != 2:
67
+ radius = min(w, h) // 4
68
+ cx, cy = w // 2, h // 2
69
+ alpha = int(140 * confidence)
70
+ draw.ellipse([cx-radius, cy-radius, cx+radius, cy+radius],
71
+ fill=(255, 0, 0, alpha), outline=(255, 255, 0, 255), width=4)
72
+
73
+ try:
74
+ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 24)
75
+ except:
76
+ font = ImageFont.load_default()
77
+
78
+ label = f"REGION OF INTEREST: {class_names[tumor_class]}"
79
+ draw.text((20, 20), label, fill=(255, 255, 255), font=font)
80
+
81
+ return overlay.convert("RGB")
82
+
83
+ # --- Interface ---
84
+ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
85
+ with gr.Row():
86
+ gr.HTML("""
87
+ <div style="text-align: center; padding: 20px;">
88
+ <h1 style="color: white; font-size: 2.5rem; margin-bottom: 0;">NEURO-SCAN AI</h1>
89
+ <p style="color: #94a3b8; font-size: 1.1rem;">Advanced Deep Learning Diagnostic Support System</p>
90
+ </div>
91
+ """)
92
+
93
+ with gr.Tabs():
94
+ with gr.TabItem("🔍 Diagnostic Scanner"):
95
+ with gr.Row():
96
+ with gr.Column(scale=1):
97
+ input_img = gr.Image(label="Input MRI Scan", type="pil", height=400)
98
+ btn = gr.Button("START NEURAL ANALYSIS", variant="primary")
99
+
100
+ with gr.Column(scale=1):
101
+ output_html = gr.HTML(label="Diagnostic Report")
102
+ overlay_img = gr.Image(label="Visualization Overlay", height=400)
103
+
104
+ with gr.TabItem("📖 Tumor Information"):
105
+ gr.Markdown("""
106
+ ### **Predefined Tumor Classification Reference**
107
+
108
+ | Tumor Type | Description | Characteristic |
109
+ | :--- | :--- | :--- |
110
+ | **Glioma** | Tumors that start in the glial cells of the brain or spine. | Most common primary brain tumor. |
111
+ | **Meningioma** | A tumor that arises from the meninges (membranes covering the brain). | Usually slow-growing and benign. |
112
+ | **Pituitary** | Abnormal growths that develop in the pituitary gland. | Can affect hormone levels and vision. |
113
+ | **No Tumor** | Normal brain tissue detected. | No significant mass effect or abnormal growth seen. |
114
+
115
+ > **Disclaimer:** This AI is for educational and research purposes only. Always consult a certified radiologist for medical diagnosis.
116
+ """)
117
+
118
+ btn.click(classify_tumor, input_img, [output_html, overlay_img])
119
+
120
+ if __name__ == "__main__":
121
+ demo.launch()