File size: 833 Bytes
2b11a19
 
 
 
 
 
 
 
 
b0014c2
2b11a19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from PIL import Image
from araclip import AraClip
import gradio as gr
model = AraClip.from_pretrained("Arabic-Clip/araclip")

def search(labels, image):
    # process labels
    labels = labels.split(",")
    labels =  [item.strip() for item in labels if item != ""]
    
    # embed data 
    image_features = model.embed(image=image)
    text_features = np.stack([model.embed(text=label) for label in labels])
    # search for most similar data
    similarities = text_features @ image_features
    best_match = labels[np.argmax(similarities)]
    return best_match

    

# data
labels = ["قطة جالسة", "قطة تقفز" ,"كلب", "حصان"]
image = Image.open("cat.png")

demo = gr.Interface(search,["text","image"],["text"],examples=[["حصان, كلب, قطة", "cat.png"]])

demo.launch(debug=True)