Spaces:
Runtime error
Runtime error
eliphatfs
commited on
Commit
·
b9f9a14
1
Parent(s):
ba37554
Retrieval filters.
Browse files
app.py
CHANGED
|
@@ -230,11 +230,33 @@ def retrieval_results(results):
|
|
| 230 |
st.markdown(f"[{quote_name}]({ext_link})")
|
| 231 |
|
| 232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
def demo_retrieval():
|
| 234 |
with tab_text:
|
| 235 |
with st.form("rtextform"):
|
| 236 |
k = st.slider("Shapes to Retrieve", 1, 100, 16, key='rtext')
|
| 237 |
text = st.text_input("Input Text", key="inputrtext")
|
|
|
|
| 238 |
if st.form_submit_button("Run with Text") or auto_submit("rtextauto"):
|
| 239 |
prog.progress(0.49, "Computing Embeddings")
|
| 240 |
device = clip_model.device
|
|
@@ -243,7 +265,7 @@ def demo_retrieval():
|
|
| 243 |
).to(device)
|
| 244 |
enc = clip_model.get_text_features(**tn).float().cpu()
|
| 245 |
prog.progress(0.7, "Running Retrieval")
|
| 246 |
-
retrieval_results(retrieval.retrieve(enc, k))
|
| 247 |
prog.progress(1.0, "Idle")
|
| 248 |
picked_sample = st.selectbox("Examples", ["Select..."] + samples_index.retrieval_texts)
|
| 249 |
text_last_example = st.session_state.get('text_last_example', None)
|
|
@@ -259,6 +281,7 @@ def demo_retrieval():
|
|
| 259 |
with st.form("rimgform"):
|
| 260 |
k = st.slider("Shapes to Retrieve", 1, 100, 16, key='rimage')
|
| 261 |
pic = st.file_uploader("Upload an Image", key='rimageinput')
|
|
|
|
| 262 |
if st.form_submit_button("Run with Image"):
|
| 263 |
submit = True
|
| 264 |
results_container = st.container()
|
|
@@ -274,13 +297,14 @@ def demo_retrieval():
|
|
| 274 |
tn = clip_prep(images=[img], return_tensors="pt").to(device)
|
| 275 |
enc = clip_model.get_image_features(pixel_values=tn['pixel_values'].type(half)).float().cpu()
|
| 276 |
prog.progress(0.7, "Running Retrieval")
|
| 277 |
-
retrieval_results(retrieval.retrieve(enc, k))
|
| 278 |
prog.progress(1.0, "Idle")
|
| 279 |
|
| 280 |
with tab_pc:
|
| 281 |
with st.form("rpcform"):
|
| 282 |
k = st.slider("Shapes to Retrieve", 1, 100, 16, key='rpc')
|
| 283 |
load_data = misc_utils.input_3d_shape('retpc')
|
|
|
|
| 284 |
if st.form_submit_button("Run with Shape") or auto_submit('rpcauto'):
|
| 285 |
pc = load_data(prog)
|
| 286 |
col2 = misc_utils.render_pc(pc)
|
|
@@ -288,7 +312,7 @@ def demo_retrieval():
|
|
| 288 |
ref_dev = next(model_g14.parameters()).device
|
| 289 |
enc = model_g14(torch.tensor(pc[:, [0, 2, 1, 3, 4, 5]].T[None], device=ref_dev)).cpu()
|
| 290 |
prog.progress(0.7, "Running Retrieval")
|
| 291 |
-
retrieval_results(retrieval.retrieve(enc, k))
|
| 292 |
prog.progress(1.0, "Idle")
|
| 293 |
if image_examples(samples_index.pret, 3):
|
| 294 |
queue_auto_submit("rpcauto")
|
|
|
|
| 230 |
st.markdown(f"[{quote_name}]({ext_link})")
|
| 231 |
|
| 232 |
|
| 233 |
+
def retrieval_filter_expand(key):
|
| 234 |
+
with st.expander("Filters"):
|
| 235 |
+
sim_th = st.slider("Similarity Threshold", 0.05, 0.5, 0.1, key=key + 'rtsimth')
|
| 236 |
+
tag = st.text_input("Has Tag", "", key=key + 'rthastag')
|
| 237 |
+
col1, col2 = st.columns(2)
|
| 238 |
+
face_min = int(col1.text_input("Face Count Min", "0", key=key + 'rtfcmin'))
|
| 239 |
+
face_max = int(col2.text_input("Face Count Max", "34985808", key=key + 'rtfcmax'))
|
| 240 |
+
col1, col2 = st.columns(2)
|
| 241 |
+
anim_min = int(col1.text_input("Animation Count Min", "0", key=key + 'rtacmin'))
|
| 242 |
+
anim_max = int(col2.text_input("Animation Count Max", "563", key=key + 'rtacmax'))
|
| 243 |
+
tag_n = not bool(tag.strip())
|
| 244 |
+
anim_n = not (anim_min > 0 or anim_max < 563)
|
| 245 |
+
face_n = not (face_min > 0 or face_max < 34985808)
|
| 246 |
+
filter_fn = lambda x: (
|
| 247 |
+
(anim_n or anim_min <= x['anims'] <= anim_max)
|
| 248 |
+
and (face_n or face_min <= x['faces'] <= face_max)
|
| 249 |
+
and (tag_n or tag in x['tags'])
|
| 250 |
+
)
|
| 251 |
+
return sim_th, filter_fn
|
| 252 |
+
|
| 253 |
+
|
| 254 |
def demo_retrieval():
|
| 255 |
with tab_text:
|
| 256 |
with st.form("rtextform"):
|
| 257 |
k = st.slider("Shapes to Retrieve", 1, 100, 16, key='rtext')
|
| 258 |
text = st.text_input("Input Text", key="inputrtext")
|
| 259 |
+
sim_th, filter_fn = retrieval_filter_expand('text')
|
| 260 |
if st.form_submit_button("Run with Text") or auto_submit("rtextauto"):
|
| 261 |
prog.progress(0.49, "Computing Embeddings")
|
| 262 |
device = clip_model.device
|
|
|
|
| 265 |
).to(device)
|
| 266 |
enc = clip_model.get_text_features(**tn).float().cpu()
|
| 267 |
prog.progress(0.7, "Running Retrieval")
|
| 268 |
+
retrieval_results(retrieval.retrieve(enc, k, sim_th, filter_fn))
|
| 269 |
prog.progress(1.0, "Idle")
|
| 270 |
picked_sample = st.selectbox("Examples", ["Select..."] + samples_index.retrieval_texts)
|
| 271 |
text_last_example = st.session_state.get('text_last_example', None)
|
|
|
|
| 281 |
with st.form("rimgform"):
|
| 282 |
k = st.slider("Shapes to Retrieve", 1, 100, 16, key='rimage')
|
| 283 |
pic = st.file_uploader("Upload an Image", key='rimageinput')
|
| 284 |
+
sim_th, filter_fn = retrieval_filter_expand('image')
|
| 285 |
if st.form_submit_button("Run with Image"):
|
| 286 |
submit = True
|
| 287 |
results_container = st.container()
|
|
|
|
| 297 |
tn = clip_prep(images=[img], return_tensors="pt").to(device)
|
| 298 |
enc = clip_model.get_image_features(pixel_values=tn['pixel_values'].type(half)).float().cpu()
|
| 299 |
prog.progress(0.7, "Running Retrieval")
|
| 300 |
+
retrieval_results(retrieval.retrieve(enc, k, sim_th, filter_fn))
|
| 301 |
prog.progress(1.0, "Idle")
|
| 302 |
|
| 303 |
with tab_pc:
|
| 304 |
with st.form("rpcform"):
|
| 305 |
k = st.slider("Shapes to Retrieve", 1, 100, 16, key='rpc')
|
| 306 |
load_data = misc_utils.input_3d_shape('retpc')
|
| 307 |
+
sim_th, filter_fn = retrieval_filter_expand('pc')
|
| 308 |
if st.form_submit_button("Run with Shape") or auto_submit('rpcauto'):
|
| 309 |
pc = load_data(prog)
|
| 310 |
col2 = misc_utils.render_pc(pc)
|
|
|
|
| 312 |
ref_dev = next(model_g14.parameters()).device
|
| 313 |
enc = model_g14(torch.tensor(pc[:, [0, 2, 1, 3, 4, 5]].T[None], device=ref_dev)).cpu()
|
| 314 |
prog.progress(0.7, "Running Retrieval")
|
| 315 |
+
retrieval_results(retrieval.retrieve(enc, k, sim_th, filter_fn))
|
| 316 |
prog.progress(1.0, "Idle")
|
| 317 |
if image_examples(samples_index.pret, 3):
|
| 318 |
queue_auto_submit("rpcauto")
|