as-cle-bert commited on
Commit
c5d6bef
1 Parent(s): 9a1bde7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, EsmForProteinFolding
2
+ from transformers.models.esm.openfold_utils.protein import to_pdb, Protein as OFProtein
3
+ from transformers.models.esm.openfold_utils.feats import atom14_to_atom37
4
+ from proteins_viz import *
5
+ import gradio as gr
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
+ tokenizer = AutoTokenizer.from_pretrained("facebook/esmfold_v1")
30
+ model = EsmForProteinFolding.from_pretrained("facebook/esmfold_v1", low_cpu_mem_usage=True)
31
+
32
+ model = model.cuda()
33
+
34
+ model.esm = model.esm.half()
35
+
36
+ import torch
37
+
38
+ torch.backends.cuda.matmul.allow_tf32 = True
39
+
40
+ model.trunk.set_chunk_size(64)
41
+
42
+ def fold_protein(test_protein):
43
+ tokenized_input = tokenizer([test_protein], return_tensors="pt", add_special_tokens=False)['input_ids']
44
+ tokenized_input = tokenized_input.cuda()
45
+ with torch.no_grad():
46
+ output = model(tokenized_input)
47
+ pdb = convert_outputs_to_pdb(output)
48
+ with open("output_structure.pdb", "w") as f:
49
+ f.write("".join(pdb))
50
+ image = take_care("output_structure.pdb")
51
+ return image
52
+
53
+ iface = gr.Interface(
54
+ title="everything-ai-proteinfold",
55
+ fn=fold_protein,
56
+ inputs="text",
57
+ outputs="image",
58
+ )
59
+
60
+ iface.launch(server_name="0.0.0.0", share=False)