IAMTFRMZA commited on
Commit
5a0ce18
·
verified ·
1 Parent(s): c9e5012

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -163,23 +163,18 @@ with tab1:
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,7 +189,7 @@ with tab2:
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,13 +213,16 @@ with tab2:
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)")
@@ -235,7 +233,7 @@ with tab2:
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.")
 
163
  except Exception:
164
  continue
165
 
166
+
 
 
 
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
 
190
  run = client.beta.threads.runs.create(
191
  thread_id=st.session_state.image_thread_id,
192
+ assistant_id=ASSISTANT_ID
193
  )
194
 
195
  with st.spinner("🔬 Searching for visual references..."):
 
213
  except Exception as e:
214
  st.error(f"Error: {e}")
215
 
216
+ # Display assistant response and render images
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
+ # More robust regex to extract URLs even with encoded characters and line breaks
222
+ url_matches = re.findall(
223
+ r'https://raw\.githubusercontent\.com/[^\s)>\]]+\.png',
224
+ st.session_state.image_response.replace("\n", " ")
225
+ )
226
 
227
  if url_matches:
228
  st.markdown("### 🖼️ Image Preview(s)")
 
233
  img = Image.open(BytesIO(response.content))
234
  st.image(img, caption=url.split("/")[-1], use_container_width=True)
235
  except Exception as e:
236
+ st.warning(f"⚠️ Could not load image: {url}")
237
+ st.error(f"🛑 Error: {str(e)}")
238
  else:
239
  st.info("ℹ️ No valid image URLs found in the assistant's response.")