File size: 6,531 Bytes
be716ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
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

# Add backend to 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 for demos
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():
                # Configuration
                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):
                # The custom component
                analyzer = MedicalImageAnalyzer(
                    label="Medical Image Analyzer",
                    analysis_mode="structured",
                    include_confidence=True,
                    include_reasoning=True,
                    elem_id="medical-analyzer"
                )
        
        # Examples section
        gr.Markdown("### 📁 Beispiele")
        
        examples = gr.Examples(
            examples=EXAMPLE_DATA,
            inputs=analyzer,
            label="Beispiel-Analysen"
        )
        
        # Info section
        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
        ```
        """)
        
        # Event handlers
        def update_config(mode, conf, reason):
            # This would update the component configuration
            # In real implementation, this would be handled by the component
            return gr.update(
                analysis_mode=mode,
                include_confidence=conf,
                include_reasoning=reason
            )
        
        # Connect configuration changes
        for config in [analysis_mode, include_confidence, include_reasoning]:
            config.change(
                fn=update_config,
                inputs=[analysis_mode, include_confidence, include_reasoning],
                outputs=analyzer
            )
        
        # Handle analysis results
        def handle_analysis_complete(data):
            if data and "analysis" in data:
                analysis = data["analysis"]
                report = data.get("report", "")
                
                # Log to console for debugging
                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",  # Visual mode for human-readable output
            elem_id="analyzer"
        )
        
        # Auto-analyze on upload
        @analyzer.upload
        def auto_analyze(file_data):
            # The component handles the analysis internally
            return file_data
        
    return demo


if __name__ == "__main__":
    # You can switch between demos
    # demo = create_demo()  # Full demo with configuration
    demo = create_simple_demo()  # Simple demo
    
    demo.launch()