AnyaSchen commited on
Commit
9e51456
1 Parent(s): 994dcd1

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +50 -0
README.md ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ datasets:
3
+ - AnyaSchen/image2poetry_ru
4
+ language:
5
+ - ru
6
+ tags:
7
+ - Esenin
8
+ - image2poetry
9
+ ---
10
+
11
+ This repo contains model for generation poetry in style of Esenin from image.
12
+
13
+ To use this model you can do:
14
+
15
+ ```
16
+ from PIL import Image
17
+ import requests
18
+ from transformers import AutoTokenizer, VisionEncoderDecoderModel, ViTImageProcessor
19
+
20
+ def generate_poetry(fine_tuned_model, image, tokenizer):
21
+ pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
22
+ pixel_values = pixel_values.to(device)
23
+
24
+ # Generate the poetry with the fine-tuned VisionEncoderDecoder model
25
+ generated_tokens = fine_tuned_model.generate(
26
+ pixel_values,
27
+ max_length=300,
28
+ num_beams=3,
29
+ top_p=0.8,
30
+ temperature=2.0,
31
+ do_sample=True,
32
+ pad_token_id=tokenizer.pad_token_id,
33
+ eos_token_id=tokenizer.eos_token_id,
34
+ )
35
+
36
+ # Decode the generated tokens
37
+ generated_poetry = tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
38
+ return generated_poetry
39
+
40
+ path = 'AnyaSchen/vit-rugpt3-medium-esenin'
41
+ fine_tuned_model = VisionEncoderDecoderModel.from_pretrained(path).to(device)
42
+ feature_extractor = ViTImageProcessor.from_pretrained(path)
43
+ tokenizer = AutoTokenizer.from_pretrained(path)
44
+
45
+ url = 'https://anandaindia.org/wp-content/uploads/2018/12/happy-man.jpg'
46
+ image = Image.open(requests.get(url, stream=True).raw)
47
+
48
+ generated_poetry = generate_poetry(fine_tuned_model, image, tokenizer)
49
+ print(generated_poetry)
50
+ ```