Vivien commited on
Commit ·
a55de09
1
Parent(s): b59b1d0
Switch from ViT-B32 to ViT-B16
Browse filesFiles changed (4)
- app.py+58-32
- data.csv
- embeddings.npy+1-1
- embeddings2.npy+1-1
3 unmodified lines4567891011121314151617181920212223245 unmodified lines30313233343536373839404142434445464748491 unmodified line5152535455565758596022 unmodified lines8384858687888990919293949596973 unmodified linesimport osfrom transformers import CLIPProcessor, CLIPModel@st.cache(show_spinner=False,hash_funcs={CLIPModel: lambda _: None,CLIPProcessor: lambda _: None,dict: lambda _: None})def load():model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")df = {0: pd.read_csv('data.csv'), 1: pd.read_csv('data2.csv')}embeddings = {0: np.load('embeddings.npy'), 1: np.load('embeddings2.npy')}for k in [0, 1]:embeddings[k] = np.divide(embeddings[k], np.sqrt(np.sum(embeddings[k]**2, axis=1, keepdims=True)))return model, processor, df, embeddingsmodel, processor, df, embeddings = load()source = {0: '\nSource: Unsplash', 1: '\nSource: The Movie Database (TMDB)'}def get_html(url_list, height=200):html = "<div style='margin-top: 20px; max-width: 1200px; display: flex; flex-wrap: wrap; justify-content: space-evenly'>"5 unmodified lineshtml += "</div>"return htmldef compute_text_embeddings(list_of_strings):inputs = processor(text=list_of_strings, return_tensors="pt", padding=True)return model.get_text_features(**inputs)st.cache(show_spinner=False)def image_search(query, corpus, n_results=24):text_embeddings = compute_text_embeddings([query]).detach().numpy()k = 0 if corpus == 'Unsplash' else 1results = np.argsort((embeddings[k]@text_embeddings.T)[:, 0])[-1:-n_results-1:-1]return [(df[k].iloc[i]['path'],df[k].iloc[i]['tooltip'] + source[k],df[k].iloc[i]['link']) for i in results]description = '''# Semantic image search**Enter your query and hit enter**1 unmodified line*Built with OpenAI's [CLIP](https://openai.com/blog/clip/) model, 🤗 Hugging Face's [transformers library](https://huggingface.co/transformers/), [Streamlit](https://streamlit.io/), 25k images from [Unsplash](https://unsplash.com/) and 8k images from [The Movie Database (TMDB)](https://www.themoviedb.org/)**Inspired by [Unsplash Image Search](https://github.com/haltakov/natural-language-image-search) from Vladimir Haltakov and [Alph, The Sacred River](https://github.com/thoppe/alph-the-sacred-river) from Travis Hoppe*'''def main():st.markdown('''<style>.block-container{max-width: 1200px;22 unmodified linesfooter {visibility: hidden;}</style>''',unsafe_allow_html=True)st.sidebar.markdown(description)_, c, _ = st.columns((1, 3, 1))query = c.text_input('', value='clouds at sunset')corpus = st.radio('', ["Unsplash","Movies"])if len(query) > 0:results = image_search(query, corpus)st.markdown(get_html(results), unsafe_allow_html=True)if __name__ == '__main__':main()3 unmodified lines456789101112131415161718192021222324252627282930313233345 unmodified lines40414243444546474849505152535455565758596061626364656667686970711 unmodified line73747576777879808182838422 unmodified lines1071081091101111121131141151161171181191201211221233 unmodified linesimport osfrom transformers import CLIPProcessor, CLIPModel@st.cache(show_spinner=False,hash_funcs={CLIPModel: lambda _: None,CLIPProcessor: lambda _: None,dict: lambda _: None,},)def load():model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")df = {0: pd.read_csv("data.csv"), 1: pd.read_csv("data2.csv")}embeddings = {0: np.load("embeddings.npy"), 1: np.load("embeddings2.npy")}for k in [0, 1]:embeddings[k] = np.divide(embeddings[k], np.sqrt(np.sum(embeddings[k] ** 2, axis=1, keepdims=True)))return model, processor, df, embeddingsmodel, processor, df, embeddings = load()source = {0: "\nSource: Unsplash", 1: "\nSource: The Movie Database (TMDB)"}def get_html(url_list, height=200):html = "<div style='margin-top: 20px; max-width: 1200px; display: flex; flex-wrap: wrap; justify-content: space-evenly'>"5 unmodified lineshtml += "</div>"return htmldef compute_text_embeddings(list_of_strings):inputs = processor(text=list_of_strings, return_tensors="pt", padding=True)return model.get_text_features(**inputs)st.cache(show_spinner=False)def image_search(query, corpus, n_results=24):text_embeddings = compute_text_embeddings([query]).detach().numpy()k = 0 if corpus == "Unsplash" else 1results = np.argsort((embeddings[k] @ text_embeddings.T)[:, 0])[-1 : -n_results - 1 : -1]return [(df[k].iloc[i]["path"],df[k].iloc[i]["tooltip"] + source[k],df[k].iloc[i]["link"],)for i in results]description = """# Semantic image search**Enter your query and hit enter**1 unmodified line*Built with OpenAI's [CLIP](https://openai.com/blog/clip/) model, 🤗 Hugging Face's [transformers library](https://huggingface.co/transformers/), [Streamlit](https://streamlit.io/), 25k images from [Unsplash](https://unsplash.com/) and 8k images from [The Movie Database (TMDB)](https://www.themoviedb.org/)**Inspired by [Unsplash Image Search](https://github.com/haltakov/natural-language-image-search) from Vladimir Haltakov and [Alph, The Sacred River](https://github.com/thoppe/alph-the-sacred-river) from Travis Hoppe*"""def main():st.markdown("""<style>.block-container{max-width: 1200px;22 unmodified linesfooter {visibility: hidden;}</style>""",unsafe_allow_html=True,)st.sidebar.markdown(description)_, c, _ = st.columns((1, 3, 1))query = c.text_input("", value="clouds at sunset")corpus = st.radio("", ["Unsplash", "Movies"])if len(query) > 0:results = image_search(query, corpus)st.markdown(get_html(results), unsafe_allow_html=True)if __name__ == "__main__":main()
123version https://git-lfs.github.com/spec/v1oid sha256:9f8c171e32276739be6b020592edc8a2c06e029ff6505a9d1d4efe3cafa073bdsize 51200128123version https://git-lfs.github.com/spec/v1oid sha256:125430e11a4a415ec0c0fc5339f97544f0447e4b0a24c20f2e59f8852e706afcsize 51200128
123version https://git-lfs.github.com/spec/v1oid sha256:9664e980f31e81c4a34e07833539fea32795d83a4262c9828ceae445fa2e412asize 16732288123version https://git-lfs.github.com/spec/v1oid sha256:153cf3fae2385d51fe8729d3a1c059f611ca47a3fc501049708114d1bbf79049size 16732288
app.py+58-32
data.csv
embeddings.npy+1-1
embeddings2.npy+1-1