Skip to content

Commit 99caa53

Browse files
committed
remove legacy code + update readme
1 parent a03e721 commit 99caa53

File tree

6 files changed

+62
-120
lines changed

6 files changed

+62
-120
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,17 @@ $ streamlit run run.py
2222
Local URL: http://localhost:8501
2323
Network URL: http://192.168.1.4:8501
2424
25+
100%|███████████████████████████████████████| 256M/256M [02:10<00:00, 1.96MiB/s]
26+
100%|█████████████████████████████████████| 1.36M/1.36M [00:01<00:00, 1.31MiB/s]
27+
Embeddings path not found, upload images to create embeddings
2528
```
2629

2730
If everything goes correctly, it should automatically open up a browser with the network URL. On the left you will see the build apps, select the one that you want to use.
2831

29-
<img src="./usage.gif">
32+
<img src="./sc.png">
33+
34+
35+
### Search in more than one way!
36+
37+
Perform `image-text`, `text-image`, `image-image`, (`text-text` as well, but it's not good) similarity.
38+

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
nbox
12
numpy
23
Pillow
34
ftfy

run.py

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import streamlit as st
22

3-
from clip.utils import get_images
4-
53
# this caches the output to store the output and not call this function again
64
# and again preventing time wastage. `allow_output_mutation = True` tells the
75
# function to not hash the output of this function and we can get away with it
@@ -10,59 +8,67 @@
108
@st.cache(allow_output_mutation=True, show_spinner=False)
119
def get_cross_modal_search_models():
1210
from clip.clip import CLIP
13-
return {
14-
'CLIP': CLIP()
15-
}
11+
return CLIP()
1612

1713
# load all the models before the app starts
18-
with st.spinner('Downloading and Loading Model with Vocabulary...'):
19-
MODELS = get_cross_modal_search_models()
14+
with st.spinner('Loading Model with Vocabulary ... (might take sometime)'):
15+
model = get_cross_modal_search_models()
16+
17+
st.write(f'''
18+
# Image Searching App
2019
21-
st.write('''
22-
# NL-Images
23-
CLIP is used to perform Cross Modal Search:
24-
- CLIP: CLIP (Contrastive Language-Image Pre-Training) is a neural network that
25-
consists of a image encoder and a text encoder. It predicts the similarity between
26-
the given images and textual descriptions.
20+
Find images using text and yes, there's an easter egg.
2721
''')
2822

29-
model_name = st.sidebar.selectbox(
30-
'Please select your app',
31-
["CLIP"]
23+
app_mode = st.sidebar.selectbox(
24+
'Please select tasks',
25+
["Text Search", "Image Search", "Text to Text Similarity"]
3226
)
3327

34-
if model_name != "CLIP":
35-
st.write("Use `CLIP` model!")
36-
model = MODELS['CLIP']
28+
st.write('''Upload more images to cache, if you want to add more!''')
29+
images = st.file_uploader("Images", accept_multiple_files=True, type=['png', 'jpg', 'jpeg'])
30+
31+
if st.button("Upload") and len(images):
32+
out = model.upload_images(images)
33+
st.write(out)
34+
st.write(f'''{model.n_images}''')
3735

38-
if model_name == "CLIP":
39-
st.write("### `CLIP` Model")
40-
st.write("Please upload images and write text of your choice")
41-
st.write("Note: Write each description in a new line")
42-
model = MODELS['CLIP']
36+
# slider to select the number of images to display
37+
n_images = st.slider('Number of images to see', min_value=1, max_value = model.n_images)
4338

44-
images = st.file_uploader("Images", accept_multiple_files=True, type=['png', 'jpg'])
39+
if app_mode == "Image Search":
40+
st.write('''### Image Search''')
41+
st.write(f"Upload any image for similarity search. Searching {n_images} images!")
42+
image = st.file_uploader("Images", accept_multiple_files=False, type=['png', 'jpg', 'jpeg'])
43+
if st.button("Process") and image:
44+
out = model.visual_search(image, n_images)
45+
for x in out:
46+
st.image(x)
4547

46-
if len(images) != 0:
47-
images, image_grid = get_images(images)
48-
st.image(image_grid)
48+
elif app_mode == "Text Search":
49+
st.write('''### Text Search''')
50+
text = st.text_input(f"Add the text to search. Searching {n_images} images!")
51+
if st.button("Process") and text:
52+
out = model.text_search(text, n_images)
53+
for x in out:
54+
st.image(x)
4955

50-
default_ = "a person stuck in traffic\na apple on the table\na garden of sunflowers"
51-
text = st.text_area("Text", value=default_, key="Text")
52-
text = text.splitlines()
56+
elif app_mode == "Text to Text Similarity":
57+
st.write('''### Text to Text Similarity
58+
59+
This requires two different inputs, first is the memory against which to check
60+
the second input query.''')
5361

54-
# `transpose_flag` tells against which input, should softmax be calculated
55-
# ie. if transpose_flag = False -> sum(text[i]) == 1 but sum(images[i]) != 1
56-
# ie. if transpose_flag = True -> sum(text[i]) != 1 but sum(images[i]) == 1
57-
transpose_flag = st.radio('Priority', ['Image', 'Text'])
58-
if len(images) == 1:
59-
transpose_flag = True
60-
elif len(text) == 1:
61-
transpose_flag = False
62-
else:
63-
transpose_flag = True if transpose_flag == 'Image' else False
62+
default_ = '''How can I sample from the EMNIST letters dataset?
63+
Simple, efficient way to create Dataset?
64+
How to use multiple models to perform inference on same data in parallel?
65+
Get target list from Dataset
66+
Sparse dataset and dataloader
67+
Element-Wise Max Between Two Tensors?'''
68+
memory = st.text_area("Memory", value=default_)
69+
query = st.text_input("Query", value="Can I run mulitple models in parallel?")
70+
matches = model.text_to_text_similarity(memory.split("\n"), query)
6471

65-
if st.button("Predict"):
66-
with st.spinner('Predicting...'):
67-
output = model.eval(images, text, transpose_flag)
68-
st.write(output)
72+
if st.button("Process"):
73+
st.write("**Query**: " + query)
74+
st.write("\n".join([f"- {m}" for m in matches]))

sc.png

247 KB
Loading

search_vis.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

usage.gif

-3.22 MB
Binary file not shown.

0 commit comments

Comments
 (0)