Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -105,67 +105,73 @@ with tab1:
|
|
105 |
st.session_state.messages.append({"role": "user", "content": st.session_state.pending_prompt})
|
106 |
st.session_state.pending_prompt = None
|
107 |
|
|
|
108 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
|
109 |
try:
|
110 |
-
|
111 |
-
st.session_state.thread_id
|
|
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
|
124 |
-
|
125 |
while True:
|
126 |
status = client.beta.threads.runs.retrieve(
|
127 |
-
thread_id=st.session_state.thread_id,
|
|
|
128 |
)
|
129 |
if status.status in ("completed", "failed", "cancelled"):
|
130 |
break
|
131 |
time.sleep(1)
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
if
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
|
|
|
|
|
|
|
|
153 |
st.rerun()
|
154 |
except Exception as e:
|
155 |
st.error(f"❌ Error: {e}")
|
156 |
|
157 |
-
# Display messages
|
158 |
for msg in st.session_state.messages:
|
159 |
with st.chat_message(msg["role"]):
|
160 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
161 |
|
162 |
-
#
|
163 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "assistant":
|
164 |
last = st.session_state.messages[-1]["content"]
|
165 |
if "Some Possible Questions:" in last:
|
166 |
-
|
167 |
-
|
168 |
-
questions = [line for line in all_lines if line.strip().endswith("?")]
|
169 |
|
170 |
if questions:
|
171 |
st.markdown("#### 💡 Follow-Up Suggestions")
|
@@ -186,6 +192,7 @@ with tab1:
|
|
186 |
except Exception:
|
187 |
st.warning(f"⚠️ Failed to load image: {url}")
|
188 |
|
|
|
189 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
190 |
import urllib.parse
|
191 |
import requests
|
|
|
105 |
st.session_state.messages.append({"role": "user", "content": st.session_state.pending_prompt})
|
106 |
st.session_state.pending_prompt = None
|
107 |
|
108 |
+
# Only trigger assistant if last message is from user
|
109 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
|
110 |
try:
|
111 |
+
with st.spinner("🔬 Analyzing..."):
|
112 |
+
if not st.session_state.thread_id:
|
113 |
+
st.session_state.thread_id = client.beta.threads.create().id
|
114 |
|
115 |
+
client.beta.threads.messages.create(
|
116 |
+
thread_id=st.session_state.thread_id,
|
117 |
+
role="user",
|
118 |
+
content=st.session_state.messages[-1]["content"]
|
119 |
+
)
|
120 |
|
121 |
+
run = client.beta.threads.runs.create(
|
122 |
+
thread_id=st.session_state.thread_id,
|
123 |
+
assistant_id=ASSISTANT_ID
|
124 |
+
)
|
125 |
|
126 |
+
# Wait for run to complete
|
127 |
while True:
|
128 |
status = client.beta.threads.runs.retrieve(
|
129 |
+
thread_id=st.session_state.thread_id,
|
130 |
+
run_id=run.id
|
131 |
)
|
132 |
if status.status in ("completed", "failed", "cancelled"):
|
133 |
break
|
134 |
time.sleep(1)
|
135 |
|
136 |
+
if status.status == "completed":
|
137 |
+
responses = client.beta.threads.messages.list(
|
138 |
+
thread_id=st.session_state.thread_id
|
139 |
+
).data
|
140 |
+
|
141 |
+
# Only take the last assistant message
|
142 |
+
for m in responses:
|
143 |
+
if m.role == "assistant":
|
144 |
+
reply = m.content[0].text.value.strip()
|
145 |
+
if not any(
|
146 |
+
reply in msg["content"] or msg["content"] in reply
|
147 |
+
for msg in st.session_state.messages if msg["role"] == "assistant"
|
148 |
+
):
|
149 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
150 |
+
|
151 |
+
# Extract image URLs
|
152 |
+
images = re.findall(
|
153 |
+
r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n"]+\.png',
|
154 |
+
reply
|
155 |
+
)
|
156 |
+
st.session_state.image_urls = images
|
157 |
+
break
|
158 |
+
else:
|
159 |
+
st.error("❌ Assistant failed to complete.")
|
160 |
st.rerun()
|
161 |
except Exception as e:
|
162 |
st.error(f"❌ Error: {e}")
|
163 |
|
164 |
+
# Display all messages
|
165 |
for msg in st.session_state.messages:
|
166 |
with st.chat_message(msg["role"]):
|
167 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
168 |
|
169 |
+
# Follow-up Questions
|
170 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "assistant":
|
171 |
last = st.session_state.messages[-1]["content"]
|
172 |
if "Some Possible Questions:" in last:
|
173 |
+
suggestions = re.findall(r"[-•]\s*(.*)", last)
|
174 |
+
questions = [q.strip() for q in suggestions if q.strip().endswith("?")]
|
|
|
175 |
|
176 |
if questions:
|
177 |
st.markdown("#### 💡 Follow-Up Suggestions")
|
|
|
192 |
except Exception:
|
193 |
st.warning(f"⚠️ Failed to load image: {url}")
|
194 |
|
195 |
+
|
196 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
197 |
import urllib.parse
|
198 |
import requests
|