Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -164,7 +164,7 @@ with tab2:
|
|
164 |
|
165 |
for key in ["image_thread_id", "image_response", "image_results", "image_lightbox"]:
|
166 |
if key not in st.session_state:
|
167 |
-
st.session_state[key] = None if
|
168 |
|
169 |
st.markdown("### π Search Histology Visual References")
|
170 |
image_input = st.chat_input("e.g. mitosis, seminiferous tubule, cell cycle")
|
@@ -189,7 +189,7 @@ with tab2:
|
|
189 |
assistant_id=ASSISTANT_ID
|
190 |
)
|
191 |
|
192 |
-
with st.spinner("𧬠Retrieving
|
193 |
while True:
|
194 |
status = client.beta.threads.runs.retrieve(
|
195 |
thread_id=st.session_state.image_thread_id,
|
@@ -203,46 +203,55 @@ with tab2:
|
|
203 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.image_thread_id)
|
204 |
for msg in reversed(messages.data):
|
205 |
if msg.role == "assistant":
|
206 |
-
response_text = msg.content[0].text.value
|
207 |
st.session_state.image_response = response_text
|
208 |
|
209 |
-
#
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
|
|
|
|
|
|
|
|
|
|
225 |
break
|
226 |
|
227 |
except Exception as e:
|
228 |
st.error(f"β Assistant Error: {e}")
|
229 |
|
230 |
-
#
|
231 |
if st.session_state.image_results:
|
232 |
st.markdown("### πΌοΈ Image Results")
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
st.
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
|
|
|
|
|
|
|
|
246 |
|
247 |
if st.session_state.image_lightbox:
|
248 |
st.markdown("### π¬ Full Image View")
|
|
|
164 |
|
165 |
for key in ["image_thread_id", "image_response", "image_results", "image_lightbox"]:
|
166 |
if key not in st.session_state:
|
167 |
+
st.session_state[key] = None if key in ["image_thread_id", "image_response", "image_lightbox"] else []
|
168 |
|
169 |
st.markdown("### π Search Histology Visual References")
|
170 |
image_input = st.chat_input("e.g. mitosis, seminiferous tubule, cell cycle")
|
|
|
189 |
assistant_id=ASSISTANT_ID
|
190 |
)
|
191 |
|
192 |
+
with st.spinner("𧬠Retrieving histology references..."):
|
193 |
while True:
|
194 |
status = client.beta.threads.runs.retrieve(
|
195 |
thread_id=st.session_state.image_thread_id,
|
|
|
203 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.image_thread_id)
|
204 |
for msg in reversed(messages.data):
|
205 |
if msg.role == "assistant":
|
206 |
+
response_text = msg.content[0].text.value.strip()
|
207 |
st.session_state.image_response = response_text
|
208 |
|
209 |
+
# β
Robust markdown block parser
|
210 |
+
blocks = response_text.split("### πΌοΈ")[1:]
|
211 |
+
results = []
|
212 |
+
for b in blocks:
|
213 |
+
lines = b.strip().splitlines()
|
214 |
+
card = {
|
215 |
+
"title": lines[0].strip() if lines else "Untitled",
|
216 |
+
"desc": "",
|
217 |
+
"url": ""
|
218 |
+
}
|
219 |
+
for line in lines:
|
220 |
+
if line.startswith("- **Description:**"):
|
221 |
+
card["desc"] = line.replace("- **Description:**", "").strip()
|
222 |
+
elif line.startswith("- **Image URL:**"):
|
223 |
+
card["url"] = line.replace("- **Image URL:**", "").strip()
|
224 |
+
elif line.startswith("http") and not card["url"]:
|
225 |
+
card["url"] = line.strip()
|
226 |
+
if card["url"]:
|
227 |
+
results.append(card)
|
228 |
+
|
229 |
+
st.session_state.image_results = results
|
230 |
break
|
231 |
|
232 |
except Exception as e:
|
233 |
st.error(f"β Assistant Error: {e}")
|
234 |
|
235 |
+
# πΌοΈ Display Image Cards in 4-column layout
|
236 |
if st.session_state.image_results:
|
237 |
st.markdown("### πΌοΈ Image Results")
|
238 |
+
for i in range(0, len(st.session_state.image_results), 4):
|
239 |
+
row = st.columns(4)
|
240 |
+
for j, col in enumerate(row):
|
241 |
+
idx = i + j
|
242 |
+
if idx < len(st.session_state.image_results):
|
243 |
+
item = st.session_state.image_results[idx]
|
244 |
+
with col:
|
245 |
+
try:
|
246 |
+
img = Image.open(BytesIO(requests.get(item["url"]).content))
|
247 |
+
st.image(img, use_container_width=True)
|
248 |
+
except:
|
249 |
+
st.image("https://via.placeholder.com/150?text=Image+Error", use_container_width=True)
|
250 |
+
|
251 |
+
st.markdown(f"**{item.get('title', 'No Title')}**", unsafe_allow_html=True)
|
252 |
+
st.caption(item.get("desc", "")[:150] + "..." if item.get("desc") else "")
|
253 |
+
if st.button("π View", key=f"img_{idx}"):
|
254 |
+
st.session_state.image_lightbox = item["url"]
|
255 |
|
256 |
if st.session_state.image_lightbox:
|
257 |
st.markdown("### π¬ Full Image View")
|