Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,20 +10,17 @@ import matplotlib.pyplot as plt
|
|
10 |
from huggingface_hub import hf_hub_download
|
11 |
from PIL import Image
|
12 |
|
13 |
-
# Download and load model and encoded images
|
14 |
model_path = hf_hub_download(repo_id="eybro/autoencoder", filename="autoencoder_model.keras", repo_type='model')
|
15 |
data_path = hf_hub_download(repo_id="eybro/encoded_images", filename="X_encoded_compressed.npy", repo_type='dataset')
|
16 |
|
17 |
autoencoder = load_model(model_path)
|
18 |
encoded_images = np.load(data_path)
|
19 |
|
20 |
-
# Load and split dataset
|
21 |
dataset = load_dataset("eybro/images")
|
22 |
split_dataset = dataset['train'].train_test_split(test_size=0.2, seed=42) # 80% train, 20% test
|
23 |
dataset['train'] = split_dataset['train']
|
24 |
dataset['test'] = split_dataset['test']
|
25 |
|
26 |
-
# Example images
|
27 |
example_images = {
|
28 |
"Example 1": "example_2.png",
|
29 |
"Example 2": "examples/example_1.png"
|
@@ -32,9 +29,7 @@ example_images = {
|
|
32 |
def create_url_from_title(title: str, timestamp: int):
|
33 |
video_urls = load_dataset("eybro/video_urls")
|
34 |
df = video_urls['train'].to_pandas()
|
35 |
-
print(df.to_string())
|
36 |
filtered = df[df['title'] == title]
|
37 |
-
print(filtered)
|
38 |
base_url = filtered.iloc[0, :]["url"]
|
39 |
return base_url + f"&t={timestamp}s"
|
40 |
|
@@ -89,17 +84,9 @@ def inference(user_image=None, selected_example=None):
|
|
89 |
else:
|
90 |
return "Please upload an image or select an example image."
|
91 |
|
92 |
-
# input_image = process_image(image)
|
93 |
-
|
94 |
nearest_neighbors = find_nearest_neighbors(encoded_images, input_image, top_n=5)
|
95 |
|
96 |
-
# Print the results
|
97 |
-
print("Nearest neighbors (index, distance):")
|
98 |
-
for neighbor in nearest_neighbors:
|
99 |
-
print(neighbor)
|
100 |
-
|
101 |
top4 = [int(i[0]) for i in nearest_neighbors[:4]]
|
102 |
-
print(f"top 4: {top4}")
|
103 |
|
104 |
for i in top4:
|
105 |
im = get_image(i)
|
@@ -109,24 +96,6 @@ def inference(user_image=None, selected_example=None):
|
|
109 |
url = create_url_from_title(result_image['label'], result_image['timestamp'])
|
110 |
result = f"{result_image['label']} {result_image['timestamp']} \n{url}"
|
111 |
|
112 |
-
n=2
|
113 |
-
plt.figure(figsize=(8, 8))
|
114 |
-
for i, (image1, image2) in enumerate(zip(top4[:2], top4[2:])):
|
115 |
-
ax = plt.subplot(2, n, i + 1)
|
116 |
-
image1 = get_image(image1)["image"]
|
117 |
-
image2 = get_image(image2)["image"]
|
118 |
-
|
119 |
-
plt.imshow(image1)
|
120 |
-
plt.gray()
|
121 |
-
ax.get_xaxis().set_visible(False)
|
122 |
-
ax.get_yaxis().set_visible(False)
|
123 |
-
|
124 |
-
ax = plt.subplot(2, n, i + 1 + n)
|
125 |
-
plt.imshow(image2)
|
126 |
-
plt.gray()
|
127 |
-
ax.get_xaxis().set_visible(False)
|
128 |
-
ax.get_yaxis().set_visible(False)
|
129 |
-
|
130 |
return result
|
131 |
|
132 |
def load_example(example_name):
|
|
|
10 |
from huggingface_hub import hf_hub_download
|
11 |
from PIL import Image
|
12 |
|
|
|
13 |
model_path = hf_hub_download(repo_id="eybro/autoencoder", filename="autoencoder_model.keras", repo_type='model')
|
14 |
data_path = hf_hub_download(repo_id="eybro/encoded_images", filename="X_encoded_compressed.npy", repo_type='dataset')
|
15 |
|
16 |
autoencoder = load_model(model_path)
|
17 |
encoded_images = np.load(data_path)
|
18 |
|
|
|
19 |
dataset = load_dataset("eybro/images")
|
20 |
split_dataset = dataset['train'].train_test_split(test_size=0.2, seed=42) # 80% train, 20% test
|
21 |
dataset['train'] = split_dataset['train']
|
22 |
dataset['test'] = split_dataset['test']
|
23 |
|
|
|
24 |
example_images = {
|
25 |
"Example 1": "example_2.png",
|
26 |
"Example 2": "examples/example_1.png"
|
|
|
29 |
def create_url_from_title(title: str, timestamp: int):
|
30 |
video_urls = load_dataset("eybro/video_urls")
|
31 |
df = video_urls['train'].to_pandas()
|
|
|
32 |
filtered = df[df['title'] == title]
|
|
|
33 |
base_url = filtered.iloc[0, :]["url"]
|
34 |
return base_url + f"&t={timestamp}s"
|
35 |
|
|
|
84 |
else:
|
85 |
return "Please upload an image or select an example image."
|
86 |
|
|
|
|
|
87 |
nearest_neighbors = find_nearest_neighbors(encoded_images, input_image, top_n=5)
|
88 |
|
|
|
|
|
|
|
|
|
|
|
89 |
top4 = [int(i[0]) for i in nearest_neighbors[:4]]
|
|
|
90 |
|
91 |
for i in top4:
|
92 |
im = get_image(i)
|
|
|
96 |
url = create_url_from_title(result_image['label'], result_image['timestamp'])
|
97 |
result = f"{result_image['label']} {result_image['timestamp']} \n{url}"
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
return result
|
100 |
|
101 |
def load_example(example_name):
|