Spaces:
Running on Zero
Running on Zero
try dynamically building the correct decorator
Browse files- app.py +12 -4
- utils/pipelines.py +13 -90
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from atomworks.io.utils.visualize import view
|
|
| 9 |
from lightning.fabric import seed_everything
|
| 10 |
from rfd3.engine import RFD3InferenceConfig, RFD3InferenceEngine
|
| 11 |
from utils.download_weights import download_weights
|
| 12 |
-
from utils.pipelines import
|
| 13 |
#from gradio_molecule3d import Molecule3D
|
| 14 |
from utils.handle_events import *
|
| 15 |
from utils.handle_files import *
|
|
@@ -101,6 +101,13 @@ with gr.Blocks(title="RFD3 Test") as demo:
|
|
| 101 |
minimum=1,
|
| 102 |
maximum=10
|
| 103 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
with gr.Accordion(label="Advanced Options", open=False):
|
| 106 |
extra_args = gr.Textbox(
|
|
@@ -138,16 +145,17 @@ with gr.Blocks(title="RFD3 Test") as demo:
|
|
| 138 |
display_state = gr.Textbox(label="Selected Batch and Design", visible=True)
|
| 139 |
display_state.value = "Please Select a Batch and Design number to show sequence"
|
| 140 |
|
| 141 |
-
def generate(config_upload, scaffold_upload, num_batches, num_designs_per_batch, extra_args):
|
| 142 |
if config_upload is None:
|
| 143 |
return gr.update(), None, None
|
| 144 |
else:
|
| 145 |
-
|
|
|
|
| 146 |
print(textbox_update)
|
| 147 |
return textbox_update, gen_directory, gen_results
|
| 148 |
|
| 149 |
run_btn.click(give_run_status, inputs=[num_batches, num_designs_per_batch, config_upload, scaffold_upload], outputs=runtextbox).then(
|
| 150 |
-
generate, inputs=[config_upload, scaffold_upload, num_batches, num_designs_per_batch, extra_args], outputs=[runtextbox, gen_directory, gen_results]
|
| 151 |
).then(
|
| 152 |
update_batch_choices,
|
| 153 |
inputs=gen_results,
|
|
|
|
| 9 |
from lightning.fabric import seed_everything
|
| 10 |
from rfd3.engine import RFD3InferenceConfig, RFD3InferenceEngine
|
| 11 |
from utils.download_weights import download_weights
|
| 12 |
+
from utils.pipelines import *
|
| 13 |
#from gradio_molecule3d import Molecule3D
|
| 14 |
from utils.handle_events import *
|
| 15 |
from utils.handle_files import *
|
|
|
|
| 101 |
minimum=1,
|
| 102 |
maximum=10
|
| 103 |
)
|
| 104 |
+
max_duration = gr.Number(
|
| 105 |
+
value=240,
|
| 106 |
+
label="Max Duration (seconds)",
|
| 107 |
+
precision=0,
|
| 108 |
+
minimum=1,
|
| 109 |
+
maximum=7199
|
| 110 |
+
)
|
| 111 |
|
| 112 |
with gr.Accordion(label="Advanced Options", open=False):
|
| 113 |
extra_args = gr.Textbox(
|
|
|
|
| 145 |
display_state = gr.Textbox(label="Selected Batch and Design", visible=True)
|
| 146 |
display_state.value = "Please Select a Batch and Design number to show sequence"
|
| 147 |
|
| 148 |
+
def generate(config_upload, scaffold_upload, num_batches, num_designs_per_batch, extra_args, max_duration):
|
| 149 |
if config_upload is None:
|
| 150 |
return gr.update(), None, None
|
| 151 |
else:
|
| 152 |
+
generate_with_correct_time_limit = generation_with_input_config_factory(max_duration)
|
| 153 |
+
textbox_update, gen_directory, gen_results = generate_with_correct_time_limit(config_upload, scaffold_upload, num_batches, num_designs_per_batch, extra_args)
|
| 154 |
print(textbox_update)
|
| 155 |
return textbox_update, gen_directory, gen_results
|
| 156 |
|
| 157 |
run_btn.click(give_run_status, inputs=[num_batches, num_designs_per_batch, config_upload, scaffold_upload], outputs=runtextbox).then(
|
| 158 |
+
generate, inputs=[config_upload, scaffold_upload, num_batches, num_designs_per_batch, extra_args, max_duration], outputs=[runtextbox, gen_directory, gen_results]
|
| 159 |
).then(
|
| 160 |
update_batch_choices,
|
| 161 |
inputs=gen_results,
|
utils/pipelines.py
CHANGED
|
@@ -12,97 +12,10 @@ import shutil
|
|
| 12 |
from time import perf_counter
|
| 13 |
import random
|
| 14 |
|
| 15 |
-
@spaces.GPU(duration=240)
|
| 16 |
-
def test_rfd3_from_notebook():
|
| 17 |
-
# Set seed for reproducibility
|
| 18 |
-
seed_everything(0)
|
| 19 |
|
| 20 |
-
# Configure RFD3 inference
|
| 21 |
-
config = RFD3InferenceConfig(
|
| 22 |
-
specification={
|
| 23 |
-
'length': 40, # Generate 80-residue proteins
|
| 24 |
-
},
|
| 25 |
-
diffusion_batch_size=2, # Generate 2 structures per batch
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
# Initialize engine and run generation
|
| 29 |
-
try:
|
| 30 |
-
model = RFD3InferenceEngine(**config)
|
| 31 |
-
outputs = model.run(
|
| 32 |
-
inputs=None, # None for unconditional generation
|
| 33 |
-
out_dir=None, # None to return in memory (no file output)
|
| 34 |
-
n_batches=1, # Generate 1 batch
|
| 35 |
-
)
|
| 36 |
-
return_str = "RDF3 test passed! Generated structures:\n"
|
| 37 |
-
|
| 38 |
-
for idx, data in outputs.items():
|
| 39 |
-
return_str += f"Batch {idx}: {len(data)} structure(s)\n"
|
| 40 |
-
for i, struct in enumerate(data):
|
| 41 |
-
return_str += f"Structure {i+1}: {struct.atom_array.array_length()} Atoms\n"
|
| 42 |
-
#return_str += struct.atom_array
|
| 43 |
-
return return_str
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return f"Error: {str(e)}"
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
@spaces.GPU(duration=240)
|
| 50 |
-
def unconditional_generation(num_batches, num_designs_per_batch, length):
|
| 51 |
-
"""
|
| 52 |
-
Runs an unconditional generation with the specified parameters for number of batches, number of designs per batch, and length of the generated proteins. Saves the generated structures to a timestamped directory in the outputs folder and returns the path to the directory along with a list of the generated structures' file paths.
|
| 53 |
-
|
| 54 |
-
Parameters:
|
| 55 |
-
----------
|
| 56 |
-
num_batches: int or gr.Number,
|
| 57 |
-
The number of batches to generate.
|
| 58 |
-
num_designs_per_batch: int or gr.Number,
|
| 59 |
-
The number of designs to generate per batch.
|
| 60 |
-
length: int or gr.Number,
|
| 61 |
-
The length of the generated proteins.
|
| 62 |
-
|
| 63 |
-
Returns:
|
| 64 |
-
-------
|
| 65 |
-
directory: str,
|
| 66 |
-
The path to the directory where the generated structures are saved.
|
| 67 |
-
results: list of dicts,
|
| 68 |
-
A list of the generated structures' file paths, where each dict contains batch number "batch", design number "design", path to cif file "cif_path", and path to pdb file "pdb_path".
|
| 69 |
-
"""
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
config = RFD3InferenceConfig(
|
| 73 |
-
specification={
|
| 74 |
-
'length': length,
|
| 75 |
-
},
|
| 76 |
-
diffusion_batch_size=num_designs_per_batch, # Generate 2 structures per batch
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
session_hash = gr.Request().session_hash
|
| 80 |
-
time_stamp = time.strftime("%Y-%m-%d-%H-%M-%S")
|
| 81 |
-
directory = f"./outputs/unconditional_generation/session_{session_hash}_{time_stamp}"
|
| 82 |
-
os.makedirs(directory, exist_ok=False)
|
| 83 |
-
|
| 84 |
-
try:
|
| 85 |
-
model = RFD3InferenceEngine(**config)
|
| 86 |
-
outputs = model.run(
|
| 87 |
-
inputs=None, # None for unconditional generation
|
| 88 |
-
out_dir=directory, # None to return in memory (no file output)
|
| 89 |
-
n_batches=num_batches, # Generate 1 batch
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
results = []
|
| 93 |
-
for batch in range(num_batches):
|
| 94 |
-
for design in range(num_designs_per_batch):
|
| 95 |
-
file_name = os.path.join(directory, f"_{batch}_model_{design}.cif.gz")
|
| 96 |
-
results.append({"batch": batch, "design": design, "cif_path": file_name, "pdb_path": mcif_gz_to_pdb(file_name)})
|
| 97 |
-
|
| 98 |
-
print(results)
|
| 99 |
-
return directory, results
|
| 100 |
-
|
| 101 |
-
except Exception as e:
|
| 102 |
-
raise RuntimeError(f"Error during generation: {str(e)}")
|
| 103 |
-
|
| 104 |
-
@spaces.GPU(duration=6120)
|
| 105 |
-
def generation_with_input_config(input_file, pdb_file, num_batches, num_designs_per_batch, extra_args):
|
| 106 |
"""
|
| 107 |
Runs an unconditional generation with the specified input config file. Saves the generated structures to a timestamped directory in the outputs folder and returns the path to the directory along with a list of the generated structures' file paths.
|
| 108 |
|
|
@@ -166,4 +79,14 @@ def generation_with_input_config(input_file, pdb_file, num_batches, num_designs_
|
|
| 166 |
|
| 167 |
except subprocess.CalledProcessError as e:
|
| 168 |
print("subprocess threw an error", e.stderr)
|
| 169 |
-
return gr.update(value=f"Generation failed:\n{e.stderr}"), None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from time import perf_counter
|
| 13 |
import random
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
#@spaces.GPU(duration=6120)
|
| 18 |
+
def generation_with_input_config_impl(input_file, pdb_file, num_batches, num_designs_per_batch, extra_args):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
Runs an unconditional generation with the specified input config file. Saves the generated structures to a timestamped directory in the outputs folder and returns the path to the directory along with a list of the generated structures' file paths.
|
| 21 |
|
|
|
|
| 79 |
|
| 80 |
except subprocess.CalledProcessError as e:
|
| 81 |
print("subprocess threw an error", e.stderr)
|
| 82 |
+
return gr.update(value=f"Generation failed:\n{e.stderr}"), None, None
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def generation_with_input_config_factory(max_duration):
|
| 86 |
+
|
| 87 |
+
@spaces.GPU(duration=max_duration)
|
| 88 |
+
def generation_with_correct_time_limit(input_file, pdb_file, num_batches, num_designs_per_batch, extra_args):
|
| 89 |
+
return generation_with_input_config_impl(input_file, pdb_file, num_batches, num_designs_per_batch, extra_args)
|
| 90 |
+
|
| 91 |
+
return generation_with_correct_time_limit
|
| 92 |
+
|