"""LOGOS-8B: One-model-fits-all scientific generation Space. LOGOS (Language Of Generative Objects in Science) uses a unified scientific grammar to encode proteins, antibodies, small molecules, chemical reactions, and materials as token sequences. A single autoregressive transformer handles generation and design across all of them. """ import spaces # MUST come before torch / any CUDA-touching import import torch import gradio as gr from transformers import AutoModelForCausalLM, AutoTokenizer MODEL_ID = "LOGOS-Hub/LOGOS-8B" # --------------------------------------------------------------------------- # Model load (module scope, eager .to("cuda") โ€“ ZeroGPU rule) # --------------------------------------------------------------------------- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) model = AutoModelForCausalLM.from_pretrained( MODEL_ID, dtype=torch.bfloat16, attn_implementation="sdpa", ).to("cuda") model.eval() EOS_TOKENS = { "retrosynthesis": "", "material": "", "pocket": "", } # --------------------------------------------------------------------------- # Inference # --------------------------------------------------------------------------- def _format_retrosynthesis(smiles: str) -> str: """Format a product SMILES into the LOGOS retrosynthesis prompt.""" return f"{smiles}" def _format_pocket(protein_seq: str) -> str: """Format a protein sequence into the LOGOS pocket-ID prompt.""" return f"{protein_seq}" def _extract_between(text: str, start_tag: str, end_tag: str) -> str: """Extract the first substring between start_tag and end_tag.""" s = text.find(start_tag) if s == -1: return text s += len(start_tag) e = text.find(end_tag, s) if e == -1: return text[s:] return text[s:e].strip() @spaces.GPU(duration=120) def generate_retrosynthesis( product_smiles: str, temperature: float = 1.2, top_p: float = 0.85, repetition_penalty: float = 1.05, max_new_tokens: int = 300, ) -> str: """Predict reactants given a target product SMILES. Args: product_smiles: A SMILES string for the target product. temperature: Sampling temperature (higher = more diverse). top_p: Nucleus sampling probability threshold. repetition_penalty: Penalty for repeated tokens. max_new_tokens: Maximum number of new tokens to generate. """ prompt = _format_retrosynthesis(product_smiles) inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=True).to("cuda") with torch.no_grad(): output_ids = model.generate( **inputs, max_new_tokens=max_new_tokens, do_sample=True, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty, pad_token_id=tokenizer.pad_token_id, eos_token_id=tokenizer.convert_tokens_to_ids(EOS_TOKENS["retrosynthesis"]), ) generated = tokenizer.decode(output_ids[0], skip_special_tokens=False) # Extract the generated reactant molecules result = _extract_between(generated, "", "") # Clean up: extract individual ... blocks import re molecules = re.findall(r"(.*?)", generated, re.DOTALL) if molecules: # Skip the first one (the input product), show the rest as reactants reactants = [m.strip() for m in molecules[1:] if m.strip()] if reactants: return " + ".join(reactants) return result.strip() if result.strip() else generated @spaces.GPU(duration=120) def generate_material( temperature: float = 1.2, top_p: float = 0.85, repetition_penalty: float = 1.05, max_new_tokens: int = 512, ) -> str: """Generate a novel material structure unconditionally. Args: temperature: Sampling temperature (higher = more diverse). top_p: Nucleus sampling probability threshold. repetition_penalty: Penalty for repeated tokens. max_new_tokens: Maximum number of new tokens to generate. """ prompt = "" inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=True).to("cuda") with torch.no_grad(): output_ids = model.generate( **inputs, max_new_tokens=max_new_tokens, do_sample=True, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty, pad_token_id=tokenizer.pad_token_id, eos_token_id=tokenizer.convert_tokens_to_ids(EOS_TOKENS["material"]), ) generated = tokenizer.decode(output_ids[0], skip_special_tokens=False) result = _extract_between(generated, "", "") return result.strip() if result.strip() else generated @spaces.GPU(duration=120) def generate_pocket( protein_sequence: str, temperature: float = 1.2, top_p: float = 0.85, repetition_penalty: float = 1.05, max_new_tokens: int = 512, ) -> str: """Identify protein ligand-binding sites from a protein sequence. Args: protein_sequence: An amino-acid sequence (single-letter code). temperature: Sampling temperature (higher = more diverse). top_p: Nucleus sampling probability threshold. repetition_penalty: Penalty for repeated tokens. max_new_tokens: Maximum number of new tokens to generate. """ prompt = _format_pocket(protein_sequence) inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=True).to("cuda") with torch.no_grad(): output_ids = model.generate( **inputs, max_new_tokens=max_new_tokens, do_sample=True, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty, pad_token_id=tokenizer.pad_token_id, eos_token_id=tokenizer.convert_tokens_to_ids(EOS_TOKENS["pocket"]), ) generated = tokenizer.decode(output_ids[0], skip_special_tokens=False) # The second ... block is the predicted pocket import re proteins = re.findall(r"(.*?)", generated, re.DOTALL) if len(proteins) >= 2: return proteins[1].strip() return generated.strip() # --------------------------------------------------------------------------- # UI # --------------------------------------------------------------------------- CSS = """ #col-container { max-width: 1100px; margin: 0 auto; } .dark .gradio-container { color: var(--body-text-color); } """ HEADER = """ # LOGOS-8B: Scientific Object Generator **LOGOS** (Language Of Generative Objects in Science) is a multi-domain generative model that encodes proteins, molecules, materials, and reactions into a unified *scientific grammar*. A single 8B-parameter autoregressive transformer handles generation across all these domains. ๐Ÿ“„ [Paper](https://arxiv.org/abs/2606.16905) ยท ๐Ÿค— [Model](https://huggingface.co/LOGOS-Hub/LOGOS-8B) ยท ๐Ÿ’ป [GitHub](https://github.com/LOGOS-Hub/LOGOS) """ with gr.Blocks(css=CSS) as demo: gr.Markdown(HEADER) with gr.Tabs(): # ------------------------------------------------------------------ # Tab 1: Retrosynthesis # ------------------------------------------------------------------ with gr.Tab("Retrosynthesis"): gr.Markdown( "Predict **reactants** given a target product SMILES. " "Enter a SMILES string for the product molecule, and the model " "will generate candidate reactant molecules." ) with gr.Row(): retro_input = gr.Textbox( label="Product SMILES", placeholder="e.g. CCOC(=O)c1ccccc1N", scale=4, ) retro_btn = gr.Button("Generate Reactants", variant="primary", scale=1) retro_output = gr.Textbox( label="Predicted Reactants", lines=4, interactive=False, ) with gr.Accordion("Advanced settings", open=False): retro_temp = gr.Slider(0.1, 2.0, value=1.2, step=0.05, label="Temperature") retro_top_p = gr.Slider(0.1, 1.0, value=0.85, step=0.05, label="Top-p") retro_rep = gr.Slider(1.0, 2.0, value=1.05, step=0.01, label="Repetition penalty") retro_max_tokens = gr.Slider(64, 1024, value=300, step=64, label="Max new tokens") retro_btn.click( fn=generate_retrosynthesis, inputs=[retro_input, retro_temp, retro_top_p, retro_rep, retro_max_tokens], outputs=retro_output, api_name="retrosynthesis", ) gr.Examples( examples=[ ["CCOC(=O)c1ccccc1N"], ["O=C(O)c1ccccc1O"], ["CC(=O)Nc1ccc(O)cc1"], ], inputs=[retro_input], outputs=retro_output, fn=generate_retrosynthesis, cache_examples=True, cache_mode="lazy", ) # ------------------------------------------------------------------ # Tab 2: Material Generation # ------------------------------------------------------------------ with gr.Tab("Material Generation"): gr.Markdown( "Generate **novel material structures** unconditionally. " "No input is needed โ€” the model generates a material from the " "`` start token." ) mat_btn = gr.Button("Generate Material", variant="primary") mat_output = gr.Textbox( label="Generated Material", lines=4, interactive=False, ) with gr.Accordion("Advanced settings", open=False): mat_temp = gr.Slider(0.1, 2.0, value=1.2, step=0.05, label="Temperature") mat_top_p = gr.Slider(0.1, 1.0, value=0.85, step=0.05, label="Top-p") mat_rep = gr.Slider(1.0, 2.0, value=1.05, step=0.01, label="Repetition penalty") mat_max_tokens = gr.Slider(64, 2048, value=512, step=64, label="Max new tokens") mat_btn.click( fn=generate_material, inputs=[mat_temp, mat_top_p, mat_rep, mat_max_tokens], outputs=mat_output, api_name="material_generation", ) gr.Examples( examples=[ [1.2, 0.85, 1.05, 512], [1.0, 0.90, 1.05, 256], [1.4, 0.80, 1.10, 1024], ], inputs=[mat_temp, mat_top_p, mat_rep, mat_max_tokens], outputs=mat_output, fn=generate_material, cache_examples=True, cache_mode="lazy", ) # ------------------------------------------------------------------ # Tab 3: Protein Pocket Prediction # ------------------------------------------------------------------ with gr.Tab("Pocket Identification"): gr.Markdown( "Identify **ligand-binding sites** from a protein sequence. " "Enter an amino-acid sequence (single-letter code), and the model " "will predict the binding pocket region." ) with gr.Row(): pocket_input = gr.Textbox( label="Protein Sequence (single-letter amino acids)", placeholder="e.g. MKTAYIAKQRQISFVKSHF", lines=3, scale=4, ) pocket_btn = gr.Button("Predict Pocket", variant="primary", scale=1) pocket_output = gr.Textbox( label="Predicted Pocket Region", lines=3, interactive=False, ) with gr.Accordion("Advanced settings", open=False): pocket_temp = gr.Slider(0.1, 2.0, value=1.2, step=0.05, label="Temperature") pocket_top_p = gr.Slider(0.1, 1.0, value=0.85, step=0.05, label="Top-p") pocket_rep = gr.Slider(1.0, 2.0, value=1.05, step=0.01, label="Repetition penalty") pocket_max_tokens = gr.Slider(64, 2048, value=512, step=64, label="Max new tokens") pocket_btn.click( fn=generate_pocket, inputs=[pocket_input, pocket_temp, pocket_top_p, pocket_rep, pocket_max_tokens], outputs=pocket_output, api_name="pocket_identification", ) gr.Examples( examples=[ ["MKTAYIAKQRQISFVKSHFSRQLEERLGLIEVQAPILSRVGDGTQD"], ["MVLSEGEWQLVLHVWAKVEGAGSIKPVLQKVNGDVVAVSGEH"], ], inputs=[pocket_input], outputs=pocket_output, fn=generate_pocket, cache_examples=True, cache_mode="lazy", ) demo.launch(mcp_server=True, theme=gr.themes.Citrus())