Spaces:
Sleeping
Sleeping
wwydmanski
commited on
Commit
•
7b093ca
1
Parent(s):
9582cf7
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, EsmForProteinFolding
|
3 |
+
from transformers.models.esm.openfold_utils.protein import to_pdb, Protein as OFProtein
|
4 |
+
from transformers.models.esm.openfold_utils.feats import atom14_to_atom37
|
5 |
+
import torch
|
6 |
+
|
7 |
+
def convert_outputs_to_pdb(outputs):
|
8 |
+
final_atom_positions = atom14_to_atom37(outputs["positions"][-1], outputs)
|
9 |
+
outputs = {k: v.to("cpu").numpy() for k, v in outputs.items()}
|
10 |
+
final_atom_positions = final_atom_positions.cpu().numpy()
|
11 |
+
final_atom_mask = outputs["atom37_atom_exists"]
|
12 |
+
pdbs = []
|
13 |
+
for i in range(outputs["aatype"].shape[0]):
|
14 |
+
aa = outputs["aatype"][i]
|
15 |
+
pred_pos = final_atom_positions[i]
|
16 |
+
mask = final_atom_mask[i]
|
17 |
+
resid = outputs["residue_index"][i] + 1
|
18 |
+
pred = OFProtein(
|
19 |
+
aatype=aa,
|
20 |
+
atom_positions=pred_pos,
|
21 |
+
atom_mask=mask,
|
22 |
+
residue_index=resid,
|
23 |
+
b_factors=outputs["plddt"][i],
|
24 |
+
chain_index=outputs["chain_index"][i] if "chain_index" in outputs else None,
|
25 |
+
)
|
26 |
+
pdbs.append(to_pdb(pred))
|
27 |
+
return pdbs
|
28 |
+
|
29 |
+
def fold_prot_locally(sequence):
|
30 |
+
tokenized_input = tokenizer([sequence], return_tensors="pt", add_special_tokens=False)['input_ids']
|
31 |
+
|
32 |
+
with torch.no_grad():
|
33 |
+
output = model(tokenized_input)
|
34 |
+
pdb = convert_outputs_to_pdb(output)
|
35 |
+
return pdb
|
36 |
+
|
37 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/esmfold_v1")
|
38 |
+
model = EsmForProteinFolding.from_pretrained("facebook/esmfold_v1", low_cpu_mem_usage=True)
|
39 |
+
|
40 |
+
iface = gr.Interface(fn=fold_prot_locally, inputs="text", outputs="text")
|
41 |
+
iface.launch()
|