Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -167,12 +167,10 @@ with tab1:
|
|
167 |
except Exception:
|
168 |
continue
|
169 |
|
170 |
-
|
171 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
172 |
with tab2:
|
173 |
-
ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
|
174 |
|
175 |
-
# Session states
|
176 |
if "image_thread_id" not in st.session_state:
|
177 |
st.session_state.image_thread_id = None
|
178 |
if "image_response" not in st.session_state:
|
@@ -182,7 +180,6 @@ with tab2:
|
|
182 |
if "image_lightbox" not in st.session_state:
|
183 |
st.session_state.image_lightbox = None
|
184 |
|
185 |
-
# User input
|
186 |
image_input = st.chat_input("Ask for histology visual references (e.g. ovary histology, mitosis)")
|
187 |
if image_input:
|
188 |
st.session_state.image_response = None
|
@@ -222,7 +219,6 @@ with tab2:
|
|
222 |
response_text = msg.content[0].text.value
|
223 |
st.session_state.image_response = response_text
|
224 |
|
225 |
-
# 🔍 Robustly extract "Image URL" entries
|
226 |
lines = response_text.splitlines()
|
227 |
image_urls = []
|
228 |
for i, line in enumerate(lines):
|
@@ -239,7 +235,6 @@ with tab2:
|
|
239 |
except Exception as e:
|
240 |
st.error(f"❌ Visual Assistant Error: {e}")
|
241 |
|
242 |
-
# Layout split
|
243 |
text_col, image_col = st.columns([2, 1])
|
244 |
|
245 |
with text_col:
|
@@ -254,9 +249,13 @@ with tab2:
|
|
254 |
image_url = item.get("image")
|
255 |
if image_url:
|
256 |
try:
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
|
|
260 |
except Exception as e:
|
261 |
st.warning(f"⚠️ Could not load: {image_url}")
|
262 |
st.error(f"{e}")
|
@@ -264,7 +263,17 @@ with tab2:
|
|
264 |
st.info("ℹ️ No image references found yet.")
|
265 |
|
266 |
if st.session_state.image_lightbox:
|
267 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
if st.button("❌ Close Preview"):
|
269 |
st.session_state.image_lightbox = None
|
270 |
st.rerun()
|
|
|
|
167 |
except Exception:
|
168 |
continue
|
169 |
|
|
|
170 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
171 |
with tab2:
|
172 |
+
ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
|
173 |
|
|
|
174 |
if "image_thread_id" not in st.session_state:
|
175 |
st.session_state.image_thread_id = None
|
176 |
if "image_response" not in st.session_state:
|
|
|
180 |
if "image_lightbox" not in st.session_state:
|
181 |
st.session_state.image_lightbox = None
|
182 |
|
|
|
183 |
image_input = st.chat_input("Ask for histology visual references (e.g. ovary histology, mitosis)")
|
184 |
if image_input:
|
185 |
st.session_state.image_response = None
|
|
|
219 |
response_text = msg.content[0].text.value
|
220 |
st.session_state.image_response = response_text
|
221 |
|
|
|
222 |
lines = response_text.splitlines()
|
223 |
image_urls = []
|
224 |
for i, line in enumerate(lines):
|
|
|
235 |
except Exception as e:
|
236 |
st.error(f"❌ Visual Assistant Error: {e}")
|
237 |
|
|
|
238 |
text_col, image_col = st.columns([2, 1])
|
239 |
|
240 |
with text_col:
|
|
|
249 |
image_url = item.get("image")
|
250 |
if image_url:
|
251 |
try:
|
252 |
+
encoded_url = requests.utils.requote_uri(image_url)
|
253 |
+
r = requests.get(encoded_url, timeout=10)
|
254 |
+
r.raise_for_status()
|
255 |
+
img = Image.open(BytesIO(r.content))
|
256 |
+
st.image(img, caption=encoded_url.split("/")[-1], use_container_width=True)
|
257 |
+
if st.button("🔍 View Full Image", key=f"full_img_{i}"):
|
258 |
+
st.session_state.image_lightbox = encoded_url
|
259 |
except Exception as e:
|
260 |
st.warning(f"⚠️ Could not load: {image_url}")
|
261 |
st.error(f"{e}")
|
|
|
263 |
st.info("ℹ️ No image references found yet.")
|
264 |
|
265 |
if st.session_state.image_lightbox:
|
266 |
+
st.markdown("### 🔬 Full Image View")
|
267 |
+
try:
|
268 |
+
img_url = st.session_state.image_lightbox
|
269 |
+
r = requests.get(img_url, timeout=10)
|
270 |
+
r.raise_for_status()
|
271 |
+
full_img = Image.open(BytesIO(r.content))
|
272 |
+
st.image(full_img, caption=img_url.split("/")[-1], use_container_width=True)
|
273 |
+
except Exception as e:
|
274 |
+
st.warning("⚠️ Could not load full image.")
|
275 |
+
st.error(str(e))
|
276 |
if st.button("❌ Close Preview"):
|
277 |
st.session_state.image_lightbox = None
|
278 |
st.rerun()
|
279 |
+
|