Delete app.py
Browse files
app.py
DELETED
|
@@ -1,95 +0,0 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
-
from transformers import set_seed
|
| 4 |
-
import torch
|
| 5 |
-
import torch.nn as nn
|
| 6 |
-
import warnings
|
| 7 |
-
from tqdm import tqdm
|
| 8 |
-
import gradio as gr
|
| 9 |
-
|
| 10 |
-
warnings.filterwarnings('ignore')
|
| 11 |
-
device = "cpu"
|
| 12 |
-
model_checkpoint1 = "facebook/esm2_t12_35M_UR50D"
|
| 13 |
-
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint1)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
class MyModel(nn.Module):
|
| 17 |
-
def __init__(self):
|
| 18 |
-
super().__init__()
|
| 19 |
-
self.bert1 = AutoModelForSequenceClassification.from_pretrained(model_checkpoint1, num_labels=3000).cuda()#3000
|
| 20 |
-
# for param in self.bert1.parameters():
|
| 21 |
-
# param.requires_grad = False
|
| 22 |
-
self.bn1 = nn.BatchNorm1d(256)
|
| 23 |
-
self.bn2 = nn.BatchNorm1d(128)
|
| 24 |
-
self.bn3 = nn.BatchNorm1d(64)
|
| 25 |
-
self.relu = nn.LeakyReLU()
|
| 26 |
-
self.fc1 = nn.Linear(3000, 256)
|
| 27 |
-
self.fc2 = nn.Linear(256, 128)
|
| 28 |
-
self.fc3 = nn.Linear(128, 64)
|
| 29 |
-
self.output_layer = nn.Linear(64, 2)
|
| 30 |
-
self.dropout = nn.Dropout(0.3) # 0.3
|
| 31 |
-
|
| 32 |
-
def forward(self, x):
|
| 33 |
-
with torch.no_grad():
|
| 34 |
-
bert_output = self.bert1(input_ids=x['input_ids'].to(device),
|
| 35 |
-
attention_mask=x['attention_mask'].to(device))
|
| 36 |
-
# output_feature = bert_output["logits"]
|
| 37 |
-
# print(output_feature.size())
|
| 38 |
-
# output_feature = self.bn1(self.fc1(output_feature))
|
| 39 |
-
# output_feature = self.bn2(self.fc1(output_feature))
|
| 40 |
-
# output_feature = self.relu(self.bn3(self.fc3(output_feature)))
|
| 41 |
-
# output_feature = self.dropout(self.output_layer(output_feature))
|
| 42 |
-
output_feature = self.dropout(bert_output["logits"])
|
| 43 |
-
output_feature = self.dropout(self.relu(self.bn1(self.fc1(output_feature))))
|
| 44 |
-
output_feature = self.dropout(self.relu(self.bn2(self.fc2(output_feature))))
|
| 45 |
-
output_feature = self.dropout(self.relu(self.bn3(self.fc3(output_feature))))
|
| 46 |
-
output_feature = self.dropout(self.output_layer(output_feature))
|
| 47 |
-
# return torch.sigmoid(output_feature),output_feature
|
| 48 |
-
return torch.softmax(output_feature, dim=1)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def AMP(test_sequences, model):
|
| 52 |
-
# 保持 AMP 函数不变,只处理传入的 test_sequences 数据
|
| 53 |
-
max_len = 18
|
| 54 |
-
test_data = tokenizer(test_sequences, max_length=max_len, padding="max_length", truncation=True,
|
| 55 |
-
return_tensors='pt')
|
| 56 |
-
model = model.to(device)
|
| 57 |
-
model.eval()
|
| 58 |
-
out_probability = []
|
| 59 |
-
with torch.no_grad():
|
| 60 |
-
predict = model(test_data).cuda()
|
| 61 |
-
out_probability.extend(np.max(np.array(predict.cpu()), axis=1).tolist())
|
| 62 |
-
test_argmax = np.argmax(predict.cpu(), axis=1).tolist()
|
| 63 |
-
id2str = {0: "non-AMP", 1: "AMP"}
|
| 64 |
-
return id2str[test_argmax[0]], out_probability[0]
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def classify_sequence(sequence):
|
| 68 |
-
# Check if the sequence is a valid amino acid sequence and has a length of at least 3
|
| 69 |
-
valid_amino_acids = set("ACDEFGHIKLMNPQRSTVWY")
|
| 70 |
-
sequence = sequence.upper()
|
| 71 |
-
|
| 72 |
-
if all(aa in valid_amino_acids for aa in sequence) and len(sequence) >= 3:
|
| 73 |
-
result, probability = AMP(sequence, model)
|
| 74 |
-
return "yes" if result == "AMP" else "no"
|
| 75 |
-
else:
|
| 76 |
-
return "Invalid Sequence"
|
| 77 |
-
|
| 78 |
-
# 加载模型
|
| 79 |
-
model = MyModel()
|
| 80 |
-
model.load_state_dict(torch.load("best_model.pth"))
|
| 81 |
-
|
| 82 |
-
iface = gr.Interface(
|
| 83 |
-
fn=classify_sequence,
|
| 84 |
-
inputs=gr.inputs.Textbox(label="Enter Sequence"),
|
| 85 |
-
outputs=gr.outputs.Textbox(label="AMP Classification (yes/no)"),
|
| 86 |
-
live=True,
|
| 87 |
-
title="AMP Sequence Detector",
|
| 88 |
-
description="Enter a sequence to detect if it is an AMP (Antimicrobial Peptide) or not (yes/no)."
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
if __name__ == "__main__":
|
| 92 |
-
iface.launch()
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|