IAMTFRMZA commited on
Commit
90cb5dd
Β·
verified Β·
1 Parent(s): fa39de6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -33
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 "id" in key or "response" in key else []
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 images..."):
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
- # Extract URLs and details for grid card display
210
- lines = response_text.splitlines()
211
- block = {}
212
- all_cards = []
213
- for line in lines:
214
- if line.strip().startswith("### πŸ–ΌοΈ"):
215
- if block: all_cards.append(block)
216
- block = {"title": line.replace("### πŸ–ΌοΈ", "").strip()}
217
- elif "Page:" in line:
218
- block["page"] = line.split("Page:")[1].strip()
219
- elif "Description:" in line:
220
- block["desc"] = line.split("Description:")[1].strip()
221
- elif "Image URL:" in line:
222
- block["url"] = line.split("Image URL:")[1].strip()
223
- if block: all_cards.append(block)
224
- st.session_state.image_results = all_cards
 
 
 
 
 
225
  break
226
 
227
  except Exception as e:
228
  st.error(f"❌ Assistant Error: {e}")
229
 
230
- # πŸ” Display Image Cards
231
  if st.session_state.image_results:
232
  st.markdown("### πŸ–ΌοΈ Image Results")
233
- cols = st.columns(4)
234
- for idx, item in enumerate(st.session_state.image_results):
235
- with cols[idx % 4]:
236
- try:
237
- img = Image.open(BytesIO(requests.get(item["url"]).content))
238
- st.image(img, use_container_width=True)
239
- except:
240
- st.image("https://via.placeholder.com/150?text=Error", use_container_width=True)
241
-
242
- st.markdown(f"**{item['title']}**", unsafe_allow_html=True)
243
- st.caption(item.get("desc", "")[:150] + "...")
244
- if st.button("πŸ” View", key=f"img_{idx}"):
245
- st.session_state.image_lightbox = item["url"]
 
 
 
 
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")