wnagleiofficial commited on
Commit
4754bea
·
1 Parent(s): ae25438

fix app batch

Browse files
NeuroPredPLM/__pycache__/model.cpython-38.pyc CHANGED
Binary files a/NeuroPredPLM/__pycache__/model.cpython-38.pyc and b/NeuroPredPLM/__pycache__/model.cpython-38.pyc differ
 
NeuroPredPLM/__pycache__/predict.cpython-38.pyc CHANGED
Binary files a/NeuroPredPLM/__pycache__/predict.cpython-38.pyc and b/NeuroPredPLM/__pycache__/predict.cpython-38.pyc differ
 
NeuroPredPLM/model.py CHANGED
@@ -44,10 +44,9 @@ class EsmModel(nn.Module):
44
  representation_cov = F.relu(self.cov_2(representation_cov))
45
  representations = rearrange(representation_cov, '(b h) d l -> b h l d', h=self.head)
46
  att = torch.einsum('bhld,hd->bhl', representations, self.W)
47
- mask = length_to_mask(torch.tensor(peptide_length)).to(device)
48
  att = att.masked_fill(mask.unsqueeze(1)==0, -np.inf)
49
  att= F.softmax(att, dim=-1)
50
- # print(att)
51
  representations = rearrange(representations * att.unsqueeze(-1), 'b h l d -> b l (h d)')
52
  representations = torch.sum(representations, dim=1)
53
  return self.fcn(representations), att
 
44
  representation_cov = F.relu(self.cov_2(representation_cov))
45
  representations = rearrange(representation_cov, '(b h) d l -> b h l d', h=self.head)
46
  att = torch.einsum('bhld,hd->bhl', representations, self.W)
47
+ mask = length_to_mask(torch.tensor(peptide_length)).to(device).int()
48
  att = att.masked_fill(mask.unsqueeze(1)==0, -np.inf)
49
  att= F.softmax(att, dim=-1)
 
50
  representations = rearrange(representations * att.unsqueeze(-1), 'b h l d -> b l (h d)')
51
  representations = torch.sum(representations, dim=1)
52
  return self.fcn(representations), att
NeuroPredPLM/predict.py CHANGED
@@ -24,8 +24,11 @@ def batch_predict(peptide_list, cutoff, model_path, device='cpu'):
24
  state_dict = torch.load(model_path, map_location="cpu")
25
  neuroPred_model.load_state_dict(state_dict)
26
  neuroPred_model = neuroPred_model.to(device)
27
- prob, att = neuroPred_model(peptide_list, device)
28
- pred = torch.softmax(prob, dim=-1).cpu().tolist()
29
- att = att.cpu().numpy()
30
- out = [[i[0], i[1], f"{j[1]:.3f}", 'Neuropeptide' if j[1] >cutoff else 'Non-neuropeptide'] for i, j in zip(peptide_list, pred)]
 
 
 
31
  return out
 
24
  state_dict = torch.load(model_path, map_location="cpu")
25
  neuroPred_model.load_state_dict(state_dict)
26
  neuroPred_model = neuroPred_model.to(device)
27
+ out = []
28
+ for item in peptide_list:
29
+ prob, att = neuroPred_model([item], device)
30
+ pred = torch.softmax(prob, dim=-1).cpu().tolist()
31
+ att = att.cpu().numpy()
32
+ temp = [[i[0], i[1], f"{j[1]:.3f}", 'Neuropeptide' if j[1] >cutoff else 'Non-neuropeptide'] for i, j in zip([item], pred)]
33
+ out.append(temp[0])
34
  return out
app.py CHANGED
@@ -9,7 +9,7 @@ def classifier(peptide_seq):
9
  data = []
10
  for record in SeqIO.parse(handle, 'fasta'):
11
  data.append((record.id, str(record.seq)))
12
- device = "cuda" if torch.cuda.is_available() else "cpu"
13
  neuropeptide_pred = predict(data, './model.pth', device)
14
  return neuropeptide_pred
15
  # {peptide_id:[Type:int(1->neuropeptide,0->non-neuropeptide), attention score:nd.array]}
@@ -19,7 +19,7 @@ def batch_classifier(file, cutoff):
19
  data = []
20
  for record in SeqIO.parse(file.name, 'fasta'):
21
  data.append((record.id, str(record.seq)))
22
- device = "cuda" if torch.cuda.is_available() else "cpu"
23
  neuropeptide_pred = batch_predict(data, cutoff, './model.pth', device)
24
  return neuropeptide_pred
25
 
@@ -49,7 +49,7 @@ with gr.Blocks() as demo:
49
  with gr.Column(scale=2):
50
  batch_cutoff = gr.Slider(0, 1, step=0.1, value=0.5, interactive=True, label="Threshold")
51
  gr.Markdown("### Note")
52
- gr.Markdown("- Limit the number of input sequences to less than 100")
53
  gr.Markdown("- The file should be the Fasta format")
54
  gr.Markdown("- The input peptide sequence length should be between 5-100")
55
  image_button = gr.Button("Submit")
 
9
  data = []
10
  for record in SeqIO.parse(handle, 'fasta'):
11
  data.append((record.id, str(record.seq)))
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
  neuropeptide_pred = predict(data, './model.pth', device)
14
  return neuropeptide_pred
15
  # {peptide_id:[Type:int(1->neuropeptide,0->non-neuropeptide), attention score:nd.array]}
 
19
  data = []
20
  for record in SeqIO.parse(file.name, 'fasta'):
21
  data.append((record.id, str(record.seq)))
22
+ device = "cuda" if torch.cuda.is_available() else "cpu"
23
  neuropeptide_pred = batch_predict(data, cutoff, './model.pth', device)
24
  return neuropeptide_pred
25
 
 
49
  with gr.Column(scale=2):
50
  batch_cutoff = gr.Slider(0, 1, step=0.1, value=0.5, interactive=True, label="Threshold")
51
  gr.Markdown("### Note")
52
+ gr.Markdown("- Limit the number of input sequences to less than 30")
53
  gr.Markdown("- The file should be the Fasta format")
54
  gr.Markdown("- The input peptide sequence length should be between 5-100")
55
  image_button = gr.Button("Submit")