Spaces:
Runtime error
Runtime error
File size: 1,596 Bytes
5374f7d 71c68b0 4bb530f af5c01f e2d993c 5374f7d 953b27b e2d993c 953b27b e2d993c 6738f4a f979ac9 6738f4a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import json
import numpy as np
import streamlit as st
from st_clickable_images import clickable_images
from clip_multilingual.search import MultiLingualSearch
from clip_multilingual.models import Tokenizer
@st.cache(
suppress_st_warning=True,
hash_funcs={
Tokenizer: lambda _: None
}
)
def load_model():
unsplash_base_folder = './'
all_embeddings = np.load(f'{unsplash_base_folder}/embeddings.npy')
with open(f'{unsplash_base_folder}/urls.json') as f:
all_urls = json.load(f)
return MultiLingualSearch(all_embeddings, all_urls)
semantic_search = load_model()
description = '''
# Multilingual Semantic Search
**Search images in any language powered by OpenAI's [CLIP](https://openai.com/blog/clip/) and XMLRoberta.**
'''
st.sidebar.markdown(description)
examples = [
'pessoas sorrindo',
'微笑的人',
'насмејани људи',
]
for example in examples:
if st.sidebar.button(example):
st.session_state.query = example
_, c, _ = st.columns((1, 3, 1))
if "query" in st.session_state:
query = c.text_input("", value=st.session_state["query"])
else:
query = c.text_input("", value="people smiling")
if len(query) > 0:
results = semantic_search.search(query)
clicked = clickable_images(
[result['image'] for result in results],
titles=[f'Prob: {result["prob"]}' for result in results],
div_style={
"display": "flex",
"justify-content": "center",
"flex-wrap": "wrap",
},
img_style={"margin": "2px", "height": "200px"},
)
|