ybelkada commited on
Commit
09ccc1d
1 Parent(s): a42385a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py CHANGED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+
4
+ from transformers import BlipForQuestionAnswering, BlipProcessor
5
+
6
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
7
+
8
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
9
+ model_vqa = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base").to(device)
10
+
11
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
+
13
+ def inference(raw_image, question, decoding_strategy):
14
+ inputs = processor(images=raw_image, text=question,return_tensors="pt")
15
+
16
+ if decoding_strategy == "Beam search":
17
+ inputs["max_length"] = 20
18
+ inputs["num_beams"] = 5
19
+ elif decoding_strategy == "Nucleus sampling":
20
+ inputs["max_length"] = 20
21
+ inputs["num_beams"] = 1
22
+ inputs["do_sample"] = True
23
+ inputs["top_k"] = 50
24
+ inputs["top_p"] = 0.95
25
+
26
+ out = model_vqa.generate(**inputs)
27
+ return processor.batch_decode(out, skip_special_tokens=True)[0]
28
+
29
+ inputs = [
30
+ gr.inputs.Image(type='pil'),
31
+ gr.inputs.Textbox(lines=2, label="Question"),
32
+ gr.inputs.Radio(choices=['Beam search','Nucleus sampling'], type="value", default="Nucleus sampling", label="Caption Decoding Strategy")
33
+ ]
34
+ outputs = gr.outputs.Textbox(label="Output")
35
+
36
+ title = "BLIP"
37
+
38
+ description = "Gradio demo for BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation (Salesforce Research). To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
39
+
40
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2201.12086' target='_blank'>BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation</a> | <a href='https://github.com/salesforce/BLIP' target='_blank'>Github Repo</a></p>"
41
+
42
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article).launch(enable_queue=True)