File size: 1,676 Bytes
b81d20a
 
 
 
 
 
 
 
 
 
 
 
4e00b00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
language:
- ru
tags:
- keywords2poetry
- Pushkin
- Mayakovsky
- Esenin
- Blok
- Tyutchev
---
This repo contains the fune-tuned version of [ai-forever/rugpt3medium_based_on_gpt2](https://huggingface.co./ai-forever/rugpt3medium_based_on_gpt2), which can generate poetry from keywords in style of Pushkin, Mayakovsky, Esenin, Blok and Tyutchev.

To use this model, you can do this:

```
from transformers import AutoTokenizer, AutoModelForCausalLM

def generate_poetry(input: str, model, num_beams=3):
  input = input if len(input) > 0 else tokenizer.bos_token
  input_ids = tokenizer.encode(input, return_tensors="pt").to(device)
  # Create an attention mask
  attention_mask = (input_ids != tokenizer.pad_token_id).float()

    # Set the pad_token_id
  tokenizer.pad_token_id = tokenizer.eos_token_id
  with torch.no_grad():
        out = model.generate(input_ids,
                            do_sample=True,
                            num_beams=num_beams,
                            temperature=2.0,
                            top_p=0.9,
                            max_length = 200,
                            eos_token_id=tokenizer.eos_token_id,
                            bos_token_id=tokenizer.bos_token_id,
                            attention_mask=attention_mask
                            ).to(device)
  return tokenizer.batch_decode(out, skip_special_tokens=True)[0]

path = 'AnyaSchen/rugpt3-medium-keywords2poetry'
tokenizer = AutoTokenizer.from_pretrained(path)
model = AutoModelForCausalLM.from_pretrained(path).to(device)

inp = 'Автор: Маяковский\nКлючевые слова:<write your keywords>'
print(generate_poetry(inp, model))
```