Then run the following on your protein sequence to predict post translational modification sites:
python
1from transformers import AutoModelForTokenClassification, AutoTokenizer
2from peft import PeftModel
3import torch
45# Path to the saved LoRA model6model_path ="AmelieSchreiber/esm2_t6_8M_ptm_lora_500K"7# ESM2 base model8base_model_path ="facebook/esm2_t6_8M_UR50D"910# Load the model11base_model = AutoModelForTokenClassification.from_pretrained(base_model_path)12loaded_model = PeftModel.from_pretrained(base_model, model_path)1314# Ensure the model is in evaluation mode15loaded_model.eval()1617# Load the tokenizer18loaded_tokenizer = AutoTokenizer.from_pretrained(base_model_path)1920# Protein sequence for inference21protein_sequence ="MAVPETRPNHTIYINNLNEKIKKDELKKSLHAIFSRFGQILDILVSRSLKMRGQAFVIFKEVSSATNALRSMQGFPFYDKPMRIQYAKTDSDIIAKMKGT"# Replace with your actual sequence2223# Tokenize the sequence24inputs = loaded_tokenizer(protein_sequence, return_tensors="pt", truncation=True, max_length=1024, padding='max_length')2526# Run the model27with torch.no_grad():28 logits = loaded_model(**inputs).logits
2930# Get predictions31tokens = loaded_tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])# Convert input ids back to tokens32predictions = torch.argmax(logits, dim=2)3334# Define labels35id2label ={360:"No ptm site",371:"ptm site"38}3940# Print the predicted labels for each token41for token, prediction inzip(tokens, predictions[0].numpy()):42if token notin['<pad>','<cls>','<eos>']:43print((token, id2label[prediction]))