Instructions to use Delower/kidney-arch5-ensemble-deployment with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use Delower/kidney-arch5-ensemble-deployment with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("Delower/kidney-arch5-ensemble-deployment", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
Download predict.py from Delower/kidney-arch5-ensemble-deployment: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/Delower/kidney-arch5-ensemble-deployment/resolve/main/predict.py
- Command line
-
hf download hf://Delower/kidney-arch5-ensemble-deployment/predict.py
-
curl -L -o predict.py https://huggingface.co/Delower/kidney-arch5-ensemble-deployment/resolve/main/predict.py
3.48 kB
| # predict.py — Kidney Arch5 feature-block ensemble deployment model. | |
| # Predicts the probability of the kidney-toxic (nephrotoxicity-concern) | |
| # class for one molecule. | |
| # | |
| # Usage: | |
| # python3 predict.py "CCO" -> prints the probability | |
| # from predict import predict; predict("CCO") | |
| # from predict import predict_many; predict_many([smi1, smi2]) | |
| # | |
| # Requires: scikit-learn, lightgbm, rdkit, joblib, numpy, torch, | |
| # transformers, and deployment_common.py (shipped in this repo) on the | |
| # import path — the shipped preprocessing objects (DescPrep) unpickle | |
| # against that module. Component C uses ChemBERTa-77M embeddings | |
| # (seyonec/ChemBERTa-zinc-base-v1, downloaded from the HF hub on first | |
| # use; pretrained encoder only, never fine-tuned). | |
| # | |
| # Featurization is the deployment pipeline's own code: featurize / | |
| # prep_matrix from deployment_common.py, embed() copied verbatim from | |
| # the deployment prep_features.py (progress print removed; computation | |
| # identical), block_X copied verbatim from train_arch5_deployment.py. | |
| # Prediction replicates screen_leads.py's arch5_prob exactly. | |
| import os, sys | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, HERE) | |
| import numpy as np | |
| import joblib | |
| from deployment_common import featurize, prep_matrix | |
| _bundle = joblib.load(os.path.join(HERE, "model.joblib")) | |
| _components, _weights = _bundle["components"], _bundle["weights"] | |
| _prep_desc, _prep_phys = _bundle["prep_desc"], _bundle["prep_phys"] | |
| def embed(smis, batch=64): | |
| """ChemBERTa-77M mean-pooled embeddings — the arm's own code, verbatim | |
| (kidney_arch5_ensemble.py, pretrained encoder, never fine-tuned).""" | |
| import torch | |
| from transformers import AutoTokenizer, AutoModel | |
| tok = AutoTokenizer.from_pretrained("seyonec/ChemBERTa-zinc-base-v1") | |
| mdl = AutoModel.from_pretrained("seyonec/ChemBERTa-zinc-base-v1") | |
| mdl.eval() | |
| smis = [s if isinstance(s, str) else "" for s in smis] | |
| E = np.zeros((len(smis), mdl.config.hidden_size), dtype=np.float32) | |
| with torch.no_grad(): | |
| for i in range(0, len(smis), batch): | |
| chunk = smis[i:i + batch] | |
| enc = tok(chunk, padding=True, truncation=True, max_length=256, return_tensors="pt") | |
| h = mdl(**enc).last_hidden_state | |
| m = enc["attention_mask"].unsqueeze(-1).float() | |
| E[i:i + len(chunk)] = ((h * m).sum(1) / m.sum(1)).numpy() | |
| return E | |
| def block_X(blocks, which, emb, prep_desc, prep_phys): | |
| # verbatim from train_arch5_deployment.py | |
| if which == "A": | |
| return blocks["morgan_bits"] | |
| if which == "B": | |
| return prep_matrix(blocks, prep_desc, "desc_maccs_counts") | |
| if which == "C": | |
| return np.hstack([emb, prep_phys.transform(blocks["physchem"])]).astype(np.float32) | |
| raise ValueError(which) | |
| def predict_many(smiles_list): | |
| """Probabilities of the toxic class; None where unparseable.""" | |
| smiles_list = list(smiles_list) | |
| blocks, valid = featurize(smiles_list) | |
| emb = embed(smiles_list) | |
| p = None | |
| for which in ["A", "B", "C"]: | |
| X = block_X(blocks, which, emb, _prep_desc, _prep_phys) | |
| pw = _weights[which] * _components[which].predict_proba(X)[:, 1] | |
| p = pw if p is None else p + pw | |
| return [float(v) if ok else None for v, ok in zip(p, valid)] | |
| def predict(smiles): | |
| """Probability of the toxic class; None if unparseable.""" | |
| return predict_many([smiles])[0] | |
| if __name__ == "__main__": | |
| print(predict(sys.argv[1])) | |