Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
This repo contains model for russian poetry generation from images. Poetry can be generated in style of poets: Маяковский, Пушкин, Есенин, Тютчев, Блок.
|
2 |
+
|
3 |
+
To use this model you can write:
|
4 |
+
|
5 |
+
```
|
6 |
+
from PIL import Image
|
7 |
+
import requests
|
8 |
+
from transformers import AutoTokenizer, VisionEncoderDecoderModel, ViTImageProcessor
|
9 |
+
|
10 |
+
def generate_poetry(fine_tuned_model, image, tokenizer, author):
|
11 |
+
pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
|
12 |
+
pixel_values = pixel_values.to(device)
|
13 |
+
|
14 |
+
# Encode author's name and prepare as input to the decoder
|
15 |
+
author_input = f"<bos> {author} <sep>"
|
16 |
+
decoder_input_ids = tokenizer.encode(author_input, return_tensors="pt").to(device)
|
17 |
+
|
18 |
+
# Generate the poetry with the fine-tuned VisionEncoderDecoder model
|
19 |
+
generated_tokens = fine_tuned_model.generate(
|
20 |
+
pixel_values,
|
21 |
+
decoder_input_ids=decoder_input_ids,
|
22 |
+
max_length=300,
|
23 |
+
num_beams=3,
|
24 |
+
top_p=0.8,
|
25 |
+
temperature=2.0,
|
26 |
+
do_sample=True,
|
27 |
+
pad_token_id=tokenizer.pad_token_id,
|
28 |
+
eos_token_id=tokenizer.eos_token_id,
|
29 |
+
)
|
30 |
+
|
31 |
+
# Decode the generated tokens
|
32 |
+
generated_poetry = tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
|
33 |
+
generated_poetry = generated_poetry.split(f'{author}')[-1]
|
34 |
+
return generated_poetry
|
35 |
+
|
36 |
+
url = 'https://anandaindia.org/wp-content/uploads/2018/12/happy-man.jpg'
|
37 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
38 |
+
|
39 |
+
generated_poetry = generate_poetry(fine_tuned_model, image, tokenizer)
|
40 |
+
print(generated_poetry)
|
41 |
+
```
|