feat(ui): Complete UI/UX overhaul - gorgeous Gradio Blocks implementation
Browse files🎨 Visual Improvements:
- Switched from gr.Interface to gr.Blocks for custom layout control
- Applied gr.themes.Soft() with custom CSS (Inter font, gradient header)
- Color-coded status cards (Green ✅ = Specific, Red ⚠️ = Non-Specific)
- Added confidence meter visualization with gr.Label
- Enhanced typography and spacing for medical/scientific aesthetic
⚙️ Functional Enhancements:
- Advanced Settings accordion with Assay Type selector (ELISA/PSR)
- Decision threshold slider (0.0-1.0) for sensitivity adjustment
- Detailed JSON output accordion for power users
- Clickable examples that pre-fill all settings
- Visual feedback with custom HTML cards
🔧 Code Quality:
- 100% type coverage (mypy --strict passes)
- All ruff checks pass (format + lint)
- Fixed SIM117 (combined nested with statements)
- Proper type annotations: tuple[str, dict[str, float], dict[str, Any]]
- Clean separation: inference code remains Hydra-free
Architecture:
- Backend: Predictor class with lazy-loading (no changes)
- Frontend: Gradio Blocks with custom CSS and layout
- Validation: Pydantic PredictionRequest with real-time feedback
Status: ✅ Production-ready for HF Spaces CPU tier
|
@@ -11,6 +11,7 @@ import logging
|
|
| 11 |
import os
|
| 12 |
import sys
|
| 13 |
from pathlib import Path
|
|
|
|
| 14 |
|
| 15 |
# Add src to Python path for local imports (HF Spaces doesn't install package)
|
| 16 |
sys.path.insert(0, str(Path(__file__).parent / "src"))
|
|
@@ -42,6 +43,7 @@ DEVICE = "cpu"
|
|
| 42 |
|
| 43 |
# Load model globally (HF Spaces best practice)
|
| 44 |
logger.info(f"Loading model from {MODEL_PATH}...")
|
|
|
|
| 45 |
predictor = Predictor(
|
| 46 |
model_name=MODEL_NAME, classifier_path=MODEL_PATH, device=DEVICE, config_path=None
|
| 47 |
)
|
|
@@ -55,19 +57,29 @@ except Exception as e:
|
|
| 55 |
logger.warning(f"Warmup failed (non-fatal): {e}")
|
| 56 |
|
| 57 |
|
| 58 |
-
def predict_sequence(
|
|
|
|
|
|
|
| 59 |
"""
|
| 60 |
Prediction function for Gradio interface.
|
| 61 |
|
| 62 |
Args:
|
| 63 |
sequence: Antibody amino acid sequence
|
|
|
|
|
|
|
| 64 |
|
| 65 |
Returns:
|
| 66 |
-
Tuple of (
|
| 67 |
"""
|
| 68 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Validate with Pydantic
|
| 70 |
-
request = PredictionRequest(
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Log request
|
| 73 |
logger.info(f"Processing sequence: length={len(request.sequence)}")
|
|
@@ -75,10 +87,42 @@ def predict_sequence(sequence: str) -> tuple[str, str]:
|
|
| 75 |
# Predict
|
| 76 |
result = predictor.predict_single(request)
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
except ValidationError as e:
|
| 84 |
# User-friendly error message
|
|
@@ -94,64 +138,194 @@ def predict_sequence(sequence: str) -> tuple[str, str]:
|
|
| 94 |
raise gr.Error(f"Prediction failed: {str(e)}") from e
|
| 95 |
|
| 96 |
|
| 97 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
examples = [
|
| 99 |
[
|
| 100 |
-
"QVQLVQSGAEVKKPGASVKVSCKASGYTFTSYNMHWVRQAPGQGLEWMGGIYPGDSDTRYSPSFQGQVTISADKSISTAYLQWSSLKASDTAMYYCARSTYYGGDWYFNVWGQGTLVTVSS"
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
[
|
| 103 |
-
"DIQMTQSPSSLSASVGDRVTITCRASQSISSYLNWYQQKPGKAPKLLIYAASSLQSGVPSRFSGSGSGTDFTLTISSLQPEDFATYYCQQSYSTPLTFGGGTKVEIK"
|
| 104 |
-
|
|
|
|
|
|
|
| 105 |
[
|
| 106 |
-
"EVQLVESGGGLVQPGGSLRLSCAASGFNIKDTYIHWVRQAPGKGLEWVARIYPTNGYTRYADSVKGRFTISADTSKNTAYLQMNSLRAEDTAVYYCARSWGQGTLVTVSS"
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
]
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
outputs=[
|
| 123 |
-
gr.Textbox(label="Prediction", show_copy_button=True),
|
| 124 |
-
gr.Textbox(label="Probability of Non-Specificity", show_copy_button=True),
|
| 125 |
-
],
|
| 126 |
-
title="🧬 Antibody Non-Specificity Predictor",
|
| 127 |
-
description=(
|
| 128 |
-
"Predict antibody polyreactivity (non-specificity) from Variable Heavy (VH) "
|
| 129 |
-
"or Variable Light (VL) sequences using ESM-1v protein language models.\n\n"
|
| 130 |
-
"**Model:** ESM-1v (650M parameters) + Logistic Regression\n"
|
| 131 |
-
"**Training:** Boughter dataset (914 antibodies, ELISA polyreactivity)\n"
|
| 132 |
-
"**Citation:** Sakhnini et al. (2025) - Prediction of Antibody Non-Specificity using PLMs"
|
| 133 |
-
),
|
| 134 |
-
article=(
|
| 135 |
-
f"**Model:** {MODEL_NAME}\n"
|
| 136 |
-
f"**Device:** {DEVICE}\n"
|
| 137 |
-
f"**Environment:** {'Hugging Face Spaces' if IS_HF_SPACE else 'Local'}"
|
| 138 |
-
),
|
| 139 |
-
examples=examples,
|
| 140 |
-
cache_examples=False, # Don't cache on HF Spaces (saves disk)
|
| 141 |
-
flagging_mode="never",
|
| 142 |
-
analytics_enabled=False,
|
| 143 |
-
submit_btn="🔬 Predict Non-Specificity",
|
| 144 |
-
clear_btn="🗑️ Clear",
|
| 145 |
-
)
|
| 146 |
|
| 147 |
-
#
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
import os
|
| 12 |
import sys
|
| 13 |
from pathlib import Path
|
| 14 |
+
from typing import Any
|
| 15 |
|
| 16 |
# Add src to Python path for local imports (HF Spaces doesn't install package)
|
| 17 |
sys.path.insert(0, str(Path(__file__).parent / "src"))
|
|
|
|
| 43 |
|
| 44 |
# Load model globally (HF Spaces best practice)
|
| 45 |
logger.info(f"Loading model from {MODEL_PATH}...")
|
| 46 |
+
# Note: We initialize with config_path=None assuming pickle or named config for npz
|
| 47 |
predictor = Predictor(
|
| 48 |
model_name=MODEL_NAME, classifier_path=MODEL_PATH, device=DEVICE, config_path=None
|
| 49 |
)
|
|
|
|
| 57 |
logger.warning(f"Warmup failed (non-fatal): {e}")
|
| 58 |
|
| 59 |
|
| 60 |
+
def predict_sequence(
|
| 61 |
+
sequence: str, threshold: float, assay_type: str | None
|
| 62 |
+
) -> tuple[str, dict[str, float], dict[str, Any]]:
|
| 63 |
"""
|
| 64 |
Prediction function for Gradio interface.
|
| 65 |
|
| 66 |
Args:
|
| 67 |
sequence: Antibody amino acid sequence
|
| 68 |
+
threshold: Decision threshold
|
| 69 |
+
assay_type: Optional assay type (ELISA/PSR)
|
| 70 |
|
| 71 |
Returns:
|
| 72 |
+
Tuple of (HTML Card, Label Dict, JSON Result)
|
| 73 |
"""
|
| 74 |
try:
|
| 75 |
+
# Handle "None" string from dropdown
|
| 76 |
+
if assay_type == "None" or assay_type == "":
|
| 77 |
+
assay_type = None
|
| 78 |
+
|
| 79 |
# Validate with Pydantic
|
| 80 |
+
request = PredictionRequest(
|
| 81 |
+
sequence=sequence, threshold=threshold, assay_type=assay_type
|
| 82 |
+
)
|
| 83 |
|
| 84 |
# Log request
|
| 85 |
logger.info(f"Processing sequence: length={len(request.sequence)}")
|
|
|
|
| 87 |
# Predict
|
| 88 |
result = predictor.predict_single(request)
|
| 89 |
|
| 90 |
+
# --- Generate HTML Card ---
|
| 91 |
+
is_specific = result.prediction == "specific"
|
| 92 |
+
|
| 93 |
+
if is_specific:
|
| 94 |
+
color_class = "status-safe"
|
| 95 |
+
icon = "✅"
|
| 96 |
+
title = "Specific (Safe)"
|
| 97 |
+
msg = "Low risk of polyreactivity"
|
| 98 |
+
else:
|
| 99 |
+
color_class = "status-danger"
|
| 100 |
+
icon = "⚠️"
|
| 101 |
+
title = "Non-Specific (Risk)"
|
| 102 |
+
msg = "High risk of polyreactivity"
|
| 103 |
+
|
| 104 |
+
html_card = f"""
|
| 105 |
+
<div class="status-card {color_class}">
|
| 106 |
+
<span class="status-icon">{icon}</span>
|
| 107 |
+
<div class="status-text">{title}</div>
|
| 108 |
+
<div class="status-subtext">{msg}</div>
|
| 109 |
+
</div>
|
| 110 |
+
"""
|
| 111 |
|
| 112 |
+
# --- Generate Label ---
|
| 113 |
+
# Gradio Label expects dict {label: prob}
|
| 114 |
+
# We return the probability of the predicted class
|
| 115 |
+
label_dict = {
|
| 116 |
+
"Non-Specificity Risk": result.probability,
|
| 117 |
+
"Specificity": 1.0 - result.probability,
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
# --- Generate JSON ---
|
| 121 |
+
json_result = result.model_dump(
|
| 122 |
+
exclude={"sequence"}
|
| 123 |
+
) # Exclude sequence to save space
|
| 124 |
+
|
| 125 |
+
return html_card, label_dict, json_result
|
| 126 |
|
| 127 |
except ValidationError as e:
|
| 128 |
# User-friendly error message
|
|
|
|
| 138 |
raise gr.Error(f"Prediction failed: {str(e)}") from e
|
| 139 |
|
| 140 |
|
| 141 |
+
# --- Custom CSS ---
|
| 142 |
+
css = """
|
| 143 |
+
.gradio-container {
|
| 144 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
|
| 145 |
+
}
|
| 146 |
+
.header-text {
|
| 147 |
+
text-align: center;
|
| 148 |
+
margin-bottom: 20px;
|
| 149 |
+
}
|
| 150 |
+
.header-title {
|
| 151 |
+
font-size: 2.5rem;
|
| 152 |
+
font-weight: 700;
|
| 153 |
+
background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
|
| 154 |
+
-webkit-background-clip: text;
|
| 155 |
+
-webkit-text-fill-color: transparent;
|
| 156 |
+
margin-bottom: 0.5rem;
|
| 157 |
+
}
|
| 158 |
+
.header-subtitle {
|
| 159 |
+
font-size: 1.1rem;
|
| 160 |
+
color: #6b7280;
|
| 161 |
+
}
|
| 162 |
+
.status-card {
|
| 163 |
+
padding: 30px;
|
| 164 |
+
border-radius: 16px;
|
| 165 |
+
text-align: center;
|
| 166 |
+
margin-bottom: 20px;
|
| 167 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 168 |
+
transition: all 0.3s ease;
|
| 169 |
+
}
|
| 170 |
+
.status-safe {
|
| 171 |
+
background-color: #ecfdf5;
|
| 172 |
+
border: 2px solid #10b981;
|
| 173 |
+
color: #065f46;
|
| 174 |
+
}
|
| 175 |
+
.status-danger {
|
| 176 |
+
background-color: #fef2f2;
|
| 177 |
+
border: 2px solid #ef4444;
|
| 178 |
+
color: #991b1b;
|
| 179 |
+
}
|
| 180 |
+
.status-icon {
|
| 181 |
+
font-size: 48px;
|
| 182 |
+
display: block;
|
| 183 |
+
margin-bottom: 15px;
|
| 184 |
+
}
|
| 185 |
+
.status-text {
|
| 186 |
+
font-size: 28px;
|
| 187 |
+
font-weight: 800;
|
| 188 |
+
letter-spacing: -0.025em;
|
| 189 |
+
margin-bottom: 5px;
|
| 190 |
+
}
|
| 191 |
+
.status-subtext {
|
| 192 |
+
font-size: 16px;
|
| 193 |
+
opacity: 0.9;
|
| 194 |
+
}
|
| 195 |
+
.footer-links {
|
| 196 |
+
text-align: center;
|
| 197 |
+
margin-top: 40px;
|
| 198 |
+
padding-top: 20px;
|
| 199 |
+
border-top: 1px solid #e5e7eb;
|
| 200 |
+
color: #9ca3af;
|
| 201 |
+
font-size: 0.9rem;
|
| 202 |
+
}
|
| 203 |
+
.footer-links a {
|
| 204 |
+
color: #6b7280;
|
| 205 |
+
text-decoration: none;
|
| 206 |
+
margin: 0 10px;
|
| 207 |
+
}
|
| 208 |
+
.footer-links a:hover {
|
| 209 |
+
color: #3b82f6;
|
| 210 |
+
text-decoration: underline;
|
| 211 |
+
}
|
| 212 |
+
"""
|
| 213 |
+
|
| 214 |
+
# --- Example Sequences ---
|
| 215 |
examples = [
|
| 216 |
[
|
| 217 |
+
"QVQLVQSGAEVKKPGASVKVSCKASGYTFTSYNMHWVRQAPGQGLEWMGGIYPGDSDTRYSPSFQGQVTISADKSISTAYLQWSSLKASDTAMYYCARSTYYGGDWYFNVWGQGTLVTVSS",
|
| 218 |
+
0.5,
|
| 219 |
+
"ELISA",
|
| 220 |
+
],
|
| 221 |
[
|
| 222 |
+
"DIQMTQSPSSLSASVGDRVTITCRASQSISSYLNWYQQKPGKAPKLLIYAASSLQSGVPSRFSGSGSGTDFTLTISSLQPEDFATYYCQQSYSTPLTFGGGTKVEIK",
|
| 223 |
+
0.5,
|
| 224 |
+
"PSR",
|
| 225 |
+
],
|
| 226 |
[
|
| 227 |
+
"EVQLVESGGGLVQPGGSLRLSCAASGFNIKDTYIHWVRQAPGKGLEWVARIYPTNGYTRYADSVKGRFTISADTSKNTAYLQMNSLRAEDTAVYYCARSWGQGTLVTVSS",
|
| 228 |
+
0.8,
|
| 229 |
+
None,
|
| 230 |
+
],
|
| 231 |
]
|
| 232 |
|
| 233 |
+
# --- Gradio Blocks App ---
|
| 234 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Antibody Predictor") as app:
|
| 235 |
+
# Header
|
| 236 |
+
with gr.Column(elem_classes="header-text"):
|
| 237 |
+
gr.Markdown(
|
| 238 |
+
"""
|
| 239 |
+
<div class="header-title">🧬 Antibody Non-Specificity Predictor</div>
|
| 240 |
+
<div class="header-subtitle">
|
| 241 |
+
Assess polyreactivity risk using ESM-1v Protein Language Models
|
| 242 |
+
</div>
|
| 243 |
+
"""
|
| 244 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
+
# Main Content
|
| 247 |
+
with gr.Row(equal_height=False):
|
| 248 |
+
# Left Column: Inputs
|
| 249 |
+
with gr.Column(scale=1):
|
| 250 |
+
with gr.Group():
|
| 251 |
+
sequence_input = gr.TextArea(
|
| 252 |
+
label="Antibody Sequence (VH or VL)",
|
| 253 |
+
placeholder="Paste amino acid sequence here (e.g., QVQL...)",
|
| 254 |
+
lines=5,
|
| 255 |
+
max_lines=15,
|
| 256 |
+
show_copy_button=True,
|
| 257 |
+
)
|
| 258 |
|
| 259 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False), gr.Row():
|
| 260 |
+
assay_input = gr.Dropdown(
|
| 261 |
+
choices=["ELISA", "PSR", "None"],
|
| 262 |
+
value="None",
|
| 263 |
+
label="Calibrated Assay",
|
| 264 |
+
info="Use threshold calibrated for specific assay",
|
| 265 |
+
)
|
| 266 |
+
threshold_input = gr.Slider(
|
| 267 |
+
minimum=0.0,
|
| 268 |
+
maximum=1.0,
|
| 269 |
+
value=0.5,
|
| 270 |
+
step=0.05,
|
| 271 |
+
label="Decision Threshold",
|
| 272 |
+
info="Probability cutoff for non-specificity",
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
submit_btn = gr.Button(
|
| 276 |
+
"🔬 Predict Non-Specificity", variant="primary", size="lg"
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
# Examples
|
| 280 |
+
gr.Examples(
|
| 281 |
+
examples=examples,
|
| 282 |
+
inputs=[sequence_input, threshold_input, assay_input],
|
| 283 |
+
label="Load Example Data",
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
# Right Column: Outputs
|
| 287 |
+
with gr.Column(scale=1):
|
| 288 |
+
# HTML Card
|
| 289 |
+
result_html = gr.HTML(
|
| 290 |
+
label="Prediction Status",
|
| 291 |
+
value="""
|
| 292 |
+
<div class="status-card" style="background-color: #f3f4f6; border: 2px dashed #d1d5db; color: #6b7280;">
|
| 293 |
+
<span class="status-icon">⏳</span>
|
| 294 |
+
<div class="status-text">Ready to Predict</div>
|
| 295 |
+
<div class="status-subtext">Enter a sequence to begin analysis</div>
|
| 296 |
+
</div>
|
| 297 |
+
""",
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
+
# Confidence Bar
|
| 301 |
+
confidence_output = gr.Label(
|
| 302 |
+
label="Model Confidence", num_top_classes=2, show_label=True
|
| 303 |
+
)
|
| 304 |
+
|
| 305 |
+
# Detailed JSON
|
| 306 |
+
with gr.Accordion("📋 Detailed JSON Output", open=False):
|
| 307 |
+
json_output = gr.JSON(label="Raw Result")
|
| 308 |
+
|
| 309 |
+
# Footer
|
| 310 |
+
gr.Markdown(
|
| 311 |
+
"""
|
| 312 |
+
<div class="footer-links">
|
| 313 |
+
Model: ESM-1v (650M) + Logistic Regression • Training: Boughter et al. (914 sequences)
|
| 314 |
+
<br>
|
| 315 |
+
<a href="https://huggingface.co/facebook/esm1v_t33_650M_UR90S_1" target="_blank">ESM-1v Model</a> •
|
| 316 |
+
<a href="#" target="_blank">Paper Citation (Sakhnini et al. 2025)</a>
|
| 317 |
+
</div>
|
| 318 |
+
"""
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
# Logic Binding
|
| 322 |
+
submit_btn.click(
|
| 323 |
+
fn=predict_sequence,
|
| 324 |
+
inputs=[sequence_input, threshold_input, assay_input],
|
| 325 |
+
outputs=[result_html, confidence_output, json_output],
|
| 326 |
)
|
| 327 |
+
|
| 328 |
+
# Launch
|
| 329 |
+
if __name__ == "__main__":
|
| 330 |
+
app.queue(default_concurrency_limit=2, max_size=10)
|
| 331 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=False, show_api=False)
|