steveyu323 commited on
Commit
5bd3c5c
·
1 Parent(s): c6956f7

Add application file

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn as nn
4
+ from transformers import PreTrainedModel, PretrainedConfig
5
+ from tape import ProteinBertForSequenceClassification, TAPETokenizer
6
+ import re
7
+
8
+ WHITESPACE_RE = re.compile(r"\s+")
9
+
10
+ def sanitize(s):
11
+ return WHITESPACE_RE.sub("", str(s).upper())
12
+
13
+ # Load model directly (avoids trust_remote_code complexity in Spaces)
14
+ # ckpt = torch.load("kinbert_v2_long.pt", map_location="cpu")
15
+ # OR load from your hub repo:
16
+ from huggingface_hub import hf_hub_download
17
+ ckpt_path = hf_hub_download("steveyu323/kinbert_v2_long", "checkpoints/best.pt")
18
+ ckpt = torch.load(ckpt_path, map_location="cpu")
19
+
20
+ model = ProteinBertForSequenceClassification.from_pretrained("bert-base", num_labels=2)
21
+ model.load_state_dict(ckpt["state_dict"])
22
+ model.eval()
23
+
24
+ tokenizer = TAPETokenizer(vocab="iupac")
25
+ THRESHOLD = float(ckpt["threshold"])
26
+ MAX_LEN = 1024
27
+
28
+ def predict(kinase_seq, substrate_seq):
29
+ kinase_seq = sanitize(kinase_seq)
30
+ substrate_seq = sanitize(substrate_seq)
31
+
32
+ if not kinase_seq or not substrate_seq:
33
+ return "Please enter both sequences.", None
34
+
35
+ kin_toks = tokenizer.tokenize(kinase_seq)
36
+ sub_toks = tokenizer.tokenize(substrate_seq)
37
+ toks = kin_toks + ["<sep>"] + sub_toks
38
+ toks = tokenizer.add_special_tokens(toks)
39
+ ids = tokenizer.convert_tokens_to_ids(toks)[:MAX_LEN]
40
+
41
+ input_ids = torch.tensor([ids], dtype=torch.long)
42
+ input_mask = torch.ones_like(input_ids)
43
+
44
+ with torch.no_grad():
45
+ (_, _), logits = model(input_ids=input_ids, input_mask=input_mask)
46
+ prob = float(torch.softmax(logits, dim=-1)[0, 1])
47
+
48
+ label = "✅ Interaction" if prob >= THRESHOLD else "❌ No Interaction"
49
+ return label, round(prob, 4)
50
+
51
+ demo = gr.Interface(
52
+ fn=predict,
53
+ inputs=[
54
+ gr.Textbox(lines=3, label="Kinase Sequence", placeholder="Paste kinase amino acid sequence..."),
55
+ gr.Textbox(lines=3, label="Substrate Sequence", placeholder="Paste substrate amino acid sequence..."),
56
+ ],
57
+ outputs=[
58
+ gr.Text(label="Prediction"),
59
+ gr.Number(label="Interaction Probability"),
60
+ ],
61
+ title="KinBERT — Kinase–Substrate Interaction Classifier",
62
+ description="Predicts whether a kinase will phosphorylate a given substrate sequence.",
63
+ examples=[
64
+ ["MGSSHHHHHHSSGENLYFQGH", "ARTKQTARKSTGGKAPRKQL"],
65
+ ],
66
+ )
67
+
68
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ tape-proteins
2
+ transformers
3
+ torch
4
+ gradio