IAMTFRMZA commited on
Commit
c9e5012
ยท
verified ยท
1 Parent(s): 7049f1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -163,18 +163,23 @@ with tab1:
163
  except Exception:
164
  continue
165
 
166
- from urllib.parse import quote # โœ… Make sure this is at the top of your file
 
 
 
167
 
168
  # ------------------ Tab 2: Visual Reference Search ------------------
169
  with tab2:
170
  st.title("๐Ÿ” Visual Reference Search")
171
  user_query = st.text_input("Enter keyword to search images (e.g. ovary, thyroid, mitosis)")
172
 
 
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:
176
  st.session_state.image_response = None
177
 
 
178
  if st.button("Ask Assistant") and user_query:
179
  try:
180
  if st.session_state.image_thread_id is None:
@@ -189,7 +194,7 @@ with tab2:
189
 
190
  run = client.beta.threads.runs.create(
191
  thread_id=st.session_state.image_thread_id,
192
- assistant_id="asst_9v09zgizdcuuhNdcFQpRo9RO"
193
  )
194
 
195
  with st.spinner("๐Ÿ”ฌ Searching for visual references..."):
@@ -213,17 +218,24 @@ with tab2:
213
  except Exception as e:
214
  st.error(f"Error: {e}")
215
 
216
- # Render assistant response and load each image using the actual URL
217
  if st.session_state.image_response:
218
  st.markdown("### ๐Ÿง  Assistant Response")
219
  st.markdown(st.session_state.image_response, unsafe_allow_html=True)
220
 
221
- # Extract valid GitHub URLs from assistant response
222
  url_matches = re.findall(r'Image URL:\s*(https?://[^\s]+)', st.session_state.image_response)
223
 
224
- for url in url_matches:
225
- try:
226
- st.image(url, caption=url.split("/")[-1], use_container_width=True)
227
- except Exception as e:
228
- st.warning(f"โš ๏ธ Could not load image: {url}")
229
- st.error(f"๐Ÿ›‘ Error: {str(e)}")
 
 
 
 
 
 
 
 
163
  except Exception:
164
  continue
165
 
166
+ from PIL import Image
167
+ import requests
168
+ import re
169
+ from io import BytesIO
170
 
171
  # ------------------ Tab 2: Visual Reference Search ------------------
172
  with tab2:
173
  st.title("๐Ÿ” Visual Reference Search")
174
  user_query = st.text_input("Enter keyword to search images (e.g. ovary, thyroid, mitosis)")
175
 
176
+ # Initialize thread and response states
177
  if "image_thread_id" not in st.session_state:
178
  st.session_state.image_thread_id = None
179
  if "image_response" not in st.session_state:
180
  st.session_state.image_response = None
181
 
182
+ # Submit user query to assistant
183
  if st.button("Ask Assistant") and user_query:
184
  try:
185
  if st.session_state.image_thread_id is None:
 
194
 
195
  run = client.beta.threads.runs.create(
196
  thread_id=st.session_state.image_thread_id,
197
+ assistant_id=ASSISTANT_ID # Reuse your assistant ID
198
  )
199
 
200
  with st.spinner("๐Ÿ”ฌ Searching for visual references..."):
 
218
  except Exception as e:
219
  st.error(f"Error: {e}")
220
 
221
+ # Display assistant text response
222
  if st.session_state.image_response:
223
  st.markdown("### ๐Ÿง  Assistant Response")
224
  st.markdown(st.session_state.image_response, unsafe_allow_html=True)
225
 
226
+ # Extract GitHub image URLs
227
  url_matches = re.findall(r'Image URL:\s*(https?://[^\s]+)', st.session_state.image_response)
228
 
229
+ if url_matches:
230
+ st.markdown("### ๐Ÿ–ผ๏ธ Image Preview(s)")
231
+ for url in url_matches:
232
+ try:
233
+ response = requests.get(url, timeout=5)
234
+ response.raise_for_status()
235
+ img = Image.open(BytesIO(response.content))
236
+ st.image(img, caption=url.split("/")[-1], use_container_width=True)
237
+ except Exception as e:
238
+ st.warning(f"โš ๏ธ Failed to load image: {url}")
239
+ st.error(f"๐Ÿ›‘ Error: {e}")
240
+ else:
241
+ st.info("โ„น๏ธ No valid image URLs found in the assistant's response.")