File size: 1,588 Bytes
bd5fb13
b2448e5
 
 
 
 
 
 
 
 
 
 
 
 
7ca5760
b2448e5
c52ceb1
63c52cf
c52ceb1
63c52cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
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
---
language: 
- zh
tags:
- t5
- pytorch
- prompt
- zh
- Text2Text-Generation
license: "apache-2.0"
widget:
- text: "宫颈癌的早期会有哪些危险信号"
- text: "夏季如何进行饮食调养养生?"
---

中文版对话机器人

在1000w+问答和对话数据上做有监督预训练

请使用下面方式调用模型输出结果,Hosted inference API的结果因为我无法修改后台推理程序,不能保证模型输出效果,只是举了两个例子展示展示。

Install package:
```
pip install transformers 
```

```python
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '-1'
import torch
from torch import cuda
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("mxmax/Chinese_Chat_T5_Base")
model = AutoModelForSeq2SeqLM.from_pretrained("mxmax/Chinese_Chat_T5_Base") 
device = 'cuda' if cuda.is_available() else 'cpu'
model_trained.to(device)
def postprocess(text):
  return text.replace(".", "").replace('</>','')

def answer_fn(text, sample=False, top_p=0.6):
  encoding = tokenizer(text=[text], truncation=True, padding=True, max_length=256, return_tensors="pt").to(device) 
  out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_length=512,temperature=0.5,do_sample=True,repetition_penalty=6.0 ,top_p=top_p)
  result = tokenizer.batch_decode(out["sequences"], skip_special_tokens=True)
  return postprocess(result[0]) 
text="宫颈癌的早期会有哪些危险信号"
result=answer_fn(text, sample=True, top_p=0.6)
print('prompt:',text)
print("result:",result)
```