|
|
|
|
|
""" |
|
|
Demo für MedicalImageAnalyzer mit Frontend Component |
|
|
Zeigt die Verwendung der vollständigen Gradio Custom Component |
|
|
""" |
|
|
|
|
|
import gradio as gr |
|
|
import numpy as np |
|
|
import sys |
|
|
import os |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'backend')) |
|
|
|
|
|
from gradio_medical_image_analyzer import MedicalImageAnalyzer |
|
|
|
|
|
|
|
|
EXAMPLE_DATA = [ |
|
|
{ |
|
|
"image": {"url": "examples/ct_chest.png"}, |
|
|
"analysis": { |
|
|
"modality": "CT", |
|
|
"point_analysis": { |
|
|
"tissue_type": {"icon": "🟡", "type": "fat"}, |
|
|
"hu_value": -75.0 |
|
|
}, |
|
|
"segmentation": { |
|
|
"interpretation": { |
|
|
"obesity_risk": "moderate" |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
{ |
|
|
"image": {"url": "examples/xray_chest.png"}, |
|
|
"analysis": { |
|
|
"modality": "CR", |
|
|
"point_analysis": { |
|
|
"tissue_type": {"icon": "🦴", "type": "bone"} |
|
|
} |
|
|
} |
|
|
} |
|
|
] |
|
|
|
|
|
def create_demo(): |
|
|
with gr.Blocks(title="Medical Image Analyzer - Component Demo") as demo: |
|
|
gr.Markdown(""" |
|
|
# 🏥 Medical Image Analyzer - Frontend Component Demo |
|
|
|
|
|
Diese Demo zeigt die vollständige Gradio Custom Component mit Frontend-Integration. |
|
|
Unterstützt DICOM-Dateien und alle gängigen Bildformate. |
|
|
""") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
|
|
|
gr.Markdown("### ⚙️ Konfiguration") |
|
|
|
|
|
analysis_mode = gr.Radio( |
|
|
choices=["structured", "visual"], |
|
|
value="structured", |
|
|
label="Analyse-Modus", |
|
|
info="structured: für AI Agents, visual: für Menschen" |
|
|
) |
|
|
|
|
|
include_confidence = gr.Checkbox( |
|
|
value=True, |
|
|
label="Konfidenzwerte einschließen" |
|
|
) |
|
|
|
|
|
include_reasoning = gr.Checkbox( |
|
|
value=True, |
|
|
label="Reasoning einschließen" |
|
|
) |
|
|
|
|
|
with gr.Column(scale=2): |
|
|
|
|
|
analyzer = MedicalImageAnalyzer( |
|
|
label="Medical Image Analyzer", |
|
|
analysis_mode="structured", |
|
|
include_confidence=True, |
|
|
include_reasoning=True, |
|
|
elem_id="medical-analyzer" |
|
|
) |
|
|
|
|
|
|
|
|
gr.Markdown("### 📁 Beispiele") |
|
|
|
|
|
examples = gr.Examples( |
|
|
examples=EXAMPLE_DATA, |
|
|
inputs=analyzer, |
|
|
label="Beispiel-Analysen" |
|
|
) |
|
|
|
|
|
|
|
|
gr.Markdown(""" |
|
|
### 📝 Verwendung |
|
|
|
|
|
1. **Datei hochladen**: Ziehen Sie eine DICOM- oder Bilddatei in den Upload-Bereich |
|
|
2. **Modalität wählen**: CT, CR, DX, RX, oder DR |
|
|
3. **Analyse-Task**: Punktanalyse, Fettsegmentierung, oder vollständige Analyse |
|
|
4. **ROI aktivieren**: Klicken Sie auf das Bild, um einen Analysepunkt zu wählen |
|
|
|
|
|
### 🔧 Features |
|
|
|
|
|
- **DICOM Support**: Automatische Erkennung von Modalität und HU-Werten |
|
|
- **Multi-Tissue Segmentation**: Erkennt Knochen, Weichgewebe, Luft, Metall, Fett, Flüssigkeit |
|
|
- **Klinische Bewertung**: Adipositas-Risiko, Gewebeverteilung, Anomalieerkennung |
|
|
- **AI-Agent Ready**: Strukturierte JSON-Ausgabe für Integration |
|
|
|
|
|
### 🔗 Integration |
|
|
|
|
|
```python |
|
|
import gradio as gr |
|
|
from gradio_medical_image_analyzer import MedicalImageAnalyzer |
|
|
|
|
|
analyzer = MedicalImageAnalyzer( |
|
|
analysis_mode="structured", |
|
|
include_confidence=True |
|
|
) |
|
|
|
|
|
# Use in your Gradio app |
|
|
with gr.Blocks() as app: |
|
|
analyzer_component = analyzer |
|
|
# ... rest of your app |
|
|
``` |
|
|
""") |
|
|
|
|
|
|
|
|
def update_config(mode, conf, reason): |
|
|
|
|
|
|
|
|
return gr.update( |
|
|
analysis_mode=mode, |
|
|
include_confidence=conf, |
|
|
include_reasoning=reason |
|
|
) |
|
|
|
|
|
|
|
|
for config in [analysis_mode, include_confidence, include_reasoning]: |
|
|
config.change( |
|
|
fn=update_config, |
|
|
inputs=[analysis_mode, include_confidence, include_reasoning], |
|
|
outputs=analyzer |
|
|
) |
|
|
|
|
|
|
|
|
def handle_analysis_complete(data): |
|
|
if data and "analysis" in data: |
|
|
analysis = data["analysis"] |
|
|
report = data.get("report", "") |
|
|
|
|
|
|
|
|
print("Analysis completed:") |
|
|
print(f"Modality: {analysis.get('modality', 'Unknown')}") |
|
|
if "point_analysis" in analysis: |
|
|
print(f"Tissue: {analysis['point_analysis'].get('tissue_type', {}).get('type', 'Unknown')}") |
|
|
|
|
|
return data |
|
|
return data |
|
|
|
|
|
analyzer.change( |
|
|
fn=handle_analysis_complete, |
|
|
inputs=analyzer, |
|
|
outputs=analyzer |
|
|
) |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
def create_simple_demo(): |
|
|
"""Einfache Demo ohne viel Konfiguration""" |
|
|
with gr.Blocks(title="Medical Image Analyzer - Simple Demo") as demo: |
|
|
gr.Markdown("# 🏥 Medical Image Analyzer") |
|
|
|
|
|
analyzer = MedicalImageAnalyzer( |
|
|
label="Laden Sie ein medizinisches Bild hoch (DICOM, PNG, JPG)", |
|
|
analysis_mode="visual", |
|
|
elem_id="analyzer" |
|
|
) |
|
|
|
|
|
|
|
|
@analyzer.upload |
|
|
def auto_analyze(file_data): |
|
|
|
|
|
return file_data |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
|
|
|
demo = create_simple_demo() |
|
|
|
|
|
demo.launch() |