IAMTFRMZA commited on
Commit
a506f2b
Β·
verified Β·
1 Parent(s): 8edd282

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -31
app.py CHANGED
@@ -61,12 +61,12 @@ if "pending_prompt" not in st.session_state:
61
  # ------------------ Tabs ------------------
62
  tab1, tab2 = st.tabs(["πŸ’¬ Chat Assistant", "πŸ–ΌοΈ Visual Reference Search"])
63
 
64
- # ------------------ Tab 1: Chat Assistant ------------------
65
  with tab1:
66
  # Sidebar
67
  with st.sidebar:
68
  st.header("πŸ§ͺ Pathology Tools")
69
-
70
  if st.button("🧹 Clear Chat"):
71
  for k in ["messages", "thread_id", "image_urls", "pending_prompt"]:
72
  st.session_state[k] = [] if k.endswith("s") else None
@@ -98,13 +98,14 @@ with tab1:
98
  st.markdown("### πŸ’¬ Ask a Pathology-Specific Question")
99
  user_input = st.chat_input("Example: What are features of squamous cell carcinoma?")
100
 
 
101
  if user_input:
102
  st.session_state.messages.append({"role": "user", "content": user_input})
103
  elif st.session_state.pending_prompt:
104
  st.session_state.messages.append({"role": "user", "content": st.session_state.pending_prompt})
105
  st.session_state.pending_prompt = None
106
 
107
- # Assistant logic
108
  if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
109
  try:
110
  if not st.session_state.thread_id:
@@ -132,26 +133,14 @@ with tab1:
132
  responses = client.beta.threads.messages.list(thread_id=st.session_state.thread_id).data
133
  for m in reversed(responses):
134
  if m.role == "assistant":
135
- full_reply = m.content[0].text.value.strip()
136
-
137
- # βœ‚οΈ Extract and remove "Some Possible Questions"
138
- question_block_match = re.search(
139
- r"Some Possible Questions:\s*(.*?)(\n{2,}|Other References:|$)",
140
- full_reply,
141
- re.DOTALL
142
- )
143
- question_block = None
144
- if question_block_match:
145
- question_block = question_block_match.group(1)
146
- full_reply = full_reply.replace(f"Some Possible Questions:\n{question_block}", "").strip()
147
-
148
- reply = full_reply
149
 
 
150
  if not any(reply in msg["content"] or msg["content"] in reply
151
  for msg in st.session_state.messages if msg["role"] == "assistant"):
152
  st.session_state.messages.append({"role": "assistant", "content": reply})
153
 
154
- # Extract image URLs
155
  images = re.findall(
156
  r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n"]+\.png',
157
  reply
@@ -164,23 +153,22 @@ with tab1:
164
  except Exception as e:
165
  st.error(f"❌ Error: {e}")
166
 
167
- # Display messages
168
  for msg in st.session_state.messages:
169
  with st.chat_message(msg["role"]):
170
  st.markdown(msg["content"], unsafe_allow_html=True)
171
 
172
- # βœ… Follow-Up Suggestions (from stripped question block)
173
- if 'question_block' in locals() and question_block:
174
- questions = [
175
- line.strip(" -β€’") for line in question_block.splitlines()
176
- if line.strip().startswith(("-", "β€’")) and line.strip().endswith("?")
177
- ]
178
- if questions:
179
- st.markdown("#### πŸ’‘ Follow-Up Suggestions")
180
- for q in questions:
181
- if st.button(f"πŸ“Œ {q}"):
182
- st.session_state.pending_prompt = q
183
- st.rerun()
184
 
185
  with image_col:
186
  if show_image and st.session_state.image_urls:
 
61
  # ------------------ Tabs ------------------
62
  tab1, tab2 = st.tabs(["πŸ’¬ Chat Assistant", "πŸ–ΌοΈ Visual Reference Search"])
63
 
64
+ ----------------
65
  with tab1:
66
  # Sidebar
67
  with st.sidebar:
68
  st.header("πŸ§ͺ Pathology Tools")
69
+
70
  if st.button("🧹 Clear Chat"):
71
  for k in ["messages", "thread_id", "image_urls", "pending_prompt"]:
72
  st.session_state[k] = [] if k.endswith("s") else None
 
98
  st.markdown("### πŸ’¬ Ask a Pathology-Specific Question")
99
  user_input = st.chat_input("Example: What are features of squamous cell carcinoma?")
100
 
101
+ # Append input or pending prompt
102
  if user_input:
103
  st.session_state.messages.append({"role": "user", "content": user_input})
104
  elif st.session_state.pending_prompt:
105
  st.session_state.messages.append({"role": "user", "content": st.session_state.pending_prompt})
106
  st.session_state.pending_prompt = None
107
 
108
+ # Trigger assistant run
109
  if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
110
  try:
111
  if not st.session_state.thread_id:
 
133
  responses = client.beta.threads.messages.list(thread_id=st.session_state.thread_id).data
134
  for m in reversed(responses):
135
  if m.role == "assistant":
136
+ reply = m.content[0].text.value.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
+ # De-dupe reply
139
  if not any(reply in msg["content"] or msg["content"] in reply
140
  for msg in st.session_state.messages if msg["role"] == "assistant"):
141
  st.session_state.messages.append({"role": "assistant", "content": reply})
142
 
143
+ # Image detection
144
  images = re.findall(
145
  r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n"]+\.png',
146
  reply
 
153
  except Exception as e:
154
  st.error(f"❌ Error: {e}")
155
 
156
+ # Display conversation
157
  for msg in st.session_state.messages:
158
  with st.chat_message(msg["role"]):
159
  st.markdown(msg["content"], unsafe_allow_html=True)
160
 
161
+ # Optional follow-ups
162
+ if st.session_state.messages and st.session_state.messages[-1]["role"] == "assistant":
163
+ last = st.session_state.messages[-1]["content"]
164
+ if "Some Possible Questions:" in last:
165
+ questions = re.findall(r"[-β€’]\s*(.*)", last)
166
+ if questions:
167
+ st.markdown("#### πŸ’‘ Follow-Up Suggestions")
168
+ for q in questions:
169
+ if st.button(f"πŸ“Œ {q}"):
170
+ st.session_state.pending_prompt = q
171
+ st.rerun()
 
172
 
173
  with image_col:
174
  if show_image and st.session_state.image_urls: