Spaces:
Build error
Build error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import from_pretrained_keras
|
2 |
+
import numpy as np
|
3 |
+
import json
|
4 |
+
import gradio as gr
|
5 |
+
import tensorflow as tf
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import matplotlib.image as mpimg
|
8 |
+
|
9 |
+
# load config
|
10 |
+
with open("image_paths.json", 'r') as f:
|
11 |
+
image_paths = json.load(f)
|
12 |
+
image_embeddings = np.load("image_embeddings.npy")
|
13 |
+
text_encoder = from_pretrained_keras("keras-io/dual-encoder-image-search")
|
14 |
+
|
15 |
+
def find_matches(image_paths, image_embeddings, queries, k=9, normalize=True):
|
16 |
+
# Get the embedding for the query.
|
17 |
+
query_embedding = text_encoder(tf.convert_to_tensor(queries))
|
18 |
+
# Normalize the query and the image embeddings.
|
19 |
+
if normalize:
|
20 |
+
image_embeddings = tf.math.l2_normalize(image_embeddings, axis=1)
|
21 |
+
query_embedding = tf.math.l2_normalize(query_embedding, axis=1)
|
22 |
+
# Compute the dot product between the query and the image embeddings.
|
23 |
+
dot_similarity = tf.matmul(query_embedding, image_embeddings, transpose_b=True)
|
24 |
+
# Retrieve top k indices.
|
25 |
+
results = tf.math.top_k(dot_similarity, k).indices.numpy()
|
26 |
+
# Return matching image paths.
|
27 |
+
return [[image_paths[idx] for idx in indices] for indices in results]
|
28 |
+
|
29 |
+
|
30 |
+
def inference(query):
|
31 |
+
matches = find_matches(image_paths, image_embeddings, [query], normalize=True)[0]
|
32 |
+
plt.figure(figsize=(20, 20))
|
33 |
+
for i in range(9):
|
34 |
+
ax = plt.subplot(3, 3, i + 1)
|
35 |
+
plt.imshow(mpimg.imread(matches[i]))
|
36 |
+
plt.axis("off")
|
37 |
+
plt.savefig("img.png")
|
38 |
+
return "img.png"
|
39 |
+
|
40 |
+
examples= ['a family standing next to the ocean on a sandy beach with a surf board',
|
41 |
+
'a group of people sitting in an audience with pen and paper',
|
42 |
+
]
|
43 |
+
|
44 |
+
gr.Interface(
|
45 |
+
fn=inference,
|
46 |
+
title="Natural language image search with a Dual Encoder",
|
47 |
+
description = "Implementation of a dual encoder model for retrieving images that match natural language queries (Note: for demo purposes, only 1k images were used as search space)",
|
48 |
+
inputs="text",
|
49 |
+
examples=examples,
|
50 |
+
outputs="image",
|
51 |
+
cache_examples=False,
|
52 |
+
article = "Author: <a href=\"https://huggingface.co/vumichien\">Vu Minh Chien</a>. Based on the keras example from <a href=\"https://keras.io/examples/nlp/nl_image_search/\">Khalid Salama</a>",
|
53 |
+
).launch(debug=True, enable_queue=True)
|