File size: 2,413 Bytes
ad24055
 
 
df6b9aa
d56daaf
df6b9aa
ad24055
 
86e5db5
 
c5f7453
d55e80c
ad24055
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24fd023
 
ad24055
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eedd04d
59e770b
ad24055
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import streamlit as st
import numpy as np
import torch

from datasets import load_dataset
from html import escape
from transformers import RobertaModel, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained('volen/nft-text', use_auth_token=st.secrets["access_token"])
text_encoder = RobertaModel.from_pretrained('volen/nft-text', use_auth_token=st.secrets["access_token"]).eval()
image_embeddings = torch.load('image_embeddings.pt', map_location=torch.device('cpu'))
links = np.load('image_links.npy', allow_pickle=True)


@st.experimental_memo
def image_search(query, top_k=10):
    with torch.no_grad():
        text_embedding = text_encoder(**tokenizer(query, return_tensors='pt')).pooler_output
    _, indices = torch.cosine_similarity(image_embeddings, text_embedding).sort(descending=True)
    return [links[i] for i in indices[:top_k]]


def get_html(url_list):
    html = "<div style='margin-top: 50px; max-width: 1100px; display: flex; flex-wrap: wrap; justify-content: space-evenly'>"
    for url in url_list:
        html2 = f"<img style='height: 180px; margin: 2px' src='{escape(url)}'>"  
        html = html + html2
    html += "</div>"
    return html


description = '''
# nft search 
- Enter your search and hit enter
- Note: So far we only support BAYC, cool cats, doodles and MAYC
'''


def main():
    st.markdown('''
                <style>
                .block-container{
                  max-width: 1200px;
                }
                section.main>div:first-child {
                  padding-top: 0px;
                }
                section:not(.main)>div:first-child {
                  padding-top: 30px;
                }
                div.reportview-container > section:first-child{
                  max-width: 320px;
                }
                #MainMenu {
                  visibility: hidden;
                }
                footer {
                  visibility: hidden;
                }
                </style>''',
            
                unsafe_allow_html=True)        
         
                
    st.sidebar.markdown(description)
    _, c, _ = st.columns((1, 3, 1))
    query = c.text_input('search box', value='cat beanie')
    c.text("It'll take a few secs to load new images")
    if len(query) > 0:
        results = image_search(query)
        st.markdown(get_html(results), unsafe_allow_html=True)


if __name__ == '__main__':
    main()