sDenisov3 commited on
Commit
65988dc
1 Parent(s): 26fc920

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoProcessor, AutoModelForCausalLM, BlipForQuestionAnswering, ViltForQuestionAnswering
3
+ import torch
4
+
5
+ torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
6
+ torch.hub.download_url_to_file('https://huggingface.co/datasets/nielsr/textcaps-sample/resolve/main/stop_sign.png', 'stop_sign.png')
7
+ torch.hub.download_url_to_file('https://cdn.openai.com/dall-e-2/demos/text2im/astronaut/horse/photo/0.jpg', 'astronaut.jpg')
8
+
9
+ git_processor_base = AutoProcessor.from_pretrained("microsoft/git-base-vqav2")
10
+ git_model_base = AutoModelForCausalLM.from_pretrained("microsoft/git-base-vqav2")
11
+
12
+ git_processor_large = AutoProcessor.from_pretrained("microsoft/git-large-vqav2")
13
+ git_model_large = AutoModelForCausalLM.from_pretrained("microsoft/git-large-vqav2")
14
+
15
+ blip_processor_base = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
16
+ blip_model_base = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
17
+
18
+ blip_processor_large = AutoProcessor.from_pretrained("Salesforce/blip-vqa-capfilt-large")
19
+ blip_model_large = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-capfilt-large")
20
+
21
+ vilt_processor = AutoProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
22
+ vilt_model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
23
+
24
+ device = "cuda" if torch.cuda.is_available() else "cpu"
25
+
26
+ git_model_base.to(device)
27
+ blip_model_base.to(device)
28
+ git_model_large.to(device)
29
+ blip_model_large.to(device)
30
+ vilt_model.to(device)
31
+
32
+ def generate_answer_git(processor, model, image, question):
33
+ # prepare image
34
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
35
+
36
+ # prepare question
37
+ input_ids = processor(text=question, add_special_tokens=False).input_ids
38
+ input_ids = [processor.tokenizer.cls_token_id] + input_ids
39
+ input_ids = torch.tensor(input_ids).unsqueeze(0)
40
+
41
+ generated_ids = model.generate(pixel_values=pixel_values, input_ids=input_ids, max_length=50)
42
+ generated_answer = processor.batch_decode(generated_ids, skip_special_tokens=True)
43
+
44
+ return generated_answer
45
+
46
+
47
+ def generate_answer_blip(processor, model, image, question):
48
+ # prepare image + question
49
+ inputs = processor(images=image, text=question, return_tensors="pt")
50
+
51
+ generated_ids = model.generate(**inputs, max_length=50)
52
+ generated_answer = processor.batch_decode(generated_ids, skip_special_tokens=True)
53
+
54
+ return generated_answer
55
+
56
+
57
+ def generate_answer_vilt(processor, model, image, question):
58
+ # prepare image + question
59
+ encoding = processor(images=image, text=question, return_tensors="pt")
60
+
61
+ with torch.no_grad():
62
+ outputs = model(**encoding)
63
+
64
+ predicted_class_idx = outputs.logits.argmax(-1).item()
65
+
66
+ return model.config.id2label[predicted_class_idx]
67
+
68
+
69
+ def generate_answers(image, question):
70
+ answer_git_base = generate_answer_git(git_processor_base, git_model_base, image, question)
71
+
72
+ answer_git_large = generate_answer_git(git_processor_large, git_model_large, image, question)
73
+
74
+ answer_blip_base = generate_answer_blip(blip_processor_base, blip_model_base, image, question)
75
+
76
+ answer_blip_large = generate_answer_blip(blip_processor_large, blip_model_large, image, question)
77
+
78
+ answer_vilt = generate_answer_vilt(vilt_processor, vilt_model, image, question)
79
+
80
+ return answer_git_base, answer_git_large, answer_blip_base, answer_blip_large, answer_vilt
81
+
82
+
83
+ examples = [["cats.jpg", "How many cats are there?"], ["stop_sign.png", "What's behind the stop sign?"], ["astronaut.jpg", "What's the astronaut riding on?"]]
84
+ outputs = [gr.outputs.Textbox(label="Answer generated by GIT-base"), gr.outputs.Textbox(label="Answer generated by GIT-large"), gr.outputs.Textbox(label="Answer generated by BLIP-base"), gr.outputs.Textbox(label="Answer generated by BLIP-large"), gr.outputs.Textbox(label="Answer generated by ViLT")]
85
+
86
+ title = "Interactive demo: comparing visual question answering (VQA) models"
87
+ description = "Gradio Demo to compare GIT, BLIP and ViLT, 3 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."
88
+ 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>"
89
+
90
+ interface = gr.Interface(fn=generate_answers,
91
+ inputs=[gr.inputs.Image(type="pil"), gr.inputs.Textbox(label="Question")],
92
+ outputs=outputs,
93
+ examples=examples,
94
+ title=title,
95
+ description=description,
96
+ article=article,
97
+ enable_queue=True)
98
+ interface.launch(debug=True)