as-cle-bert commited on
Commit
884b6ae
1 Parent(s): 1468add

Create apps.py

Browse files
Files changed (1) hide show
  1. apps.py +106 -0
apps.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import login
2
+ from esm.models.esm3 import ESM3
3
+ from esm.sdk.api import ESM3InferenceClient, ESMProtein, GenerationConfig
4
+ import spaces
5
+ import os
6
+ from gradio_molecule3d import Molecule3D
7
+
8
+
9
+ # This will prompt you to get an API key from huggingface hub, make one with
10
+ # "Read" or "Write" permission and copy it back here.
11
+ TOKEN = os.getenv("HF_TOKEN")
12
+
13
+ login(TOKEN)
14
+
15
+ # This will download the model weights and instantiate the model on your machine.
16
+ model: ESM3InferenceClient = ESM3.from_pretrained("esm3_sm_open_v1").to("cuda")
17
+
18
+ def molecule(input_pdb):
19
+ mol = read_mol(input_pdb)
20
+
21
+ x = (
22
+ """<!DOCTYPE html>
23
+ <html>
24
+ <head>
25
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
26
+ <style>
27
+ body{
28
+ font-family:sans-serif
29
+ }
30
+ .mol-container {
31
+ width: 100%;
32
+ height: 600px;
33
+ position: relative;
34
+ }
35
+ .mol-container select{
36
+ background-image:None;
37
+ }
38
+ </style>
39
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
40
+ <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
41
+ </head>
42
+ <body>
43
+ <div id="container" class="mol-container"></div>
44
+
45
+ <script>
46
+ let pdb = `"""
47
+ + mol
48
+ + """`
49
+
50
+ $(document).ready(function () {
51
+ let element = $("#container");
52
+ let config = { backgroundColor: "white" };
53
+ let viewer = $3Dmol.createViewer(element, config);
54
+ viewer.addModel(pdb, "pdb");
55
+ viewer.getModel(0).setStyle({}, { cartoon: { colorscheme:"whiteCarbon" } });
56
+ viewer.zoomTo();
57
+ viewer.render();
58
+ viewer.zoom(0.8, 2000);
59
+ })
60
+ </script>
61
+ </body></html>"""
62
+ )
63
+
64
+ return f"""<iframe style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
65
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
66
+ allow-scripts allow-same-origin allow-popups
67
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
68
+ allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
69
+
70
+ @spaces.GPU(duration=300)
71
+ def prediction(prompt, temperature, do_structure, enable_roundtrip):
72
+ protein = ESMProtein(sequence=prompt)
73
+ protein = model.generate(protein, GenerationConfig(track="sequence", num_steps=8, temperature=temperature))
74
+ if do_structure == "Yes":
75
+ protein = model.generate(protein, GenerationConfig(track="structure", num_steps=8))
76
+ protein.to_pdb("./generation.pdb")
77
+ html = molecule("./generation.pdb")
78
+ if enable_roundtrip == "Yes":
79
+ seq = protein.sequence
80
+ protein.sequence = None
81
+ protein = model.generate(protein, GenerationConfig(track="sequence", num_steps=8))
82
+ protein.coordinates = None
83
+ protein = model.generate(protein, GenerationConfig(track="structure", num_steps=8))
84
+ protein.to_pdb("./round_tripped.pdb")
85
+ html1 = molecule("./round_tripped.pdb")
86
+ return seq, protein.sequemce, html, html1, "./generation.pdb", "./round_tripped.pdb"
87
+ else:
88
+ html1 = "<h3>Inverse folding and re-generation not enabled</h3>"
89
+ f = open("./round_tripped.pdb", "w")
90
+ f.write("\n")
91
+ f.close()
92
+ return protein.sequence, "Inverse folding and re-generation not enabled", html, html1, "./generation.pdb", "./round_tripped.pdb"
93
+ else:
94
+ f = open("./empty.pdb", "w")
95
+ f.write("\n")
96
+ f.close()
97
+ return protein.sequence, "Inverse folding and re-generation not enabled", "<h3>Structure reconstruction not enabled</h3>", "<h3>Inverse folding and re-generation not enabled</h3>", "./empty.pdb", "./empty.pdb"
98
+
99
+
100
+ demo = gr.Interface(fn = prediction, inputs = [gr.Textbox(label="Masked protein sequence", info="Use '_' as masking character", value="___________________________________________________DQATSLRILNNGHAFNVEFDDSQDKAVLKGGPLDGTYRLIQFHFHWGSLDGQGSEHTVDKKKYAAELHLVHWNTKYGDFGKAVQQPDGLAVLGIFLKVGSAKPGLQKVVDVLDSIKTKGKSADFTNFDPRGLLPESLDYWTYPGSLTTPP___________________________________________________________"), gr.Slider(0,1,label="Temperature"), gr.Radio(["Yes", "No"], label="Reconstruct structure", info="Choose wheter to reconstruct structure or not"), gr.Radio(["Yes", "No"], label="Allow inverse-folding", info="Choose wether to allow double check of prediction with inverse folding (ONLY when 'Reconstruct structure' is set to 'Yes')")], outputs = [gr.Textbox(label="Originally predicted sequence"),gr.Textbox(label="Inverse folding predicted sequence"),gr.HTML(label="Predicted 3D structure"),gr.HTML(label="Inverse-folding predicted 3D structure"), Molecule3D("Predicted molecular structure"), Molecule3D(label="Inverse-folding predicted molecular structure")], title="""<h1 align='center'>Proteins with ESM</h1>
101
+ <h2 align='center'>Predict the whole sequence and 3D structure of masked protein sequences!</h2>
102
+ <h3 align='center'>Support this space with a ⭐ on <a href='https://github.com/AstraBert/proteins-w-esm'>GitHub</a></h3>
103
+ <h3 align='center'>Support Evolutionary Scale's ESM with a ⭐ on <a href='https://github.com/evolutionaryscale/esm'>GitHub</a></h3>""",
104
+ examples = [["___________________________________________________DQATSLRILNNGHAFNVEFDDSQDKAVLKGGPLDGTYRLIQFHFHWGSLDGQGSEHTVDKKKYAAELHLVHWNTKYGDFGKAVQQPDGLAVLGIFLKVGSAKPGLQKVVDVLDSIKTKGKSADFTNFDPRGLLPESLDYWTYPGSLTTPP___________________________________________________________", 0.7, "No", "No"], ["__________________________________________________________AGQEEYSAMRDQYMRTGEGFLCVFAINNTKSFEDIHQYREQIKRVKDSDDVPMVLVGNKCDLAARTVESRQAQDLARSYGIPYIETSAKTRQGVEDAFYTLVRE___________________________", 0.2, "Yes", "No"], ["__________KTITLEVEPSDTIENVKAKIQDKEGIPPDQQRLIFAGKQLEDGRTLSDYNIQKESTLH________", 0.5, "Yes", "Yes"]], cache_examples=False)
105
+
106
+ demo.launch()