Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -163,18 +163,23 @@ with tab1:
|
|
163 |
except Exception:
|
164 |
continue
|
165 |
|
166 |
-
from
|
|
|
|
|
|
|
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=
|
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 |
-
#
|
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
|
222 |
url_matches = re.findall(r'Image URL:\s*(https?://[^\s]+)', st.session_state.image_response)
|
223 |
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.")
|