Views
No views yet
1import torch
2import transformers
3
4use_cuda = torch.cuda.is_available()
5device = torch.device("cuda" if use_cuda else "cpu")
6
7t5_tokenizer = transformers.GPT2Tokenizer.from_pretrained("SiberiaSoft/SiberianPersonaFred")
8t5_model = transformers.T5ForConditionalGeneration.from_pretrained("SiberiaSoft/SiberianPersonaFred")
9
10
11while True:
12 print('-'*80)
13 dialog = []
14 while True:
15 msg = input('H:> ').strip()
16 if len(msg) == 0:
17 break
18 msg = msg[0].upper() + msg[1:]
19 dialog.append('Собеседник: ' + msg)
20 # В начале ставится промпт персонажа.
21 prompt = '<SC6>Ты парень, консультант по разным вопросам. Ты очень умный. Любишь помогать собеседнику. Продолжи диалог:' + '\n'.join(dialog) + '\nТы: <extra_id_0>'
22
23 input_ids = t5_tokenizer(prompt, return_tensors='pt').input_ids
24 out_ids = t5_model.generate(input_ids=input_ids.to(device), do_sample=True, temperature=0.9, max_new_tokens=512, top_p=0.85,
25 top_k=2, repetition_penalty=1.2)
26 t5_output = t5_tokenizer.decode(out_ids[0][1:])
27 if '</s>' in t5_output:
28 t5_output = t5_output[:t5_output.find('</s>')].strip()
29
30 t5_output = t5_output.replace('<extra_id_0>', '').strip()
31 t5_output = t5_output.split('Собеседник')[0].strip()
32 print('B:> {}'.format(t5_output))
33 dialog.append('Ты: ' + t5_output)
34