---
title: MolGen-7b
canonical_url: "https://www.modelscope.cn/models/AI-ModelScope/MolGen-7b"
md_url: "https://www.modelscope.cn/models/AI-ModelScope/MolGen-7b.md"
repository: AI-ModelScope/MolGen-7b
last_updated: 2024-03-12
license: apache-2.0
pipeline_tag: text-generation
tasks:
  - text-generation
model_type:
  - llama
architectures:
  - LlamaForCausalLM
library_name:
  - pytorch
frameworks:
  - pytorch
inference_backends:
  - "deploy_task text/emb"
  - "lmdeploy 0.9.1"
  - "lmdeploy_turbomind 0.9.1"
  - "sglang 0.5.2"
  - "vllm 0.9.2"
downloads: 59
stars: 0
tags:
  - chemistry
  - biology
  - text-generation-inference
---

# MolGen-7b

> MolGen-7b - AI-ModelScope 在 ModelScope 开源的模型。💡 Model description This repo contains a large molecular generative model built with molecular language SELFIES.

AI-ModelScope/MolGen-7b 是 ModelScope 魔搭社区上的text-generation模型，采用 apache-2.0 许可，可用 deploy_task text/emb、lmdeploy 0.9.1、lmdeploy_turbomind 0.9.1 部署。

- **Repository**: AI-ModelScope/MolGen-7b
- **License**: apache-2.0
- **Tasks**: text-generation
- **Inference backends**: deploy_task text/emb, lmdeploy 0.9.1, lmdeploy_turbomind 0.9.1, sglang 0.5.2, vllm 0.9.2
- **Tags**: chemistry, biology, text-generation-inference
- **Downloads**: 59
- **Stars**: 0
- **Last updated**: 2024-03-12

Source: https://www.modelscope.cn/models/AI-ModelScope/MolGen-7b

---

## 💡 Model description
This repo contains a large molecular generative model built with molecular language SELFIES.

## 🔍 Intended uses
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.

## 🛠️ How to use
We have provided two types of examples. You can modify the input, generation parameters, etc., according to your needs.

- Denovo molecule generation example:
```python
from modelscope import AutoTokenizer, AutoModelForCausalLM
import torch

tokenizer = AutoTokenizer.from_pretrained("AI-ModelScope/MolGen-7b")
model = AutoModelForCausalLM.from_pretrained(
                              "AI-ModelScope/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]']
```

- Molecular completion example:
```python
from modelscope import AutoTokenizer, AutoModelForCausalLM
import torch

tokenizer = AutoTokenizer.from_pretrained("AI-ModelScope/MolGen-7b")
model = AutoModelForCausalLM.from_pretrained(
                              "AI-ModelScope/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]']
```

## 📚 Citation
If you use our repository, please cite:
```bibtex
@article{fang2023molecular,
  title={Molecular Language Model as Multi-task Generator},
  author={Fang, Yin and Zhang, Ningyu and Chen, Zhuo and Fan, Xiaohui and Chen, Huajun},
  journal={arXiv preprint arXiv:2301.11259},
  year={2023}
}
```
