KeXing commited on
Commit
5321388
1 Parent(s): 6da398d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -12
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import gradio as gr
 
2
 
 
3
 
 
4
  from tape import ProteinBertModel, ProteinBertConfig, TAPETokenizer # type: ignore
5
  from tape.models import modeling_bert
6
  import numpy as np
@@ -14,14 +17,11 @@ bert_model = torch.load('models/transformer1500_95p_500.pt')
14
  class_model=torch.load('models/down_model_500_kfold1.pt')
15
 
16
  bert_model=bert_model.module
17
- bert_model=bert_model.to('cpu')
18
  bert_model=bert_model.eval()
19
 
20
 
21
-
22
- def greet(name):
23
-
24
-
25
 
26
  translation_table = str.maketrans("", "", " \t\n\r\f\v")
27
  name = name.translate(translation_table)
@@ -36,14 +36,85 @@ def greet(name):
36
  return "cluster "+str(cluster)
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
 
 
 
40
 
41
 
42
- demo = gr.Interface(
43
- fn=greet,
44
- # 自定义输入框
45
- # 具体设置方法查看官方文档
46
- inputs=gr.Textbox(lines=3, placeholder="",label="Paste a protein sequence in plain text (not in FASTA format)"),
47
- outputs=gr.Textbox(lines=3, placeholder="",label="Cluster prediction"),
48
- )
49
  demo.launch(share=True)
 
1
  import gradio as gr
2
+ import pandas as pd
3
 
4
+ import io
5
 
6
+ from Bio import SeqIO
7
  from tape import ProteinBertModel, ProteinBertConfig, TAPETokenizer # type: ignore
8
  from tape.models import modeling_bert
9
  import numpy as np
 
17
  class_model=torch.load('models/down_model_500_kfold1.pt')
18
 
19
  bert_model=bert_model.module
20
+ bert_model=bert_model.to("cpu")
21
  bert_model=bert_model.eval()
22
 
23
 
24
+ def func(name):
 
 
 
25
 
26
  translation_table = str.maketrans("", "", " \t\n\r\f\v")
27
  name = name.translate(translation_table)
 
36
  return "cluster "+str(cluster)
37
 
38
 
39
+ def func_mult(name):
40
+ sequence_list = name.split("\n")
41
+ sequence_list = [s.strip() for s in sequence_list]
42
+ sequence_list = [x for x in sequence_list if x] # 列表推导式
43
+ output=[]
44
+ for i in range(0, len(sequence_list), 1):
45
+ output.append(func(sequence_list[i]))
46
+ result = "\n".join(output)
47
+ return result
48
+
49
+
50
+ def read_fasta_file(file_path):
51
+ sequences = []
52
+ for seq_record in SeqIO.parse(file_path, "fasta"):
53
+ sequences.append(str(seq_record.seq))
54
+ return sequences
55
+
56
+
57
+ def func_file(file_path):
58
+ sequence_list = read_fasta_file(file_path)
59
+ output=[]
60
+ for i in range(0, len(sequence_list), 1):
61
+ output.append(func(sequence_list[i]))
62
+ result = "\n".join(output)
63
+ return result
64
+
65
+
66
+ def upload_file(files):
67
+ file_paths = [file.name for file in files]
68
+ return file_paths[0]
69
+
70
+
71
+ def save_to_txt(data):
72
+ # 写入数据到 TXT 文件
73
+ file_name="output.txt"
74
+ with open(file_name, mode='w') as file:
75
+ file.write(data)
76
+
77
+ # 返回文件路径
78
+ return file_name
79
+
80
+
81
+ css = """
82
+ .gradio-container {background-color: #EDEFF7}
83
+ .button {background-color: #515D90; color:#FFFFFF}
84
+ .feedback {font-size: 36px}
85
+ """
86
+
87
+ with gr.Blocks(css=css, title="GH29 Prediction", theme=gr.themes.Soft()) as demo:
88
+ gr.Markdown("GH29 Prediction", elem_classes="feedback")
89
+
90
+ # 创建一个包含Markdown说明的示例块
91
+
92
+ with gr.Tab("Single sequence input"):
93
+ with gr.Row():
94
+ single_input = gr.Textbox(lines=10, placeholder="Please input sequence data (note: do not input fasta data)", label="Input")
95
+ single_output = gr.Textbox(lines=10, label="Output", show_copy_button=True)
96
+ single_button = gr.Button("Predict", elem_classes="button")
97
+
98
+ with gr.Tab("Multiple sequence input"):
99
+ multiple_input = gr.Textbox(lines=10, placeholder="Please enter multiple sequence data separated by line breaks (do not enter fasta data)", label="Input")
100
+ multiple_button = gr.Button("Predict", elem_classes="button")
101
+ multiple_output = gr.Textbox(lines=10, label="Output", show_copy_button=True)
102
+
103
+ with gr.Tab("FASTA input"):
104
+ with gr.Row():
105
+ file_upload = gr.File(label="Fasta File", interactive=False, scale=2)
106
+ file_output_textbox = gr.Textbox(lines=15, label="Output", scale=3, container=True, autoscroll=True, show_copy_button=True)
107
+ file_output_file = gr.File(label="Output File", scale=2)
108
+ with gr.Row():
109
+ upload_button = gr.UploadButton("Click to Upload a File", file_types=["fasta"], scale=2, size="sm", file_count="multiple")
110
+ upload_button.upload(upload_file, upload_button, file_upload)
111
+ file_button = gr.Button("Predict", scale=3, size="lg", elem_classes="button")
112
+ file_button_GenerateFile = gr.Button("Save to file", scale=2, size="sm")
113
 
114
+ single_button.click(func, inputs=single_input, outputs=single_output)
115
+ multiple_button.click(func_mult, inputs=multiple_input, outputs=multiple_output)
116
+ file_button.click(func_file, inputs=file_upload, outputs=file_output_textbox)
117
+ file_button_GenerateFile.click(save_to_txt, inputs=file_output_textbox, outputs=file_output_file)
118
 
119
 
 
 
 
 
 
 
 
120
  demo.launch(share=True)