File size: 1,067 Bytes
0f87246
f5b6fb3
0f87246
 
 
ff9e314
f5b6fb3
0f87246
 
ff9e314
26f5f3a
 
 
ff9e314
26f5f3a
 
 
 
f5b6fb3
0f87246
 
f5b6fb3
26f5f3a
0f87246
ff9e314
 
 
 
1b80bbe
 
 
 
 
ff9e314
 
1b80bbe
f5b6fb3
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
import gradio as gr
from transformers import CLIPProcessor, CLIPModel
from PIL import Image
import torch

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

# Define predict function
def predict(image, labels):
    print("Received image:", image)
    print("Received labels:", labels)

    if not labels or not image:
        return [{"label": "Invalid input", "score": 0.0}]

    labels = labels.split(",")
    inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
    outputs = model(**inputs)
    logits_per_image = outputs.logits_per_image
    probs = logits_per_image.softmax(dim=1)[0].tolist()
    return [{"label": lbl.strip(), "score": round(score, 4)} for lbl, score in zip(labels, probs)]


# Define the Gradio interface
demo = gr.Interface(
    fn=predict,
    inputs=[
        gr.Image(type="pil", label="Image"),
        gr.Textbox(label="Labels (comma-separated)")
    ],
    outputs="json"
)


demo.launch()