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06b
 
de2e2e5
 
 
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
import gradio as gr

# Custom Premium CSS for medical dashboard styling
GRADIO_CUSTOM_CSS = """
/* Import Inter Font */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap');

/* Main Container Styles - Deep Obsidian Background */
.gradio-container {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important;
    background: radial-gradient(circle at 50% 0%, #101626 0%, #060912 100%) !important;
    color: #e2e8f0 !important;
    max-width: 1400px !important;
    margin: auto !important;
    padding: 24px !important;
    border: none !important;
    box-shadow: none !important;
}

/* Typography & Headers */
h1, h2, h3, h4, h5, h6 {
    font-family: 'Inter', sans-serif !important;
    font-weight: 700 !important;
    color: #f1f5f9 !important;
    letter-spacing: -0.02em !important;
}

.clinical-header {
    text-align: center;
    margin-bottom: 30px;
    padding-bottom: 24px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.clinical-header h1 {
    font-size: 3rem !important;
    font-weight: 900 !important;
    background: linear-gradient(135deg, #14b8a6 0%, #3b82f6 50%, #8b5cf6 100%) !important;
    -webkit-background-clip: text !important;
    -webkit-text-fill-color: transparent !important;
    letter-spacing: -0.06em !important;
    margin-bottom: 12px !important;
    text-shadow: 0 0 40px rgba(59, 130, 246, 0.15) !important;
}

.clinical-header p {
    font-size: 1.15rem !important;
    color: #94a3b8 !important;
    font-weight: 400 !important;
    max-width: 700px;
    margin: 0 auto !important;
    line-height: 1.5;
}

/* UI Card Glassmorphism Elements */
.glass-panel, .block, .contain {
    background: rgba(13, 20, 35, 0.45) !important;
    backdrop-filter: blur(20px) !important;
    -webkit-backdrop-filter: blur(20px) !important;
    border: 1px solid rgba(255, 255, 255, 0.06) !important;
    border-radius: 16px !important;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3) !important;
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1) !important;
}

.glass-panel:hover {
    border-color: rgba(255, 255, 255, 0.1) !important;
    box-shadow: 0 12px 40px 0 rgba(0, 0, 0, 0.4) !important;
}

/* Button & Interactions */
.run-btn {
    background: linear-gradient(135deg, #14b8a6 0%, #0d9488 100%) !important;
    color: white !important;
    font-weight: 700 !important;
    border: none !important;
    border-radius: 12px !important;
    font-size: 1rem !important;
    letter-spacing: 0.02em !important;
    box-shadow: 0 0 20px rgba(20, 184, 166, 0.25) !important;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1) !important;
    padding: 14px 24px !important;
    text-transform: uppercase !important;
    cursor: pointer !important;
}

.run-btn:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 0 30px rgba(20, 184, 166, 0.5) !important;
    filter: brightness(1.1) !important;
}

.run-btn:active {
    transform: translateY(1px) !important;
}

/* Custom Pathology Report Markdown Styling */
.report-card {
    background: rgba(15, 23, 42, 0.35) !important;
    border: 1px solid rgba(255, 255, 255, 0.04) !important;
    border-radius: 12px !important;
    padding: 24px !important;
    color: #cbd5e1 !important;
    line-height: 1.6 !important;
    font-size: 0.95rem !important;
}

.report-card h3 {
    color: #14b8a6 !important;
    font-size: 1.2rem !important;
    margin-top: 0 !important;
    margin-bottom: 12px !important;
    border-bottom: 1px solid rgba(20, 184, 166, 0.15) !important;
    padding-bottom: 8px !important;
}

.report-card strong {
    color: #f1f5f9 !important;
}

/* Custom Cards for FAISS Matches */
.case-card {
    background: rgba(15, 23, 42, 0.55);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 12px;
    padding: 16px 20px;
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.case-card:hover {
    border-color: rgba(255, 255, 255, 0.12);
    transform: translateX(6px);
    background: rgba(20, 30, 50, 0.6);
}

.case-badge-msih {
    background: rgba(248, 113, 113, 0.12) !important;
    color: #f87171 !important;
    padding: 3px 10px !important;
    border-radius: 6px !important;
    font-size: 0.72rem !important;
    font-weight: 700 !important;
    border: 1px solid rgba(248, 113, 113, 0.25) !important;
    text-transform: uppercase !important;
    letter-spacing: 0.05em !important;
}

.case-badge-mss {
    background: rgba(16, 185, 129, 0.1) !important;
    color: #34d399 !important;
    padding: 3px 10px !important;
    border-radius: 6px !important;
    font-size: 0.72rem !important;
    font-weight: 700 !important;
    border: 1px solid rgba(16, 185, 129, 0.2) !important;
    text-transform: uppercase !important;
    letter-spacing: 0.05em !important;
}

/* Tab & Input Customizations */
.tabs {
    border-bottom: 1px solid rgba(255, 255, 255, 0.06) !important;
}

.tab-nav button {
    font-weight: 600 !important;
    font-size: 0.85rem !important;
    color: #94a3b8 !important;
    transition: all 0.2s ease !important;
}

.tab-nav button.selected {
    color: #14b8a6 !important;
    border-bottom-color: #14b8a6 !important;
    background: rgba(20, 184, 166, 0.05) !important;
}

/* Animations */
@keyframes pulse {
    0% { opacity: 0.4; }
    50% { opacity: 1; }
    100% { opacity: 0.4; }
}

.status-pulse {
    animation: pulse 2.5s infinite;
}
"""

SYSTEM_STATUS_CONSOLE = """
<div style="display: flex; justify-content: space-between; align-items: center; background: rgba(10, 15, 28, 0.65); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border: 1px solid rgba(255, 255, 255, 0.06); border-radius: 12px; padding: 12px 24px; margin-bottom: 24px; box-shadow: 0 4px 30px rgba(0,0,0,0.35);">
    <div style="display: flex; align-items: center; gap: 10px;">
        <span class="status-pulse" style="display: inline-block; width: 9px; height: 9px; background-color: #10b981; border-radius: 50%; box-shadow: 0 0 12px #10b981;"></span>
        <span style="font-family: 'Inter', sans-serif; font-size: 0.78rem; font-weight: 700; color: #10b981; letter-spacing: 0.06em; text-transform: uppercase;">CLINICAL CORE: ONLINE</span>
    </div>
    <div style="display: flex; gap: 28px; font-family: 'Inter', sans-serif; font-size: 0.75rem; color: #94a3b8; font-weight: 600;">
        <div>PIPELINE: <span style="color: #f1f5f9; font-weight: 700; font-family: monospace;">ZeroGPU / CUDA v12.1</span></div>
        <div>FUSION: <span style="color: #f1f5f9; font-weight: 700;">PathViT-384 + MedSigLIP-1152 (1536-dim Representation)</span></div>
        <div>DIAGNOSTICS: <span style="color: #8b5cf6; font-weight: 700;">MEDGEMMA-4B-IT ACTIVE</span></div>
    </div>
</div>
"""

def format_retrieved_cases_html(cases: list) -> str:
    """
    Synthesizes similar historical cases retrieved via FAISS into beautiful, 
    clinical-grade dashboard cards with custom status badges, metric gauges, and rich clinical metadata.
    """
    if not cases:
        return "<p style='color: #64748b; padding: 12px;'>No clinical reference matches found in indexing database.</p>"
        
    html = "<div style='display: flex; flex-direction: column; gap: 12px; padding: 4px 0;'>"
    for i, c in enumerate(cases):
        status = c["status"]
        patient_id = c["patient_id"]
        similarity = c["similarity"]
        description = c["visual_description"]
        
        # Extract rich clinical metadata with safe fallbacks
        age = c.get("age", 64)
        stage = c.get("stage", "Stage II")
        site = c.get("primary_site", "Colon")
        differentiation = c.get("differentiation", "Moderately Differentiated")
        
        # Select colors based on MSI status
        if status == "MSI-High":
            badge_class = "case-badge-msih"
            sim_color = "#f87171"
            bg_bar = "rgba(248, 113, 113, 0.15)"
        else:
            badge_class = "case-badge-mss"
            sim_color = "#34d399"
            bg_bar = "rgba(16, 185, 129, 0.1)"
            
        html += f"""
        <div class="case-card">
            <div style="flex-grow: 1; padding-right: 20px;">
                <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 6px;">
                    <span style="font-weight: 700; font-size: 0.95rem; color: #f1f5f9; letter-spacing: -0.01em;">SPECIMEN #{patient_id}</span>
                    <span class="{badge_class}">{status}</span>
                </div>
                
                <p style="font-size: 0.82rem; color: #cbd5e1; margin: 0 0 8px 0; line-height: 1.4;">
                    <strong>Histology:</strong> {description}
                </p>
                
                <!-- Clinical Metadata Row -->
                <div style="display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 12px; font-size: 0.72rem; color: #64748b; font-weight: 600;">
                    <span style="background: rgba(255,255,255,0.03); padding: 2px 8px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.04);">Age: <span style="color: #cbd5e1;">{age}</span></span>
                    <span style="background: rgba(255,255,255,0.03); padding: 2px 8px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.04);">Stage: <span style="color: #cbd5e1;">{stage}</span></span>
                    <span style="background: rgba(255,255,255,0.03); padding: 2px 8px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.04);">Site: <span style="color: #cbd5e1;">{site}</span></span>
                    <span style="background: rgba(255,255,255,0.03); padding: 2px 8px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.04); color: #cbd5e1;">{differentiation}</span>
                </div>
                
                <div style="width: 100%; height: 4px; background: rgba(255,255,255,0.04); border-radius: 2px; overflow: hidden;">
                    <div style="width: {similarity:.1%}; height: 100%; background: {sim_color}; border-radius: 2px;"></div>
                </div>
            </div>
            <div style="text-align: right; min-width: 90px; border-left: 1px solid rgba(255,255,255,0.05); padding-left: 16px;">
                <span style="display: block; font-size: 0.65rem; text-transform: uppercase; color: #64748b; font-weight: 700; letter-spacing: 0.05em; margin-bottom: 2px;">SIMILARITY</span>
                <span style="font-size: 1.25rem; font-weight: 800; color: {sim_color}; font-family: monospace;">{similarity:.1%}</span>
            </div>
        </div>
        """
    html += "</div>"
    return html

def format_prediction_stat_html(probability: float, status_label: str) -> str:
    """
    Creates a prominent, beautiful dashboard statistic showing the final diagnostic result.
    Mathematically handles confidence thresholds relative to the predicted target class.
    """
    is_msi = status_label == "MSI-High"
    theme_color = "#f87171" if is_msi else "#34d399"
    glow_shadow = "0 0 30px rgba(248, 113, 113, 0.15)" if is_msi else "0 0 30px rgba(52, 211, 153, 0.1)"
    bg_tint = "rgba(248, 113, 113, 0.03)" if is_msi else "rgba(52, 211, 153, 0.02)"
    border_tint = "rgba(248, 113, 113, 0.15)" if is_msi else "rgba(52, 211, 153, 0.12)"
    
    # Calculate confidence as the probability of the PREDICTED class (always >= 50%)
    confidence_val = probability if is_msi else (1.0 - probability)
    
    subtext = (
        "High Microsatellite Instability (MSI-H) detected. Highly correlating with immune-checkpoint inhibitor (ICI) response rates."
        if is_msi else
        "Microsatellite Stable (MSS) baseline. Specimen exhibits normal mismatch repair (MMR) expression signature."
    )
    
    html = f"""
    <div style="text-align: center; padding: 24px; border-radius: 14px; background: {bg_tint}; border: 1px solid {border_tint}; box-shadow: {glow_shadow}; margin-bottom: 16px;">
        <span style="font-size: 0.75rem; text-transform: uppercase; color: #94a3b8; font-weight: 700; letter-spacing: 0.08em; display: block; margin-bottom: 8px;">PREDICTIVE DECISION STATUS</span>
        <span style="font-size: 2.6rem; font-weight: 900; color: {theme_color}; letter-spacing: -0.04em; display: block; line-height: 1.1; margin-bottom: 8px; text-shadow: 0 0 15px {theme_color}30;">{status_label}</span>
        
        <div style="display: flex; flex-direction: column; align-items: center; gap: 8px; margin: 16px 0;">
            <div style="display: flex; justify-content: space-between; width: 100%; max-width: 240px; font-size: 0.8rem; color: #cbd5e1; font-weight: 600;">
                <span>Model Confidence</span>
                <span style="color: #cbd5e1; font-weight: 800; font-family: monospace;">{confidence_val:.2%}</span>
            </div>
            <div style="width: 100%; max-width: 240px; height: 6px; background: rgba(255,255,255,0.04); border-radius: 3px; overflow: hidden;">
                <div style="width: {confidence_val:.1%}; height: 100%; background: {theme_color}; border-radius: 3px;"></div>
            </div>
        </div>
        
        <p style="font-size: 0.85rem; color: #94a3b8; margin: 0; line-height: 1.4; font-weight: 400; max-width: 320px; margin-left: auto; margin-right: auto;">{subtext}</p>
    </div>
    """
    return html