Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,17 +39,13 @@ def get_clip_embeddings(input_data, input_type='text'):
|
|
| 39 |
|
| 40 |
return embeddings.numpy()
|
| 41 |
|
| 42 |
-
|
| 43 |
veggies = load_dataset('vojtam/vegetables')
|
| 44 |
with open('img_embeddings.pkl', 'rb') as file:
|
| 45 |
img_embeddings = pickle.load(file)
|
| 46 |
|
| 47 |
cos = nn.CosineSimilarity(dim=1, eps=1e-6)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
image = gr.Gallery()
|
| 51 |
-
|
| 52 |
-
def get_similar_images(text, n = 4):
|
| 53 |
if text:
|
| 54 |
text_embedding = get_clip_embeddings(text, input_type='text')
|
| 55 |
sims = cos(torch.tensor(text_embedding), torch.tensor(img_embeddings))
|
|
@@ -59,8 +55,32 @@ def get_similar_images(text, n = 4):
|
|
| 59 |
for index in top_n:
|
| 60 |
imgs.append(veggies['train'][index.item()]['image'])
|
| 61 |
return imgs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
-
intf = gr.Interface(fn = get_similar_images, inputs = text, outputs = image)
|
| 66 |
intf.launch(share=True)
|
|
|
|
| 39 |
|
| 40 |
return embeddings.numpy()
|
| 41 |
|
|
|
|
| 42 |
veggies = load_dataset('vojtam/vegetables')
|
| 43 |
with open('img_embeddings.pkl', 'rb') as file:
|
| 44 |
img_embeddings = pickle.load(file)
|
| 45 |
|
| 46 |
cos = nn.CosineSimilarity(dim=1, eps=1e-6)
|
| 47 |
|
| 48 |
+
def get_similar_images(text, n=4):
|
|
|
|
|
|
|
|
|
|
| 49 |
if text:
|
| 50 |
text_embedding = get_clip_embeddings(text, input_type='text')
|
| 51 |
sims = cos(torch.tensor(text_embedding), torch.tensor(img_embeddings))
|
|
|
|
| 55 |
for index in top_n:
|
| 56 |
imgs.append(veggies['train'][index.item()]['image'])
|
| 57 |
return imgs
|
| 58 |
+
return []
|
| 59 |
+
|
| 60 |
+
css = """
|
| 61 |
+
.full-height-gallery {
|
| 62 |
+
height: calc(100vh - 250px);
|
| 63 |
+
overflow-y: auto;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
#submit-btn {
|
| 67 |
+
background-color: #ff5b00;
|
| 68 |
+
color: #ffffff;
|
| 69 |
+
}
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
with gr.Blocks(css=css) as intf:
|
| 73 |
+
with gr.Row():
|
| 74 |
+
text_input = gr.Textbox(label="Enter the description of the images you want to search for", placeholder='Your text goes here')
|
| 75 |
+
with gr.Row():
|
| 76 |
+
submit_btn = gr.Button("Submit", elem_id="submit-btn")
|
| 77 |
+
clear_btn = gr.Button("Clear")
|
| 78 |
+
with gr.Row():
|
| 79 |
+
gallery = gr.Gallery(label="Similar Images", show_label=False, elem_classes = ["full-height-gallery"])
|
| 80 |
+
|
| 81 |
+
submit_btn.click(fn=get_similar_images, inputs=text_input, outputs=gallery)
|
| 82 |
+
clear_btn.click(fn=lambda: [None, []], inputs=None, outputs=[text_input, gallery])
|
| 83 |
|
| 84 |
|
| 85 |
|
|
|
|
| 86 |
intf.launch(share=True)
|