Spaces:
Running on Zero
Running on Zero
| 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 | |