Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,34 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
demo = gr.Interface(fn=greet,
|
7 |
-
inputs=[gr.File('Upload image')]
|
8 |
outputs="text")
|
9 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def find_nearest_neighbors(encoded_images, input_image, top_n=5):
|
4 |
+
"""
|
5 |
+
Find the closest neighbors to the input image in the encoded image space.
|
6 |
+
|
7 |
+
Args:
|
8 |
+
encoded_images (np.ndarray): Array of encoded images (shape: (n_samples, n_features)).
|
9 |
+
input_image (np.ndarray): The encoded input image (shape: (1, n_features)).
|
10 |
+
top_n (int): The number of nearest neighbors to return.
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
List of tuples: (index, distance) of the top_n nearest neighbors.
|
14 |
+
"""
|
15 |
+
# Compute pairwise distances
|
16 |
+
distances = euclidean_distances(encoded_images, input_image.reshape(1, -1)).flatten()
|
17 |
+
|
18 |
+
# Sort by distance
|
19 |
+
nearest_neighbors = np.argsort(distances)[:top_n]
|
20 |
+
return [(index, distances[index]) for index in nearest_neighbors]
|
21 |
+
|
22 |
+
def get_image(index):
|
23 |
+
split = len(dataset["train"])
|
24 |
+
if index < split:
|
25 |
+
return dataset["train"][index]
|
26 |
+
else:
|
27 |
+
return dataset["test"][index-split]
|
28 |
+
|
29 |
+
|
30 |
|
31 |
demo = gr.Interface(fn=greet,
|
32 |
+
inputs=[gr.File(label='Upload image')]
|
33 |
outputs="text")
|
34 |
demo.launch()
|