Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from araclip import AraClip
|
| 4 |
+
import gradio as gr
|
| 5 |
+
model = AraClip.from_pretrained("Arabic-Clip/araclip")
|
| 6 |
+
|
| 7 |
+
def search(labels, image):
|
| 8 |
+
# process labels
|
| 9 |
+
labels = labels.split(",")
|
| 10 |
+
labels = [item.strip() for item in original_list if item != "a"]
|
| 11 |
+
|
| 12 |
+
# embed data
|
| 13 |
+
image_features = model.embed(image=image)
|
| 14 |
+
text_features = np.stack([model.embed(text=label) for label in labels])
|
| 15 |
+
# search for most similar data
|
| 16 |
+
similarities = text_features @ image_features
|
| 17 |
+
best_match = labels[np.argmax(similarities)]
|
| 18 |
+
return best_match
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# data
|
| 23 |
+
labels = ["قطة جالسة", "قطة تقفز" ,"كلب", "حصان"]
|
| 24 |
+
image = Image.open("cat.png")
|
| 25 |
+
|
| 26 |
+
demo = gr.Interface(search,["text","image"],["text"],examples=[["حصان, كلب, قطة", "cat.png"]])
|
| 27 |
+
|
| 28 |
+
demo.launch(debug=True)
|