Spaces:
Running
on
Zero
Running
on
Zero
fix: fix showing of examples and transformer decoding method
Browse files
app.py
CHANGED
@@ -48,10 +48,10 @@ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
48 |
FP16 = DEVICE == "cuda"
|
49 |
|
50 |
# --- Global Variables (Load Models and Knapsack Once) ---
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
KNAPSACK: Knapsack | None = None
|
56 |
RESIDUE_SET: ResidueSet | None = None
|
57 |
|
@@ -64,27 +64,25 @@ if not temp_dir.exists():
|
|
64 |
temp_dir.mkdir()
|
65 |
|
66 |
# Logging configuration
|
|
|
|
|
67 |
log_file = "/tmp/instanovo_gradio_log.txt"
|
68 |
Path(log_file).touch()
|
69 |
|
70 |
-
logger = logging.getLogger("
|
71 |
logger.setLevel(logging.INFO)
|
72 |
if not logger.handlers:
|
73 |
file_handler = logging.FileHandler(log_file)
|
74 |
file_handler.setLevel(logging.INFO)
|
75 |
-
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
|
76 |
-
file_handler.setFormatter(formatter)
|
77 |
-
logger.addHandler(file_handler)
|
78 |
stream_handler = logging.StreamHandler()
|
79 |
stream_handler.setLevel(logging.INFO)
|
80 |
-
stream_handler.setFormatter(formatter)
|
81 |
logger.addHandler(stream_handler)
|
82 |
|
83 |
|
84 |
def load_models_and_knapsack():
|
85 |
"""Loads the InstaNovo models and generates/loads the knapsack."""
|
86 |
-
global
|
87 |
-
models_loaded =
|
88 |
if models_loaded:
|
89 |
logger.info("Models already loaded.")
|
90 |
# Still check knapsack if not loaded
|
@@ -94,13 +92,13 @@ def load_models_and_knapsack():
|
|
94 |
return # All loaded
|
95 |
|
96 |
# --- Load Transformer Model ---
|
97 |
-
if
|
98 |
logger.info(f"Loading InstaNovo (Transformer) model: {TRANSFORMER_MODEL_ID} to {DEVICE}...")
|
99 |
try:
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
RESIDUE_SET =
|
104 |
logger.info("Transformer model loaded successfully.")
|
105 |
except Exception as e:
|
106 |
logger.error(f"Error loading Transformer model: {e}")
|
@@ -110,29 +108,29 @@ def load_models_and_knapsack():
|
|
110 |
|
111 |
|
112 |
# --- Load Diffusion Model ---
|
113 |
-
if
|
114 |
logger.info(f"Loading InstaNovo+ (Diffusion) model: {DIFFUSION_MODEL_ID} to {DEVICE}...")
|
115 |
try:
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
if RESIDUE_SET is not None and
|
120 |
-
logger.warning("Residue sets between Transformer and Diffusion models
|
121 |
elif RESIDUE_SET is None:
|
122 |
-
RESIDUE_SET =
|
123 |
|
124 |
logger.info("Diffusion model loaded successfully.")
|
125 |
except Exception as e:
|
126 |
logger.error(f"Error loading Diffusion model: {e}")
|
127 |
gr.Warning(f"Failed to load InstaNovo+ Diffusion model ({DIFFUSION_MODEL_ID}): {e}. Diffusion modes will be unavailable.")
|
128 |
-
|
129 |
else:
|
130 |
logger.info("Diffusion model already loaded.")
|
131 |
|
132 |
|
133 |
# --- Knapsack Handling ---
|
134 |
# Only attempt knapsack loading/generation if the Transformer model is loaded
|
135 |
-
if
|
136 |
knapsack_exists = (
|
137 |
(KNAPSACK_DIR / "parameters.pkl").exists()
|
138 |
and (KNAPSACK_DIR / "masses.npy").exists()
|
@@ -187,7 +185,7 @@ def load_models_and_knapsack():
|
|
187 |
KNAPSACK = None
|
188 |
elif KNAPSACK is not None:
|
189 |
logger.info("Knapsack already loaded.")
|
190 |
-
elif
|
191 |
logger.warning("Transformer model not loaded, skipping Knapsack loading/generation.")
|
192 |
|
193 |
|
@@ -248,8 +246,8 @@ def create_inference_config(
|
|
248 |
|
249 |
def _get_transformer_decoder(selection: str, config: DictConfig) -> tuple[Decoder, int, bool]:
|
250 |
"""Helper to instantiate the correct transformer decoder based on selection."""
|
251 |
-
global
|
252 |
-
if
|
253 |
raise gr.Error("InstaNovo Transformer model not loaded.")
|
254 |
|
255 |
num_beams = 1
|
@@ -258,7 +256,7 @@ def _get_transformer_decoder(selection: str, config: DictConfig) -> tuple[Decode
|
|
258 |
|
259 |
if "Greedy" in selection:
|
260 |
decoder = GreedyDecoder(
|
261 |
-
model=
|
262 |
mass_scale=MASS_SCALE,
|
263 |
suppressed_residues=config.get("suppressed_residues", None),
|
264 |
disable_terminal_residues_anywhere=config.get("disable_terminal_residues_anywhere", True),
|
@@ -266,7 +264,7 @@ def _get_transformer_decoder(selection: str, config: DictConfig) -> tuple[Decode
|
|
266 |
elif "Knapsack" in selection:
|
267 |
if KNAPSACK is None:
|
268 |
raise gr.Error("Knapsack is not available. Cannot use Knapsack Beam Search.")
|
269 |
-
decoder = KnapsackBeamSearchDecoder(model=
|
270 |
num_beams = 5 # Default beam size for knapsack
|
271 |
use_knapsack = True
|
272 |
else:
|
@@ -312,11 +310,11 @@ def run_transformer_prediction(dl, config, transformer_decoder_selection):
|
|
312 |
|
313 |
def run_diffusion_prediction(dl, config):
|
314 |
"""Runs prediction using only the diffusion model."""
|
315 |
-
global
|
316 |
-
if
|
317 |
raise gr.Error("InstaNovo+ Diffusion model not loaded.")
|
318 |
|
319 |
-
diffusion_decoder = DiffusionDecoder(model=
|
320 |
logger.info(f"Using decoder: {type(diffusion_decoder).__name__}")
|
321 |
|
322 |
results_sequences = []
|
@@ -370,9 +368,9 @@ def run_diffusion_prediction(dl, config):
|
|
370 |
|
371 |
def run_refinement_prediction(dl, config, transformer_decoder_selection):
|
372 |
"""Runs transformer prediction followed by diffusion refinement."""
|
373 |
-
global
|
374 |
-
if
|
375 |
-
missing = [m for m, v in [("Transformer",
|
376 |
raise gr.Error(f"Cannot run refinement: {', '.join(missing)} model not loaded.")
|
377 |
|
378 |
# 1. Run Transformer Prediction (using selected decoder)
|
@@ -408,7 +406,7 @@ def run_refinement_prediction(dl, config, transformer_decoder_selection):
|
|
408 |
# 2. Prepare Transformer Predictions as Initial Sequences for Diffusion
|
409 |
logger.info("Encoding transformer predictions for diffusion input...")
|
410 |
encoded_transformer_preds = []
|
411 |
-
max_len_diffusion =
|
412 |
|
413 |
for res in transformer_results_list:
|
414 |
if isinstance(res, ScoredSequence) and res.sequence:
|
@@ -438,7 +436,7 @@ def run_refinement_prediction(dl, config, transformer_decoder_selection):
|
|
438 |
|
439 |
# 3. Run Diffusion Refinement
|
440 |
logger.info("Running Diffusion refinement...")
|
441 |
-
diffusion_decoder = DiffusionDecoder(model=
|
442 |
refined_sequences = []
|
443 |
refined_log_probs = []
|
444 |
start_time_diffusion = time.time()
|
@@ -505,13 +503,13 @@ def predict_peptides(input_file, mode_selection, transformer_decoder_selection):
|
|
505 |
Main function to load data, select mode, run prediction, and return results.
|
506 |
"""
|
507 |
# Ensure models are loaded
|
508 |
-
if
|
509 |
load_models_and_knapsack() # Try reload
|
510 |
-
if
|
511 |
raise gr.Error("InstaNovo Transformer model failed to load. Cannot perform prediction.")
|
512 |
-
if ("
|
513 |
load_models_and_knapsack() # Try reload diffusion
|
514 |
-
if
|
515 |
raise gr.Error("InstaNovo+ Diffusion model failed to load. Cannot perform Refinement or InstaNovo+ Only prediction.")
|
516 |
if "Knapsack" in transformer_decoder_selection and KNAPSACK is None:
|
517 |
load_models_and_knapsack() # Try reload knapsack
|
@@ -523,7 +521,7 @@ def predict_peptides(input_file, mode_selection, transformer_decoder_selection):
|
|
523 |
raise gr.Error("Please upload a mass spectrometry file.")
|
524 |
|
525 |
input_path = input_file.name
|
526 |
-
logger.info(
|
527 |
logger.info(f"Input File: {input_path}")
|
528 |
logger.info(f"Selected Mode: {mode_selection}")
|
529 |
if "Refinement" in mode_selection or "InstaNovo Only" in mode_selection:
|
@@ -577,7 +575,7 @@ def predict_peptides(input_file, mode_selection, transformer_decoder_selection):
|
|
577 |
reverse_for_transformer = "InstaNovo+ Only" not in mode_selection
|
578 |
ds = SpectrumDataset(
|
579 |
sdf, RESIDUE_SET,
|
580 |
-
|
581 |
return_str=True, annotated=False,
|
582 |
pad_spectrum_max_length=config.get("compile_model", False) or config.get("use_flash_attention", False),
|
583 |
bin_spectra=config.get("conv_peak_encoder", False),
|
@@ -723,26 +721,29 @@ with gr.Blocks(
|
|
723 |
input_file = gr.File(
|
724 |
label="Upload Mass Spectrometry File (.mgf, .mzml, .mzxml)",
|
725 |
file_types=[".mgf", ".mzml", ".mzxml"],
|
|
|
726 |
)
|
727 |
mode_selection = gr.Radio(
|
728 |
[
|
729 |
-
"InstaNovo +
|
730 |
"InstaNovo Only (Transformer)",
|
731 |
-
"InstaNovo+ Only (Diffusion, Alpha)",
|
732 |
],
|
733 |
label="Prediction Mode",
|
734 |
-
value="InstaNovo +
|
|
|
735 |
)
|
736 |
# Transformer decoder selection - visible for relevant modes
|
737 |
transformer_decoder_selection = gr.Radio(
|
738 |
[
|
739 |
"Greedy Search (Fast)",
|
740 |
-
|
741 |
],
|
742 |
label="Transformer Decoding Method",
|
743 |
value="Greedy Search (Fast)",
|
744 |
visible=True, # Start visible as default mode uses it
|
745 |
interactive=True,
|
|
|
746 |
)
|
747 |
|
748 |
submit_btn = gr.Button("Predict Sequences", variant="primary")
|
@@ -751,14 +752,7 @@ with gr.Blocks(
|
|
751 |
def update_transformer_options(mode):
|
752 |
# Show decoder selection if mode uses the transformer
|
753 |
show_decoder = "InstaNovo+ Only" not in mode
|
754 |
-
|
755 |
-
knapsack_available = KNAPSACK is not None
|
756 |
-
choices = ["Greedy Search (Fast)"]
|
757 |
-
if knapsack_available:
|
758 |
-
choices.append("Knapsack Beam Search (Accurate, Slower)")
|
759 |
-
else:
|
760 |
-
logger.info("Knapsack check: Not available, disabling Knapsack Beam Search option.")
|
761 |
-
# Reset to Greedy if Knapsack was selected but becomes unavailable
|
762 |
current_value = "Greedy Search (Fast)" # Default reset value
|
763 |
return gr.update(visible=show_decoder, choices=choices, value=current_value)
|
764 |
|
@@ -767,14 +761,6 @@ with gr.Blocks(
|
|
767 |
inputs=mode_selection,
|
768 |
outputs=transformer_decoder_selection,
|
769 |
)
|
770 |
-
# Initial check in case knapsack fails on startup
|
771 |
-
# This requires JS or a different approach in Gradio.
|
772 |
-
# For simplicity, we rely on the check during prediction.
|
773 |
-
# We can set initial choices based on load status here though.
|
774 |
-
initial_choices = ["Greedy Search (Fast)"]
|
775 |
-
if KNAPSACK is not None:
|
776 |
-
initial_choices.append("Knapsack Beam Search (Accurate, Slower)")
|
777 |
-
transformer_decoder_selection.choices = initial_choices
|
778 |
|
779 |
|
780 |
with gr.Column(scale=2):
|
@@ -792,22 +778,22 @@ with gr.Blocks(
|
|
792 |
|
793 |
gr.Examples(
|
794 |
[
|
795 |
-
["assets/sample_spectra.mgf", "InstaNovo +
|
796 |
-
["assets/sample_spectra.mgf", "InstaNovo +
|
797 |
["assets/sample_spectra.mgf", "InstaNovo Only (Transformer)", "Greedy Search (Fast)"],
|
798 |
["assets/sample_spectra.mgf", "InstaNovo Only (Transformer)", "Knapsack Beam Search (Accurate, Slower)"],
|
799 |
-
["assets/sample_spectra.mgf", "InstaNovo+ Only (Diffusion, Alpha)", "
|
800 |
],
|
801 |
inputs=[input_file, mode_selection, transformer_decoder_selection],
|
802 |
# outputs=[output_df, output_file],
|
803 |
cache_examples=False,
|
804 |
-
label="Example Usage
|
805 |
)
|
806 |
|
807 |
gr.Markdown(
|
808 |
f"""
|
809 |
**Notes:**
|
810 |
-
* Predictions use `{TRANSFORMER_MODEL_ID}` (Transformer) and `{DIFFUSION_MODEL_ID}` (Diffusion, Alpha).
|
811 |
* **Refinement Mode:** Runs initial prediction with the selected Transformer method (Greedy/Knapsack), then refines using InstaNovo+.
|
812 |
* **InstaNovo Only Mode:** Uses only the Transformer with the selected decoding method.
|
813 |
* **InstaNovo+ Only Mode:** Predicts directly using the Diffusion model (alpha version).
|
@@ -818,7 +804,7 @@ with gr.Blocks(
|
|
818 |
elem_classes="feedback"
|
819 |
)
|
820 |
|
821 |
-
with gr.Accordion("Application Logs", open=
|
822 |
log_display = Log(log_file, dark=True, height=300)
|
823 |
|
824 |
gr.Markdown(
|
@@ -847,9 +833,11 @@ If you use InstaNovo in your research, please cite:
|
|
847 |
|
848 |
# --- Launch the App ---
|
849 |
if __name__ == "__main__":
|
|
|
|
|
850 |
# Set share=True for temporary public link if running locally
|
851 |
# Set server_name="0.0.0.0" to allow access from network if needed
|
852 |
# demo.launch(server_name="0.0.0.0", server_port=7860)
|
853 |
# For Hugging Face Spaces, just demo.launch() is usually sufficient
|
854 |
-
demo.launch()
|
855 |
# demo.launch(share=True) # For local testing with public URL
|
|
|
48 |
FP16 = DEVICE == "cuda"
|
49 |
|
50 |
# --- Global Variables (Load Models and Knapsack Once) ---
|
51 |
+
INSTANOVO: InstaNovo | None = None
|
52 |
+
INSTANOVO_CONFIG: DictConfig | None = None
|
53 |
+
INSTANOVOPLUS: InstaNovoPlus | None = None
|
54 |
+
INSTANOVOPLUS_CONFIG: DictConfig | None = None
|
55 |
KNAPSACK: Knapsack | None = None
|
56 |
RESIDUE_SET: ResidueSet | None = None
|
57 |
|
|
|
64 |
temp_dir.mkdir()
|
65 |
|
66 |
# Logging configuration
|
67 |
+
# TODO: create logfile per user/session
|
68 |
+
# see https://www.gradio.app/guides/resource-cleanup
|
69 |
log_file = "/tmp/instanovo_gradio_log.txt"
|
70 |
Path(log_file).touch()
|
71 |
|
72 |
+
logger = logging.getLogger("instanovo")
|
73 |
logger.setLevel(logging.INFO)
|
74 |
if not logger.handlers:
|
75 |
file_handler = logging.FileHandler(log_file)
|
76 |
file_handler.setLevel(logging.INFO)
|
|
|
|
|
|
|
77 |
stream_handler = logging.StreamHandler()
|
78 |
stream_handler.setLevel(logging.INFO)
|
|
|
79 |
logger.addHandler(stream_handler)
|
80 |
|
81 |
|
82 |
def load_models_and_knapsack():
|
83 |
"""Loads the InstaNovo models and generates/loads the knapsack."""
|
84 |
+
global INSTANOVO, KNAPSACK, INSTANOVO_CONFIG, RESIDUE_SET, INSTANOVOPLUS, INSTANOVOPLUS_CONFIG
|
85 |
+
models_loaded = INSTANOVO is not None and INSTANOVOPLUS is not None
|
86 |
if models_loaded:
|
87 |
logger.info("Models already loaded.")
|
88 |
# Still check knapsack if not loaded
|
|
|
92 |
return # All loaded
|
93 |
|
94 |
# --- Load Transformer Model ---
|
95 |
+
if INSTANOVO is None:
|
96 |
logger.info(f"Loading InstaNovo (Transformer) model: {TRANSFORMER_MODEL_ID} to {DEVICE}...")
|
97 |
try:
|
98 |
+
INSTANOVO, INSTANOVO_CONFIG = InstaNovo.from_pretrained(TRANSFORMER_MODEL_ID)
|
99 |
+
INSTANOVO.to(DEVICE)
|
100 |
+
INSTANOVO.eval()
|
101 |
+
RESIDUE_SET = INSTANOVO.residue_set
|
102 |
logger.info("Transformer model loaded successfully.")
|
103 |
except Exception as e:
|
104 |
logger.error(f"Error loading Transformer model: {e}")
|
|
|
108 |
|
109 |
|
110 |
# --- Load Diffusion Model ---
|
111 |
+
if INSTANOVOPLUS is None:
|
112 |
logger.info(f"Loading InstaNovo+ (Diffusion) model: {DIFFUSION_MODEL_ID} to {DEVICE}...")
|
113 |
try:
|
114 |
+
INSTANOVOPLUS, INSTANOVOPLUS_CONFIG = InstaNovoPlus.from_pretrained(DIFFUSION_MODEL_ID)
|
115 |
+
INSTANOVOPLUS.to(DEVICE)
|
116 |
+
INSTANOVOPLUS.eval()
|
117 |
+
if RESIDUE_SET is not None and INSTANOVOPLUS.residues != RESIDUE_SET:
|
118 |
+
logger.warning("Residue sets between Transformer and Diffusion models differ. Using Transformer's set.")
|
119 |
elif RESIDUE_SET is None:
|
120 |
+
RESIDUE_SET = INSTANOVOPLUS.residues
|
121 |
|
122 |
logger.info("Diffusion model loaded successfully.")
|
123 |
except Exception as e:
|
124 |
logger.error(f"Error loading Diffusion model: {e}")
|
125 |
gr.Warning(f"Failed to load InstaNovo+ Diffusion model ({DIFFUSION_MODEL_ID}): {e}. Diffusion modes will be unavailable.")
|
126 |
+
INSTANOVOPLUS = None
|
127 |
else:
|
128 |
logger.info("Diffusion model already loaded.")
|
129 |
|
130 |
|
131 |
# --- Knapsack Handling ---
|
132 |
# Only attempt knapsack loading/generation if the Transformer model is loaded
|
133 |
+
if INSTANOVO is not None and RESIDUE_SET is not None and KNAPSACK is None:
|
134 |
knapsack_exists = (
|
135 |
(KNAPSACK_DIR / "parameters.pkl").exists()
|
136 |
and (KNAPSACK_DIR / "masses.npy").exists()
|
|
|
185 |
KNAPSACK = None
|
186 |
elif KNAPSACK is not None:
|
187 |
logger.info("Knapsack already loaded.")
|
188 |
+
elif INSTANOVO is None:
|
189 |
logger.warning("Transformer model not loaded, skipping Knapsack loading/generation.")
|
190 |
|
191 |
|
|
|
246 |
|
247 |
def _get_transformer_decoder(selection: str, config: DictConfig) -> tuple[Decoder, int, bool]:
|
248 |
"""Helper to instantiate the correct transformer decoder based on selection."""
|
249 |
+
global INSTANOVO, KNAPSACK
|
250 |
+
if INSTANOVO is None:
|
251 |
raise gr.Error("InstaNovo Transformer model not loaded.")
|
252 |
|
253 |
num_beams = 1
|
|
|
256 |
|
257 |
if "Greedy" in selection:
|
258 |
decoder = GreedyDecoder(
|
259 |
+
model=INSTANOVO,
|
260 |
mass_scale=MASS_SCALE,
|
261 |
suppressed_residues=config.get("suppressed_residues", None),
|
262 |
disable_terminal_residues_anywhere=config.get("disable_terminal_residues_anywhere", True),
|
|
|
264 |
elif "Knapsack" in selection:
|
265 |
if KNAPSACK is None:
|
266 |
raise gr.Error("Knapsack is not available. Cannot use Knapsack Beam Search.")
|
267 |
+
decoder = KnapsackBeamSearchDecoder(model=INSTANOVO, knapsack=KNAPSACK)
|
268 |
num_beams = 5 # Default beam size for knapsack
|
269 |
use_knapsack = True
|
270 |
else:
|
|
|
310 |
|
311 |
def run_diffusion_prediction(dl, config):
|
312 |
"""Runs prediction using only the diffusion model."""
|
313 |
+
global INSTANOVOPLUS, RESIDUE_SET
|
314 |
+
if INSTANOVOPLUS is None or RESIDUE_SET is None:
|
315 |
raise gr.Error("InstaNovo+ Diffusion model not loaded.")
|
316 |
|
317 |
+
diffusion_decoder = DiffusionDecoder(model=INSTANOVOPLUS)
|
318 |
logger.info(f"Using decoder: {type(diffusion_decoder).__name__}")
|
319 |
|
320 |
results_sequences = []
|
|
|
368 |
|
369 |
def run_refinement_prediction(dl, config, transformer_decoder_selection):
|
370 |
"""Runs transformer prediction followed by diffusion refinement."""
|
371 |
+
global INSTANOVO, INSTANOVOPLUS, RESIDUE_SET, INSTANOVOPLUS_CONFIG
|
372 |
+
if INSTANOVO is None or INSTANOVOPLUS is None or RESIDUE_SET is None or INSTANOVOPLUS_CONFIG is None:
|
373 |
+
missing = [m for m, v in [("Transformer", INSTANOVO), ("Diffusion", INSTANOVOPLUS)] if v is None]
|
374 |
raise gr.Error(f"Cannot run refinement: {', '.join(missing)} model not loaded.")
|
375 |
|
376 |
# 1. Run Transformer Prediction (using selected decoder)
|
|
|
406 |
# 2. Prepare Transformer Predictions as Initial Sequences for Diffusion
|
407 |
logger.info("Encoding transformer predictions for diffusion input...")
|
408 |
encoded_transformer_preds = []
|
409 |
+
max_len_diffusion = INSTANOVOPLUS_CONFIG.get("max_length", 40)
|
410 |
|
411 |
for res in transformer_results_list:
|
412 |
if isinstance(res, ScoredSequence) and res.sequence:
|
|
|
436 |
|
437 |
# 3. Run Diffusion Refinement
|
438 |
logger.info("Running Diffusion refinement...")
|
439 |
+
diffusion_decoder = DiffusionDecoder(model=INSTANOVOPLUS)
|
440 |
refined_sequences = []
|
441 |
refined_log_probs = []
|
442 |
start_time_diffusion = time.time()
|
|
|
503 |
Main function to load data, select mode, run prediction, and return results.
|
504 |
"""
|
505 |
# Ensure models are loaded
|
506 |
+
if INSTANOVO is None or RESIDUE_SET is None:
|
507 |
load_models_and_knapsack() # Try reload
|
508 |
+
if INSTANOVO is None:
|
509 |
raise gr.Error("InstaNovo Transformer model failed to load. Cannot perform prediction.")
|
510 |
+
if ("refinement" in mode_selection or "InstaNovo+" in mode_selection) and INSTANOVOPLUS is None:
|
511 |
load_models_and_knapsack() # Try reload diffusion
|
512 |
+
if INSTANOVOPLUS is None:
|
513 |
raise gr.Error("InstaNovo+ Diffusion model failed to load. Cannot perform Refinement or InstaNovo+ Only prediction.")
|
514 |
if "Knapsack" in transformer_decoder_selection and KNAPSACK is None:
|
515 |
load_models_and_knapsack() # Try reload knapsack
|
|
|
521 |
raise gr.Error("Please upload a mass spectrometry file.")
|
522 |
|
523 |
input_path = input_file.name
|
524 |
+
logger.info("--- New Prediction Request ---")
|
525 |
logger.info(f"Input File: {input_path}")
|
526 |
logger.info(f"Selected Mode: {mode_selection}")
|
527 |
if "Refinement" in mode_selection or "InstaNovo Only" in mode_selection:
|
|
|
575 |
reverse_for_transformer = "InstaNovo+ Only" not in mode_selection
|
576 |
ds = SpectrumDataset(
|
577 |
sdf, RESIDUE_SET,
|
578 |
+
INSTANOVO_CONFIG.get("n_peaks", 200) if INSTANOVO_CONFIG else 200,
|
579 |
return_str=True, annotated=False,
|
580 |
pad_spectrum_max_length=config.get("compile_model", False) or config.get("use_flash_attention", False),
|
581 |
bin_spectra=config.get("conv_peak_encoder", False),
|
|
|
721 |
input_file = gr.File(
|
722 |
label="Upload Mass Spectrometry File (.mgf, .mzml, .mzxml)",
|
723 |
file_types=[".mgf", ".mzml", ".mzxml"],
|
724 |
+
scale=1
|
725 |
)
|
726 |
mode_selection = gr.Radio(
|
727 |
[
|
728 |
+
"InstaNovo with InstaNovo+ refinement (Default, Recommended)",
|
729 |
"InstaNovo Only (Transformer)",
|
730 |
+
"InstaNovo+ Only (Diffusion, Alpha release)",
|
731 |
],
|
732 |
label="Prediction Mode",
|
733 |
+
value="InstaNovo with InstaNovo+ refinement (Default, Recommended)",
|
734 |
+
scale=1
|
735 |
)
|
736 |
# Transformer decoder selection - visible for relevant modes
|
737 |
transformer_decoder_selection = gr.Radio(
|
738 |
[
|
739 |
"Greedy Search (Fast)",
|
740 |
+
"Knapsack Beam Search (Accurate, Slower)"
|
741 |
],
|
742 |
label="Transformer Decoding Method",
|
743 |
value="Greedy Search (Fast)",
|
744 |
visible=True, # Start visible as default mode uses it
|
745 |
interactive=True,
|
746 |
+
scale=1
|
747 |
)
|
748 |
|
749 |
submit_btn = gr.Button("Predict Sequences", variant="primary")
|
|
|
752 |
def update_transformer_options(mode):
|
753 |
# Show decoder selection if mode uses the transformer
|
754 |
show_decoder = "InstaNovo+ Only" not in mode
|
755 |
+
choices = ["Greedy Search (Fast)", "Knapsack Beam Search (Accurate, Slower)"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
756 |
current_value = "Greedy Search (Fast)" # Default reset value
|
757 |
return gr.update(visible=show_decoder, choices=choices, value=current_value)
|
758 |
|
|
|
761 |
inputs=mode_selection,
|
762 |
outputs=transformer_decoder_selection,
|
763 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
764 |
|
765 |
|
766 |
with gr.Column(scale=2):
|
|
|
778 |
|
779 |
gr.Examples(
|
780 |
[
|
781 |
+
["assets/sample_spectra.mgf", "InstaNovo with InstaNovo+ refinement (Default, Recommended)", "Greedy Search (Fast)"],
|
782 |
+
["assets/sample_spectra.mgf", "InstaNovo with InstaNovo+ refinement (Default, Recommended)", "Knapsack Beam Search (Accurate, Slower)"],
|
783 |
["assets/sample_spectra.mgf", "InstaNovo Only (Transformer)", "Greedy Search (Fast)"],
|
784 |
["assets/sample_spectra.mgf", "InstaNovo Only (Transformer)", "Knapsack Beam Search (Accurate, Slower)"],
|
785 |
+
["assets/sample_spectra.mgf", "InstaNovo+ Only (Diffusion, Alpha release)", ""],
|
786 |
],
|
787 |
inputs=[input_file, mode_selection, transformer_decoder_selection],
|
788 |
# outputs=[output_df, output_file],
|
789 |
cache_examples=False,
|
790 |
+
label="Example Usage:",
|
791 |
)
|
792 |
|
793 |
gr.Markdown(
|
794 |
f"""
|
795 |
**Notes:**
|
796 |
+
* Predictions use `{TRANSFORMER_MODEL_ID}` (Transformer) and `{DIFFUSION_MODEL_ID}` (Diffusion, Alpha release).
|
797 |
* **Refinement Mode:** Runs initial prediction with the selected Transformer method (Greedy/Knapsack), then refines using InstaNovo+.
|
798 |
* **InstaNovo Only Mode:** Uses only the Transformer with the selected decoding method.
|
799 |
* **InstaNovo+ Only Mode:** Predicts directly using the Diffusion model (alpha version).
|
|
|
804 |
elem_classes="feedback"
|
805 |
)
|
806 |
|
807 |
+
with gr.Accordion("Application Logs", open=True):
|
808 |
log_display = Log(log_file, dark=True, height=300)
|
809 |
|
810 |
gr.Markdown(
|
|
|
833 |
|
834 |
# --- Launch the App ---
|
835 |
if __name__ == "__main__":
|
836 |
+
# https://www.gradio.app/guides/setting-up-a-demo-for-maximum-performance
|
837 |
+
demo.queue(default_concurrency_limit=5)
|
838 |
# Set share=True for temporary public link if running locally
|
839 |
# Set server_name="0.0.0.0" to allow access from network if needed
|
840 |
# demo.launch(server_name="0.0.0.0", server_port=7860)
|
841 |
# For Hugging Face Spaces, just demo.launch() is usually sufficient
|
842 |
+
demo.launch(debug=True, show_error=True)
|
843 |
# demo.launch(share=True) # For local testing with public URL
|