File size: 34,187 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#!/usr/bin/env python3
"""
Frontend Wrapper Test fΓΌr Medical Image Analyzer
Nutzt Standard Gradio Komponenten um die Backend-FunktionalitΓ€t zu testen
"""

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
import cv2

def draw_roi_on_image(image, roi_x, roi_y, roi_radius):
    """Draw ROI circle on the image"""
    # Convert to RGB if grayscale
    if len(image.shape) == 2:
        image_rgb = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    else:
        image_rgb = image.copy()
    
    # Draw ROI circle
    center = (int(roi_x), int(roi_y))
    radius = int(roi_radius)
    
    # Draw outer circle (white)
    cv2.circle(image_rgb, center, radius, (255, 255, 255), 2)
    # Draw inner circle (red)
    cv2.circle(image_rgb, center, radius-1, (255, 0, 0), 2)
    # Draw center cross
    cv2.line(image_rgb, (center[0]-5, center[1]), (center[0]+5, center[1]), (255, 0, 0), 2)
    cv2.line(image_rgb, (center[0], center[1]-5), (center[0], center[1]+5), (255, 0, 0), 2)
    
    return image_rgb

def create_fat_overlay(base_image, segmentation_results):
    """Create overlay image with fat segmentation highlighted"""
    # Convert to RGB
    if len(base_image.shape) == 2:
        overlay_img = cv2.cvtColor(base_image, cv2.COLOR_GRAY2RGB)
    else:
        overlay_img = base_image.copy()
    
    # Check if we have segmentation masks
    if not segmentation_results or 'segments' not in segmentation_results:
        return overlay_img
    
    segments = segmentation_results.get('segments', {})
    
    # Apply subcutaneous fat overlay (yellow)
    if 'subcutaneous' in segments and segments['subcutaneous'].get('mask') is not None:
        mask = segments['subcutaneous']['mask']
        yellow_overlay = np.zeros_like(overlay_img)
        yellow_overlay[mask > 0] = [255, 255, 0]  # Yellow
        overlay_img = cv2.addWeighted(overlay_img, 0.7, yellow_overlay, 0.3, 0)
    
    # Apply visceral fat overlay (red)
    if 'visceral' in segments and segments['visceral'].get('mask') is not None:
        mask = segments['visceral']['mask']
        red_overlay = np.zeros_like(overlay_img)
        red_overlay[mask > 0] = [255, 0, 0]  # Red
        overlay_img = cv2.addWeighted(overlay_img, 0.7, red_overlay, 0.3, 0)
    
    # Add legend
    cv2.putText(overlay_img, "Yellow: Subcutaneous Fat", (10, 30), 
                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 0), 2)
    cv2.putText(overlay_img, "Red: Visceral Fat", (10, 60), 
                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
    
    return overlay_img

def process_and_analyze(file_obj, task, roi_x, roi_y, roi_radius, symptoms, show_overlay=False):
    """
    Processes uploaded file and performs analysis
    """
    if file_obj is None:
        return None, "No file selected", "", {}, None
    
    # Create analyzer instance
    analyzer = MedicalImageAnalyzer(
        analysis_mode="structured",
        include_confidence=True,
        include_reasoning=True
    )
    
    try:
        # Process the file (DICOM or image)
        file_path = file_obj.name if hasattr(file_obj, 'name') else str(file_obj)
        pixel_array, display_array, metadata = analyzer.process_file(file_path)
        
        # Update modality from file metadata
        modality = metadata.get('modality', 'CR')
        
        # Prepare analysis parameters
        analysis_params = {
            "image": pixel_array,
            "modality": modality,
            "task": task
        }
        
        # Add ROI if applicable
        if task in ["analyze_point", "full_analysis"]:
            # Scale ROI coordinates to image size
            h, w = pixel_array.shape
            roi_x_scaled = int(roi_x * w / 512)  # Assuming slider max is 512
            roi_y_scaled = int(roi_y * h / 512)
            
            analysis_params["roi"] = {
                "x": roi_x_scaled,
                "y": roi_y_scaled,
                "radius": roi_radius
            }
        
        # Add clinical context
        if symptoms:
            analysis_params["clinical_context"] = ", ".join(symptoms)
        
        # Perform analysis
        results = analyzer.analyze_image(**analysis_params)
        
        # Create visual report
        if analyzer.analysis_mode == "visual":
            visual_report = analyzer._create_html_report(results)
        else:
            # Create our own visual report
            visual_report = create_visual_report(results, metadata)
        
        # Add metadata info
        info = f"πŸ“„ {metadata.get('file_type', 'Unknown')} | "
        info += f"πŸ₯ {modality} | "
        info += f"πŸ“ {metadata.get('shape', 'Unknown')}"
        
        if metadata.get('window_center'):
            info += f" | Window C:{metadata['window_center']:.0f} W:{metadata['window_width']:.0f}"
        
        # Create overlay image if requested
        overlay_image = None
        if show_overlay:
            # For ROI visualization
            if task in ["analyze_point", "full_analysis"] and roi_x and roi_y:
                overlay_image = draw_roi_on_image(display_array.copy(), roi_x, roi_y, roi_radius)
            
            # For fat segmentation overlay
            if task == "segment_fat" and 'segmentation' in results and modality == 'CT':
                # Get segmentation masks from results
                seg_results = {
                    'segments': {
                        'subcutaneous': {'mask': None},
                        'visceral': {'mask': None}
                    }
                }
                
                # Check if we have actual mask data
                if 'segments' in results['segmentation']:
                    seg_results = results['segmentation']
                
                overlay_image = create_fat_overlay(display_array.copy(), seg_results)
        
        return display_array, info, visual_report, results, overlay_image
        
    except Exception as e:
        error_msg = f"Error: {str(e)}"
        return None, error_msg, f"<div style='color: red;'>❌ {error_msg}</div>", {"error": error_msg}, None


def create_visual_report(results, metadata):
    """Creates a visual HTML report with improved styling"""
    html = f"""
    <div class='medical-report' style='font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; 
                padding: 24px; 
                background: #ffffff; 
                border-radius: 12px; 
                max-width: 100%; 
                box-shadow: 0 2px 8px rgba(0,0,0,0.1);
                color: #1a1a1a !important;'>
        
        <h2 style='color: #1e40af !important; 
                   border-bottom: 3px solid #3b82f6; 
                   padding-bottom: 12px; 
                   margin-bottom: 20px;
                   font-size: 24px;
                   font-weight: 600;'>
            πŸ₯ Medical Image Analysis Report
        </h2>
        
        <div style='background: #f0f9ff; 
                    padding: 20px; 
                    margin: 16px 0; 
                    border-radius: 8px; 
                    box-shadow: 0 1px 3px rgba(0,0,0,0.1);'>
            <h3 style='color: #1e3a8a !important; 
                       font-size: 18px; 
                       font-weight: 600; 
                       margin-bottom: 12px;'>
                πŸ“‹ Metadata
            </h3>
            <table style='width: 100%; border-collapse: collapse;'>
                <tr>
                    <td style='padding: 8px 0; color: #4b5563 !important; width: 40%;'><strong style='color: #374151 !important;'>File Type:</strong></td>
                    <td style='padding: 8px 0; color: #1f2937 !important;'>{metadata.get('file_type', 'Unknown')}</td>
                </tr>
                <tr>
                    <td style='padding: 8px 0; color: #4b5563 !important;'><strong style='color: #374151 !important;'>Modality:</strong></td>
                    <td style='padding: 8px 0; color: #1f2937 !important;'>{results.get('modality', 'Unknown')}</td>
                </tr>
                <tr>
                    <td style='padding: 8px 0; color: #4b5563 !important;'><strong style='color: #374151 !important;'>Image Size:</strong></td>
                    <td style='padding: 8px 0; color: #1f2937 !important;'>{metadata.get('shape', 'Unknown')}</td>
                </tr>
                <tr>
                    <td style='padding: 8px 0; color: #4b5563 !important;'><strong style='color: #374151 !important;'>Timestamp:</strong></td>
                    <td style='padding: 8px 0; color: #1f2937 !important;'>{results.get('timestamp', 'N/A')}</td>
                </tr>
            </table>
        </div>
    """
    
    # Point Analysis
    if 'point_analysis' in results:
        pa = results['point_analysis']
        tissue = pa.get('tissue_type', {})
        
        html += f"""
        <div style='background: #f0f9ff; 
                    padding: 20px; 
                    margin: 16px 0; 
                    border-radius: 8px; 
                    box-shadow: 0 1px 3px rgba(0,0,0,0.1);'>
            <h3 style='color: #1e3a8a !important; 
                       font-size: 18px; 
                       font-weight: 600; 
                       margin-bottom: 12px;'>
                🎯 Point Analysis
            </h3>
            <table style='width: 100%; border-collapse: collapse;'>
                <tr>
                    <td style='padding: 8px 0; color: #4b5563 !important; width: 40%;'><strong style='color: #374151 !important;'>Position:</strong></td>
                    <td style='padding: 8px 0; color: #1f2937 !important;'>({pa.get('location', {}).get('x', 'N/A')}, {pa.get('location', {}).get('y', 'N/A')})</td>
                </tr>
        """
        
        if results.get('modality') == 'CT':
            html += f"""
                <tr>
                    <td style='padding: 8px 0; color: #4b5563 !important;'><strong style='color: #374151 !important;'>HU Value:</strong></td>
                    <td style='padding: 8px 0; color: #1f2937 !important; font-weight: 500;'>{pa.get('hu_value', 'N/A'):.1f}</td>
                </tr>
            """
        else:
            html += f"""
                <tr>
                    <td style='padding: 8px 0; color: #4b5563 !important;'><strong style='color: #374151 !important;'>Intensity:</strong></td>
                    <td style='padding: 8px 0; color: #1f2937 !important;'>{pa.get('intensity', 'N/A'):.3f}</td>
                </tr>
            """
        
        html += f"""
                <tr>
                    <td style='padding: 8px 0; color: #4b5563 !important;'><strong style='color: #374151 !important;'>Tissue Type:</strong></td>
                    <td style='padding: 8px 0; color: #1f2937 !important;'>
                        <span style='font-size: 1.3em; vertical-align: middle;'>{tissue.get('icon', '')}</span> 
                        <span style='font-weight: 500; text-transform: capitalize;'>{tissue.get('type', 'Unknown').replace('_', ' ')}</span>
                    </td>
                </tr>
                <tr>
                    <td style='padding: 8px 0; color: #4b5563 !important;'><strong style='color: #374151 !important;'>Confidence:</strong></td>
                    <td style='padding: 8px 0; color: #1f2937 !important;'>{pa.get('confidence', 'N/A')}</td>
                </tr>
            </table>
        """
        
        if 'reasoning' in pa:
            html += f"""
            <div style='margin-top: 12px; 
                        padding: 12px; 
                        background: #f0f7ff; 
                        border-left: 3px solid #0066cc; 
                        border-radius: 4px;'>
                <p style='margin: 0; color: #4b5563 !important; font-style: italic;'>
                    πŸ’­ {pa['reasoning']}
                </p>
            </div>
            """
        
        html += "</div>"
    
    # Segmentation Results
    if 'segmentation' in results and results['segmentation']:
        seg = results['segmentation']
        
        if 'statistics' in seg:
            # Fat segmentation for CT
            stats = seg['statistics']
            html += f"""
            <div style='background: #f0f9ff; 
                        padding: 20px; 
                        margin: 16px 0; 
                        border-radius: 8px; 
                        box-shadow: 0 1px 3px rgba(0,0,0,0.1);'>
                <h3 style='color: #1e3a8a !important; 
                           font-size: 18px; 
                           font-weight: 600; 
                           margin-bottom: 12px;'>
                    πŸ”¬ Fat Segmentation Analysis
                </h3>
                <div style='display: grid; grid-template-columns: 1fr 1fr; gap: 16px;'>
                    <div style='padding: 16px; background: #ffffff; border-radius: 6px; border: 1px solid #e5e7eb;'>
                        <h4 style='color: #6b7280 !important; font-size: 14px; margin: 0 0 8px 0; font-weight: 500;'>Total Fat</h4>
                        <p style='color: #1f2937 !important; font-size: 24px; font-weight: 600; margin: 0;'>{stats.get('total_fat_percentage', 0):.1f}%</p>
                    </div>
                    <div style='padding: 16px; background: #fffbeb; border-radius: 6px; border: 1px solid #fbbf24;'>
                        <h4 style='color: #92400e !important; font-size: 14px; margin: 0 0 8px 0; font-weight: 500;'>Subcutaneous</h4>
                        <p style='color: #d97706 !important; font-size: 24px; font-weight: 600; margin: 0;'>{stats.get('subcutaneous_fat_percentage', 0):.1f}%</p>
                    </div>
                    <div style='padding: 16px; background: #fef2f2; border-radius: 6px; border: 1px solid #fca5a5;'>
                        <h4 style='color: #991b1b !important; font-size: 14px; margin: 0 0 8px 0; font-weight: 500;'>Visceral</h4>
                        <p style='color: #dc2626 !important; font-size: 24px; font-weight: 600; margin: 0;'>{stats.get('visceral_fat_percentage', 0):.1f}%</p>
                    </div>
                    <div style='padding: 16px; background: #eff6ff; border-radius: 6px; border: 1px solid #93c5fd;'>
                        <h4 style='color: #1e3a8a !important; font-size: 14px; margin: 0 0 8px 0; font-weight: 500;'>V/S Ratio</h4>
                        <p style='color: #1e40af !important; font-size: 24px; font-weight: 600; margin: 0;'>{stats.get('visceral_subcutaneous_ratio', 0):.2f}</p>
                    </div>
                </div>
            """
            
            if 'interpretation' in seg:
                interp = seg['interpretation']
                obesity_color = "#27ae60" if interp.get("obesity_risk") == "normal" else "#f39c12" if interp.get("obesity_risk") == "moderate" else "#e74c3c"
                visceral_color = "#27ae60" if interp.get("visceral_risk") == "normal" else "#f39c12" if interp.get("visceral_risk") == "moderate" else "#e74c3c"
                
                html += f"""
                <div style='margin-top: 16px; padding: 16px; background: #f8f9fa; border-radius: 6px;'>
                    <h4 style='color: #333; font-size: 16px; font-weight: 600; margin-bottom: 8px;'>Risk Assessment</h4>
                    <div style='display: grid; grid-template-columns: 1fr 1fr; gap: 12px;'>
                        <div>
                            <span style='color: #666; font-size: 14px;'>Obesity Risk:</span>
                            <span style='color: {obesity_color}; font-weight: 600; margin-left: 8px;'>{interp.get('obesity_risk', 'N/A').upper()}</span>
                        </div>
                        <div>
                            <span style='color: #666; font-size: 14px;'>Visceral Risk:</span>
                            <span style='color: {visceral_color}; font-weight: 600; margin-left: 8px;'>{interp.get('visceral_risk', 'N/A').upper()}</span>
                        </div>
                    </div>
                """
                
                if interp.get('recommendations'):
                    html += """
                    <div style='margin-top: 12px; padding-top: 12px; border-top: 1px solid #e0e0e0;'>
                        <h5 style='color: #333; font-size: 14px; font-weight: 600; margin-bottom: 8px;'>πŸ’‘ Recommendations</h5>
                        <ul style='margin: 0; padding-left: 20px; color: #555;'>
                    """
                    for rec in interp['recommendations']:
                        html += f"<li style='margin: 4px 0;'>{rec}</li>"
                    html += "</ul></div>"
                
                html += "</div>"
            html += "</div>"
        
        elif 'tissue_distribution' in seg:
            # X-ray tissue distribution
            html += f"""
            <div style='background: white; 
                        padding: 20px; 
                        margin: 16px 0; 
                        border-radius: 8px; 
                        box-shadow: 0 1px 3px rgba(0,0,0,0.1);'>
                <h3 style='color: #333; 
                           font-size: 18px; 
                           font-weight: 600; 
                           margin-bottom: 12px;'>
                    🦴 Tissue Distribution
                </h3>
                <div style='display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px;'>
            """
            
            tissue_dist = seg['tissue_distribution']
            tissue_icons = {
                'bone': '🦴', 'soft_tissue': 'πŸ”΄', 'air': '🌫️',
                'metal': 'βš™οΈ', 'fat': '🟑', 'fluid': 'πŸ’§'
            }
            
            tissue_colors = {
                'bone': '#fff7e6',
                'soft_tissue': '#fee',
                'air': '#e6f3ff',
                'metal': '#f0f0f0',
                'fat': '#fffbe6',
                'fluid': '#e6f7ff'
            }
            
            for tissue, percentage in tissue_dist.items():
                if percentage > 0:
                    icon = tissue_icons.get(tissue, 'πŸ“')
                    bg_color = tissue_colors.get(tissue, '#f8f9fa')
                    html += f"""
                    <div style='text-align: center; 
                                padding: 16px; 
                                background: {bg_color}; 
                                border-radius: 8px;
                                transition: transform 0.2s;'>
                        <div style='font-size: 2.5em; margin-bottom: 8px;'>{icon}</div>
                        <div style='font-weight: 600; 
                                    color: #333; 
                                    font-size: 14px; 
                                    margin-bottom: 4px;'>
                            {tissue.replace('_', ' ').title()}
                        </div>
                        <div style='color: #0066cc; 
                                    font-size: 20px; 
                                    font-weight: 700;'>
                            {percentage:.1f}%
                        </div>
                    </div>
                    """
            
            html += "</div>"
            
            if seg.get('clinical_findings'):
                html += """
                <div style='margin-top: 16px; 
                            padding: 16px; 
                            background: #fff3cd; 
                            border-left: 4px solid #ffc107; 
                            border-radius: 4px;'>
                    <h4 style='color: #856404; 
                               font-size: 16px; 
                               font-weight: 600; 
                               margin: 0 0 8px 0;'>
                        ⚠️ Clinical Findings
                    </h4>
                    <ul style='margin: 0; padding-left: 20px; color: #856404;'>
                """
                for finding in seg['clinical_findings']:
                    html += f"<li style='margin: 4px 0;'>{finding.get('description', 'Unknown finding')}</li>"
                html += "</ul></div>"
            
            html += "</div>"
    
    # Quality Assessment
    if 'quality_metrics' in results:
        quality = results['quality_metrics']
        quality_colors = {
            'excellent': '#27ae60',
            'good': '#27ae60',
            'fair': '#f39c12',
            'poor': '#e74c3c',
            'unknown': '#95a5a6'
        }
        q_color = quality_colors.get(quality.get('overall_quality', 'unknown'), '#95a5a6')
        
        html += f"""
        <div style='background: #f0f9ff; 
                    padding: 20px; 
                    margin: 16px 0; 
                    border-radius: 8px; 
                    box-shadow: 0 1px 3px rgba(0,0,0,0.1);'>
            <h3 style='color: #1e3a8a !important; 
                       font-size: 18px; 
                       font-weight: 600; 
                       margin-bottom: 12px;'>
                πŸ“Š Image Quality Assessment
            </h3>
            <div style='display: flex; align-items: center; gap: 16px;'>
                <div>
                    <span style='color: #4b5563 !important; font-size: 14px;'>Overall Quality:</span>
                    <span style='color: {q_color} !important; 
                                 font-size: 18px; 
                                 font-weight: 700; 
                                 margin-left: 8px;'>
                        {quality.get('overall_quality', 'unknown').upper()}
                    </span>
                </div>
            </div>
        """
        
        if quality.get('issues'):
            html += f"""
            <div style='margin-top: 12px; 
                        padding: 12px; 
                        background: #fff3cd; 
                        border-left: 3px solid #ffc107; 
                        border-radius: 4px;'>
                <strong style='color: #856404;'>Issues Detected:</strong>
                <ul style='margin: 4px 0 0 0; padding-left: 20px; color: #856404;'>
            """
            for issue in quality['issues']:
                html += f"<li style='margin: 2px 0;'>{issue}</li>"
            html += "</ul></div>"
        
        html += "</div>"
    
    html += "</div>"
    return html


# Create Gradio interface
with gr.Blocks(
    title="Medical Image Analyzer - Wrapper Test", 
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="blue",
        neutral_hue="slate",
        text_size="md",
        spacing_size="md",
        radius_size="md",
    ).set(
        # Medical blue theme colors
        body_background_fill="*neutral_950",
        body_background_fill_dark="*neutral_950",
        block_background_fill="*neutral_900",
        block_background_fill_dark="*neutral_900",
        border_color_primary="*primary_600",
        border_color_primary_dark="*primary_600",
        # Text colors for better contrast
        body_text_color="*neutral_100",
        body_text_color_dark="*neutral_100",
        body_text_color_subdued="*neutral_300",
        body_text_color_subdued_dark="*neutral_300",
        # Button colors
        button_primary_background_fill="*primary_600",
        button_primary_background_fill_dark="*primary_600",
        button_primary_text_color="white",
        button_primary_text_color_dark="white",
    ),
    css="""
    /* Medical blue theme with high contrast */
    :root {
        --medical-blue: #1e40af;
        --medical-blue-light: #3b82f6;
        --medical-blue-dark: #1e3a8a;
        --text-primary: #f9fafb;
        --text-secondary: #e5e7eb;
        --bg-primary: #0f172a;
        --bg-secondary: #1e293b;
        --bg-tertiary: #334155;
    }
    
    /* Override default text colors for medical theme */
    * {
        color: var(--text-primary) !important;
    }
    
    /* Fix text contrast issues */
    .svelte-12ioyct { color: var(--text-primary) !important; }
    
    /* Override table cell colors for better readability */
    td { 
        color: var(--text-primary) !important; 
        padding: 8px 12px !important;
    }
    td strong { 
        color: var(--text-primary) !important; 
        font-weight: 600 !important; 
    }
    
    /* Fix upload component text and translate */
    .wrap.svelte-12ioyct::before {
        content: 'Drop file here' !important;
        color: var(--text-primary) !important;
    }
    .wrap.svelte-12ioyct::after {
        content: 'Click to upload' !important;
        color: var(--text-secondary) !important;
    }
    .wrap.svelte-12ioyct span { 
        display: none !important; /* Hide German text */
    }
    
    /* Style the file upload area */
    .file-upload {
        border: 2px dashed var(--medical-blue-light) !important;
        border-radius: 8px !important;
        padding: 20px !important;
        text-align: center !important;
        background: var(--bg-secondary) !important;
        transition: all 0.3s ease !important;
        color: var(--text-primary) !important;
    }
    
    .file-upload:hover {
        border-color: var(--medical-blue) !important;
        background: var(--bg-tertiary) !important;
        box-shadow: 0 0 20px rgba(59, 130, 246, 0.2) !important;
    }
    
    /* Make sure all text in tables is readable */
    table { 
        width: 100%;
        border-collapse: collapse;
    }
    th { 
        font-weight: 600; 
        background: var(--bg-tertiary);
        padding: 8px 12px;
    }
    
    /* Ensure report text is readable with white background */
    .medical-report {
        background: #ffffff !important;
        border: 2px solid var(--medical-blue-light) !important;
        border-radius: 8px !important;
        padding: 16px !important;
        color: #1a1a1a !important;
    }
    
    .medical-report * {
        color: #1f2937 !important; /* Dark gray text */
    }
    
    .medical-report h2 {
        color: #1e40af !important; /* Medical blue for main heading */
    }
    
    .medical-report h3, .medical-report h4 {
        color: #1e3a8a !important; /* Darker medical blue for subheadings */
    }
    
    .medical-report strong {
        color: #374151 !important; /* Darker gray for labels */
    }
    
    .medical-report td {
        color: #1f2937 !important; /* Ensure table text is dark */
    }
    
    /* Report sections with light blue background */
    .medical-report > div {
        background: #f0f9ff !important;
        color: #1f2937 !important;
    }
    
    /* Medical blue accents for UI elements */
    .gr-button-primary {
        background: var(--medical-blue) !important;
        border-color: var(--medical-blue) !important;
    }
    
    .gr-button-primary:hover {
        background: var(--medical-blue-dark) !important;
        border-color: var(--medical-blue-dark) !important;
    }
    
    /* Accordion styling with medical theme */
    .gr-accordion {
        border-color: var(--medical-blue-light) !important;
    }
    
    /* Slider track in medical blue */
    input[type="range"]::-webkit-slider-track {
        background: var(--bg-tertiary) !important;
    }
    
    input[type="range"]::-webkit-slider-thumb {
        background: var(--medical-blue) !important;
    }
    
    /* Tab styling */
    .gr-tab-item {
        border-color: var(--medical-blue-light) !important;
    }
    
    .gr-tab-item.selected {
        background: var(--medical-blue) !important;
        color: white !important;
    }
    """
) as demo:
    gr.Markdown("""
    # πŸ₯ Medical Image Analyzer
    
    Supports **DICOM** (.dcm) and all image formats with automatic modality detection!
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            # File upload with custom styling - no file type restrictions
            with gr.Group():
                gr.Markdown("### πŸ“€ Upload Medical Image")
                file_input = gr.File(
                    label="Select Medical Image File (.dcm, .dicom, IM_*, .png, .jpg, etc.)",
                    file_count="single",
                    type="filepath",
                    elem_classes="file-upload"
                    # Note: NO file_types parameter = accepts ALL files
                )
                gr.Markdown("""
                <small style='color: #666;'>
                Accepts: DICOM (.dcm, .dicom), Images (.png, .jpg, .jpeg, .tiff, .bmp), 
                and files without extensions (e.g., IM_0001, IM_0002, etc.)
                </small>
                """)
            
            # Task selection
            task = gr.Dropdown(
                choices=[
                    ("🎯 Point Analysis", "analyze_point"),
                    ("πŸ”¬ Fat Segmentation (CT only)", "segment_fat"),
                    ("πŸ“Š Full Analysis", "full_analysis")
                ],
                value="full_analysis",
                label="Analysis Task"
            )
            
            # ROI settings
            with gr.Accordion("🎯 Region of Interest (ROI)", open=True):
                roi_x = gr.Slider(0, 512, 256, label="X Position", step=1)
                roi_y = gr.Slider(0, 512, 256, label="Y Position", step=1)
                roi_radius = gr.Slider(5, 50, 10, label="Radius", step=1)
            
            # Clinical context
            with gr.Accordion("πŸ₯ Clinical Context", open=False):
                symptoms = gr.CheckboxGroup(
                    choices=[
                        "Dyspnea", "Chest Pain", "Abdominal Pain",
                        "Trauma", "Obesity Screening", "Routine Check"
                    ],
                    label="Symptoms/Indication"
                )
            
            # Visualization options
            with gr.Accordion("🎨 Visualization Options", open=True):
                show_overlay = gr.Checkbox(
                    label="Show ROI/Segmentation Overlay",
                    value=True,
                    info="Display ROI circle or fat segmentation overlay on the image"
                )
            
            analyze_btn = gr.Button("πŸ”¬ Analyze", variant="primary", size="lg")
        
        with gr.Column(scale=2):
            # Results with tabs for different views
            with gr.Tab("πŸ–ΌοΈ Original Image"):
                image_display = gr.Image(label="Medical Image", type="numpy")
                
            with gr.Tab("🎯 Overlay View"):
                overlay_display = gr.Image(label="Image with Overlay", type="numpy")
            
            file_info = gr.Textbox(label="File Information", lines=1)
            
            with gr.Tab("πŸ“Š Visual Report"):
                report_html = gr.HTML()
            
            with gr.Tab("πŸ”§ JSON Output"):
                json_output = gr.JSON(label="Structured Data for AI Agents")
    
    # Examples and help
    with gr.Row():
        gr.Markdown("""
        ### πŸ“ Supported Formats
        - **DICOM**: Automatic HU value extraction and modality detection
        - **PNG/JPG**: Interpreted as X-ray (CR) unless filename contains 'CT'
        - **All Formats**: Automatic grayscale conversion
        - **Files without extension**: Supported (e.g., IM_0001)
        
        ### 🎯 Usage
        1. Upload a medical image file
        2. Select the desired analysis task
        3. For point analysis: Adjust the ROI position
        4. Click "Analyze"
        
        ### πŸ’‘ Tips
        - The ROI sliders will automatically adjust to your image size
        - CT images show HU values, X-rays show intensity values
        - Fat segmentation is only available for CT images
        """)
    
    # Connect the interface
    analyze_btn.click(
        fn=process_and_analyze,
        inputs=[file_input, task, roi_x, roi_y, roi_radius, symptoms, show_overlay],
        outputs=[image_display, file_info, report_html, json_output, overlay_display]
    )
    
    # Auto-update ROI limits when image is loaded
    def update_roi_on_upload(file_obj):
        if file_obj is None:
            return gr.update(), gr.update()
        
        try:
            analyzer = MedicalImageAnalyzer()
            _, _, metadata = analyzer.process_file(file_obj.name if hasattr(file_obj, 'name') else str(file_obj))
            
            if 'shape' in metadata:
                h, w = metadata['shape']
                return gr.update(maximum=w-1, value=w//2), gr.update(maximum=h-1, value=h//2)
        except:
            pass
        
        return gr.update(), gr.update()
    
    file_input.change(
        fn=update_roi_on_upload,
        inputs=[file_input],
        outputs=[roi_x, roi_y]
    )

    # Add custom JavaScript to translate upload text
    demo.load(
        None,
        None,
        None,
        js="""
        () => {
            // Wait for the page to load
            setTimeout(() => {
                // Find and replace German text in upload component
                const uploadElements = document.querySelectorAll('.wrap.svelte-12ioyct');
                uploadElements.forEach(el => {
                    if (el.textContent.includes('Datei hier ablegen')) {
                        el.innerHTML = el.innerHTML
                            .replace('Datei hier ablegen', 'Drop file here')
                            .replace('oder', 'or')
                            .replace('Hochladen', 'Click to upload');
                    }
                });
                
                // Also update the button text if it exists
                const uploadButtons = document.querySelectorAll('button');
                uploadButtons.forEach(btn => {
                    if (btn.textContent.includes('Hochladen')) {
                        btn.textContent = 'Upload';
                    }
                });
            }, 100);
        }
        """
    )

if __name__ == "__main__":
    demo.launch()