Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, Blip2ForConditionalGeneration, VisionEncoderDecoderModel
|
3 |
import torch
|
4 |
import open_clip
|
5 |
|
@@ -15,7 +15,7 @@ torch.hub.download_url_to_file('https://cdn.openai.com/dall-e-2/demos/text2im/as
|
|
15 |
git_processor_large_coco = AutoProcessor.from_pretrained("microsoft/git-large-coco")
|
16 |
git_model_large_coco = AutoModelForCausalLM.from_pretrained("microsoft/git-large-coco")
|
17 |
|
18 |
-
git_processor_large_textcaps = AutoProcessor.from_pretrained("microsoft/git-large-r-textcaps")
|
19 |
# git_model_large_textcaps = AutoModelForCausalLM.from_pretrained("microsoft/git-large-r-textcaps")
|
20 |
|
21 |
# blip_processor_base = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
@@ -27,17 +27,20 @@ blip_model_large = BlipForConditionalGeneration.from_pretrained("Salesforce/blip
|
|
27 |
# blip2_processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
28 |
# blip2_model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16)
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
32 |
|
33 |
# vitgpt_processor = AutoImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
34 |
# vitgpt_model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
35 |
# vitgpt_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
36 |
|
37 |
-
coca_model, _, coca_transform = open_clip.create_model_and_transforms(
|
38 |
-
model_name="coca_ViT-L-14",
|
39 |
-
pretrained="mscoco_finetuned_laion2B-s13B-b90k"
|
40 |
-
)
|
41 |
|
42 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
43 |
|
@@ -47,7 +50,7 @@ git_model_large_coco.to(device)
|
|
47 |
# git_model_large_textcaps.to(device)
|
48 |
blip_model_large.to(device)
|
49 |
# vitgpt_model.to(device)
|
50 |
-
coca_model.to(device)
|
51 |
# blip2_model.to(device)
|
52 |
|
53 |
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False):
|
@@ -73,6 +76,18 @@ def generate_caption_coca(model, transform, image):
|
|
73 |
return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
|
74 |
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
def generate_captions(image):
|
77 |
# caption_git_base = generate_caption(git_processor_base, git_model_base, image)
|
78 |
|
@@ -86,20 +101,22 @@ def generate_captions(image):
|
|
86 |
|
87 |
# caption_vitgpt = generate_caption(vitgpt_processor, vitgpt_model, image, vitgpt_tokenizer)
|
88 |
|
89 |
-
caption_coca = generate_caption_coca(coca_model, coca_transform, image)
|
90 |
|
91 |
# caption_blip2 = generate_caption(blip2_processor, blip2_model, image, use_float_16=True).strip()
|
92 |
|
93 |
-
caption_blip2_8_bit = generate_caption(
|
|
|
|
|
94 |
|
95 |
-
return caption_git_large_coco, caption_blip_large,
|
96 |
|
97 |
|
98 |
examples = [["cats.jpg"], ["stop_sign.png"], ["astronaut.jpg"]]
|
99 |
-
outputs = [gr.outputs.Textbox(label="Caption generated by GIT-large fine-tuned on COCO"), gr.outputs.Textbox(label="Caption generated by BLIP-large"), gr.outputs.Textbox(label="Caption generated by
|
100 |
|
101 |
title = "Interactive demo: comparing image captioning models"
|
102 |
-
description = "Gradio Demo to compare GIT, BLIP,
|
103 |
article = "<p style='text-align: center'><a href='https://huggingface.co/docs/transformers/main/model_doc/blip' target='_blank'>BLIP docs</a> | <a href='https://huggingface.co/docs/transformers/main/model_doc/git' target='_blank'>GIT docs</a></p>"
|
104 |
|
105 |
interface = gr.Interface(fn=generate_captions,
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, Blip2ForConditionalGeneration, VisionEncoderDecoderModel, InstructBlipForConditionalGeneration
|
3 |
import torch
|
4 |
import open_clip
|
5 |
|
|
|
15 |
git_processor_large_coco = AutoProcessor.from_pretrained("microsoft/git-large-coco")
|
16 |
git_model_large_coco = AutoModelForCausalLM.from_pretrained("microsoft/git-large-coco")
|
17 |
|
18 |
+
# git_processor_large_textcaps = AutoProcessor.from_pretrained("microsoft/git-large-r-textcaps")
|
19 |
# git_model_large_textcaps = AutoModelForCausalLM.from_pretrained("microsoft/git-large-r-textcaps")
|
20 |
|
21 |
# blip_processor_base = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
27 |
# blip2_processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
28 |
# blip2_model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16)
|
29 |
|
30 |
+
blip2_processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-6.7b")
|
31 |
+
blip2_model_4_bit = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-6.7b", device_map="auto", load_in_4bit=True, torch_dtype=torch.float16)
|
32 |
+
|
33 |
+
instructblip_processor = AutoProcessor.from_pretrained("Salesforce/instructblip-vicuna-7b")
|
34 |
+
instructblip_model_4_bit = InstructBlipForConditionalGeneration.from_pretrained("Salesforce/instructblip-vicuna-7b", device_map="auto", load_in_4bit=True, torch_dtype=torch.float16)
|
35 |
|
36 |
# vitgpt_processor = AutoImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
37 |
# vitgpt_model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
38 |
# vitgpt_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
39 |
|
40 |
+
# coca_model, _, coca_transform = open_clip.create_model_and_transforms(
|
41 |
+
# model_name="coca_ViT-L-14",
|
42 |
+
# pretrained="mscoco_finetuned_laion2B-s13B-b90k"
|
43 |
+
# )
|
44 |
|
45 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
46 |
|
|
|
50 |
# git_model_large_textcaps.to(device)
|
51 |
blip_model_large.to(device)
|
52 |
# vitgpt_model.to(device)
|
53 |
+
# coca_model.to(device)
|
54 |
# blip2_model.to(device)
|
55 |
|
56 |
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False):
|
|
|
76 |
return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
|
77 |
|
78 |
|
79 |
+
def generate_caption_instructblip(processor, model, image):
|
80 |
+
prompt = "Generate a caption for the image:"
|
81 |
+
|
82 |
+
inputs = processor(images=image, text=prompt, return_tensors="pt").to(device=device, torch_dtype=torch.float16)
|
83 |
+
|
84 |
+
generated_ids = model.generate(pixel_values=inputs.pixel_values,
|
85 |
+
num_beams=5, max_length=256, min_length=1, top_p=0.9, repetition_penalty=1.5, length_penalty=1.0, temperature=1)
|
86 |
+
generated_ids[generated_ids == 0] = 2 # TODO remove once https://github.com/huggingface/transformers/pull/24492 is merged
|
87 |
+
|
88 |
+
return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
89 |
+
|
90 |
+
|
91 |
def generate_captions(image):
|
92 |
# caption_git_base = generate_caption(git_processor_base, git_model_base, image)
|
93 |
|
|
|
101 |
|
102 |
# caption_vitgpt = generate_caption(vitgpt_processor, vitgpt_model, image, vitgpt_tokenizer)
|
103 |
|
104 |
+
# caption_coca = generate_caption_coca(coca_model, coca_transform, image)
|
105 |
|
106 |
# caption_blip2 = generate_caption(blip2_processor, blip2_model, image, use_float_16=True).strip()
|
107 |
|
108 |
+
caption_blip2_8_bit = generate_caption(blip2_processor, blip2_model_8_bit, image, use_float_16=True).strip()
|
109 |
+
|
110 |
+
caption_instructblip_4_bit = generate_caption_instructblip(instructblip_processor, instructblip_model_4_bit, image)
|
111 |
|
112 |
+
return caption_git_large_coco, caption_blip_large, caption_blip2_8_bit, caption_instructblip_4_bit
|
113 |
|
114 |
|
115 |
examples = [["cats.jpg"], ["stop_sign.png"], ["astronaut.jpg"]]
|
116 |
+
outputs = [gr.outputs.Textbox(label="Caption generated by GIT-large fine-tuned on COCO"), gr.outputs.Textbox(label="Caption generated by BLIP-large"), gr.outputs.Textbox(label="Caption generated by BLIP-2 OPT 6.7b"), gr.outputs.Textbox(label="Caption generated by InstructBLIP"), ]
|
117 |
|
118 |
title = "Interactive demo: comparing image captioning models"
|
119 |
+
description = "Gradio Demo to compare GIT, BLIP, BLIP-2 and InstructBLIP, 4 state-of-the-art vision+language models. To use it, simply upload your image and click 'submit', or click one of the examples to load them. Read more at the links below."
|
120 |
article = "<p style='text-align: center'><a href='https://huggingface.co/docs/transformers/main/model_doc/blip' target='_blank'>BLIP docs</a> | <a href='https://huggingface.co/docs/transformers/main/model_doc/git' target='_blank'>GIT docs</a></p>"
|
121 |
|
122 |
interface = gr.Interface(fn=generate_captions,
|