File size: 693 Bytes
fd2c1e0
 
 
 
 
 
 
 
 
 
479108b
02a2188
479108b
1e62ffa
02a2188
 
 
fd2c1e0
02a2188
 
fd2c1e0
479108b
fd2c1e0
 
 
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
import gradio as gr
import torch
import clip
from PIL import Image

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

def clip(image, text):

    text = text.split(",")
    image = preprocess(image).unsqueeze(0).to(device)
    text = clip.tokenize(text).to(device)

    with torch.no_grad():
        image_features = model.encode_image(image)
        text_features = model.encode_text(text)
        
        logits_per_image, logits_per_text = model(image, text)
        probs = logits_per_image.softmax(dim=-1).cpu().numpy()

    return probs

demo = gr.Interface(fn=clip, inputs=["text", "image"], outputs="text")
demo.launch()