Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ VALID_USERS = {
|
|
17 |
}
|
18 |
|
19 |
def login():
|
20 |
-
st.title("
|
21 |
email = st.text_input("Email")
|
22 |
password = st.text_input("Password", type="password")
|
23 |
if st.button("Login"):
|
@@ -25,7 +25,7 @@ def login():
|
|
25 |
st.session_state.authenticated = True
|
26 |
st.rerun()
|
27 |
else:
|
28 |
-
st.error("
|
29 |
|
30 |
if "authenticated" not in st.session_state:
|
31 |
st.session_state.authenticated = False
|
@@ -35,12 +35,12 @@ if not st.session_state.authenticated:
|
|
35 |
|
36 |
# ------------------ Configuration ------------------
|
37 |
st.set_page_config(page_title="AI Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
|
38 |
-
st.title("
|
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("
|
44 |
st.stop()
|
45 |
|
46 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
@@ -58,17 +58,17 @@ if "pending_prompt" not in st.session_state:
|
|
58 |
|
59 |
# ------------------ Sidebar ------------------
|
60 |
with st.sidebar:
|
61 |
-
st.header("
|
62 |
-
if st.button("
|
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("
|
70 |
keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
|
71 |
-
if st.button("
|
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,7 +91,7 @@ with st.sidebar:
|
|
91 |
chat_col, image_col = st.columns([2, 1])
|
92 |
|
93 |
with chat_col:
|
94 |
-
st.markdown("###
|
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,7 +116,7 @@ with chat_col:
|
|
116 |
assistant_id=ASSISTANT_ID
|
117 |
)
|
118 |
|
119 |
-
with st.spinner("
|
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"):
|
@@ -143,52 +143,28 @@ with chat_col:
|
|
143 |
st.session_state.messages.append({"role": "assistant", "content": reply_cleaned})
|
144 |
break
|
145 |
else:
|
146 |
-
st.error("
|
147 |
st.rerun()
|
148 |
except Exception as e:
|
149 |
-
st.error(f"
|
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 |
-
# ------------------
|
156 |
with image_col:
|
157 |
if show_image and st.session_state.image_urls:
|
158 |
-
st.markdown("###
|
159 |
-
st.markdown("""
|
160 |
-
<style>
|
161 |
-
.carousel-wrapper {
|
162 |
-
display: flex;
|
163 |
-
overflow-x: auto;
|
164 |
-
scroll-snap-type: x mandatory;
|
165 |
-
gap: 1rem;
|
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>
|
181 |
-
""", unsafe_allow_html=True)
|
182 |
-
|
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 |
-
|
|
|
|
|
|
|
191 |
except Exception as e:
|
192 |
-
st.error(f"
|
193 |
-
html += "</div>"
|
194 |
-
st.markdown(html, unsafe_allow_html=True)
|
|
|
17 |
}
|
18 |
|
19 |
def login():
|
20 |
+
st.title("π 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("β 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("𧬠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("β 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("π§ͺ Pathology Tools")
|
62 |
+
if st.button("π§Ή 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("πΈ Show Images", value=True)
|
70 |
keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
|
71 |
+
if st.button("π 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("### π¬ 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("π¬ 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"):
|
|
|
143 |
st.session_state.messages.append({"role": "assistant", "content": reply_cleaned})
|
144 |
break
|
145 |
else:
|
146 |
+
st.error("β Assistant failed to respond.")
|
147 |
st.rerun()
|
148 |
except Exception as e:
|
149 |
+
st.error(f"β 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 |
+
# ------------------ Image Display (Vertical Stack) ------------------
|
156 |
with image_col:
|
157 |
if show_image and st.session_state.image_urls:
|
158 |
+
st.markdown("### πΌοΈ Image(s)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
for raw_url in st.session_state.image_urls:
|
160 |
try:
|
161 |
_, raw_path = raw_url.split("githubusercontent.com/", 1)
|
162 |
segments = raw_path.strip().split("/")
|
163 |
encoded_segments = [quote(seg) for seg in segments]
|
164 |
encoded_url = "https://raw.githubusercontent.com/" + "/".join(encoded_segments)
|
165 |
+
r = requests.get(encoded_url)
|
166 |
+
r.raise_for_status()
|
167 |
+
img = Image.open(BytesIO(r.content))
|
168 |
+
st.image(img, caption=f"π· {encoded_segments[-1]}", use_container_width=True)
|
169 |
except Exception as e:
|
170 |
+
st.error(f"β Failed to load image: {e}")
|
|
|
|