Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,36 @@
|
|
1 |
-
|
2 |
-
import
|
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 |
-
theme="grass",
|
38 |
-
outputs=output,
|
39 |
-
examples = examples,
|
40 |
-
title=title,
|
41 |
-
description=description,
|
42 |
-
article = article,
|
43 |
-
)
|
44 |
-
interface.launch(debug=True)
|
|
|
1 |
+
|
2 |
+
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
3 |
+
|
4 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
5 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
7 |
+
|
8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
+
model.to(device)
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
max_length = 16
|
14 |
+
num_beams = 4
|
15 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
16 |
+
|
17 |
+
def predict_step(image_paths):
|
18 |
+
images = []
|
19 |
+
for image_path in image_paths:
|
20 |
+
i_image = Image.open(image_path)
|
21 |
+
if i_image.mode != "RGB":
|
22 |
+
i_image = i_image.convert(mode="RGB")
|
23 |
+
|
24 |
+
images.append(i_image)
|
25 |
+
|
26 |
+
pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
|
27 |
+
pixel_values = pixel_values.to(device)
|
28 |
+
|
29 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
30 |
+
|
31 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
32 |
+
preds = [pred.strip() for pred in preds]
|
33 |
+
return preds
|
34 |
+
|
35 |
+
|
36 |
+
predict_step(['doctor.e16ba4e4.jpg'] # ['a woman in a hospital bed with a woman in a hospital bed']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|