Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
from html import escape
|
| 2 |
import re
|
| 3 |
import streamlit as st
|
| 4 |
import pandas as pd
|
|
@@ -6,14 +5,6 @@ import numpy as np
|
|
| 6 |
from transformers import CLIPProcessor, CLIPModel
|
| 7 |
from st_clickable_images import clickable_images
|
| 8 |
|
| 9 |
-
@st.cache(
|
| 10 |
-
show_spinner=False,
|
| 11 |
-
hash_funcs={
|
| 12 |
-
CLIPModel: lambda _: None,
|
| 13 |
-
CLIPProcessor: lambda _: None,
|
| 14 |
-
dict: lambda _: None,
|
| 15 |
-
},
|
| 16 |
-
)
|
| 17 |
def load():
|
| 18 |
model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
|
| 19 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
|
|
@@ -87,17 +78,6 @@ def image_search(query, corpus, n_results=24):
|
|
| 87 |
for i in results
|
| 88 |
]
|
| 89 |
|
| 90 |
-
description = """
|
| 91 |
-
# Semantic image search
|
| 92 |
-
**Enter your query and hit enter**
|
| 93 |
-
"""
|
| 94 |
-
|
| 95 |
-
howto = """
|
| 96 |
-
- Click image to find similar images
|
| 97 |
-
- Use "**;**" to combine multiple queries)
|
| 98 |
-
- Use "**EXCLUDING**", to exclude a query
|
| 99 |
-
"""
|
| 100 |
-
|
| 101 |
def main():
|
| 102 |
st.markdown(
|
| 103 |
"""
|
|
@@ -132,36 +112,42 @@ def main():
|
|
| 132 |
</style>""",
|
| 133 |
unsafe_allow_html=True,
|
| 134 |
)
|
| 135 |
-
|
| 136 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
if "query" in st.session_state:
|
| 139 |
-
query = st.text_input("", value=st.session_state["query"])
|
| 140 |
else:
|
| 141 |
-
query = st.text_input("", value="lighthouse")
|
| 142 |
-
corpus = st.radio("", ["Unsplash"])
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
if
|
| 158 |
-
change_query =
|
| 159 |
-
|
| 160 |
-
if clicked != st.session_state["last_clicked"]:
|
| 161 |
change_query = True
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
if __name__ == "__main__":
|
| 167 |
main()
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
|
|
|
| 5 |
from transformers import CLIPProcessor, CLIPModel
|
| 6 |
from st_clickable_images import clickable_images
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def load():
|
| 9 |
model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
|
| 10 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
|
|
|
|
| 78 |
for i in results
|
| 79 |
]
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def main():
|
| 82 |
st.markdown(
|
| 83 |
"""
|
|
|
|
| 112 |
</style>""",
|
| 113 |
unsafe_allow_html=True,
|
| 114 |
)
|
| 115 |
+
|
| 116 |
+
st.sidebar.markdown("# Semantic Image Search")
|
| 117 |
+
st.sidebar.markdown("**Enter your query and hit enter**")
|
| 118 |
+
st.sidebar.markdown("- Click image to find similar images")
|
| 119 |
+
st.sidebar.markdown("- Use '**;**' to combine multiple queries")
|
| 120 |
+
st.sidebar.markdown("- Use '**EXCLUDING**' to exclude a query")
|
| 121 |
|
| 122 |
if "query" in st.session_state:
|
| 123 |
+
query = st.sidebar.text_input("Query", value=st.session_state["query"])
|
| 124 |
else:
|
| 125 |
+
query = st.sidebar.text_input("Query", value="lighthouse")
|
| 126 |
+
corpus = st.sidebar.radio("Corpus", ["Unsplash"])
|
| 127 |
+
|
| 128 |
+
if st.sidebar.button("Submit"):
|
| 129 |
+
if len(query) > 0:
|
| 130 |
+
results = image_search(query, corpus)
|
| 131 |
+
clicked = clickable_images(
|
| 132 |
+
[result[0] for result in results],
|
| 133 |
+
titles=[result[1] for result in results],
|
| 134 |
+
div_style={
|
| 135 |
+
"display": "flex",
|
| 136 |
+
"justify-content": "center",
|
| 137 |
+
"flex-wrap": "wrap",
|
| 138 |
+
},
|
| 139 |
+
img_style={"margin": "2px", "height": "200px"},
|
| 140 |
+
)
|
| 141 |
+
if clicked >= 0:
|
| 142 |
+
change_query = False
|
| 143 |
+
if "last_clicked" not in st.session_state:
|
|
|
|
| 144 |
change_query = True
|
| 145 |
+
else:
|
| 146 |
+
if clicked != st.session_state["last_clicked"]:
|
| 147 |
+
change_query = True
|
| 148 |
+
if change_query:
|
| 149 |
+
st.session_state["query"] = f"[{corpus}:{results[clicked][2]}]"
|
| 150 |
+
st.experimental_rerun()
|
| 151 |
|
| 152 |
if __name__ == "__main__":
|
| 153 |
main()
|