Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -166,16 +166,25 @@ with tab1:
|
|
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:
|
181 |
thread = client.beta.threads.create()
|
@@ -184,7 +193,7 @@ with tab2:
|
|
184 |
client.beta.threads.messages.create(
|
185 |
thread_id=st.session_state.image_thread_id,
|
186 |
role="user",
|
187 |
-
content=
|
188 |
)
|
189 |
|
190 |
run = client.beta.threads.runs.create(
|
@@ -192,7 +201,7 @@ with tab2:
|
|
192 |
assistant_id=ASSISTANT_ID
|
193 |
)
|
194 |
|
195 |
-
with st.spinner("🔬 Searching for
|
196 |
while True:
|
197 |
run_status = client.beta.threads.runs.retrieve(
|
198 |
thread_id=st.session_state.image_thread_id,
|
@@ -204,36 +213,48 @@ with tab2:
|
|
204 |
|
205 |
if run_status.status == "completed":
|
206 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.image_thread_id)
|
207 |
-
for
|
208 |
-
if
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
break
|
211 |
-
else:
|
212 |
-
st.error("❌ Assistant failed to return an image match.")
|
213 |
except Exception as e:
|
214 |
-
st.error(f"Error: {e}")
|
215 |
|
216 |
-
# Display
|
217 |
-
|
218 |
-
st.markdown("### 🧠 Assistant Response")
|
219 |
-
st.markdown(st.session_state.image_response, unsafe_allow_html=True)
|
220 |
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
st.session_state.image_response
|
225 |
-
)
|
226 |
|
227 |
-
|
|
|
228 |
st.markdown("### 🖼️ Image Preview(s)")
|
229 |
-
for
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
|
|
238 |
else:
|
239 |
-
st.info("ℹ️ No
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
175 |
st.session_state.image_response = None
|
176 |
+
if "image_results" not in st.session_state:
|
177 |
+
st.session_state.image_results = []
|
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
|
185 |
+
st.session_state.image_results = []
|
186 |
+
st.session_state.image_lightbox = None
|
187 |
|
|
|
188 |
try:
|
189 |
if st.session_state.image_thread_id is None:
|
190 |
thread = client.beta.threads.create()
|
|
|
193 |
client.beta.threads.messages.create(
|
194 |
thread_id=st.session_state.image_thread_id,
|
195 |
role="user",
|
196 |
+
content=image_input
|
197 |
)
|
198 |
|
199 |
run = client.beta.threads.runs.create(
|
|
|
201 |
assistant_id=ASSISTANT_ID
|
202 |
)
|
203 |
|
204 |
+
with st.spinner("🔬 Searching for histology references..."):
|
205 |
while True:
|
206 |
run_status = client.beta.threads.runs.retrieve(
|
207 |
thread_id=st.session_state.image_thread_id,
|
|
|
213 |
|
214 |
if run_status.status == "completed":
|
215 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.image_thread_id)
|
216 |
+
for msg in reversed(messages.data):
|
217 |
+
if msg.role == "assistant":
|
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:
|
236 |
+
if st.session_state.image_response:
|
237 |
+
st.markdown("### 🧠 Assistant Response")
|
238 |
+
st.markdown(st.session_state.image_response, unsafe_allow_html=True)
|
|
|
239 |
|
240 |
+
with image_col:
|
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)
|
248 |
+
if st.button("🖼️ View Full Image", key=f"img_{i}"):
|
249 |
+
st.session_state.image_lightbox = image_url
|
250 |
+
except Exception as e:
|
251 |
+
st.warning(f"⚠️ Could not load: {image_url}")
|
252 |
+
st.error(f"{e}")
|
253 |
else:
|
254 |
+
st.info("ℹ️ No image references found yet.")
|
255 |
+
|
256 |
+
if st.session_state.image_lightbox:
|
257 |
+
st.image(st.session_state.image_lightbox, caption="🔍 Full Image View", use_container_width=True)
|
258 |
+
if st.button("❌ Close Preview"):
|
259 |
+
st.session_state.image_lightbox = None
|
260 |
+
st.rerun()
|