Instructions to use Stable-string/gpt2-zh-novel-ancient with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Stable-string/gpt2-zh-novel-ancient with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Stable-string/gpt2-zh-novel-ancient")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("Stable-string/gpt2-zh-novel-ancient") model = AutoModelForCausalLM.from_pretrained("Stable-string/gpt2-zh-novel-ancient", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Stable-string/gpt2-zh-novel-ancient with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Stable-string/gpt2-zh-novel-ancient" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Stable-string/gpt2-zh-novel-ancient", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Stable-string/gpt2-zh-novel-ancient
- SGLang
How to use Stable-string/gpt2-zh-novel-ancient with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Stable-string/gpt2-zh-novel-ancient" \ --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": "Stable-string/gpt2-zh-novel-ancient", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
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 "Stable-string/gpt2-zh-novel-ancient" \ --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": "Stable-string/gpt2-zh-novel-ancient", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Stable-string/gpt2-zh-novel-ancient with Docker Model Runner:
docker model run hf.co/Stable-string/gpt2-zh-novel-ancient
Commit ·
a28dcd7
1
Parent(s): f22f79f
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,106 +1,215 @@
|
|
| 1 |
这是一个基于cpm训练的模型,你可以使用这个模型来续写文章,文章大体接近于古代🤗。
|
| 2 |
-
您可以将模型保存到./mdoel,使用下面的
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
model =
|
| 70 |
-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小一些🤓。
|
| 105 |
运行上面的例子,还需要一些环境👽。
|
| 106 |
在cmd输入以下命令:
|
|
|
|
| 1 |
这是一个基于cpm训练的模型,你可以使用这个模型来续写文章,文章大体接近于古代🤗。
|
| 2 |
+
您可以将模型保存到./mdoel,使用下面的两个示例中的一个来运行这个模型🥳:
|
| 3 |
+
<details>
|
| 4 |
+
<summary>示例1</summary>
|
| 5 |
+
```python
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
from transformers import GPT2LMHeadModel, CpmTokenizer
|
| 9 |
+
|
| 10 |
+
def generate_next_token(input_ids):
|
| 11 |
+
input_ids = input_ids[:, -context_len:]
|
| 12 |
+
outputs = model(input_ids=input_ids)
|
| 13 |
+
logits = outputs.logits
|
| 14 |
+
next_token_logits = logits[0, -1, :]
|
| 15 |
+
next_token_logits = next_token_logits / temperature
|
| 16 |
+
next_token_logits[unk_id] = -float('Inf')
|
| 17 |
+
filtered_logits = top_k_top_p_filtering(next_token_logits, top_k=topk, top_p=topp)
|
| 18 |
+
next_token_id = torch.multinomial(F.softmax(filtered_logits, dim=-1), num_samples=1)
|
| 19 |
+
return next_token_id
|
| 20 |
+
|
| 21 |
+
def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
|
| 22 |
+
assert logits.dim() == 1
|
| 23 |
+
top_k = min(top_k, logits.size(-1))
|
| 24 |
+
if top_k > 0:
|
| 25 |
+
indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
|
| 26 |
+
logits[indices_to_remove] = filter_value
|
| 27 |
+
|
| 28 |
+
if top_p > 0.0:
|
| 29 |
+
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
|
| 30 |
+
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
|
| 31 |
+
sorted_indices_to_remove = cumulative_probs > top_p
|
| 32 |
+
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
|
| 33 |
+
sorted_indices_to_remove[..., 0] = 0
|
| 34 |
+
indices_to_remove = sorted_indices[sorted_indices_to_remove]
|
| 35 |
+
logits[indices_to_remove] = filter_value
|
| 36 |
+
return logits
|
| 37 |
+
|
| 38 |
+
def set_logger(log_path):
|
| 39 |
+
logger = logging.getLogger(__name__)
|
| 40 |
+
logger.setLevel(logging.INFO)
|
| 41 |
+
formatter = logging.Formatter(
|
| 42 |
+
'%(asctime)s - %(levelname)s - %(message)s')
|
| 43 |
+
file_handler = logging.FileHandler(
|
| 44 |
+
filename=log_path)
|
| 45 |
+
file_handler.setFormatter(formatter)
|
| 46 |
+
file_handler.setLevel(logging.INFO)
|
| 47 |
+
logger.addHandler(file_handler)
|
| 48 |
+
console = logging.StreamHandler()
|
| 49 |
+
console.setLevel(logging.DEBUG)
|
| 50 |
+
console.setFormatter(formatter)
|
| 51 |
+
logger.addHandler(console)
|
| 52 |
+
return logger
|
| 53 |
+
|
| 54 |
+
# 参数
|
| 55 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 56 |
+
temperature = 1
|
| 57 |
+
topk = 0
|
| 58 |
+
topp = 0.85
|
| 59 |
+
context_len = 800
|
| 60 |
+
model_path = "./model"
|
| 61 |
+
|
| 62 |
+
# 加载tokenizer
|
| 63 |
+
tokenizer = CpmTokenizer(vocab_file=model_path+"/chinese_vocab.model")
|
| 64 |
+
eod_id = tokenizer.convert_tokens_to_ids("<eod>") # 文档结束符
|
| 65 |
+
sep_id = tokenizer.sep_token_id
|
| 66 |
+
unk_id = tokenizer.unk_token_id
|
| 67 |
+
|
| 68 |
+
# 加载模型
|
| 69 |
+
model = GPT2LMHeadModel.from_pretrained(model_path)
|
| 70 |
+
model.eval()
|
| 71 |
+
model = model.to(device)
|
| 72 |
+
|
| 73 |
+
def generate_zuowen(title, context, max_len):
|
| 74 |
+
title_ids = tokenizer.encode(title, add_special_tokens=False)
|
| 75 |
+
context_ids = tokenizer.encode(context, add_special_tokens=False)
|
| 76 |
+
input_ids = title_ids + [sep_id] + context_ids
|
| 77 |
+
cur_len = len(input_ids)
|
| 78 |
+
last_token_id = input_ids[-1]
|
| 79 |
+
input_ids = torch.tensor([input_ids], dtype=torch.long, device=device)
|
| 80 |
+
|
| 81 |
+
while True:
|
| 82 |
+
next_token_id = generate_next_token(input_ids)
|
| 83 |
+
input_ids = torch.cat((input_ids, next_token_id.unsqueeze(0)), dim=1)
|
| 84 |
+
cur_len += 1
|
| 85 |
+
word = tokenizer.convert_ids_to_tokens(next_token_id.item())
|
| 86 |
+
if cur_len >= max_len and last_token_id == 8 and next_token_id == 3:
|
| 87 |
+
break
|
| 88 |
+
if cur_len >= max_len and word in [".", "。", "!", "!", "?", "?", ",", ","]:
|
| 89 |
+
break
|
| 90 |
+
if next_token_id == eod_id:
|
| 91 |
+
break
|
| 92 |
+
word_mok = word.replace('▁', '').replace('▃', '\n ')
|
| 93 |
+
print(word_mok, end='', flush=True)
|
| 94 |
+
result = tokenizer.decode(input_ids.squeeze(0))
|
| 95 |
+
content = result.split("<sep>")[1]
|
| 96 |
+
return content
|
| 97 |
+
|
| 98 |
+
if __name__ == '__main__':
|
| 99 |
+
title='天下大势'#标题
|
| 100 |
+
context='论天下大势'#上文,可以为空
|
| 101 |
+
max_len=100#续写的字数
|
| 102 |
+
max_len += len(context)
|
| 103 |
+
print(context,end='')
|
| 104 |
+
content = generate_zuowen(title, context, max_len)
|
| 105 |
+
```
|
| 106 |
+
<details>
|
| 107 |
+
<summary>示例2</summary>
|
| 108 |
+
```python
|
| 109 |
+
import torch
|
| 110 |
+
import torch.nn.functional as F
|
| 111 |
+
from transformers import AutoModel, AutoTokenizer
|
| 112 |
+
|
| 113 |
+
def generate_next_token(input_ids):
|
| 114 |
+
input_ids = input_ids[:, -context_len:]
|
| 115 |
+
outputs = model(input_ids=input_ids)
|
| 116 |
+
logits = outputs.logits
|
| 117 |
+
next_token_logits = logits[0, -1, :]
|
| 118 |
+
next_token_logits = next_token_logits / temperature
|
| 119 |
+
next_token_logits[unk_id] = -float('Inf')
|
| 120 |
+
filtered_logits = top_k_top_p_filtering(next_token_logits, top_k=topk, top_p=topp)
|
| 121 |
+
next_token_id = torch.multinomial(F.softmax(filtered_logits, dim=-1), num_samples=1)
|
| 122 |
+
return next_token_id
|
| 123 |
+
|
| 124 |
+
def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
|
| 125 |
+
assert logits.dim() == 1
|
| 126 |
+
top_k = min(top_k, logits.size(-1))
|
| 127 |
+
if top_k > 0:
|
| 128 |
+
indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
|
| 129 |
+
logits[indices_to_remove] = filter_value
|
| 130 |
+
|
| 131 |
+
if top_p > 0.0:
|
| 132 |
+
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
|
| 133 |
+
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
|
| 134 |
+
sorted_indices_to_remove = cumulative_probs > top_p
|
| 135 |
+
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
|
| 136 |
+
sorted_indices_to_remove[..., 0] = 0
|
| 137 |
+
indices_to_remove = sorted_indices[sorted_indices_to_remove]
|
| 138 |
+
logits[indices_to_remove] = filter_value
|
| 139 |
+
return logits
|
| 140 |
+
|
| 141 |
+
def set_logger(log_path):
|
| 142 |
+
logger = logging.getLogger(__name__)
|
| 143 |
+
logger.setLevel(logging.INFO)
|
| 144 |
+
formatter = logging.Formatter(
|
| 145 |
+
'%(asctime)s - %(levelname)s - %(message)s')
|
| 146 |
+
file_handler = logging.FileHandler(
|
| 147 |
+
filename=log_path)
|
| 148 |
+
file_handler.setFormatter(formatter)
|
| 149 |
+
file_handler.setLevel(logging.INFO)
|
| 150 |
+
logger.addHandler(file_handler)
|
| 151 |
+
# 创建一个handler,用于将日志输出到控制台
|
| 152 |
+
console = logging.StreamHandler()
|
| 153 |
+
console.setLevel(logging.DEBUG)
|
| 154 |
+
console.setFormatter(formatter)
|
| 155 |
+
logger.addHandler(console)
|
| 156 |
+
return logger
|
| 157 |
+
|
| 158 |
+
# 参数
|
| 159 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 160 |
+
temperature = 1
|
| 161 |
+
topk = 0
|
| 162 |
+
topp = 0.85
|
| 163 |
+
context_len = 800
|
| 164 |
+
model_path = "model"
|
| 165 |
+
|
| 166 |
+
# 加载tokenizer
|
| 167 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 168 |
+
eod_id = tokenizer.convert_tokens_to_ids("<eod>") # 文档结束符
|
| 169 |
+
sep_id = tokenizer.sep_token_id
|
| 170 |
+
unk_id = tokenizer.unk_token_id
|
| 171 |
+
|
| 172 |
+
# 加载模型
|
| 173 |
+
model = AutoModel.from_pretrained(model_path)
|
| 174 |
+
model.eval()
|
| 175 |
+
model = model.to(device)
|
| 176 |
+
|
| 177 |
+
def generate_zuowen(title, context, max_len):
|
| 178 |
+
title_ids = tokenizer.encode(title, add_special_tokens=False)
|
| 179 |
+
context_ids = tokenizer.encode(context, add_special_tokens=False)
|
| 180 |
+
input_ids = title_ids + [sep_id] + context_ids
|
| 181 |
+
cur_len = len(input_ids)
|
| 182 |
+
last_token_id = input_ids[-1]
|
| 183 |
+
input_ids = torch.tensor([input_ids], dtype=torch.long, device=device)
|
| 184 |
+
|
| 185 |
+
while True:
|
| 186 |
+
next_token_id = generate_next_token(input_ids)
|
| 187 |
+
input_ids = torch.cat((input_ids, next_token_id.unsqueeze(0)), dim=1)
|
| 188 |
+
cur_len += 1
|
| 189 |
+
word = tokenizer.convert_ids_to_tokens(next_token_id.item())
|
| 190 |
+
if cur_len >= max_len and last_token_id == 8 and next_token_id == 3:
|
| 191 |
+
break
|
| 192 |
+
if cur_len >= max_len and word in [".", "。", "!", "!", "?", "?", ",", ","]:
|
| 193 |
+
break
|
| 194 |
+
if next_token_id == eod_id:
|
| 195 |
+
break
|
| 196 |
+
word_mok = word.replace('▁', '').replace('▃', '\n ')
|
| 197 |
+
print(word_mok, end='', flush=True)
|
| 198 |
+
result = tokenizer.decode(input_ids.squeeze(0))
|
| 199 |
+
content = result.split("<sep>")[1]
|
| 200 |
+
return content
|
| 201 |
+
|
| 202 |
+
if __name__ == '__main__':
|
| 203 |
+
title='标题'#标题
|
| 204 |
+
context='上文'#上文,可以为空
|
| 205 |
+
max_len=100#续写的字数
|
| 206 |
+
max_len += len(context)
|
| 207 |
+
print(context,end='')
|
| 208 |
+
content = generate_zuowen(title, context, max_len)
|
| 209 |
+
```
|
| 210 |
+
</details>
|
| 211 |
+
|
| 212 |
+
|
| 213 |
如果您认为生成到后期变慢,可以设置context_len(参考的上文的长度)小一些🤓。
|
| 214 |
运行上面的例子,还需要一些环境👽。
|
| 215 |
在cmd输入以下命令:
|