IAMTFRMZA commited on
Commit
bff8693
Β·
verified Β·
1 Parent(s): 2c8e0e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -36
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import streamlit as st
2
  import os
3
  import time
@@ -29,28 +30,24 @@ def login():
29
 
30
  if "authenticated" not in st.session_state:
31
  st.session_state.authenticated = False
32
-
33
  if not st.session_state.authenticated:
34
  login()
35
  st.stop()
36
 
37
- # ------------------ App Configuration ------------------
38
  st.set_page_config(page_title="AI Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
39
  st.title("🧬 AI Pathology Assistant")
40
  st.caption("AI-powered exploration of pathology, anatomy, and histology documents via OCR + GPT")
41
 
42
- # ------------------ Load OpenAI API Key ------------------
43
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
44
  if not OPENAI_API_KEY:
45
  st.error("❌ Missing OPENAI_API_KEY environment variable.")
46
  st.stop()
47
 
48
  client = OpenAI(api_key=OPENAI_API_KEY)
 
49
 
50
- # ------------------ Assistant Configuration ------------------
51
- ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB" # Replace with your assistant ID
52
-
53
- # ------------------ Session State ------------------
54
  if "messages" not in st.session_state:
55
  st.session_state.messages = []
56
  if "thread_id" not in st.session_state:
@@ -70,7 +67,7 @@ with st.sidebar:
70
  st.session_state.pending_prompt = None
71
  st.rerun()
72
 
73
- show_image = st.toggle("πŸ“Έ Show Slide Images", value=True)
74
  keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
75
  if st.button("πŸ”Ž Search") and keyword:
76
  st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
@@ -91,7 +88,7 @@ with st.sidebar:
91
  if action != actions[0]:
92
  st.session_state.pending_prompt = action
93
 
94
- # ------------------ Main Chat UI ------------------
95
  chat_col, image_col = st.columns([2, 1])
96
 
97
  with chat_col:
@@ -132,22 +129,13 @@ with chat_col:
132
  for m in reversed(messages.data):
133
  if m.role == "assistant":
134
  reply = m.content[0].text.value
 
135
 
136
- # βœ‚οΈ Optional: split out references
137
- split_marker = "---"
138
- if split_marker in reply:
139
- main_answer, references = reply.split(split_marker, 1)
140
- st.session_state.messages.append({"role": "assistant", "content": main_answer.strip()})
141
- st.session_state.messages.append({"role": "references", "content": references.strip()})
142
- else:
143
- st.session_state.messages.append({"role": "assistant", "content": reply})
144
-
145
- # Extract raw GitHub-hosted image links
146
- image_matches = re.findall(
147
- r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n]+\.png',
148
  reply
149
  )
150
- st.session_state.image_urls = image_matches
151
  break
152
  else:
153
  st.error("❌ Assistant failed to respond.")
@@ -156,31 +144,38 @@ with chat_col:
156
  st.error(f"❌ Error: {e}")
157
 
158
  for msg in st.session_state.messages:
159
- with st.chat_message("assistant" if msg["role"] != "references" else "πŸ“„"):
160
  st.markdown(msg["content"], unsafe_allow_html=True)
161
 
162
- # ------------------ Slide Viewer (Horizontal Scroll) ------------------
163
  with image_col:
164
  if show_image and st.session_state.image_urls:
165
- st.markdown("### πŸ–ΌοΈ Slide Previews")
166
-
167
  st.markdown("""
168
  <style>
169
- .image-scroll-container {
170
  display: flex;
171
  overflow-x: auto;
172
- gap: 16px;
 
173
  padding: 0.5rem 0;
174
  }
175
- .image-scroll-container img {
 
 
 
 
 
 
 
 
176
  height: 500px;
177
- border-radius: 10px;
178
  }
179
  </style>
180
  """, unsafe_allow_html=True)
181
 
182
- st.markdown('<div class="image-scroll-container">', unsafe_allow_html=True)
183
-
184
  for raw_url in st.session_state.image_urls:
185
  try:
186
  if "githubusercontent.com" not in raw_url:
@@ -189,9 +184,8 @@ with image_col:
189
  segments = raw_path.strip().split("/")
190
  encoded_segments = [quote(seg) for seg in segments]
191
  encoded_url = "https://raw.githubusercontent.com/" + "/".join(encoded_segments)
192
-
193
- st.markdown(f'<img src="{encoded_url}" alt="slide" />', unsafe_allow_html=True)
194
  except Exception as e:
195
  st.error(f"❌ Failed to load image: {e}")
196
-
197
- st.markdown('</div>', unsafe_allow_html=True)
 
1
+ # ------------------ Import ------------------
2
  import streamlit as st
3
  import os
4
  import time
 
30
 
31
  if "authenticated" not in st.session_state:
32
  st.session_state.authenticated = False
 
33
  if not st.session_state.authenticated:
34
  login()
35
  st.stop()
36
 
37
+ # ------------------ Configuration ------------------
38
  st.set_page_config(page_title="AI Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
39
  st.title("🧬 AI Pathology Assistant")
40
  st.caption("AI-powered exploration of pathology, anatomy, and histology documents via OCR + GPT")
41
 
 
42
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
43
  if not OPENAI_API_KEY:
44
  st.error("❌ Missing OPENAI_API_KEY environment variable.")
45
  st.stop()
46
 
47
  client = OpenAI(api_key=OPENAI_API_KEY)
48
+ ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB"
49
 
50
+ # ------------------ State Setup ------------------
 
 
 
51
  if "messages" not in st.session_state:
52
  st.session_state.messages = []
53
  if "thread_id" not in st.session_state:
 
67
  st.session_state.pending_prompt = None
68
  st.rerun()
69
 
70
+ show_image = st.toggle("πŸ“Έ Show Images", value=True)
71
  keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
72
  if st.button("πŸ”Ž Search") and keyword:
73
  st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
 
88
  if action != actions[0]:
89
  st.session_state.pending_prompt = action
90
 
91
+ # ------------------ Chat UI ------------------
92
  chat_col, image_col = st.columns([2, 1])
93
 
94
  with chat_col:
 
129
  for m in reversed(messages.data):
130
  if m.role == "assistant":
131
  reply = m.content[0].text.value
132
+ st.session_state.messages.append({"role": "assistant", "content": reply})
133
 
134
+ matches = re.findall(
135
+ r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s]+\.png',
 
 
 
 
 
 
 
 
 
 
136
  reply
137
  )
138
+ st.session_state.image_urls = matches
139
  break
140
  else:
141
  st.error("❌ Assistant failed to respond.")
 
144
  st.error(f"❌ Error: {e}")
145
 
146
  for msg in st.session_state.messages:
147
+ with st.chat_message(msg["role"]):
148
  st.markdown(msg["content"], unsafe_allow_html=True)
149
 
150
+ # ------------------ Scrollable Image Carousel ------------------
151
  with image_col:
152
  if show_image and st.session_state.image_urls:
153
+ st.markdown("### πŸ–ΌοΈ Image(s)")
 
154
  st.markdown("""
155
  <style>
156
+ .carousel-wrapper {
157
  display: flex;
158
  overflow-x: auto;
159
+ scroll-snap-type: x mandatory;
160
+ gap: 1rem;
161
  padding: 0.5rem 0;
162
  }
163
+ .carousel-wrapper::-webkit-scrollbar {
164
+ height: 10px;
165
+ }
166
+ .carousel-wrapper::-webkit-scrollbar-thumb {
167
+ background: #888;
168
+ border-radius: 5px;
169
+ }
170
+ .carousel-wrapper img {
171
+ scroll-snap-align: start;
172
  height: 500px;
173
+ border-radius: 8px;
174
  }
175
  </style>
176
  """, unsafe_allow_html=True)
177
 
178
+ html = '<div class="carousel-wrapper">'
 
179
  for raw_url in st.session_state.image_urls:
180
  try:
181
  if "githubusercontent.com" not in raw_url:
 
184
  segments = raw_path.strip().split("/")
185
  encoded_segments = [quote(seg) for seg in segments]
186
  encoded_url = "https://raw.githubusercontent.com/" + "/".join(encoded_segments)
187
+ html += f'<img src="{encoded_url}" alt="slide preview">'
 
188
  except Exception as e:
189
  st.error(f"❌ Failed to load image: {e}")
190
+ html += "</div>"
191
+ st.markdown(html, unsafe_allow_html=True)