AnyaSchen commited on
Commit
4e00b00
1 Parent(s): ef04902

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +35 -0
README.md ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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.
2
+
3
+ To use this model, you can do this:
4
+
5
+ ```
6
+ from transformers import AutoTokenizer, AutoModelForCausalLM
7
+
8
+ def generate_poetry(input: str, model, num_beams=3):
9
+ input = input if len(input) > 0 else tokenizer.bos_token
10
+ input_ids = tokenizer.encode(input, return_tensors="pt").to(device)
11
+ # Create an attention mask
12
+ attention_mask = (input_ids != tokenizer.pad_token_id).float()
13
+
14
+ # Set the pad_token_id
15
+ tokenizer.pad_token_id = tokenizer.eos_token_id
16
+ with torch.no_grad():
17
+ out = model.generate(input_ids,
18
+ do_sample=True,
19
+ num_beams=num_beams,
20
+ temperature=2.0,
21
+ top_p=0.9,
22
+ max_length = 200,
23
+ eos_token_id=tokenizer.eos_token_id,
24
+ bos_token_id=tokenizer.bos_token_id,
25
+ attention_mask=attention_mask
26
+ ).to(device)
27
+ return tokenizer.batch_decode(out, skip_special_tokens=True)[0]
28
+
29
+ path = 'AnyaSchen/rugpt3-medium-keywords2poetry'
30
+ tokenizer = AutoTokenizer.from_pretrained(path)
31
+ model = AutoModelForCausalLM.from_pretrained(path).to(device)
32
+
33
+ inp = 'Автор: Маяковский\nКлючевые слова:<write your keywords>'
34
+ print(generate_poetry(inp, model))
35
+ ```