Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# ------------------ Import ------------------
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
import time
|
|
@@ -18,7 +17,7 @@ VALID_USERS = {
|
|
| 18 |
}
|
| 19 |
|
| 20 |
def login():
|
| 21 |
-
st.title("
|
| 22 |
email = st.text_input("Email")
|
| 23 |
password = st.text_input("Password", type="password")
|
| 24 |
if st.button("Login"):
|
|
@@ -26,7 +25,7 @@ def login():
|
|
| 26 |
st.session_state.authenticated = True
|
| 27 |
st.rerun()
|
| 28 |
else:
|
| 29 |
-
st.error("
|
| 30 |
|
| 31 |
if "authenticated" not in st.session_state:
|
| 32 |
st.session_state.authenticated = False
|
|
@@ -36,12 +35,12 @@ if not st.session_state.authenticated:
|
|
| 36 |
|
| 37 |
# ------------------ Configuration ------------------
|
| 38 |
st.set_page_config(page_title="AI Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
|
| 39 |
-
st.title("
|
| 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("
|
| 45 |
st.stop()
|
| 46 |
|
| 47 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
|
@@ -59,17 +58,17 @@ if "pending_prompt" not in st.session_state:
|
|
| 59 |
|
| 60 |
# ------------------ Sidebar ------------------
|
| 61 |
with st.sidebar:
|
| 62 |
-
st.header("
|
| 63 |
-
if st.button("
|
| 64 |
st.session_state.messages = []
|
| 65 |
st.session_state.thread_id = None
|
| 66 |
st.session_state.image_urls = []
|
| 67 |
st.session_state.pending_prompt = None
|
| 68 |
st.rerun()
|
| 69 |
|
| 70 |
-
show_image = st.toggle("
|
| 71 |
keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
|
| 72 |
-
if st.button("
|
| 73 |
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
|
| 74 |
|
| 75 |
section = st.text_input("Section Lookup", placeholder="e.g. Connective Tissue")
|
|
@@ -92,7 +91,7 @@ with st.sidebar:
|
|
| 92 |
chat_col, image_col = st.columns([2, 1])
|
| 93 |
|
| 94 |
with chat_col:
|
| 95 |
-
st.markdown("###
|
| 96 |
user_input = st.chat_input("Example: What are features of squamous cell carcinoma?")
|
| 97 |
if user_input:
|
| 98 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
|
@@ -117,7 +116,7 @@ with chat_col:
|
|
| 117 |
assistant_id=ASSISTANT_ID
|
| 118 |
)
|
| 119 |
|
| 120 |
-
with st.spinner("
|
| 121 |
while True:
|
| 122 |
status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
|
| 123 |
if status.status in ("completed", "failed", "cancelled"):
|
|
@@ -128,29 +127,35 @@ with chat_col:
|
|
| 128 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
| 129 |
for m in reversed(messages.data):
|
| 130 |
if m.role == "assistant":
|
| 131 |
-
|
| 132 |
-
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 133 |
|
| 134 |
-
|
| 135 |
-
r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[
|
| 136 |
-
|
| 137 |
)
|
| 138 |
-
st.session_state.image_urls =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
break
|
| 140 |
else:
|
| 141 |
-
st.error("
|
| 142 |
st.rerun()
|
| 143 |
except Exception as e:
|
| 144 |
-
st.error(f"
|
| 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
|
| 151 |
with image_col:
|
| 152 |
if show_image and st.session_state.image_urls:
|
| 153 |
-
st.markdown("###
|
| 154 |
st.markdown("""
|
| 155 |
<style>
|
| 156 |
.carousel-wrapper {
|
|
@@ -161,15 +166,15 @@ with image_col:
|
|
| 161 |
padding: 0.5rem 0;
|
| 162 |
}
|
| 163 |
.carousel-wrapper::-webkit-scrollbar {
|
| 164 |
-
height:
|
| 165 |
}
|
| 166 |
.carousel-wrapper::-webkit-scrollbar-thumb {
|
| 167 |
-
background: #
|
| 168 |
-
border-radius:
|
| 169 |
}
|
| 170 |
.carousel-wrapper img {
|
| 171 |
scroll-snap-align: start;
|
| 172 |
-
height:
|
| 173 |
border-radius: 8px;
|
| 174 |
}
|
| 175 |
</style>
|
|
@@ -178,14 +183,12 @@ with image_col:
|
|
| 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:
|
| 182 |
-
continue
|
| 183 |
_, raw_path = raw_url.split("githubusercontent.com/", 1)
|
| 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"
|
| 190 |
html += "</div>"
|
| 191 |
-
st.markdown(html, unsafe_allow_html=True)
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import time
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
def login():
|
| 20 |
+
st.title("\U0001F512 Login Required")
|
| 21 |
email = st.text_input("Email")
|
| 22 |
password = st.text_input("Password", type="password")
|
| 23 |
if st.button("Login"):
|
|
|
|
| 25 |
st.session_state.authenticated = True
|
| 26 |
st.rerun()
|
| 27 |
else:
|
| 28 |
+
st.error("\u274C Incorrect email or password.")
|
| 29 |
|
| 30 |
if "authenticated" not in st.session_state:
|
| 31 |
st.session_state.authenticated = False
|
|
|
|
| 35 |
|
| 36 |
# ------------------ Configuration ------------------
|
| 37 |
st.set_page_config(page_title="AI Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
|
| 38 |
+
st.title("\U0001F9EC AI Pathology Assistant")
|
| 39 |
st.caption("AI-powered exploration of pathology, anatomy, and histology documents via OCR + GPT")
|
| 40 |
|
| 41 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 42 |
if not OPENAI_API_KEY:
|
| 43 |
+
st.error("\u274C Missing OPENAI_API_KEY environment variable.")
|
| 44 |
st.stop()
|
| 45 |
|
| 46 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
|
| 58 |
|
| 59 |
# ------------------ Sidebar ------------------
|
| 60 |
with st.sidebar:
|
| 61 |
+
st.header("\U0001F9EA Pathology Tools")
|
| 62 |
+
if st.button("\U0001F9F9 Clear Chat"):
|
| 63 |
st.session_state.messages = []
|
| 64 |
st.session_state.thread_id = None
|
| 65 |
st.session_state.image_urls = []
|
| 66 |
st.session_state.pending_prompt = None
|
| 67 |
st.rerun()
|
| 68 |
|
| 69 |
+
show_image = st.toggle("\U0001F4F8 Show Slide Images", value=True)
|
| 70 |
keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
|
| 71 |
+
if st.button("\U0001F50E Search") and keyword:
|
| 72 |
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
|
| 73 |
|
| 74 |
section = st.text_input("Section Lookup", placeholder="e.g. Connective Tissue")
|
|
|
|
| 91 |
chat_col, image_col = st.columns([2, 1])
|
| 92 |
|
| 93 |
with chat_col:
|
| 94 |
+
st.markdown("### \U0001F4AC Ask a Pathology-Specific Question")
|
| 95 |
user_input = st.chat_input("Example: What are features of squamous cell carcinoma?")
|
| 96 |
if user_input:
|
| 97 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
|
|
|
| 116 |
assistant_id=ASSISTANT_ID
|
| 117 |
)
|
| 118 |
|
| 119 |
+
with st.spinner("\U0001F52C Analyzing..."):
|
| 120 |
while True:
|
| 121 |
status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
|
| 122 |
if status.status in ("completed", "failed", "cancelled"):
|
|
|
|
| 127 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
| 128 |
for m in reversed(messages.data):
|
| 129 |
if m.role == "assistant":
|
| 130 |
+
raw_reply = m.content[0].text.value
|
|
|
|
| 131 |
|
| 132 |
+
image_matches = re.findall(
|
| 133 |
+
r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^"]+?\.png',
|
| 134 |
+
raw_reply
|
| 135 |
)
|
| 136 |
+
st.session_state.image_urls = image_matches
|
| 137 |
+
|
| 138 |
+
reply_cleaned = re.sub(
|
| 139 |
+
r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^"]+?\.png',
|
| 140 |
+
'[Image reference available in Slide Previews β]',
|
| 141 |
+
raw_reply
|
| 142 |
+
)
|
| 143 |
+
st.session_state.messages.append({"role": "assistant", "content": reply_cleaned})
|
| 144 |
break
|
| 145 |
else:
|
| 146 |
+
st.error("\u274C Assistant failed to respond.")
|
| 147 |
st.rerun()
|
| 148 |
except Exception as e:
|
| 149 |
+
st.error(f"\u274C Error: {e}")
|
| 150 |
|
| 151 |
for msg in st.session_state.messages:
|
| 152 |
with st.chat_message(msg["role"]):
|
| 153 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
| 154 |
|
| 155 |
+
# ------------------ Scrollable Image Preview ------------------
|
| 156 |
with image_col:
|
| 157 |
if show_image and st.session_state.image_urls:
|
| 158 |
+
st.markdown("### \U0001F5BC Image(s)")
|
| 159 |
st.markdown("""
|
| 160 |
<style>
|
| 161 |
.carousel-wrapper {
|
|
|
|
| 166 |
padding: 0.5rem 0;
|
| 167 |
}
|
| 168 |
.carousel-wrapper::-webkit-scrollbar {
|
| 169 |
+
height: 8px;
|
| 170 |
}
|
| 171 |
.carousel-wrapper::-webkit-scrollbar-thumb {
|
| 172 |
+
background: #999;
|
| 173 |
+
border-radius: 6px;
|
| 174 |
}
|
| 175 |
.carousel-wrapper img {
|
| 176 |
scroll-snap-align: start;
|
| 177 |
+
height: 420px;
|
| 178 |
border-radius: 8px;
|
| 179 |
}
|
| 180 |
</style>
|
|
|
|
| 183 |
html = '<div class="carousel-wrapper">'
|
| 184 |
for raw_url in st.session_state.image_urls:
|
| 185 |
try:
|
|
|
|
|
|
|
| 186 |
_, raw_path = raw_url.split("githubusercontent.com/", 1)
|
| 187 |
segments = raw_path.strip().split("/")
|
| 188 |
encoded_segments = [quote(seg) for seg in segments]
|
| 189 |
encoded_url = "https://raw.githubusercontent.com/" + "/".join(encoded_segments)
|
| 190 |
html += f'<img src="{encoded_url}" alt="slide preview">'
|
| 191 |
except Exception as e:
|
| 192 |
+
st.error(f"\u274C Failed to load image: {e}")
|
| 193 |
html += "</div>"
|
| 194 |
+
st.markdown(html, unsafe_allow_html=True)
|