File size: 1,448 Bytes
6683ddd
39e7e19
 
 
 
 
 
8c26685
4077239
8c26685
 
 
 
 
 
 
 
 
 
4077239
 
8c26685
 
 
 
 
 
 
 
 
 
39e7e19
8c26685
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
import gradio as gr
from transformers import CLIPProcessor, CLIPModel

model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")


def inference(input_img, captions):
    captions_list = captions.split(",")   
    inputs = processor(text=captions_list, images=input_img, return_tensors="pt", padding=True)
    outputs = model(**inputs)
    logits_per_image = outputs.logits_per_image  # this is the image-text similarity score
    probs = logits_per_image.softmax(dim=1) 
    probabilities_percentages = ', '.join(['{:.2f}%'.format(prob.item() * 100) for prob in probs[0]])
    return probabilities_percentages

title = "TSAI S18 Assignment: Use a pretrained CLIP model and give a demo on its workig"
description = "A simple Gradio interface that accepts an image and some captions, and gives a score as to how much the caption describes the image "
 
examples = [["cats.jpg","a photo of a cat, a photo of a dog"],
            ["personBicycle.jpg","person riding bicycle, person driving car, photo of a dog"]
           ]

demo = gr.Interface(
    inference, 
    inputs = [gr.Image(shape=(416, 416), label="Input Image"), gr.Textbox(placeholder="Enter different captions for image, separated by comma")], 
    outputs = [gr.Textbox(label="Probability score of captions")],
    title = title,
    description = description,
    examples = examples,
)

demo.launch()