IAMTFRMZA commited on
Commit
021e796
ยท
verified ยท
1 Parent(s): 5c33d13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -54
app.py CHANGED
@@ -162,21 +162,25 @@ with tab1:
162
  with tab2:
163
  ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
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")
 
 
171
 
 
172
  if image_input:
173
  st.session_state.image_response = None
174
  st.session_state.image_results = []
175
  st.session_state.image_lightbox = None
176
 
177
  try:
178
- if not st.session_state.image_thread_id:
179
- st.session_state.image_thread_id = client.beta.threads.create().id
 
180
 
181
  client.beta.threads.messages.create(
182
  thread_id=st.session_state.image_thread_id,
@@ -189,72 +193,86 @@ with tab2:
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,
196
  run_id=run.id
197
  )
198
- if status.status in ("completed", "failed", "cancelled"):
199
  break
200
  time.sleep(1)
201
 
202
- if status.status == "completed":
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
- # โœ… Reliable parser for markdown blocks
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 "Description:" in line:
221
- card["desc"] = line.split("Description:")[1].strip()
222
- elif "Image URL:" in line:
223
- card["url"] = line.split("Image URL:")[1].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 images in vertical stack
236
- if st.session_state.image_results:
237
- st.markdown("### ๐Ÿ–ผ๏ธ Image Results")
238
- for idx, item in enumerate(st.session_state.image_results):
239
- try:
240
- img = Image.open(BytesIO(requests.get(item["url"], timeout=10).content))
241
- st.image(img, use_container_width=True)
242
- except:
243
- st.image("https://via.placeholder.com/150?text=Image+Error", use_container_width=True)
244
 
245
- st.markdown(f"**{item.get('title', 'No Title')}**", unsafe_allow_html=True)
246
- st.caption(item.get("desc", "")[:200] + "..." if item.get("desc") else "")
247
- if st.button("๐Ÿ” View", key=f"img_{idx}"):
248
- st.session_state.image_lightbox = item["url"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
- # ๐Ÿ”ฌ Full image lightbox
251
  if st.session_state.image_lightbox:
252
  st.markdown("### ๐Ÿ”ฌ Full Image View")
253
  try:
254
- full_img = Image.open(BytesIO(requests.get(st.session_state.image_lightbox).content))
255
- st.image(full_img, use_container_width=True)
 
 
 
256
  except Exception as e:
257
- st.error(f"โš ๏ธ Could not load full image: {e}")
258
- if st.button("โŒ Close Image View"):
 
259
  st.session_state.image_lightbox = None
260
  st.rerun()
 
162
  with tab2:
163
  ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
164
 
165
+ if "image_thread_id" not in st.session_state:
166
+ st.session_state.image_thread_id = None
167
+ if "image_response" not in st.session_state:
168
+ st.session_state.image_response = None
169
+ if "image_results" not in st.session_state:
170
+ st.session_state.image_results = []
171
+ if "image_lightbox" not in st.session_state:
172
+ st.session_state.image_lightbox = None
173
 
174
+ image_input = st.chat_input("Ask for histology visual references (e.g. ovary histology, mitosis)")
175
  if image_input:
176
  st.session_state.image_response = None
177
  st.session_state.image_results = []
178
  st.session_state.image_lightbox = None
179
 
180
  try:
181
+ if st.session_state.image_thread_id is None:
182
+ thread = client.beta.threads.create()
183
+ st.session_state.image_thread_id = thread.id
184
 
185
  client.beta.threads.messages.create(
186
  thread_id=st.session_state.image_thread_id,
 
193
  assistant_id=ASSISTANT_ID
194
  )
195
 
196
+ with st.spinner("๐Ÿ”ฌ Searching for histology references..."):
197
  while True:
198
+ run_status = client.beta.threads.runs.retrieve(
199
  thread_id=st.session_state.image_thread_id,
200
  run_id=run.id
201
  )
202
+ if run_status.status in ("completed", "failed", "cancelled"):
203
  break
204
  time.sleep(1)
205
 
206
+ if run_status.status == "completed":
207
  messages = client.beta.threads.messages.list(thread_id=st.session_state.image_thread_id)
208
  for msg in reversed(messages.data):
209
  if msg.role == "assistant":
210
+ response_text = msg.content[0].text.value
211
  st.session_state.image_response = response_text
212
 
213
+ # โœ… Extract Image URLs from assistant markdown
214
+ lines = response_text.splitlines()
215
+ image_urls = []
216
+ expecting_url = False
217
+
218
+ for line in lines:
219
+ line_clean = line.strip().replace("**", "")
220
+ if "Image URL:" in line_clean:
221
+ parts = line_clean.split("Image URL:")
222
+ if len(parts) > 1 and parts[1].strip().startswith("http"):
223
+ image_urls.append(parts[1].strip())
224
+ else:
225
+ expecting_url = True
226
+ elif expecting_url:
227
+ if line_clean.startswith("http"):
228
+ image_urls.append(line_clean)
229
+ expecting_url = False
230
+
231
+ st.session_state.image_results = [{"image": url} for url in image_urls]
232
+
233
+ if image_urls and not st.session_state.image_lightbox:
234
+ st.session_state.image_lightbox = image_urls[0]
235
  break
 
236
  except Exception as e:
237
+ st.error(f"โŒ Visual Assistant Error: {e}")
238
 
239
+ text_col, image_col = st.columns([2, 1])
240
+
241
+ with text_col:
242
+ if st.session_state.image_response:
243
+ st.markdown("### ๐Ÿง  Assistant Response")
244
+ st.markdown(st.session_state.image_response, unsafe_allow_html=True)
 
 
 
245
 
246
+ with image_col:
247
+ if st.session_state.image_results:
248
+ st.markdown("### ๐Ÿ–ผ๏ธ Image Preview(s)")
249
+ for i, item in enumerate(st.session_state.image_results):
250
+ image_url = item.get("image")
251
+ if image_url:
252
+ try:
253
+ r = requests.get(image_url, timeout=10) # โœ… Use as-is
254
+ r.raise_for_status()
255
+ img = Image.open(BytesIO(r.content))
256
+ st.image(img, caption=image_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 = image_url
259
+ except Exception as e:
260
+ st.warning(f"โš ๏ธ Could not load: {image_url}")
261
+ st.error(f"{e}")
262
+ else:
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()