Instructions to use naniltx/codonGPT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use naniltx/codonGPT with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="naniltx/codonGPT")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("naniltx/codonGPT") model = AutoModelForCausalLM.from_pretrained("naniltx/codonGPT", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use naniltx/codonGPT with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "naniltx/codonGPT" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "naniltx/codonGPT", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/naniltx/codonGPT
- SGLang
How to use naniltx/codonGPT with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "naniltx/codonGPT" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "naniltx/codonGPT", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "naniltx/codonGPT" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "naniltx/codonGPT", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use naniltx/codonGPT with Docker Model Runner:
docker model run hf.co/naniltx/codonGPT
Download synonymous_logit_processor.py from naniltx/codonGPT: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/naniltx/codonGPT/resolve/refs%2Fpr%2F3/synonymous_logit_processor.py
- Command line
-
hf download hf://naniltx/codonGPT@refs/pr/3/synonymous_logit_processor.py
-
curl -L -o synonymous_logit_processor.py https://huggingface.co/naniltx/codonGPT/resolve/refs%2Fpr%2F3/synonymous_logit_processor.py
3.7 kB
| import torch | |
| from transformers import LogitsProcessor | |
| from Bio.Seq import Seq | |
| # Complete amino acid to codon mapping (human genetic code) | |
| aa_to_codon_human = { | |
| 'F': ['TTT', 'TTC'], 'L': ['TTA', 'TTG', 'CTT', 'CTC', 'CTA', 'CTG'], | |
| 'S': ['TCT', 'TCC', 'TCA', 'TCG', 'AGT', 'AGC'], 'Y': ['TAT', 'TAC'], | |
| 'C': ['TGT', 'TGC'], 'W': ['TGG'], 'P': ['CCT', 'CCC', 'CCA', 'CCG'], | |
| 'H': ['CAT', 'CAC'], 'Q': ['CAA', 'CAG'], 'R': ['CGT', 'CGC', 'CGA', 'CGG', 'AGA', 'AGG'], | |
| 'I': ['ATT', 'ATC', 'ATA'], 'M': ['ATG'], 'T': ['ACT', 'ACC', 'ACA', 'ACG'], | |
| 'N': ['AAT', 'AAC'], 'K': ['AAA', 'AAG'], 'V': ['GTT', 'GTC', 'GTA', 'GTG'], | |
| 'A': ['GCT', 'GCC', 'GCA', 'GCG'], 'D': ['GAT', 'GAC'], 'E': ['GAA', 'GAG'], | |
| 'G': ['GGT', 'GGC', 'GGA', 'GGG'], '*': ['TAA', 'TAG', 'TGA'] | |
| } | |
| class SynonymMaskingLogitsProcessor(LogitsProcessor): | |
| def __init__(self, current_aa, tokenizer, aa_to_codon=None): | |
| self.current_aa = current_aa | |
| self.tokenizer = tokenizer | |
| self.aa_to_codon = aa_to_codon if aa_to_codon is not None else aa_to_codon_human | |
| def __call__(self, input_ids: torch.Tensor, scores: torch.Tensor) -> torch.Tensor: | |
| synonymous_codons = self.aa_to_codon.get(self.current_aa, []) | |
| synonym_token_ids = self.tokenizer.convert_tokens_to_ids(synonymous_codons) | |
| mask = torch.ones_like(scores) * -float('inf') | |
| mask[:, synonym_token_ids] = 0 | |
| return scores + mask | |
| def generate_candidate_codons_with_generate(initial_codons, temperature=1.0, top_k=None, top_p=None, aa_to_codon=None, model=None, tokenizer=None): | |
| """ | |
| Generate synonymous codon alternatives for a given set of codons. | |
| Args: | |
| initial_codons: List of codons to optimize | |
| temperature: Sampling temperature | |
| top_k: Top-k sampling parameter | |
| top_p: Top-p (nucleus) sampling parameter | |
| aa_to_codon: Amino acid to codon mapping (defaults to human genetic code) | |
| model: The CodonGPT model (if None, uses global 'model' variable) | |
| tokenizer: The codon tokenizer (if None, uses global 'tokenizer' variable) | |
| Returns: | |
| List of optimized codons | |
| """ | |
| # Use global variables if not provided as parameters | |
| if model is None: | |
| import builtins | |
| model = getattr(builtins, 'model', globals().get('model')) | |
| if model is None: | |
| raise ValueError("Model not provided and no global 'model' variable found") | |
| if tokenizer is None: | |
| import builtins | |
| tokenizer = getattr(builtins, 'tokenizer', globals().get('tokenizer')) | |
| if tokenizer is None: | |
| raise ValueError("Tokenizer not provided and no global 'tokenizer' variable found") | |
| if aa_to_codon is None: | |
| aa_to_codon = aa_to_codon_human | |
| optimized_codons = [] | |
| current_sequence_tokens = [tokenizer.bos_token_id] | |
| for codon in initial_codons: | |
| aa = str(Seq(codon).translate()) | |
| logits_processor = [SynonymMaskingLogitsProcessor(aa, tokenizer, aa_to_codon)] | |
| input_ids = torch.tensor([current_sequence_tokens]) | |
| output = model.generate( | |
| input_ids, | |
| max_length=len(current_sequence_tokens) + 1, | |
| temperature=temperature, | |
| top_k=top_k, | |
| top_p=top_p, | |
| num_return_sequences=1, | |
| pad_token_id=tokenizer.pad_token_id, | |
| logits_processor=logits_processor, | |
| do_sample=True | |
| ) | |
| next_token_id = output[0][-1].item() | |
| predicted_codon = tokenizer.decode([next_token_id]) | |
| optimized_codons.append(predicted_codon.upper()) | |
| current_sequence_tokens.append(next_token_id) | |
| return optimized_codons |