jackrui commited on
Commit
eed06d2
·
1 Parent(s): c8e395f

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -141
app.py DELETED
@@ -1,141 +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=512)#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(512, 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'],
35
- attention_mask=x['attention_mask'])
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)
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
-
83
- with gr.Blocks() as demo:
84
- gr.Markdown(
85
- """
86
-
87
- # Welcome to Antimicrobial Peptide Recognition Model
88
- This is an antimicrobial peptide recognition model derived from Diff-AMP, which is a branch of a comprehensive system integrating generation, recognition, and optimization. In this recognition model, you can simply input a sequence, and it will predict whether it is an antimicrobial peptide. Due to limited website capacity, we can only perform simple predictions.
89
- If you require large-scale computations, please contact my email at [email protected]. Feel free to reach out if you have any questions or inquiries.
90
-
91
- """)
92
-
93
- # 定义美化样式
94
- custom_css = """
95
- body {
96
- font-family: Arial, sans-serif;
97
- background-color: #f0f0f0;
98
- color: #333;
99
- }
100
-
101
- .gr-input-container {
102
- border: 2px solid #007BFF;
103
- border-radius: 5px;
104
- padding: 10px;
105
- }
106
-
107
- .gr-button {
108
- background-color: #007BFF;
109
- color: white;
110
- border: none;
111
- border-radius: 5px;
112
- }
113
-
114
- .gr-button:hover {
115
- background-color: #0056b3;
116
- }
117
- """
118
-
119
- # 添加示例输入和输出
120
- examples = [
121
- ["QGLFFLGAKLFYLLTLFL"],
122
- ["FLGLLFHGVHHVGKWIHGLIHGHH"],
123
- ["GLMSTLKGAATNAAVTLLNKLQCKLTGTC"]
124
- ]
125
-
126
- # 创建 Gradio 接口并应用美化样式和示例
127
- iface = gr.Interface(
128
- fn=classify_sequence,
129
- inputs=gr.inputs.Textbox(label="Enter Sequence"),
130
- outputs=gr.outputs.Textbox(label="AMP Classification (yes/no)"),
131
- live=True,
132
- title="AMP Sequence Detector",
133
- description="Enter a sequence to detect if it is an AMP (Antimicrobial Peptide) or not (yes/no)."
134
- )
135
-
136
- if __name__ == "__main__":
137
- demo.launch()
138
-
139
-
140
-
141
-