Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -162,21 +162,25 @@ with tab1:
|
|
162 |
with tab2:
|
163 |
ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
|
|
|
|
171 |
|
|
|
172 |
if image_input:
|
173 |
st.session_state.image_response = None
|
174 |
st.session_state.image_results = []
|
175 |
st.session_state.image_lightbox = None
|
176 |
|
177 |
try:
|
178 |
-
if
|
179 |
-
|
|
|
180 |
|
181 |
client.beta.threads.messages.create(
|
182 |
thread_id=st.session_state.image_thread_id,
|
@@ -189,72 +193,86 @@ with tab2:
|
|
189 |
assistant_id=ASSISTANT_ID
|
190 |
)
|
191 |
|
192 |
-
with st.spinner("
|
193 |
while True:
|
194 |
-
|
195 |
thread_id=st.session_state.image_thread_id,
|
196 |
run_id=run.id
|
197 |
)
|
198 |
-
if
|
199 |
break
|
200 |
time.sleep(1)
|
201 |
|
202 |
-
if
|
203 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.image_thread_id)
|
204 |
for msg in reversed(messages.data):
|
205 |
if msg.role == "assistant":
|
206 |
-
response_text = msg.content[0].text.value
|
207 |
st.session_state.image_response = response_text
|
208 |
|
209 |
-
# โ
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
"
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
st.session_state.
|
|
|
230 |
break
|
231 |
-
|
232 |
except Exception as e:
|
233 |
-
st.error(f"โ Assistant Error: {e}")
|
234 |
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
st.image(img, use_container_width=True)
|
242 |
-
except:
|
243 |
-
st.image("https://via.placeholder.com/150?text=Image+Error", use_container_width=True)
|
244 |
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
|
250 |
-
# ๐ฌ Full image lightbox
|
251 |
if st.session_state.image_lightbox:
|
252 |
st.markdown("### ๐ฌ Full Image View")
|
253 |
try:
|
254 |
-
|
255 |
-
|
|
|
|
|
|
|
256 |
except Exception as e:
|
257 |
-
st.
|
258 |
-
|
|
|
259 |
st.session_state.image_lightbox = None
|
260 |
st.rerun()
|
|
|
162 |
with tab2:
|
163 |
ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
|
164 |
|
165 |
+
if "image_thread_id" not in st.session_state:
|
166 |
+
st.session_state.image_thread_id = None
|
167 |
+
if "image_response" not in st.session_state:
|
168 |
+
st.session_state.image_response = None
|
169 |
+
if "image_results" not in st.session_state:
|
170 |
+
st.session_state.image_results = []
|
171 |
+
if "image_lightbox" not in st.session_state:
|
172 |
+
st.session_state.image_lightbox = None
|
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,
|
|
|
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 |
+
with image_col:
|
247 |
+
if st.session_state.image_results:
|
248 |
+
st.markdown("### ๐ผ๏ธ Image Preview(s)")
|
249 |
+
for i, item in enumerate(st.session_state.image_results):
|
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 |
st.rerun()
|