Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,37 +12,42 @@ Original file is located at
|
|
12 |
from PIL import Image
|
13 |
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, PreTrainedTokenizerFast
|
14 |
import requests
|
|
|
|
|
|
|
15 |
|
16 |
-
model = VisionEncoderDecoderModel.from_pretrained("
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
tokenizer = PreTrainedTokenizerFast.from_pretrained("distilgpt2")
|
21 |
|
22 |
-
|
|
|
23 |
|
24 |
-
# with Image.open(requests.get(url, stream=True).raw) as img:
|
25 |
-
# pixel_values = vit_feature_extractor(images=img, return_tensors="pt").pixel_values
|
26 |
|
27 |
-
#encoder_outputs = model.generate(pixel_values.to('cpu'),num_beams=5)
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
generated_sentences = tokenizer.batch_decode(encoder_outputs, skip_special_tokens=True)
|
42 |
|
43 |
-
|
44 |
|
45 |
-
#!wget https://media.glamour.com/photos/5f171c4fd35176eaedb36823/master/w_2560%2Cc_limit/bike.jpg
|
46 |
|
47 |
import gradio as gr
|
48 |
|
@@ -65,7 +70,7 @@ examples = [
|
|
65 |
]
|
66 |
|
67 |
gr.Interface(
|
68 |
-
|
69 |
inputs,
|
70 |
outputs,
|
71 |
title=title,
|
|
|
12 |
from PIL import Image
|
13 |
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, PreTrainedTokenizerFast
|
14 |
import requests
|
15 |
+
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
16 |
+
import torch
|
17 |
+
from PIL import Image
|
18 |
|
19 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
20 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
|
|
|
|
22 |
|
23 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
24 |
+
model.to(device)
|
25 |
|
|
|
|
|
26 |
|
|
|
27 |
|
28 |
+
max_length = 16
|
29 |
+
num_beams = 4
|
30 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
31 |
+
def predict_step(image_paths):
|
32 |
+
images = []
|
33 |
+
for image_path in image_paths:
|
34 |
+
i_image = Image.open(image_path)
|
35 |
+
if i_image.mode != "RGB":
|
36 |
+
i_image = i_image.convert(mode="RGB")
|
37 |
|
38 |
+
images.append(i_image)
|
39 |
|
40 |
+
pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
|
41 |
+
pixel_values = pixel_values.to(device)
|
42 |
|
43 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
44 |
|
45 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
46 |
+
preds = [pred.strip() for pred in preds]
|
47 |
+
return preds
|
|
|
48 |
|
49 |
+
#predict_step(['/content/drive/MyDrive/caption generator/horses.png'])
|
50 |
|
|
|
51 |
|
52 |
import gradio as gr
|
53 |
|
|
|
70 |
]
|
71 |
|
72 |
gr.Interface(
|
73 |
+
predict_step,
|
74 |
inputs,
|
75 |
outputs,
|
76 |
title=title,
|