IAMTFRMZA commited on
Commit
ae60e08
·
verified ·
1 Parent(s): af56ec6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -166,9 +166,10 @@ with tab1:
166
 
167
 
168
  # ------------------ Tab 2: Visual Reference Search ------------------
169
-
170
  with tab2:
171
- ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO" # Your pathology assistant
 
 
172
  if "image_thread_id" not in st.session_state:
173
  st.session_state.image_thread_id = None
174
  if "image_response" not in st.session_state:
@@ -178,7 +179,7 @@ with tab2:
178
  if "image_lightbox" not in st.session_state:
179
  st.session_state.image_lightbox = None
180
 
181
- # User input for assistant
182
  image_input = st.chat_input("Ask for histology visual references (e.g. ovary histology, mitosis)")
183
  if image_input:
184
  st.session_state.image_response = None
@@ -218,18 +219,24 @@ with tab2:
218
  response_text = msg.content[0].text.value
219
  st.session_state.image_response = response_text
220
 
221
- # Try to extract structured data (JSON-like block) if present
222
- try:
223
- st.session_state.image_results = json.loads(response_text.strip("`json "))
224
- except:
225
- # Fallback to manually extract all GitHub image URLs
226
- urls = re.findall(r'https://raw\.githubusercontent\.com/[^\s)>\]]+\.png', response_text.replace("\n", " "))
227
- st.session_state.image_results = [{"image": url} for url in urls]
 
 
 
 
 
 
228
  break
229
  except Exception as e:
230
  st.error(f"❌ Visual Assistant Error: {e}")
231
 
232
- # Display split UI (image results right, response text left)
233
  text_col, image_col = st.columns([2, 1])
234
 
235
  with text_col:
@@ -241,7 +248,7 @@ with tab2:
241
  if st.session_state.image_results:
242
  st.markdown("### 🖼️ Image Preview(s)")
243
  for i, item in enumerate(st.session_state.image_results):
244
- image_url = item.get("image") or item.get("Image URL") or item.get("ImageURL")
245
  if image_url:
246
  try:
247
  st.image(image_url, caption=image_url.split("/")[-1], use_container_width=True)
 
166
 
167
 
168
  # ------------------ Tab 2: Visual Reference Search ------------------
 
169
  with tab2:
170
+ ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO" # Your visual reference assistant ID
171
+
172
+ # Session states
173
  if "image_thread_id" not in st.session_state:
174
  st.session_state.image_thread_id = None
175
  if "image_response" not in st.session_state:
 
179
  if "image_lightbox" not in st.session_state:
180
  st.session_state.image_lightbox = None
181
 
182
+ # User input
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
+ # 🔍 Robustly extract "Image URL" entries
223
+ lines = response_text.splitlines()
224
+ image_urls = []
225
+ for i, line in enumerate(lines):
226
+ if "Image URL:" in line:
227
+ url = line.split("Image URL:")[-1].strip()
228
+ if not url.startswith("http") and i + 1 < len(lines):
229
+ next_line = lines[i + 1].strip()
230
+ if next_line.startswith("http"):
231
+ url = next_line
232
+ if url.startswith("http"):
233
+ image_urls.append(url)
234
+ st.session_state.image_results = [{"image": url} for url in image_urls]
235
  break
236
  except Exception as e:
237
  st.error(f"❌ Visual Assistant Error: {e}")
238
 
239
+ # Layout split
240
  text_col, image_col = st.columns([2, 1])
241
 
242
  with text_col:
 
248
  if st.session_state.image_results:
249
  st.markdown("### 🖼️ Image Preview(s)")
250
  for i, item in enumerate(st.session_state.image_results):
251
+ image_url = item.get("image")
252
  if image_url:
253
  try:
254
  st.image(image_url, caption=image_url.split("/")[-1], use_container_width=True)