Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,37 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import CLIPModel, AutoTokenizer, RawImage
|
3 |
|
4 |
+
# Load the CLIP model and tokenizer
|
5 |
+
model = CLIPModel.from_pretrained("Xenova/mobileclip_blt")
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("Xenova/mobileclip_blt")
|
7 |
|
8 |
+
# Define the inference function
|
9 |
+
def compute_probability(image):
|
10 |
+
# Process the image
|
11 |
+
image = RawImage.read(image)
|
12 |
+
image_inputs = processor(image)
|
13 |
+
image_embeds = vision_model(image_inputs)
|
14 |
+
normalized_image_embeds = image_embeds.normalize().tolist()
|
15 |
+
|
16 |
+
# Compute the probability
|
17 |
+
text_inputs = tokenizer(["cats", "dogs", "birds"], padding="max_length", truncation=True)
|
18 |
+
text_embeds = model(text_inputs)
|
19 |
+
normalized_text_embeds = text_embeds.normalize().tolist()
|
20 |
+
|
21 |
+
probabilities = normalized_image_embeds.map(
|
22 |
+
x => softmax(normalized_text_embeds.map(y => 100 * dot(x, y)))
|
23 |
+
)
|
24 |
+
|
25 |
+
return {"probability": probabilities[0][0]}
|
26 |
+
|
27 |
+
# Create the Gradio interface
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=compute_probability,
|
30 |
+
inputs="image",
|
31 |
+
outputs="text",
|
32 |
+
title="CLIP Probability",
|
33 |
+
description="Upload an image and get the probability scores!"
|
34 |
+
)
|
35 |
+
|
36 |
+
# Launch the interface
|
37 |
+
iface.launch()
|