Views
No views yet
H5AD_PATH and GENE_VOCAB_PATH appropriately:H5AD_PATH: Path to your .h5ad single-cell data file (e.g., H5AD_PATH = "path/to/your/data.h5ad").GENE_VOCAB_PATH: Path to your gene vocabulary file (e.g., GENE_VOCAB_PATH = "path/to/your/gene_vocab.npy").1from mmllm.module import InstructCell
2import anndata
3import numpy as np
4from utils import unify_gene_features
5# Load the pre-trained InstructCell model from HuggingFace
6model = InstructCell.from_pretrained("zjunlp/InstructCell-chat")
7# Load the single-cell data (H5AD format) and gene vocabulary file (numpy format)
8adata = anndata.read_h5ad(H5AD_PATH)
9gene_vocab = np.load(GENE_VOCAB_PATH)
10adata = unify_gene_features(adata, gene_vocab, force_gene_symbol_uppercase=False)
11# Select a random single-cell sample and extract its gene counts and metadata
12k = np.random.randint(0, len(adata))
13gene_counts = adata[k, :].X.toarray()
14sc_metadata = adata[k, :].obs.iloc[0].to_dict()
15# Define the model prompt with placeholders for metadata and gene expression profile
16prompt = (
17 "Can you help me annotate this single cell from a {species}? "
18 "It was sequenced using {sequencing_method} and is derived from {tissue}. "
19 "The gene expression profile is {input}. Thanks!"
20)
21# Use the model to generate predictions
22for key, value in model.predict(
23 prompt,
24 gene_counts=gene_counts,
25 sc_metadata=sc_metadata,
26 do_sample=True,
27 top_p=0.95,
28 top_k=50,
29 max_new_tokens=256,
30).items():
31 # Print each key-value pair
32 print(f"{key}: {value}")1@article{fang2025instructcell,
2 title={A Multi-Modal AI Copilot for Single-Cell Analysis with Instruction Following},
3 author={Fang, Yin and Deng, Xinle and Liu, Kangwei and Zhang, Ningyu and Qian, Jingyang and Yang, Penghui and Fan, Xiaohui and Chen, Huajun},
4 journal={arXiv preprint arXiv:2501.08187},
5 year={2025}
6}