oucgc1996 commited on
Commit
3a0932b
1 Parent(s): d3b9133
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ from transformers import AutoTokenizer,AutoModelForSequenceClassification
4
+ from transformers import set_seed
5
+ from torch.utils.data import Dataset,DataLoader
6
+ import torch
7
+ import torch.nn as nn
8
+ import numpy as np
9
+ import warnings
10
+ warnings.filterwarnings('ignore')
11
+ set_seed(4)
12
+ device = "cpu"
13
+ model_checkpoint = "facebook/esm2_t6_8M_UR50D"
14
+ tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
15
+
16
+ def AMP(file):
17
+ test_sequences = file
18
+ max_len = 30
19
+ test_data = tokenizer(test_sequences, max_length=max_len, padding="max_length",truncation=True, return_tensors='pt')
20
+
21
+ class MyModel(nn.Module):
22
+ def __init__(self):
23
+ super().__init__()
24
+ self.bert = AutoModelForSequenceClassification.from_pretrained(model_checkpoint,num_labels=320)
25
+ self.bn1 = nn.BatchNorm1d(256)
26
+ self.bn2 = nn.BatchNorm1d(128)
27
+ self.bn3 = nn.BatchNorm1d(64)
28
+ self.relu = nn.ReLU()
29
+ self.fc1 = nn.Linear(320,256)
30
+ self.fc2 = nn.Linear(256,128)
31
+ self.fc3 = nn.Linear(128,64)
32
+ self.output_layer = nn.Linear(64,2)
33
+ self.dropout = nn.Dropout(0)
34
+ def forward(self,x):
35
+ with torch.no_grad():
36
+ bert_output = self.bert(input_ids=x['input_ids'].to(device),attention_mask=x['attention_mask'].to(device))
37
+ output_feature = self.dropout(bert_output["logits"])
38
+ output_feature = self.relu(self.bn1(self.fc1(output_feature)))
39
+ output_feature = self.relu(self.bn2(self.fc2(output_feature)))
40
+ output_feature = self.relu(self.bn3(self.fc3(output_feature)))
41
+ output_feature = self.output_layer(output_feature)
42
+ return torch.softmax(output_feature,dim=1)
43
+
44
+ model = MyModel()
45
+ model.load_state_dict(torch.load("Best_model.pth",map_location=torch.device('cpu')))
46
+ model = model.to(device)
47
+ model.eval()
48
+ out_probability = []
49
+ with torch.no_grad():
50
+ predict = model(test_data)
51
+ out_probability.extend(np.max(np.array(predict.cpu()),axis=1).tolist())
52
+ test_argmax = np.argmax(predict.cpu(), axis=1).tolist()
53
+ id2str = {0:"non-AMP", 1:"AMP"}
54
+ return id2str[test_argmax[0]], out_probability[0]
55
+
56
+ iface = gr.Interface(fn=AMP,
57
+ inputs="text",
58
+ outputs= ["text", "text"])
59
+ iface.launch()