Ahsen Khaliq commited on
Commit
0ae6b5c
1 Parent(s): cefb9dd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system("git clone https://github.com/salesforce/BLIP")
3
+ os.chdir("BLIP")
4
+ os.system("wget https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/1024px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg -O mona.jpg")
5
+
6
+ from PIL import Image
7
+ import requests
8
+ import torch
9
+ from torchvision import transforms
10
+ from torchvision.transforms.functional import InterpolationMode
11
+
12
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
13
+
14
+
15
+
16
+
17
+
18
+ import gradio as gr
19
+
20
+ from models.blip import blip_decoder
21
+
22
+ image_size = 384
23
+ transform = transforms.Compose([
24
+ transforms.Resize((image_size,image_size),interpolation=InterpolationMode.BICUBIC),
25
+ transforms.ToTensor(),
26
+ transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711))
27
+ ])
28
+
29
+ model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model*_base_caption.pth'
30
+
31
+ model = blip_decoder(pretrained=model_url, image_size=384, vit='base')
32
+ model.eval()
33
+ model = model.to(device)
34
+
35
+
36
+ def inference(raw_image):
37
+ image = transform(raw_image).unsqueeze(0).to(device)
38
+ with torch.no_grad():
39
+ caption = model.generate(image, sample=False, num_beams=3, max_length=20, min_length=5)
40
+ print('caption: '+caption[0])
41
+
42
+ return 'caption: '+caption[0]
43
+
44
+ inputs = gr.inputs.Image(type='pil')
45
+ outputs = gr.outputs.Textbox(label="Output")
46
+
47
+ title = "Omnivore"
48
+
49
+ description = "Gradio demo for Omnivore: A Single Model for Many Visual Modalities. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
50
+
51
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2201.08377' target='_blank'>Omnivore: A Single Model for Many Visual Modalities</a> | <a href='https://github.com/facebookresearch/omnivore' target='_blank'>Github Repo</a></p>"
52
+
53
+
54
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=[['mona.jpg']]).launch(enable_queue=True,cache_examples=True)