File size: 4,086 Bytes
0e912cb
 
702953a
 
 
 
 
 
 
0e912cb
702953a
47a9523
702953a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
license: mit
language:
- ru
pipeline_tag: text2text-generation
widget:
- text: '<SC6>Человек: Ответь на вопрос. Почему трава зеленая?\nБот: <extra_id_0>'
- text: '<SC1>Тебя зовут Анфиса. Тебе интересно машинное обучение.\nСобеседник сказал: Привет\nТы ответил: <extra_id_0>'
- text: '<SC6>Тебя зовут Анфиса. Тебе интересно машинное обучение.\nСобеседник сказал: Что делать, если шалят нервишки?\nТы ответил: <extra_id_0>'
---
# Den4ikAI/FRED-T5-XL_instructor_chitchat
Инструкционная модель на FRED-T5-XL. Обратите внимание на промпты в примере чит-чата.
# Пример использования [Instruct]
```python
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, GenerationConfig
import torch
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
tokenizer = AutoTokenizer.from_pretrained("Den4ikAI/FRED-T5-XL_instructor_chitchat")
model = AutoModelForSeq2SeqLM.from_pretrained("Den4ikAI/FRED-T5-XL_instructor_chitchat", torch_dtype=torch.float16).to(device)
model.eval()
generation_config = GenerationConfig.from_pretrained("Den4ikAI/FRED-T5-XL_instructor_chitchat")
def generate(prompt):
  data = tokenizer(f"<SC6>Человек: {prompt}\nБот: <extra_id_0>", return_tensors="pt").to(model.device)
  output_ids = model.generate(
      **data,
      generation_config=generation_config
  )[0]
  print(tokenizer.decode(data["input_ids"][0].tolist()))
  out = tokenizer.decode(output_ids.tolist())
  return out
while 1:
  generate(input(":> "))

```
# Пример использования [Chitchat]
```python
import torch
import transformers

use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")

t5_tokenizer = transformers.GPT2Tokenizer.from_pretrained("Den4ikAI/FRED-T5-XL_instructor_chitchat")
t5_model = transformers.T5ForConditionalGeneration.from_pretrained("Den4ikAI/FRED-T5-XL_instructor_chitchat")
generation_config = transformers.GenerationConfig.from_pretrained("Den4ikAI/FRED-T5-XL_instructor_chitchat")

while True:
    print('-'*80)
    dialog = []
    while True:
        msg = input('H:> ').strip()
        if len(msg) == 0:
            break
        msg = msg[0].upper() + msg[1:]
        dialog.append('Собеседник сказал: ' + msg)
        # Данный пример промпта позволяет вести диалог и писать инструкции.
        # prompt = '<SC6>Тебя зовут Анфиса. Тебе интересно машинное обучение.' + '\n'.join(dialog) + '\nТы ответил: <extra_id_0>'
        # Второй пример - промпт просто для диалогов. В таком режиме не будет глюков, когда модель кидает кусок промпта в ответ.
        prompt = '<SC6>Тебя зовут Анфиса. Тебе интересно машинное обучение.' + '\n'.join(dialog) + '\nТы ответил: <extra_id_0>'

        input_ids = t5_tokenizer(prompt, return_tensors='pt').input_ids
        out_ids = t5_model.generate(input_ids=input_ids.to(device), generation_config=generation_config)
        t5_output = t5_tokenizer.decode(out_ids[0][1:])
        if '</s>' in t5_output:
            t5_output = t5_output[:t5_output.find('</s>')].strip()

        t5_output = t5_output.replace('<extra_id_0>', '').strip()
        t5_output = t5_output.split('Собеседник')[0].strip()
        print('B:> {}'.format(t5_output))
        dialog.append('Ты ответил: ' + t5_output)
```
# Citation
```
@MISC{Den4ikAI/FRED-T5-XL_instructor_chitchat,
    author  = {Denis Petrov},
    title   = {Russian Instruct and Chitchat model},
    url     = {https://huggingface.co/Den4ikAI/FRED-T5-XL_instructor_chitchat/},
    year    = 2023
}
```