MolGen
Collection
5 items β’ Updated β’ 2
How to use zjunlp/MolGen-7b with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="zjunlp/MolGen-7b") # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("zjunlp/MolGen-7b")
model = AutoModelForCausalLM.from_pretrained("zjunlp/MolGen-7b")How to use zjunlp/MolGen-7b with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "zjunlp/MolGen-7b"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "zjunlp/MolGen-7b",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker model run hf.co/zjunlp/MolGen-7b
How to use zjunlp/MolGen-7b with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "zjunlp/MolGen-7b" \
--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": "zjunlp/MolGen-7b",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'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 "zjunlp/MolGen-7b" \
--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": "zjunlp/MolGen-7b",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'How to use zjunlp/MolGen-7b with Docker Model Runner:
docker model run hf.co/zjunlp/MolGen-7b
This repo contains a large molecular generative model built with molecular language SELFIES.
You can use the model to generate molecules from scratch (i.e., inputting the bos_token), or input a partial structure for the model to complete.
We have provided two types of examples. You can modify the input, generation parameters, etc., according to your needs.
>>> from transformers import AutoTokenizer, LlamaForCausalLM
>>> import torch
>>> tokenizer = AutoTokenizer.from_pretrained("zjunlp/MolGen-7b")
>>> model = LlamaForCausalLM.from_pretrained(
"zjunlp/MolGen-7b",
load_in_8bit=True,
torch_dtype=torch.float16,
device_map="auto",
)
>>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
>>> sf_input = tokenizer(tokenizer.bos_token, return_tensors="pt").to(device)
>>> molecules = model.generate(input_ids=sf_input["input_ids"],
attention_mask=sf_input["attention_mask"],
do_sample=True,
max_new_tokens=10,
top_p=0.75,
top_k=30,
return_dict_in_generate=False,
num_return_sequences=5,
)
>>> sf_output = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=True).replace(" ","") for g in molecules]
['[C][C][=C][C][=C][Branch2][Ring1][=Branch2][C][=Branch1]',
'[C][N][C][C][C][Branch2][Ring2][Ring2][N][C]',
'[C][O][C][=C][C][=C][C][Branch2][Ring1][Branch1]',
'[C][N][C][C][C@H1][Branch2][Ring1][Branch2][N][Branch1]',
'[C][=C][C][Branch2][Ring1][#C][C][=Branch1][C][=O]']
>>> from transformers import AutoTokenizer, LlamaForCausalLM
>>> import torch
>>> tokenizer = AutoTokenizer.from_pretrained("zjunlp/MolGen-7b")
>>> model = LlamaForCausalLM.from_pretrained(
"zjunlp/MolGen-7b",
load_in_8bit=True,
torch_dtype=torch.float16,
device_map="auto",
)
>>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
>>> sf_input = tokenizer("[C][N][O]", return_tensors="pt").to(device)
>>> molecules = model.generate(input_ids=sf_input["input_ids"],
attention_mask=sf_input["attention_mask"],
do_sample=True,
max_new_tokens=10,
top_p=0.75,
top_k=30,
return_dict_in_generate=False,
num_return_sequences=5,
)
>>> sf_output = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=True).replace(" ","") for g in molecules]
['[C][N][O][C][=Branch1][C][=O][/C][Ring1][=Branch1][=C][/C][=C]',
'[C][N][O][/C][=Branch1][#Branch1][=C][/N][Branch1][C][C][C][C]',
'[C][N][O][/C][=C][/C][=C][C][=Branch1][C][=O][C][=C]',
'[C][N][O][C][=Branch1][C][=O][N][Branch1][C][C][C][=Branch1]',
'[C][N][O][Ring1][Branch1][C][C][C][C][C][C][C][C]']
If you use our repository, please cite:
@inproceedings{fang2023domain,
author = {Yin Fang and
Ningyu Zhang and
Zhuo Chen and
Xiaohui Fan and
Huajun Chen},
title = {Domain-Agnostic Molecular Generation with Chemical Feedback},
booktitle = {{ICLR}},
publisher = {OpenReview.net},
year = {2024},
url = {https://openreview.net/pdf?id=9rPyHyjfwP}
}