Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import gradio as gr
|
|
| 6 |
from transformers import AutoTokenizer, AutoModel
|
| 7 |
|
| 8 |
# ==========================
|
| 9 |
-
#
|
| 10 |
# ==========================
|
| 11 |
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
| 12 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
|
@@ -16,14 +16,11 @@ for path in ["/tmp/hf_cache", os.path.expanduser("~/.cache/huggingface")]:
|
|
| 16 |
shutil.rmtree(path, ignore_errors=True)
|
| 17 |
os.makedirs(path, exist_ok=True)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
# 1. Model Definition (保持不变)
|
| 21 |
-
# ==========================
|
| 22 |
class AttentionPooling(nn.Module):
|
| 23 |
def __init__(self, d_model):
|
| 24 |
super().__init__()
|
| 25 |
self.attention_net = nn.Linear(d_model, 1)
|
| 26 |
-
|
| 27 |
def forward(self, x, mask):
|
| 28 |
attn_logits = self.attention_net(x).squeeze(2)
|
| 29 |
attn_logits.masked_fill_(mask == 0, -float('inf'))
|
|
@@ -35,24 +32,16 @@ class ProtDualBranchEnhancedClassifier(nn.Module):
|
|
| 35 |
super().__init__()
|
| 36 |
self.cls_projector = nn.Linear(d_model, projection_dim)
|
| 37 |
self.token_refiner = nn.Sequential(
|
| 38 |
-
nn.Conv1d(d_model, d_model, kernel_size, padding='same'),
|
| 39 |
-
nn.ReLU()
|
| 40 |
)
|
| 41 |
self.attention_pooling = AttentionPooling(d_model)
|
| 42 |
self.tok_projector = nn.Linear(d_model, projection_dim)
|
| 43 |
fused_dim = projection_dim * 2
|
| 44 |
-
self.gate = nn.Sequential(
|
| 45 |
-
nn.Linear(fused_dim, fused_dim),
|
| 46 |
-
nn.Sigmoid()
|
| 47 |
-
)
|
| 48 |
self.classifier_head = nn.Sequential(
|
| 49 |
-
nn.LayerNorm(fused_dim),
|
| 50 |
-
nn.
|
| 51 |
-
nn.ReLU(),
|
| 52 |
-
nn.Dropout(dropout),
|
| 53 |
-
nn.Linear(fused_dim * 2, num_classes)
|
| 54 |
)
|
| 55 |
-
|
| 56 |
def forward(self, cls_embedding, token_embeddings, mask):
|
| 57 |
z_cls = self.cls_projector(cls_embedding)
|
| 58 |
tok_emb_permuted = token_embeddings.permute(0, 2, 1)
|
|
@@ -64,16 +53,14 @@ class ProtDualBranchEnhancedClassifier(nn.Module):
|
|
| 64 |
z_fused_gated = z_fused_concat * gate_values
|
| 65 |
return self.classifier_head(z_fused_gated)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
# 2. Load Models (保持不变)
|
| 69 |
-
# ==========================
|
| 70 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 71 |
-
PLM_MODEL_NAME = "facebook/esm2_t30_150M_UR50D"
|
| 72 |
CLASSIFIER_PATH = "best_model_esm2_t30_150M_UR50D.pth"
|
| 73 |
LABEL_MAP_PATH = "label_map.json"
|
| 74 |
|
| 75 |
if not os.path.exists(LABEL_MAP_PATH):
|
| 76 |
-
raise FileNotFoundError(f"
|
| 77 |
with open(LABEL_MAP_PATH, 'r') as f:
|
| 78 |
label_to_idx = json.load(f)
|
| 79 |
idx_to_label = {v: k for k, v in label_to_idx.items()}
|
|
@@ -81,238 +68,224 @@ with open(LABEL_MAP_PATH, 'r') as f:
|
|
| 81 |
NUM_CLASSES = len(idx_to_label)
|
| 82 |
D_MODEL = 640
|
| 83 |
|
| 84 |
-
print("🔹
|
| 85 |
tokenizer = AutoTokenizer.from_pretrained(PLM_MODEL_NAME)
|
| 86 |
-
plm_model = AutoModel.from_pretrained(PLM_MODEL_NAME).to(DEVICE)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
classifier = ProtDualBranchEnhancedClassifier(
|
| 90 |
-
d_model=D_MODEL, projection_dim=32, num_classes=NUM_CLASSES,
|
| 91 |
-
dropout=0.3, kernel_size=3
|
| 92 |
-
).to(DEVICE)
|
| 93 |
-
|
| 94 |
if not os.path.exists(CLASSIFIER_PATH):
|
| 95 |
-
raise FileNotFoundError(f"
|
| 96 |
-
|
| 97 |
classifier.load_state_dict(torch.load(CLASSIFIER_PATH, map_location=DEVICE))
|
| 98 |
classifier.eval()
|
| 99 |
print("✅ Ready.")
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
# 3. Predict Logic (保持不变)
|
| 103 |
-
# ==========================
|
| 104 |
def predict(sequence_input):
|
| 105 |
if not sequence_input or sequence_input.isspace():
|
| 106 |
-
raise gr.Error("
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
sequence = "".join(sequence_input.split('\n')[1:]) if sequence_input.startswith('>') else sequence_input
|
| 109 |
-
sequence = re.sub(r'[^A-Z]', '', sequence.upper())
|
| 110 |
-
|
| 111 |
-
if not sequence:
|
| 112 |
-
raise gr.Error("Invalid sequence.")
|
| 113 |
-
|
| 114 |
with torch.no_grad():
|
| 115 |
-
inputs = tokenizer(
|
| 116 |
outputs = plm_model(**inputs)
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
logits = classifier(cls_embedding, token_embeddings, token_mask)
|
| 123 |
-
probabilities = F.softmax(logits, dim=1)[0]
|
| 124 |
-
|
| 125 |
-
confidences = {idx_to_label[i]: float(prob) for i, prob in enumerate(probabilities)}
|
| 126 |
-
return confidences
|
| 127 |
|
| 128 |
# ==========================
|
| 129 |
-
# 4.
|
| 130 |
# ==========================
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
@import url('https://fonts.googleapis.com/css2?family=
|
| 135 |
|
| 136 |
-
body {
|
| 137 |
-
font-family: 'Inter', sans-serif !important;
|
| 138 |
-
background-color: #f8fafc;
|
| 139 |
-
}
|
| 140 |
|
| 141 |
-
/*
|
| 142 |
-
.
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
font-weight: 800;
|
| 150 |
-
margin-bottom: 0.5rem;
|
| 151 |
-
background: -webkit-linear-gradient(45deg, #0f172a, #334155);
|
| 152 |
-
-webkit-background-clip: text;
|
| 153 |
-
-webkit-text-fill-color: transparent;
|
| 154 |
-
letter-spacing: -1px;
|
| 155 |
}
|
| 156 |
-
.
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
| 162 |
}
|
| 163 |
|
| 164 |
-
/*
|
| 165 |
-
.
|
| 166 |
background: white;
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
border: 1px solid #
|
| 170 |
-
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05)
|
| 171 |
-
transition: all 0.3s ease;
|
| 172 |
-
}
|
| 173 |
-
.modern-card:hover {
|
| 174 |
-
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
/* 3. 输入框优化 - 模仿代码编辑器 */
|
| 178 |
-
textarea {
|
| 179 |
-
font-family: 'SF Mono', 'Menlo', 'Monaco', 'Courier New', monospace !important;
|
| 180 |
-
font-size: 14px !important;
|
| 181 |
-
background-color: #f8fafc !important;
|
| 182 |
-
border: 1px solid #e2e8f0 !important;
|
| 183 |
-
border-radius: 8px !important;
|
| 184 |
}
|
| 185 |
|
| 186 |
-
/*
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
letter-spacing: 0.5px !important;
|
| 192 |
-
transition: transform 0.1s ease-in-out !important;
|
| 193 |
-
}
|
| 194 |
-
button.primary:hover {
|
| 195 |
-
transform: translateY(-2px);
|
| 196 |
-
box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
|
| 197 |
-
}
|
| 198 |
-
|
| 199 |
-
/* 5. 标签页优化 */
|
| 200 |
-
.tabs {
|
| 201 |
-
border: none !important;
|
| 202 |
-
background: transparent !important;
|
| 203 |
-
}
|
| 204 |
-
.tab-nav {
|
| 205 |
-
border-bottom: 1px solid #e2e8f0;
|
| 206 |
-
margin-bottom: 20px;
|
| 207 |
-
}
|
| 208 |
-
.tab-nav button {
|
| 209 |
-
font-weight: 600;
|
| 210 |
-
color: #64748b;
|
| 211 |
-
}
|
| 212 |
-
.tab-nav button.selected {
|
| 213 |
-
color: #2563eb;
|
| 214 |
-
border-bottom: 2px solid #2563eb;
|
| 215 |
-
}
|
| 216 |
-
|
| 217 |
-
/* 6. Footer */
|
| 218 |
-
.footer-text {
|
| 219 |
-
text-align: center;
|
| 220 |
-
color: #94a3b8;
|
| 221 |
-
font-size: 0.8rem;
|
| 222 |
-
margin-top: 40px;
|
| 223 |
-
padding-bottom: 20px;
|
| 224 |
-
}
|
| 225 |
"""
|
| 226 |
|
| 227 |
-
# 使用极简主题作为底子
|
| 228 |
theme = gr.themes.Soft(
|
| 229 |
primary_hue="blue",
|
| 230 |
-
radius_size="
|
| 231 |
-
font=[gr.themes.GoogleFont("
|
| 232 |
)
|
| 233 |
|
| 234 |
-
with gr.Blocks(theme=theme, css=
|
| 235 |
|
| 236 |
-
# ---
|
| 237 |
-
with gr.Column(elem_classes="
|
| 238 |
gr.HTML("""
|
| 239 |
-
<div class="
|
| 240 |
-
<div
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
</div>
|
| 243 |
""")
|
| 244 |
|
| 245 |
-
# --- Main Content ---
|
| 246 |
with gr.Tabs():
|
| 247 |
|
| 248 |
-
# === TAB 1: Predict ===
|
| 249 |
-
with gr.TabItem("Predict", id="
|
| 250 |
with gr.Row():
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
| 254 |
sequence_input = gr.Textbox(
|
| 255 |
-
lines=
|
| 256 |
-
placeholder=">
|
| 257 |
-
show_label=False
|
| 258 |
-
container=False
|
| 259 |
)
|
| 260 |
|
| 261 |
with gr.Row():
|
| 262 |
-
clear_btn = gr.ClearButton(
|
| 263 |
-
submit_btn = gr.Button("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
-
#
|
| 266 |
-
with gr.Column(scale=2, elem_classes="
|
| 267 |
-
gr.Markdown("### Analysis Result")
|
| 268 |
-
|
| 269 |
output_label = gr.Label(num_top_classes=NUM_CLASSES, show_label=False)
|
| 270 |
|
| 271 |
gr.HTML("""
|
| 272 |
-
<div style="
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
</div>
|
| 275 |
""")
|
| 276 |
|
| 277 |
-
# === TAB 2:
|
| 278 |
-
with gr.TabItem("
|
| 279 |
-
with gr.
|
| 280 |
-
gr.
|
| 281 |
-
|
| 282 |
-
"""
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
| 287 |
|
| 288 |
-
|
| 289 |
-
"""
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
gr.Code(
|
| 297 |
value="""@article{LocPredProk2025,
|
| 298 |
-
title={LocPred-Prok: Prokaryotic protein subcellular localization prediction with a dual-branch architecture},
|
| 299 |
-
author={Your Name
|
| 300 |
-
journal={Bioinformatics},
|
| 301 |
year={2025}
|
| 302 |
}""",
|
| 303 |
-
label=
|
| 304 |
-
language=None,
|
| 305 |
interactive=False
|
| 306 |
)
|
| 307 |
|
| 308 |
# --- Footer ---
|
| 309 |
gr.HTML("""
|
| 310 |
-
<div
|
| 311 |
-
© 2025 iSysLab HUST
|
| 312 |
</div>
|
| 313 |
""")
|
| 314 |
|
| 315 |
-
#
|
| 316 |
submit_btn.click(fn=predict, inputs=sequence_input, outputs=output_label)
|
| 317 |
clear_btn.click(lambda: None, outputs=[output_label])
|
| 318 |
|
|
|
|
| 6 |
from transformers import AutoTokenizer, AutoModel
|
| 7 |
|
| 8 |
# ==========================
|
| 9 |
+
# 0-3. 基础设置与模型定义 (保持你的核心逻辑不变)
|
| 10 |
# ==========================
|
| 11 |
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
| 12 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
|
|
|
| 16 |
shutil.rmtree(path, ignore_errors=True)
|
| 17 |
os.makedirs(path, exist_ok=True)
|
| 18 |
|
| 19 |
+
# --- 模型类定义 ---
|
|
|
|
|
|
|
| 20 |
class AttentionPooling(nn.Module):
|
| 21 |
def __init__(self, d_model):
|
| 22 |
super().__init__()
|
| 23 |
self.attention_net = nn.Linear(d_model, 1)
|
|
|
|
| 24 |
def forward(self, x, mask):
|
| 25 |
attn_logits = self.attention_net(x).squeeze(2)
|
| 26 |
attn_logits.masked_fill_(mask == 0, -float('inf'))
|
|
|
|
| 32 |
super().__init__()
|
| 33 |
self.cls_projector = nn.Linear(d_model, projection_dim)
|
| 34 |
self.token_refiner = nn.Sequential(
|
| 35 |
+
nn.Conv1d(d_model, d_model, kernel_size, padding='same'), nn.ReLU()
|
|
|
|
| 36 |
)
|
| 37 |
self.attention_pooling = AttentionPooling(d_model)
|
| 38 |
self.tok_projector = nn.Linear(d_model, projection_dim)
|
| 39 |
fused_dim = projection_dim * 2
|
| 40 |
+
self.gate = nn.Sequential(nn.Linear(fused_dim, fused_dim), nn.Sigmoid())
|
|
|
|
|
|
|
|
|
|
| 41 |
self.classifier_head = nn.Sequential(
|
| 42 |
+
nn.LayerNorm(fused_dim), nn.Linear(fused_dim, fused_dim * 2),
|
| 43 |
+
nn.ReLU(), nn.Dropout(dropout), nn.Linear(fused_dim * 2, num_classes)
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
|
|
|
| 45 |
def forward(self, cls_embedding, token_embeddings, mask):
|
| 46 |
z_cls = self.cls_projector(cls_embedding)
|
| 47 |
tok_emb_permuted = token_embeddings.permute(0, 2, 1)
|
|
|
|
| 53 |
z_fused_gated = z_fused_concat * gate_values
|
| 54 |
return self.classifier_head(z_fused_gated)
|
| 55 |
|
| 56 |
+
# --- 加载资源 ---
|
|
|
|
|
|
|
| 57 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 58 |
+
PLM_MODEL_NAME = "facebook/esm2_t30_150M_UR50D"
|
| 59 |
CLASSIFIER_PATH = "best_model_esm2_t30_150M_UR50D.pth"
|
| 60 |
LABEL_MAP_PATH = "label_map.json"
|
| 61 |
|
| 62 |
if not os.path.exists(LABEL_MAP_PATH):
|
| 63 |
+
raise FileNotFoundError(f"Missing {LABEL_MAP_PATH}")
|
| 64 |
with open(LABEL_MAP_PATH, 'r') as f:
|
| 65 |
label_to_idx = json.load(f)
|
| 66 |
idx_to_label = {v: k for k, v in label_to_idx.items()}
|
|
|
|
| 68 |
NUM_CLASSES = len(idx_to_label)
|
| 69 |
D_MODEL = 640
|
| 70 |
|
| 71 |
+
print("🔹 Init models...")
|
| 72 |
tokenizer = AutoTokenizer.from_pretrained(PLM_MODEL_NAME)
|
| 73 |
+
plm_model = AutoModel.from_pretrained(PLM_MODEL_NAME).to(DEVICE).eval()
|
| 74 |
+
classifier = ProtDualBranchEnhancedClassifier(D_MODEL, 32, NUM_CLASSES, 0.3, 3).to(DEVICE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
if not os.path.exists(CLASSIFIER_PATH):
|
| 76 |
+
raise FileNotFoundError(f"Missing {CLASSIFIER_PATH}")
|
|
|
|
| 77 |
classifier.load_state_dict(torch.load(CLASSIFIER_PATH, map_location=DEVICE))
|
| 78 |
classifier.eval()
|
| 79 |
print("✅ Ready.")
|
| 80 |
|
| 81 |
+
# --- 预测逻辑 ---
|
|
|
|
|
|
|
| 82 |
def predict(sequence_input):
|
| 83 |
if not sequence_input or sequence_input.isspace():
|
| 84 |
+
raise gr.Error("Please input a sequence.")
|
| 85 |
+
seq = "".join(sequence_input.split('\n')[1:]) if sequence_input.startswith('>') else sequence_input
|
| 86 |
+
seq = re.sub(r'[^A-Z]', '', seq.upper())
|
| 87 |
+
if not seq: raise gr.Error("Invalid Amino Acid Sequence")
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
with torch.no_grad():
|
| 90 |
+
inputs = tokenizer(seq, return_tensors="pt", truncation=True, max_length=1024).to(DEVICE)
|
| 91 |
outputs = plm_model(**inputs)
|
| 92 |
+
logits = classifier(outputs.last_hidden_state[:, 0, :], outputs.last_hidden_state[:, 1:-1, :], inputs['attention_mask'][:, 1:-1])
|
| 93 |
+
probs = F.softmax(logits, dim=1)[0]
|
| 94 |
+
|
| 95 |
+
return {idx_to_label[i]: float(p) for i, p in enumerate(probs)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# ==========================
|
| 98 |
+
# 4. 旗舰版 UI (Rich & Modern)
|
| 99 |
# ==========================
|
| 100 |
|
| 101 |
+
# CSS:结合了学术严谨性和现代视觉
|
| 102 |
+
flagship_css = """
|
| 103 |
+
@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;600;700&display=swap');
|
| 104 |
|
| 105 |
+
body { font-family: 'IBM Plex Sans', sans-serif !important; background-color: #f0f2f5; }
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
/* 标题区域 */
|
| 108 |
+
.header-box {
|
| 109 |
+
background: linear-gradient(120deg, #0284c7 0%, #2563eb 100%);
|
| 110 |
+
color: white;
|
| 111 |
+
padding: 2rem;
|
| 112 |
+
border-radius: 12px;
|
| 113 |
+
margin-bottom: 1.5rem;
|
| 114 |
+
box-shadow: 0 10px 15px -3px rgba(37, 99, 235, 0.2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
}
|
| 116 |
+
.header-title { font-size: 2.2rem; font-weight: 700; letter-spacing: -0.5px; }
|
| 117 |
+
.header-badges { display: flex; gap: 10px; margin-top: 10px; flex-wrap: wrap; }
|
| 118 |
+
.badge {
|
| 119 |
+
background: rgba(255,255,255,0.2);
|
| 120 |
+
padding: 4px 12px;
|
| 121 |
+
border-radius: 99px;
|
| 122 |
+
font-size: 0.85rem;
|
| 123 |
+
backdrop-filter: blur(4px);
|
| 124 |
+
border: 1px solid rgba(255,255,255,0.3);
|
| 125 |
}
|
| 126 |
|
| 127 |
+
/* 内容卡片 */
|
| 128 |
+
.content-box {
|
| 129 |
background: white;
|
| 130 |
+
padding: 1.5rem;
|
| 131 |
+
border-radius: 12px;
|
| 132 |
+
border: 1px solid #e5e7eb;
|
| 133 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
}
|
| 135 |
|
| 136 |
+
/* 表格美化 */
|
| 137 |
+
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
|
| 138 |
+
th { text-align: left; padding: 12px; background: #f8fafc; color: #475569; border-bottom: 2px solid #e2e8f0; }
|
| 139 |
+
td { padding: 12px; border-bottom: 1px solid #e2e8f0; color: #1e293b; }
|
| 140 |
+
tr:last-child td { border-bottom: none; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
"""
|
| 142 |
|
|
|
|
| 143 |
theme = gr.themes.Soft(
|
| 144 |
primary_hue="blue",
|
| 145 |
+
radius_size="md",
|
| 146 |
+
font=[gr.themes.GoogleFont("IBM Plex Sans"), "ui-sans-serif", "system-ui"]
|
| 147 |
)
|
| 148 |
|
| 149 |
+
with gr.Blocks(theme=theme, css=flagship_css, title="LocPred-Prok") as app:
|
| 150 |
|
| 151 |
+
# --- Header ---
|
| 152 |
+
with gr.Column(elem_classes="header-box"):
|
| 153 |
gr.HTML("""
|
| 154 |
+
<div class="header-title">LocPred-Prok</div>
|
| 155 |
+
<div style="opacity: 0.9; font-size: 1.1rem; margin-bottom: 1rem;">
|
| 156 |
+
State-of-the-Art Prokaryotic Subcellular Localization Prediction
|
| 157 |
+
</div>
|
| 158 |
+
<div class="header-badges">
|
| 159 |
+
<span class="badge">🧬 ESM-2 Enhanced</span>
|
| 160 |
+
<span class="badge">🚀 Dual-Branch Architecture</span>
|
| 161 |
+
<span class="badge">🏆 91.2% Accuracy</span>
|
| 162 |
+
<span class="badge">🎯 MCC 0.889</span>
|
| 163 |
</div>
|
| 164 |
""")
|
| 165 |
|
|
|
|
| 166 |
with gr.Tabs():
|
| 167 |
|
| 168 |
+
# === TAB 1: Predict (功能区) ===
|
| 169 |
+
with gr.TabItem("🚀 Predict", id="predict"):
|
| 170 |
with gr.Row():
|
| 171 |
+
|
| 172 |
+
# 左侧:输入 + 示例
|
| 173 |
+
with gr.Column(scale=3, elem_classes="content-box"):
|
| 174 |
+
gr.Markdown("### 📥 Sequence Input")
|
| 175 |
+
gr.Markdown("Enter a protein sequence (FASTA format supported).")
|
| 176 |
+
|
| 177 |
sequence_input = gr.Textbox(
|
| 178 |
+
lines=10,
|
| 179 |
+
placeholder=">Header\nMKFKLTAGCLAVAGVLLASSFGAD...",
|
| 180 |
+
show_label=False
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
with gr.Row():
|
| 184 |
+
clear_btn = gr.ClearButton(sequence_input, value="Clear Input")
|
| 185 |
+
submit_btn = gr.Button("✨ Run Prediction", variant="primary", scale=2)
|
| 186 |
+
|
| 187 |
+
# ✅ 示例回归:这对用户极其重要
|
| 188 |
+
gr.Markdown("### 💡 Quick Examples")
|
| 189 |
+
gr.Examples(
|
| 190 |
+
examples=[
|
| 191 |
+
[">Gram-negative Outer Membrane\nMSKLVKTLTISEISKAQNNGGKPAWCWYTLAMCGAGYDSGTCDYMYSHCFGIKHHSSGSSSYHC"],
|
| 192 |
+
[">Gram-positive Cell Wall\nMKFKLTAGCLAVAGVLLASSFGADAEIVVNAIYDQVARTEDGVYTQGQLTGRRIELLNKLGIEPEDSLASTVIHEFVARVGDDHGIETIIDEFYRQHPSASL"],
|
| 193 |
+
[">Cytoplasmic Protein\nMAKQDYYEILGVSKTAEEREIRKAYKRLAMKYHPDRNQGDKEAEAKFKEIKEAYEVLTDSQKRAAYDQYGHAAFEQGPE"],
|
| 194 |
+
],
|
| 195 |
+
inputs=sequence_input,
|
| 196 |
+
label=None
|
| 197 |
+
)
|
| 198 |
|
| 199 |
+
# 右侧:输出 + 简要说明
|
| 200 |
+
with gr.Column(scale=2, elem_classes="content-box"):
|
| 201 |
+
gr.Markdown("### 📊 Analysis Result")
|
| 202 |
+
|
| 203 |
output_label = gr.Label(num_top_classes=NUM_CLASSES, show_label=False)
|
| 204 |
|
| 205 |
gr.HTML("""
|
| 206 |
+
<div style="background: #eff6ff; padding: 15px; border-radius: 8px; margin-top: 20px; border-left: 4px solid #3b82f6;">
|
| 207 |
+
<h4 style="margin: 0 0 5px 0; color: #1e40af;">Performance Note</h4>
|
| 208 |
+
<p style="margin: 0; font-size: 0.9rem; color: #1e3a8a;">
|
| 209 |
+
This model excels at distinguishing <b>Outer Membrane</b> and <b>Cell Wall</b> proteins,
|
| 210 |
+
outperforming traditional methods by utilizing deep semantic features from ESM-2.
|
| 211 |
+
</p>
|
| 212 |
</div>
|
| 213 |
""")
|
| 214 |
|
| 215 |
+
# === TAB 2: Model Details (学术区) ===
|
| 216 |
+
with gr.TabItem("📈 Model Performance", id="stats"):
|
| 217 |
+
with gr.Row():
|
| 218 |
+
with gr.Column(elem_classes="content-box"):
|
| 219 |
+
gr.Markdown("### 🔬 Why LocPred-Prok?")
|
| 220 |
+
gr.Markdown("""
|
| 221 |
+
Existing predictors often struggle with "Hard Classes" like Cell Wall and Outer Membrane proteins.
|
| 222 |
+
**LocPred-Prok** solves this by fusing:
|
| 223 |
+
1. **Global Semantics:** From the pre-trained `ESM-2-150M` model.
|
| 224 |
+
2. **Local Motifs:** Captured by our custom CNN + Attention pooling branch.
|
| 225 |
+
""")
|
| 226 |
|
| 227 |
+
# ✅ 找回数据表格:增加专业度
|
| 228 |
+
gr.HTML("""
|
| 229 |
+
<h3>Comparative Performance (Homology Partitioned)</h3>
|
| 230 |
+
<table>
|
| 231 |
+
<thead>
|
| 232 |
+
<tr>
|
| 233 |
+
<th>Method</th>
|
| 234 |
+
<th>Accuracy</th>
|
| 235 |
+
<th>MCC (Overall)</th>
|
| 236 |
+
<th>Outer Membrane MCC</th>
|
| 237 |
+
</tr>
|
| 238 |
+
</thead>
|
| 239 |
+
<tbody>
|
| 240 |
+
<tr style="background-color: #f0fdf4; font-weight: bold;">
|
| 241 |
+
<td>✨ LocPred-Prok (Ours)</td>
|
| 242 |
+
<td>91.2%</td>
|
| 243 |
+
<td>0.889</td>
|
| 244 |
+
<td>0.910</td>
|
| 245 |
+
</tr>
|
| 246 |
+
<tr>
|
| 247 |
+
<td>Standard ESM-2 Only</td>
|
| 248 |
+
<td>89.5%</td>
|
| 249 |
+
<td>0.865</td>
|
| 250 |
+
<td>0.872</td>
|
| 251 |
+
</tr>
|
| 252 |
+
<tr>
|
| 253 |
+
<td>DeepLoc 2.0 (Prok)</td>
|
| 254 |
+
<td>87.1%</td>
|
| 255 |
+
<td>0.840</td>
|
| 256 |
+
<td>0.855</td>
|
| 257 |
+
</tr>
|
| 258 |
+
</tbody>
|
| 259 |
+
</table>
|
| 260 |
+
<p style="margin-top: 10px; font-size: 0.8rem; color: #666;">* Benchmarked on strict homology-reduced datasets.</p>
|
| 261 |
+
""")
|
| 262 |
+
|
| 263 |
+
# === TAB 3: Citation (引用区) ===
|
| 264 |
+
with gr.TabItem("📝 Citation", id="cite"):
|
| 265 |
+
with gr.Column(elem_classes="content-box"):
|
| 266 |
+
gr.Markdown("### Cite This Work")
|
| 267 |
+
gr.Markdown("If you find this tool useful, please cite our paper:")
|
| 268 |
+
# 修复了 Code 组件的报错,去掉了 language="bibtex"
|
| 269 |
gr.Code(
|
| 270 |
value="""@article{LocPredProk2025,
|
| 271 |
+
title={LocPred-Prok: Prokaryotic protein subcellular localization prediction with a dual-branch architecture and protein language model},
|
| 272 |
+
author={Your Name and Co-authors},
|
| 273 |
+
journal={Submitted to Bioinformatics},
|
| 274 |
year={2025}
|
| 275 |
}""",
|
| 276 |
+
label="BibTeX",
|
| 277 |
+
language=None,
|
| 278 |
interactive=False
|
| 279 |
)
|
| 280 |
|
| 281 |
# --- Footer ---
|
| 282 |
gr.HTML("""
|
| 283 |
+
<div style="text-align: center; margin-top: 40px; color: #94a3b8; font-size: 0.85rem;">
|
| 284 |
+
© 2025 iSysLab HUST • Powered by PyTorch & Hugging Face
|
| 285 |
</div>
|
| 286 |
""")
|
| 287 |
|
| 288 |
+
# 逻辑绑定
|
| 289 |
submit_btn.click(fn=predict, inputs=sequence_input, outputs=output_label)
|
| 290 |
clear_btn.click(lambda: None, outputs=[output_label])
|
| 291 |
|