File size: 1,442 Bytes
4427aba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f3f504
 
 
 
7725d56
4427aba
0f497f0
4427aba
 
 
 
beaa97f
4427aba
0f497f0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from PIL import Image
import requests
import torch
from torchvision import transforms
from torchvision.transforms.functional import InterpolationMode

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

import gradio as gr

from models.blip import blip_decoder

image_size = 384
transform = transforms.Compose([
    transforms.Resize((image_size,image_size),interpolation=InterpolationMode.BICUBIC),
    transforms.ToTensor(),
    transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711))
    ]) 

model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_large_caption.pth'
    
model = blip_decoder(pretrained=model_url, image_size=384, vit='large')
model.eval()
model = model.to(device)

def inference_image_caption(raw_image):
    image = transform(raw_image).unsqueeze(0).to(device)   
    with torch.no_grad():
        caption = model.generate(image, sample=True, top_p=0.9, max_length=20, min_length=5)
    return caption[0]

inputs = gr.Image(type='pil', label="Input")
outputs = gr.outputs.Textbox(label="Output")

title = "BLIP"

description = "Gradio demo for BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation"

app = gr.Interface(inference_image_caption, inputs, outputs, title=title, description=description, examples=[['starrynight.jpeg',]])
app.launch(enable_queue=True, share=True)