Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,16 +45,38 @@ with tab_caption:
|
|
| 45 |
else:
|
| 46 |
st.error("Error: " + resp.text)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
with tab_text2img:
|
| 49 |
if text_input and st.button("Search Images"):
|
| 50 |
data = {"model_name": model_name, "query": text_input, "top_k": top_k}
|
| 51 |
resp = requests.post(f"{API_BASE}/search/text2img", data=data)
|
| 52 |
if resp.status_code == 200:
|
| 53 |
results = resp.json()
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
else:
|
| 57 |
-
st.error("
|
| 58 |
|
| 59 |
with tab_img2text:
|
| 60 |
if image_file and st.button("Retrieve Text"):
|
|
@@ -66,17 +88,59 @@ with tab_img2text:
|
|
| 66 |
else:
|
| 67 |
st.error("Error: " + resp.text)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
with tab_img2img:
|
| 70 |
if image_file and st.button("Retrieve Similar Images"):
|
| 71 |
files = {"file": image_file.getvalue()}
|
| 72 |
data = {"model_name": model_name, "top_k": top_k}
|
| 73 |
resp = requests.post(f"{API_BASE}/search/img2img", files=files, data=data)
|
|
|
|
| 74 |
if resp.status_code == 200:
|
| 75 |
results = resp.json()
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
else:
|
| 79 |
-
st.error("
|
| 80 |
|
| 81 |
with tab_text2text:
|
| 82 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
else:
|
| 46 |
st.error("Error: " + resp.text)
|
| 47 |
|
| 48 |
+
# with tab_text2img:
|
| 49 |
+
# if text_input and st.button("Search Images"):
|
| 50 |
+
# data = {"model_name": model_name, "query": text_input, "top_k": top_k}
|
| 51 |
+
# resp = requests.post(f"{API_BASE}/search/text2img", data=data)
|
| 52 |
+
# if resp.status_code == 200:
|
| 53 |
+
# results = resp.json()
|
| 54 |
+
# for res in results:
|
| 55 |
+
# st.image(res["image_path"], caption=f"Score: {res['score']:.3f}")
|
| 56 |
+
# else:
|
| 57 |
+
# st.error("Error: " + resp.text)
|
| 58 |
+
|
| 59 |
with tab_text2img:
|
| 60 |
if text_input and st.button("Search Images"):
|
| 61 |
data = {"model_name": model_name, "query": text_input, "top_k": top_k}
|
| 62 |
resp = requests.post(f"{API_BASE}/search/text2img", data=data)
|
| 63 |
if resp.status_code == 200:
|
| 64 |
results = resp.json()
|
| 65 |
+
if results:
|
| 66 |
+
cols = st.columns(3)
|
| 67 |
+
for idx, res in enumerate(results):
|
| 68 |
+
with cols[idx % 3]:
|
| 69 |
+
try:
|
| 70 |
+
st.image(res["image_path"],
|
| 71 |
+
caption=f"Score: {res['score']:.3f}",
|
| 72 |
+
use_column_width=True)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
st.warning(f"Could not load: {res['image_path']}")
|
| 75 |
+
st.write(f"Score: {res['score']:.3f}")
|
| 76 |
+
else:
|
| 77 |
+
st.info("No matching images in demo set.")
|
| 78 |
else:
|
| 79 |
+
st.error(f"Backend error: {resp.status_code} - {resp.text}")
|
| 80 |
|
| 81 |
with tab_img2text:
|
| 82 |
if image_file and st.button("Retrieve Text"):
|
|
|
|
| 88 |
else:
|
| 89 |
st.error("Error: " + resp.text)
|
| 90 |
|
| 91 |
+
# with tab_img2img:
|
| 92 |
+
# if image_file and st.button("Retrieve Similar Images"):
|
| 93 |
+
# files = {"file": image_file.getvalue()}
|
| 94 |
+
# data = {"model_name": model_name, "top_k": top_k}
|
| 95 |
+
# resp = requests.post(f"{API_BASE}/search/img2img", files=files, data=data)
|
| 96 |
+
# if resp.status_code == 200:
|
| 97 |
+
# results = resp.json()
|
| 98 |
+
# for res in results:
|
| 99 |
+
# st.image(res["image_path"], caption=f"Score: {res['score']:.3f}")
|
| 100 |
+
# else:
|
| 101 |
+
# st.error("Error: " + resp.text)
|
| 102 |
+
|
| 103 |
with tab_img2img:
|
| 104 |
if image_file and st.button("Retrieve Similar Images"):
|
| 105 |
files = {"file": image_file.getvalue()}
|
| 106 |
data = {"model_name": model_name, "top_k": top_k}
|
| 107 |
resp = requests.post(f"{API_BASE}/search/img2img", files=files, data=data)
|
| 108 |
+
|
| 109 |
if resp.status_code == 200:
|
| 110 |
results = resp.json()
|
| 111 |
+
if results:
|
| 112 |
+
cols = st.columns(3)
|
| 113 |
+
for idx, res in enumerate(results):
|
| 114 |
+
with cols[idx % 3]:
|
| 115 |
+
try:
|
| 116 |
+
st.image(
|
| 117 |
+
res["image_path"],
|
| 118 |
+
caption=f"Score: {res['score']:.3f}",
|
| 119 |
+
use_column_width=True
|
| 120 |
+
)
|
| 121 |
+
except Exception as e:
|
| 122 |
+
st.warning(f"Could not load image: {res['image_path']}")
|
| 123 |
+
st.write(f"Score: {res['score']:.3f}")
|
| 124 |
+
else:
|
| 125 |
+
st.info("No similar images found in the demo set.")
|
| 126 |
else:
|
| 127 |
+
st.error(f"Backend error: {resp.status_code} - {resp.text}")
|
| 128 |
|
| 129 |
with tab_text2text:
|
| 130 |
+
text_input_tt = st.text_input("Enter text to find similar captions",
|
| 131 |
+
placeholder="A child playing with water in the garden")
|
| 132 |
+
|
| 133 |
+
if text_input_tt and st.button("Search Similar Captions"):
|
| 134 |
+
data = {"model_name": model_name, "query": text_input_tt, "top_k": top_k}
|
| 135 |
+
resp = requests.post(f"{API_BASE}/search/text2text", data=data)
|
| 136 |
+
|
| 137 |
+
if resp.status_code == 200:
|
| 138 |
+
results = resp.json()
|
| 139 |
+
if results:
|
| 140 |
+
st.subheader("Top similar captions:")
|
| 141 |
+
for idx, res in enumerate(results, 1):
|
| 142 |
+
st.markdown(f"**{idx}.** {res['caption']} \nScore: `{res['score']:.4f}`")
|
| 143 |
+
else:
|
| 144 |
+
st.info("No similar captions found.")
|
| 145 |
+
else:
|
| 146 |
+
st.error(f"Error: {resp.status_code} - {resp.text}")
|