IAMTFRMZA commited on
Commit
33cafda
·
verified ·
1 Parent(s): e5ad2d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -38
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
- if not st.session_state.thread_id:
111
- st.session_state.thread_id = client.beta.threads.create().id
 
112
 
113
- client.beta.threads.messages.create(
114
- thread_id=st.session_state.thread_id,
115
- role="user",
116
- content=st.session_state.messages[-1]["content"]
117
- )
118
 
119
- run = client.beta.threads.runs.create(
120
- thread_id=st.session_state.thread_id,
121
- assistant_id=ASSISTANT_ID
122
- )
123
 
124
- with st.spinner("🔬 Analyzing..."):
125
  while True:
126
  status = client.beta.threads.runs.retrieve(
127
- thread_id=st.session_state.thread_id, run_id=run.id
 
128
  )
129
  if status.status in ("completed", "failed", "cancelled"):
130
  break
131
  time.sleep(1)
132
 
133
- if status.status == "completed":
134
- responses = client.beta.threads.messages.list(
135
- thread_id=st.session_state.thread_id
136
- ).data
137
- for m in reversed(responses):
138
- if m.role == "assistant":
139
- reply = m.content[0].text.value.strip()
140
- if not any(reply in msg["content"] or msg["content"] in reply
141
- for msg in st.session_state.messages if msg["role"] == "assistant"):
142
- st.session_state.messages.append({"role": "assistant", "content": reply})
143
-
144
- # Extract image URLs from response
145
- images = re.findall(
146
- r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n"]+\.png',
147
- reply
148
- )
149
- st.session_state.image_urls = images
150
- break
151
- else:
152
- st.error("❌ Assistant failed to complete.")
 
 
 
 
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
- # Display follow-up questions
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
- # Filter only lines that are actual questions
167
- all_lines = re.findall(r"[-•]\s*(.*)", last)
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