nielsr HF staff commited on
Commit
bd5ba96
1 Parent(s): 8e55ef5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, VisionEncoderDecoderModel
3
  import torch
4
  import open_clip
5
 
@@ -24,9 +24,12 @@ git_model_large_textcaps = AutoModelForCausalLM.from_pretrained("microsoft/git-l
24
  blip_processor_large = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
25
  blip_model_large = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
26
 
27
- vitgpt_processor = AutoImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
28
- vitgpt_model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
29
- vitgpt_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
 
 
 
30
 
31
  coca_model, _, coca_transform = open_clip.create_model_and_transforms(
32
  model_name="coca_ViT-L-14",
@@ -40,8 +43,9 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
40
  git_model_large_coco.to(device)
41
  git_model_large_textcaps.to(device)
42
  blip_model_large.to(device)
43
- vitgpt_model.to(device)
44
  coca_model.to(device)
 
45
 
46
  def generate_caption(processor, model, image, tokenizer=None):
47
  inputs = processor(images=image, return_tensors="pt").to(device)
@@ -74,18 +78,20 @@ def generate_captions(image):
74
 
75
  caption_blip_large = generate_caption(blip_processor_large, blip_model_large, image)
76
 
77
- caption_vitgpt = generate_caption(vitgpt_processor, vitgpt_model, image, vitgpt_tokenizer)
78
 
79
  caption_coca = generate_caption_coca(coca_model, coca_transform, image)
80
 
81
- return caption_git_large_coco, caption_git_large_textcaps, caption_blip_large, caption_vitgpt, caption_coca
 
 
82
 
83
 
84
  examples = [["cats.jpg"], ["stop_sign.png"], ["astronaut.jpg"]]
85
- outputs = [gr.outputs.Textbox(label="Caption generated by GIT-large fine-tuned on COCO"), gr.outputs.Textbox(label="Caption generated by GIT-large fine-tuned on TextCaps"), gr.outputs.Textbox(label="Caption generated by BLIP-large"), gr.outputs.Textbox(label="Caption generated by ViT+GPT-2"), gr.outputs.Textbox(label="Caption generated by CoCa")]
86
 
87
  title = "Interactive demo: comparing image captioning models"
88
- description = "Gradio Demo to compare GIT, BLIP, ViT+GPT2 and CoCa, 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."
89
  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>"
90
 
91
  interface = gr.Interface(fn=generate_captions,
 
1
  import gradio as gr
2
+ from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, Blip2ForConditionalGeneration, VisionEncoderDecoderModel
3
  import torch
4
  import open_clip
5
 
 
24
  blip_processor_large = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
25
  blip_model_large = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
26
 
27
+ blip2_processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
28
+ blip2_model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
29
+
30
+ # vitgpt_processor = AutoImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
31
+ # vitgpt_model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
32
+ # vitgpt_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
33
 
34
  coca_model, _, coca_transform = open_clip.create_model_and_transforms(
35
  model_name="coca_ViT-L-14",
 
43
  git_model_large_coco.to(device)
44
  git_model_large_textcaps.to(device)
45
  blip_model_large.to(device)
46
+ # vitgpt_model.to(device)
47
  coca_model.to(device)
48
+ blip2_model.to(device)
49
 
50
  def generate_caption(processor, model, image, tokenizer=None):
51
  inputs = processor(images=image, return_tensors="pt").to(device)
 
78
 
79
  caption_blip_large = generate_caption(blip_processor_large, blip_model_large, image)
80
 
81
+ # caption_vitgpt = generate_caption(vitgpt_processor, vitgpt_model, image, vitgpt_tokenizer)
82
 
83
  caption_coca = generate_caption_coca(coca_model, coca_transform, image)
84
 
85
+ caption_blip2 = generate_caption(blip2_processor, blip2_model, image)
86
+
87
+ return caption_git_large_coco, caption_git_large_textcaps, caption_blip_large, caption_coca, caption_blip2
88
 
89
 
90
  examples = [["cats.jpg"], ["stop_sign.png"], ["astronaut.jpg"]]
91
+ outputs = [gr.outputs.Textbox(label="Caption generated by GIT-large fine-tuned on COCO"), gr.outputs.Textbox(label="Caption generated by GIT-large fine-tuned on TextCaps"), gr.outputs.Textbox(label="Caption generated by BLIP-large"), gr.outputs.Textbox(label="Caption generated by CoCa"), gr.outputs.Textbox(label="Caption generated by BLIP-2 OPT 2.7b")]
92
 
93
  title = "Interactive demo: comparing image captioning models"
94
+ description = "Gradio Demo to compare GIT, BLIP, CoCa, and BLIP-2, 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."
95
  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>"
96
 
97
  interface = gr.Interface(fn=generate_captions,