File size: 16,132 Bytes
640840e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8d1284
640840e
 
 
 
 
 
 
f9254f4
 
 
640840e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8d1284
640840e
 
 
 
 
1f7093d
 
0c89e5f
f9254f4
1f7093d
 
 
 
 
640840e
b8d1284
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""
RadAssist-MedGemma β€” HuggingFace Space (Showcase Demo)
=======================================================
This is a CPU-only showcase demo with pre-computed example outputs.
For live inference, see the Kaggle notebook linked below.

MedGemma Impact Challenge Submission by AIencoder
"""

import gradio as gr
from PIL import Image
import requests
from io import BytesIO

# ============================================================
# PRE-COMPUTED DEMO RESULTS
# These were generated using MedGemma 1.5 4B on a T4 GPU
# ============================================================

DEMO_CASES = {
    "Normal Chest X-Ray": {
        "image_url": "https://upload.wikimedia.org/wikipedia/commons/c/c8/Chest_Xray_PA_3-8-2010.png",
        "severity": "NORMAL",
        "confidence": 0.92,
        "key_finding": "No acute cardiopulmonary abnormality",
        "triage_summary": """## 🟒 Triage Result: **NORMAL**

- **Confidence:** 92%
- **Key Finding:** No acute cardiopulmonary abnormality
- **Urgency:** Routine β€” no immediate action required""",
        "analysis": """### Systematic Chest X-Ray Analysis

**Technical Quality:** The radiograph is a well-penetrated PA view with adequate inspiration. No significant rotation is noted.

**Airway:** The trachea is midline. The main bronchi appear patent without evidence of narrowing or deviation.

**Bones & Soft Tissues:** The osseous structures of the thorax appear intact without fractures or lytic lesions. The soft tissues are unremarkable. No subcutaneous emphysema is identified.

**Cardiac:** The cardiac silhouette is within normal limits in size and configuration. The cardiothoracic ratio is within the normal range. The mediastinal contours appear normal. The aortic knob is unremarkable.

**Diaphragm:** Both hemidiaphragms are smooth and well-defined. The costophrenic angles are sharp bilaterally, without evidence of pleural effusion.

**Lungs:** The lung fields are clear bilaterally. There are no focal consolidations, masses, or nodules identified. The pulmonary vascularity appears normal. The hila are symmetric and unremarkable. No pneumothorax is seen.""",
        "report": """**CHEST X-RAY REPORT**

**Clinical Indication:** Routine screening / baseline imaging

**Technique:** PA chest radiograph

**Findings:**
The lungs are well-expanded and clear bilaterally. No focal airspace opacities, masses, or nodules are identified. The pulmonary vasculature appears normal. The cardiac silhouette is normal in size with a cardiothoracic ratio within normal limits. The mediastinal contours are unremarkable. The trachea is midline. Both hemidiaphragms are smooth, and the costophrenic angles are sharp. No pleural effusion or pneumothorax is identified. The osseous structures are intact.

**Impression:**
1. No acute cardiopulmonary abnormality identified.
2. Normal cardiac silhouette size.
3. Clear lung fields bilaterally.

**Recommendation:**
No further imaging follow-up is recommended based on these findings. Routine follow-up as clinically indicated.""",
    },
    "Pneumonia (Abnormal)": {
        "image_url": None,
        "severity": "ABNORMAL",
        "confidence": 0.87,
        "key_finding": "Right lower lobe consolidation consistent with pneumonia",
        "triage_summary": """## 🟑 Triage Result: **ABNORMAL**

- **Confidence:** 87%
- **Key Finding:** Right lower lobe consolidation consistent with pneumonia
- **Urgency:** Follow-up required β€” clinical correlation recommended""",
        "analysis": """### Systematic Chest X-Ray Analysis

**Technical Quality:** Adequate PA radiograph with good penetration and inspiration.

**Airway:** The trachea is midline. No airway deviation or narrowing.

**Bones & Soft Tissues:** No acute osseous abnormalities. Soft tissues are unremarkable.

**Cardiac:** The cardiac silhouette is at the upper limits of normal size. The mediastinal contours are within normal limits.

**Diaphragm:** The left hemidiaphragm is well-defined with a sharp costophrenic angle. The right costophrenic angle shows mild blunting, suggesting a small right-sided pleural effusion.

**Lungs:** There is a focal area of airspace opacity in the right lower lobe, demonstrating an air bronchogram sign, consistent with consolidation. The right middle lobe and upper lobe are clear. The left lung is clear throughout. No masses, nodules, or cavitary lesions are identified. No pneumothorax is seen.""",
        "report": """**CHEST X-RAY REPORT**

**Clinical Indication:** Cough and fever β€” evaluate for pneumonia

**Technique:** PA chest radiograph

**Findings:**
There is a focal area of airspace consolidation within the right lower lobe with visible air bronchograms, consistent with a pneumonic infiltrate. Mild blunting of the right costophrenic angle suggests a small associated parapneumonic effusion. The remaining lung fields are clear. The cardiac silhouette is at the upper limits of normal. The mediastinum is midline and unremarkable. No pneumothorax.

**Impression:**
1. Right lower lobe consolidation, most consistent with community-acquired pneumonia.
2. Small right-sided pleural effusion, likely parapneumonic in nature.

**Recommendation:**
Clinical correlation with laboratory findings (CBC, CRP, blood cultures) is recommended. Consider follow-up chest radiograph in 4-6 weeks to document resolution. If clinical concern for complicated effusion, lateral decubitus view or ultrasound may be considered.""",
    },
    "Critical Finding (Pneumothorax)": {
        "image_url": None,
        "severity": "CRITICAL",
        "confidence": 0.94,
        "key_finding": "Large left-sided tension pneumothorax with mediastinal shift",
        "triage_summary": """## πŸ”΄ Triage Result: **CRITICAL**

- **Confidence:** 94%
- **Key Finding:** Large left-sided tension pneumothorax with mediastinal shift
- **Urgency:** IMMEDIATE β€” requires emergent chest tube placement""",
        "analysis": """### Systematic Chest X-Ray Analysis

**Technical Quality:** AP portable radiograph, slightly rotated. Despite technical limitations, critical findings are clearly identifiable.

**Airway:** The trachea is deviated to the RIGHT, indicating mediastinal shift away from the left hemithorax. This is a critical finding.

**Bones & Soft Tissues:** No acute fractures identified. Subcutaneous emphysema is noted in the left lateral chest wall.

**Cardiac:** The cardiac silhouette appears shifted to the right. Assessment of true cardiac size is limited by mediastinal displacement.

**Diaphragm:** The left hemidiaphragm appears flattened and depressed. The right hemidiaphragm appears normal.

**Lungs:** There is complete absence of lung markings in the left hemithorax with a visible visceral pleural line, consistent with a large left-sided pneumothorax. The left lung appears significantly collapsed toward the hilum. The right lung shows mildly increased vascular markings, likely from compensatory hyperinflation. No focal consolidation in the right lung.""",
        "report": """**CHEST X-RAY REPORT**

**Clinical Indication:** Acute dyspnea and chest pain β€” STAT portable

**Technique:** AP portable chest radiograph

**Findings:**
There is a large left-sided pneumothorax with near-complete collapse of the left lung. The visceral pleural line is clearly visible. There is rightward deviation of the trachea and mediastinal structures, consistent with tension physiology. The left hemidiaphragm is depressed and flattened. Subcutaneous emphysema is present in the left lateral chest wall. The right lung appears hyperinflated but clear without focal consolidation.

**Impression:**
1. **CRITICAL: Large left-sided tension pneumothorax with mediastinal shift** β€” requires emergent decompression.
2. Subcutaneous emphysema of the left chest wall.
3. Near-complete left lung collapse.

**Recommendation:**
EMERGENT needle decompression followed by chest tube placement is indicated. Immediate clinical intervention required. This finding has been communicated urgently.""",
    },
}

# ============================================================
# SEVERITY BADGE HTML
# ============================================================
SEVERITY_STYLES = {
    "NORMAL": {"emoji": "🟒", "color": "#22c55e", "label": "Normal"},
    "ABNORMAL": {"emoji": "🟑", "color": "#f59e0b", "label": "Abnormal β€” Follow Up"},
    "CRITICAL": {"emoji": "πŸ”΄", "color": "#ef4444", "label": "Critical β€” Immediate"},
}


def make_badge(severity, confidence):
    s = SEVERITY_STYLES.get(severity, SEVERITY_STYLES["ABNORMAL"])
    return f"""
    <div style="
        background: {s['color']}15;
        border: 2px solid {s['color']};
        border-radius: 16px;
        padding: 20px 32px;
        text-align: center;
        margin: 12px 0;
    ">
        <span style="font-size: 56px;">{s['emoji']}</span>
        <h2 style="margin: 8px 0 4px; color: {s['color']}; font-size: 1.5em;">{s['label']}</h2>
        <p style="margin: 0; opacity: 0.7; font-size: 1.1em;">Confidence: {confidence}%</p>
    </div>
    """


# ============================================================
# DEMO HANDLER
# ============================================================
def run_demo(case_name):
    """Load pre-computed results for the selected demo case."""
    if case_name not in DEMO_CASES:
        return (
            "<p>Select a demo case above.</p>",
            "", "", "",
        )

    case = DEMO_CASES[case_name]
    badge = make_badge(case["severity"], int(case["confidence"] * 100))

    return (
        badge,
        case["triage_summary"],
        case["analysis"],
        case["report"],
    )


def load_demo_image(case_name):
    """Load the demo image for the selected case."""
    if case_name not in DEMO_CASES:
        return None
    url = DEMO_CASES[case_name].get("image_url")
    if url:
        try:
            resp = requests.get(url, headers={"User-Agent": "RadAssist-MedGemma"}, timeout=10)
            return Image.open(BytesIO(resp.content))
        except Exception:
            return None
    return None


# ============================================================
# GRADIO UI
# ============================================================
with gr.Blocks(
    title="RadAssist-MedGemma",
    theme=gr.themes.Soft(primary_hue="blue", secondary_hue="slate"),
    css="""
    .hero-section { text-align: center; padding: 20px 0; }
    .pipeline-diagram {
        background: linear-gradient(135deg, #1e3a5f20, #3b82f620);
        border-radius: 12px; padding: 20px; margin: 16px 0;
        font-family: monospace; font-size: 0.95em;
    }
    .info-badge {
        display: inline-block; background: #3b82f615; border: 1px solid #3b82f650;
        border-radius: 8px; padding: 4px 12px; margin: 2px 4px; font-size: 0.9em;
    }
    """,
) as demo:

    # --- Hero Section ---
    gr.Markdown("""
    <div class="hero-section">

    # πŸ₯ RadAssist-MedGemma
    ### AI Radiology Triage Assistant β€” MedGemma Impact Challenge

    <span class="info-badge">🧠 MedGemma 1.5 4B</span>
    <span class="info-badge">πŸ”— 3-Step Agentic Pipeline</span>
    <span class="info-badge">πŸ”’ Privacy-Preserving (Local)</span>
    <span class="info-badge">⚑ Edge-Deployable (4-bit)</span>

    </div>
    """)

    gr.Markdown("""
    > **How it works:** RadAssist-MedGemma uses a 3-step agentic pipeline where MedGemma 1.5 4B
    > assumes three distinct clinical roles in sequence β€” Triage Specialist β†’ Diagnostic Radiologist β†’ Report Generator β€”
    > with context passing between steps for coherent, comprehensive analysis.

    <div class="pipeline-diagram">
    πŸ“€ Upload CXR β†’ 🚨 <b>Step 1: TRIAGE</b> (severity classification) β†’ πŸ” <b>Step 2: ANALYZE</b> (systematic findings) β†’ πŸ“‹ <b>Step 3: REPORT</b> (formal radiology report)
    </div>

    ⬇️ **Select a demo case below** to see the pipeline output. For live inference, see the
    [Kaggle Notebook β€” Live Inference](https://www.kaggle.com/code/ary5272/radassist-medgemma-ai-radiology-triage-assistant).
    """)

    gr.Markdown("---")

    # --- Demo Selection ---
    with gr.Row():
        with gr.Column(scale=1):
            # Using gr.Dropdown instead of gr.Radio to avoid Gradio 5.x SSR bug
            # (TypeError: argument of type 'bool' is not iterable in json_schema_to_python_type)
            case_selector = gr.Dropdown(
                choices=list(DEMO_CASES.keys()),
                value="Normal Chest X-Ray",
                label="πŸ“‚ Select Demo Case",
                info="Pre-computed results from MedGemma 1.5 4B on T4 GPU",
            )
            demo_image = gr.Image(
                label="🩻 Chest X-Ray",
                height=380,
                interactive=False,
            )
            run_btn = gr.Button(
                "πŸ”¬ View Pipeline Results",
                variant="primary",
                size="lg",
            )

            gr.Markdown("""
            <div style="background: #fef3c7; border: 1px solid #f59e0b;
                 border-radius: 8px; padding: 12px; margin: 12px 0; font-size: 0.9em;">
            ⚠️ <b>Research prototype only.</b> Not for clinical diagnosis.
            All AI outputs require review by qualified medical professionals.
            </div>
            """)

        # --- Results Panel ---
        with gr.Column(scale=2):
            with gr.Tabs():
                with gr.Tab("🚨 Triage"):
                    triage_badge = gr.HTML(
                        "<p style='text-align:center; opacity:0.5; padding: 40px;'>Select a case and click 'View Pipeline Results'</p>"
                    )
                    triage_text = gr.Markdown("")

                with gr.Tab("πŸ” Detailed Analysis"):
                    analysis_output = gr.Markdown(
                        "*Select a case to view the systematic analysis*"
                    )

                with gr.Tab("πŸ“‹ Radiology Report"):
                    report_output = gr.Markdown(
                        "*Select a case to view the generated report*"
                    )

    # --- Event Handlers ---
    run_btn.click(
        fn=run_demo,
        inputs=[case_selector],
        outputs=[triage_badge, triage_text, analysis_output, report_output],
    )

    case_selector.change(
        fn=load_demo_image,
        inputs=[case_selector],
        outputs=[demo_image],
    )

    # --- About Section ---
    gr.Markdown("---")
    gr.Markdown("""
    ## πŸ—οΈ Architecture & Technical Details

    | Component | Details |
    |-----------|---------|
    | **Model** | [MedGemma 1.5 4B IT](https://huggingface.co/google/medgemma-1.5-4b-it) (4.3B params, multimodal) |
    | **Pipeline** | 3-step agentic: Triage β†’ Analysis β†’ Report with context chaining |
    | **Edge Support** | 4-bit NF4 quantization via bitsandbytes (~3GB VRAM) |
    | **Inference** | Deterministic (`do_sample=False`) for clinical reproducibility |
    | **UI** | Gradio with progressive result streaming |

    ### Why This Matters
    - **2/3 of the world** lacks access to diagnostic imaging interpretation (WHO)
    - **30% of critical findings** experience reporting delays in ERs
    - MedGemma 1.5 4B runs **locally on a single GPU**, keeping patient data private
    - The agentic pipeline mimics real clinical workflows for higher quality output

    ### Links
    - πŸ““ [Kaggle Notebook β€” Live Inference](https://www.kaggle.com/code/ary5272/radassist-medgemma-ai-radiology-triage-assistant)
    - πŸ† [MedGemma Impact Challenge](https://www.kaggle.com/competitions/med-gemma-impact-challenge)
    - πŸ€— [MedGemma 1.5 4B on HuggingFace](https://huggingface.co/google/medgemma-1.5-4b-it)
    - πŸ“„ [MedGemma Technical Report](https://arxiv.org/abs/2507.05201)

    ---
    *Built by [AIencoder](https://huggingface.co/AIencoder) for the MedGemma Impact Challenge 2026*
    """)

    # Load initial demo image on startup
    demo.load(
        fn=load_demo_image,
        inputs=[case_selector],
        outputs=[demo_image],
    )

demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)