Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -163,6 +163,7 @@ with tab1:
|
|
163 |
except Exception:
|
164 |
continue
|
165 |
|
|
|
166 |
# ------------------ Tab 2: Visual Search ------------------
|
167 |
with tab2:
|
168 |
st.title("🔍 Visual Reference Search")
|
@@ -175,19 +176,16 @@ with tab2:
|
|
175 |
|
176 |
if st.button("Ask Assistant") and user_query:
|
177 |
try:
|
178 |
-
# Create thread if needed
|
179 |
if st.session_state.image_thread_id is None:
|
180 |
thread = client.beta.threads.create()
|
181 |
st.session_state.image_thread_id = thread.id
|
182 |
|
183 |
-
# Send user message
|
184 |
client.beta.threads.messages.create(
|
185 |
thread_id=st.session_state.image_thread_id,
|
186 |
role="user",
|
187 |
content=user_query
|
188 |
)
|
189 |
|
190 |
-
# Run assistant
|
191 |
run = client.beta.threads.runs.create(
|
192 |
thread_id=st.session_state.image_thread_id,
|
193 |
assistant_id="asst_9v09zgizdcuuhNdcFQpRo9RO"
|
@@ -195,7 +193,10 @@ with tab2:
|
|
195 |
|
196 |
with st.spinner("🔬 Searching for visual references..."):
|
197 |
while True:
|
198 |
-
run_status = client.beta.threads.runs.retrieve(
|
|
|
|
|
|
|
199 |
if run_status.status in ("completed", "failed", "cancelled"):
|
200 |
break
|
201 |
time.sleep(1)
|
@@ -211,19 +212,28 @@ with tab2:
|
|
211 |
except Exception as e:
|
212 |
st.error(f"Error: {e}")
|
213 |
|
214 |
-
# Render assistant
|
215 |
if st.session_state.image_response:
|
216 |
st.markdown("### 🧠 Assistant Response")
|
217 |
st.markdown(st.session_state.image_response, unsafe_allow_html=True)
|
218 |
|
219 |
-
# Extract
|
220 |
-
|
221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
try:
|
223 |
-
r = requests.get(
|
224 |
r.raise_for_status()
|
225 |
img = Image.open(BytesIO(r.content))
|
226 |
-
st.image(img, caption=
|
227 |
-
except:
|
228 |
-
st.warning(f"⚠️ Failed to load: {
|
229 |
-
|
|
|
163 |
except Exception:
|
164 |
continue
|
165 |
|
166 |
+
# ------------------ Tab 2: Visual Search ------------------
|
167 |
# ------------------ Tab 2: Visual Search ------------------
|
168 |
with tab2:
|
169 |
st.title("🔍 Visual Reference Search")
|
|
|
176 |
|
177 |
if st.button("Ask Assistant") and user_query:
|
178 |
try:
|
|
|
179 |
if st.session_state.image_thread_id is None:
|
180 |
thread = client.beta.threads.create()
|
181 |
st.session_state.image_thread_id = thread.id
|
182 |
|
|
|
183 |
client.beta.threads.messages.create(
|
184 |
thread_id=st.session_state.image_thread_id,
|
185 |
role="user",
|
186 |
content=user_query
|
187 |
)
|
188 |
|
|
|
189 |
run = client.beta.threads.runs.create(
|
190 |
thread_id=st.session_state.image_thread_id,
|
191 |
assistant_id="asst_9v09zgizdcuuhNdcFQpRo9RO"
|
|
|
193 |
|
194 |
with st.spinner("🔬 Searching for visual references..."):
|
195 |
while True:
|
196 |
+
run_status = client.beta.threads.runs.retrieve(
|
197 |
+
thread_id=st.session_state.image_thread_id,
|
198 |
+
run_id=run.id
|
199 |
+
)
|
200 |
if run_status.status in ("completed", "failed", "cancelled"):
|
201 |
break
|
202 |
time.sleep(1)
|
|
|
212 |
except Exception as e:
|
213 |
st.error(f"Error: {e}")
|
214 |
|
215 |
+
# Render assistant response + build GitHub image URLs from filenames
|
216 |
if st.session_state.image_response:
|
217 |
st.markdown("### 🧠 Assistant Response")
|
218 |
st.markdown(st.session_state.image_response, unsafe_allow_html=True)
|
219 |
|
220 |
+
# Extract image filenames from assistant response
|
221 |
+
filename_matches = re.findall(r'Image Filename:\s*(.*?\\.png)', st.session_state.image_response)
|
222 |
+
|
223 |
+
for fname in filename_matches:
|
224 |
+
if "Autopsy" in fname:
|
225 |
+
folder = "Handbook Autopsy Practice Waters"
|
226 |
+
else:
|
227 |
+
folder = "JunqueirasBasicHistologyTextAtlas14thed.AnthonyMescher (2)"
|
228 |
+
|
229 |
+
encoded_folder = folder.replace(" ", "%20")
|
230 |
+
encoded_fname = fname.replace(" ", "%20")
|
231 |
+
url = f"https://raw.githubusercontent.com/AndrewLORTech/witspathologaihistotech/main/{encoded_folder}/{encoded_fname}"
|
232 |
+
|
233 |
try:
|
234 |
+
r = requests.get(url, timeout=5)
|
235 |
r.raise_for_status()
|
236 |
img = Image.open(BytesIO(r.content))
|
237 |
+
st.image(img, caption=fname, use_container_width=True)
|
238 |
+
except Exception:
|
239 |
+
st.warning(f"⚠️ Failed to load: {url}")
|
|