Spaces:
Runtime error
Runtime error
added resizing
Browse files
app.py
CHANGED
@@ -21,9 +21,18 @@ embeddings = torch.tensor(embeddings)
|
|
21 |
|
22 |
img_df = pd.read_csv('image_data.csv')
|
23 |
|
24 |
-
def url2img(url):
|
25 |
data = requests.get(url, allow_redirects = True).content
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def find_topk(text):
|
29 |
|
@@ -34,10 +43,11 @@ def find_topk(text):
|
|
34 |
text_data = model_multi.preprocess_text(text)
|
35 |
text_features, text_embedding = model_multi.encode_text(text_data, return_features=True)
|
36 |
|
|
|
|
|
37 |
sims = F.cosine_similarity(text_embedding, embeddings)
|
38 |
|
39 |
vals, inds = sims.topk(top_k)
|
40 |
-
print(img_df)
|
41 |
top_k_urls = img_df.iloc[inds]['photo_image_url'].values
|
42 |
|
43 |
print('top_k_urls', top_k_urls)
|
|
|
21 |
|
22 |
img_df = pd.read_csv('image_data.csv')
|
23 |
|
24 |
+
def url2img(url, resize = False, fix_height = 200):
|
25 |
data = requests.get(url, allow_redirects = True).content
|
26 |
+
img = Image.open(io.BytesIO(data))
|
27 |
+
if resize:
|
28 |
+
width, height = img.size
|
29 |
+
img = img.resize((resize_wh(width, height, fix_height)))
|
30 |
+
return img
|
31 |
+
|
32 |
+
def resize_wh(w, h, fix_height):
|
33 |
+
ratio = w / h
|
34 |
+
w_n = int(fix_height * ratio)
|
35 |
+
return w_n, fix_height
|
36 |
|
37 |
def find_topk(text):
|
38 |
|
|
|
43 |
text_data = model_multi.preprocess_text(text)
|
44 |
text_features, text_embedding = model_multi.encode_text(text_data, return_features=True)
|
45 |
|
46 |
+
print('Got features', datetime.now().strftime("%H:%M:%S"))
|
47 |
+
|
48 |
sims = F.cosine_similarity(text_embedding, embeddings)
|
49 |
|
50 |
vals, inds = sims.topk(top_k)
|
|
|
51 |
top_k_urls = img_df.iloc[inds]['photo_image_url'].values
|
52 |
|
53 |
print('top_k_urls', top_k_urls)
|