Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -54,7 +54,13 @@ for key in ["messages", "thread_id", "image_urls", "pending_prompt", "image_url"
|
|
54 |
st.session_state[key] = [] if key.endswith("s") else None if "url" in key else False
|
55 |
|
56 |
# ------------------ Tabs ------------------
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
# ------------------ Tab 1: Chat Assistant ------------------
|
60 |
with tab1:
|
@@ -159,120 +165,121 @@ with tab1:
|
|
159 |
|
160 |
|
161 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
st.session_state
|
167 |
-
|
168 |
-
st.session_state
|
169 |
-
|
170 |
-
st.session_state
|
171 |
-
|
172 |
-
st.session_state
|
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,
|
187 |
-
role="user",
|
188 |
-
content=image_input
|
189 |
-
)
|
190 |
-
|
191 |
-
run = client.beta.threads.runs.create(
|
192 |
-
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 |
-
|
247 |
-
if
|
248 |
-
st.
|
249 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
st.session_state[key] = [] if key.endswith("s") else None if "url" in key else False
|
55 |
|
56 |
# ------------------ Tabs ------------------
|
57 |
+
show_tab2 = False # Toggle this to True to activate Tab 2
|
58 |
+
|
59 |
+
if show_tab2:
|
60 |
+
tab1, tab2 = st.tabs(["💬 Chat Assistant", "📷 Visual Reference Search"])
|
61 |
+
else:
|
62 |
+
tab1, = st.tabs(["💬 Chat Assistant"])
|
63 |
+
|
64 |
|
65 |
# ------------------ Tab 1: Chat Assistant ------------------
|
66 |
with tab1:
|
|
|
165 |
|
166 |
|
167 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
168 |
+
if show_tab2:
|
169 |
+
with tab2:
|
170 |
+
ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
|
171 |
+
|
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 |
+
image_input = st.chat_input("Ask for histology visual references (e.g. ovary histology, mitosis)")
|
182 |
+
if image_input:
|
183 |
+
st.session_state.image_response = None
|
184 |
+
st.session_state.image_results = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
st.session_state.image_lightbox = None
|
186 |
+
|
187 |
+
try:
|
188 |
+
if st.session_state.image_thread_id is None:
|
189 |
+
thread = client.beta.threads.create()
|
190 |
+
st.session_state.image_thread_id = thread.id
|
191 |
+
|
192 |
+
client.beta.threads.messages.create(
|
193 |
+
thread_id=st.session_state.image_thread_id,
|
194 |
+
role="user",
|
195 |
+
content=image_input
|
196 |
+
)
|
197 |
+
|
198 |
+
run = client.beta.threads.runs.create(
|
199 |
+
thread_id=st.session_state.image_thread_id,
|
200 |
+
assistant_id=ASSISTANT_ID
|
201 |
+
)
|
202 |
+
|
203 |
+
with st.spinner("🔬 Searching for histology references..."):
|
204 |
+
while True:
|
205 |
+
run_status = client.beta.threads.runs.retrieve(
|
206 |
+
thread_id=st.session_state.image_thread_id,
|
207 |
+
run_id=run.id
|
208 |
+
)
|
209 |
+
if run_status.status in ("completed", "failed", "cancelled"):
|
210 |
+
break
|
211 |
+
time.sleep(1)
|
212 |
+
|
213 |
+
if run_status.status == "completed":
|
214 |
+
messages = client.beta.threads.messages.list(thread_id=st.session_state.image_thread_id)
|
215 |
+
for msg in reversed(messages.data):
|
216 |
+
if msg.role == "assistant":
|
217 |
+
response_text = msg.content[0].text.value
|
218 |
+
st.session_state.image_response = response_text
|
219 |
+
|
220 |
+
# ✅ Extract Image URLs from assistant markdown
|
221 |
+
lines = response_text.splitlines()
|
222 |
+
image_urls = []
|
223 |
+
expecting_url = False
|
224 |
+
|
225 |
+
for line in lines:
|
226 |
+
line_clean = line.strip().replace("**", "")
|
227 |
+
if "Image URL:" in line_clean:
|
228 |
+
parts = line_clean.split("Image URL:")
|
229 |
+
if len(parts) > 1 and parts[1].strip().startswith("http"):
|
230 |
+
image_urls.append(parts[1].strip())
|
231 |
+
else:
|
232 |
+
expecting_url = True
|
233 |
+
elif expecting_url:
|
234 |
+
if line_clean.startswith("http"):
|
235 |
+
image_urls.append(line_clean)
|
236 |
+
expecting_url = False
|
237 |
+
|
238 |
+
st.session_state.image_results = [{"image": url} for url in image_urls]
|
239 |
+
|
240 |
+
if image_urls and not st.session_state.image_lightbox:
|
241 |
+
st.session_state.image_lightbox = image_urls[0]
|
242 |
+
break
|
243 |
+
except Exception as e:
|
244 |
+
st.error(f"❌ Visual Assistant Error: {e}")
|
245 |
+
|
246 |
+
text_col, image_col = st.columns([2, 1])
|
247 |
+
|
248 |
+
with text_col:
|
249 |
+
if st.session_state.image_response:
|
250 |
+
st.markdown("### 🧠 Assistant Response")
|
251 |
+
st.markdown(st.session_state.image_response, unsafe_allow_html=True)
|
252 |
+
|
253 |
+
with image_col:
|
254 |
+
if st.session_state.image_results:
|
255 |
+
st.markdown("### 🖼️ Image Preview(s)")
|
256 |
+
for i, item in enumerate(st.session_state.image_results):
|
257 |
+
image_url = item.get("image")
|
258 |
+
if image_url:
|
259 |
+
try:
|
260 |
+
r = requests.get(image_url, timeout=10)
|
261 |
+
r.raise_for_status()
|
262 |
+
img = Image.open(BytesIO(r.content))
|
263 |
+
st.image(img, caption=image_url.split("/")[-1], use_container_width=True)
|
264 |
+
if st.button("🔍 View Full Image", key=f"full_img_{i}"):
|
265 |
+
st.session_state.image_lightbox = image_url
|
266 |
+
except Exception as e:
|
267 |
+
st.warning(f"⚠️ Could not load: {image_url}")
|
268 |
+
st.error(f"{e}")
|
269 |
+
else:
|
270 |
+
st.info("ℹ️ No image references found yet.")
|
271 |
+
|
272 |
+
if st.session_state.image_lightbox:
|
273 |
+
st.markdown("### 🔬 Full Image View")
|
274 |
+
try:
|
275 |
+
img_url = st.session_state.image_lightbox
|
276 |
+
r = requests.get(img_url, timeout=10)
|
277 |
+
r.raise_for_status()
|
278 |
+
full_img = Image.open(BytesIO(r.content))
|
279 |
+
st.image(full_img, caption=img_url.split("/")[-1], use_container_width=True)
|
280 |
+
except Exception as e:
|
281 |
+
st.warning("⚠️ Could not load full image.")
|
282 |
+
st.error(str(e))
|
283 |
+
if st.button("❌ Close Preview"):
|
284 |
+
st.session_state.image_lightbox = None
|
285 |
+
st.rerun()
|