Update app.py
Browse files
app.py
CHANGED
@@ -29,16 +29,74 @@ class Config:
|
|
29 |
|
30 |
|
31 |
|
32 |
-
def predict_stability_with_pdb(model_choice, organism_choice,
|
33 |
try:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
except Exception as e:
|
43 |
return f"An error occurred: {str(e)}"
|
44 |
|
@@ -159,8 +217,8 @@ with gr.Blocks() as demo:
|
|
159 |
|
160 |
with gr.Tabs():
|
161 |
with gr.TabItem("Upload PDB File"):
|
162 |
-
gr.Markdown("### Upload your PDB
|
163 |
-
|
164 |
predict_button = gr.Button("Predict Stability")
|
165 |
prediction_output = gr.HTML(
|
166 |
label="Stability Prediction"
|
@@ -168,7 +226,7 @@ with gr.Blocks() as demo:
|
|
168 |
|
169 |
predict_button.click(
|
170 |
fn=predict_stability_with_pdb,
|
171 |
-
inputs=[model_choice, organism_choice,
|
172 |
outputs=prediction_output,
|
173 |
)
|
174 |
|
|
|
29 |
|
30 |
|
31 |
|
32 |
+
def predict_stability_with_pdb(model_choice, organism_choice, pdb_files, cfg=Config()):
|
33 |
try:
|
34 |
+
results = []
|
35 |
+
for pdb_file in pdb_files:
|
36 |
+
pdb_path = pdb_file.name
|
37 |
+
os.system("chmod 777 bin/foldseek")
|
38 |
+
sequences = get_foldseek_seq(pdb_path)
|
39 |
+
if not sequences:
|
40 |
+
return "Failed to extract sequence from the PDB file."
|
41 |
+
sequence = sequences[2] if model_choice == "SaProt" else sequences[0]
|
42 |
+
|
43 |
+
prediction = predict_stability_core(model_choice, organism_choice, sequence, cfg)
|
44 |
+
|
45 |
+
To modify the existing Gradio interface to accept multiple PDB files and process them, you need to adjust the file input component and modify the function that processes the PDB files. Here’s how you can do it:
|
46 |
+
|
47 |
+
Adjusting the Gradio Interface
|
48 |
+
Allow Multiple File Uploads: Modify the gr.File component to accept multiple files.
|
49 |
+
Process Each File Separately: Adjust the prediction function to loop over all uploaded files and process each one.
|
50 |
+
Here's the updated code:
|
51 |
+
|
52 |
+
python
|
53 |
+
Copy code
|
54 |
+
import gradio as gr
|
55 |
+
import sys
|
56 |
+
import random
|
57 |
+
import os
|
58 |
+
import pandas as pd
|
59 |
+
import torch
|
60 |
+
import itertools
|
61 |
+
from torch.utils.data import DataLoader
|
62 |
+
from transformers import AutoTokenizer
|
63 |
|
64 |
+
sys.path.append("scripts/")
|
65 |
+
from foldseek_util import get_struc_seq
|
66 |
+
from utils import seed_everything
|
67 |
+
from models import PLTNUM_PreTrainedModel
|
68 |
+
from datasets_ import PLTNUMDataset
|
69 |
+
|
70 |
+
|
71 |
+
class Config:
|
72 |
+
def __init__(self):
|
73 |
+
self.batch_size = 2
|
74 |
+
self.use_amp = False
|
75 |
+
self.num_workers = 1
|
76 |
+
self.max_length = 512
|
77 |
+
self.used_sequence = "left"
|
78 |
+
self.padding_side = "right"
|
79 |
+
self.task = "classification"
|
80 |
+
self.sequence_col = "sequence"
|
81 |
+
self.seed = 42
|
82 |
+
|
83 |
+
|
84 |
+
def predict_stability_with_pdb(model_choice, organism_choice, pdb_files, cfg=Config()):
|
85 |
+
try:
|
86 |
+
results = []
|
87 |
+
for pdb_file in pdb_files:
|
88 |
+
pdb_path = pdb_file.name
|
89 |
+
os.system("chmod 777 bin/foldseek")
|
90 |
+
sequences = get_foldseek_seq(pdb_path)
|
91 |
+
if not sequences:
|
92 |
+
results.append(f"Failed to extract sequence from {pdb_file.name}.")
|
93 |
+
continue
|
94 |
+
sequence = sequences[2] if model_choice == "SaProt" else sequences[0]
|
95 |
+
|
96 |
+
prediction = predict_stability_core(model_choice, organism_choice, sequence, cfg)
|
97 |
+
results.append(f"Prediction for {pdb_file.name}: {prediction}")
|
98 |
+
|
99 |
+
return "<br>".join(results)
|
100 |
except Exception as e:
|
101 |
return f"An error occurred: {str(e)}"
|
102 |
|
|
|
217 |
|
218 |
with gr.Tabs():
|
219 |
with gr.TabItem("Upload PDB File"):
|
220 |
+
gr.Markdown("### Upload your PDB files:")
|
221 |
+
pdb_files = gr.File(label="Upload PDB Files", file_count="multiple")
|
222 |
predict_button = gr.Button("Predict Stability")
|
223 |
prediction_output = gr.HTML(
|
224 |
label="Stability Prediction"
|
|
|
226 |
|
227 |
predict_button.click(
|
228 |
fn=predict_stability_with_pdb,
|
229 |
+
inputs=[model_choice, organism_choice, pdb_files],
|
230 |
outputs=prediction_output,
|
231 |
)
|
232 |
|