Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import os
|
3 |
import time
|
4 |
import re
|
@@ -22,20 +24,17 @@ def login():
|
|
22 |
password = st.text_input("Password", type="password")
|
23 |
if st.button("Login"):
|
24 |
if VALID_USERS.get(email) == password:
|
25 |
-
st.session_state
|
|
|
26 |
st.rerun()
|
27 |
else:
|
28 |
st.error("❌ Incorrect email or password.")
|
29 |
|
30 |
-
if
|
31 |
-
st.session_state.authenticated = False
|
32 |
-
|
33 |
-
if not st.session_state.authenticated:
|
34 |
login()
|
35 |
st.stop()
|
36 |
|
37 |
-
# ------------------ App
|
38 |
-
st.set_page_config(page_title="AI Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
|
39 |
st.title("🧬 AI Pathology Assistant")
|
40 |
|
41 |
# ------------------ Load OpenAI ------------------
|
@@ -49,36 +48,25 @@ client = OpenAI(api_key=OPENAI_API_KEY)
|
|
49 |
# ------------------ Assistant Setup ------------------
|
50 |
ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB"
|
51 |
|
52 |
-
# ------------------ State ------------------
|
53 |
-
|
54 |
-
st.session_state
|
55 |
-
if "
|
56 |
-
st.session_state.thread_id = None
|
57 |
-
if "image_urls" not in st.session_state:
|
58 |
-
st.session_state.image_urls = []
|
59 |
-
if "pending_prompt" not in st.session_state:
|
60 |
-
st.session_state.pending_prompt = None
|
61 |
|
62 |
# ------------------ Tabs ------------------
|
63 |
tab1, tab2 = st.tabs(["💬 Chat Assistant", "🖼️ Visual Reference Search"])
|
64 |
|
65 |
-
# ------------------ Tab 1 ------------------
|
66 |
-
|
67 |
-
# ✅ Ensure required keys exist
|
68 |
-
if "image_url" not in st.session_state:
|
69 |
-
st.session_state.image_url = None
|
70 |
-
if "image_updated" not in st.session_state:
|
71 |
-
st.session_state.image_updated = False
|
72 |
-
|
73 |
with tab1:
|
74 |
with st.sidebar:
|
75 |
st.header("🔧 Tools")
|
76 |
if st.button("🧹 Clear Chat"):
|
77 |
-
|
78 |
-
|
79 |
-
st.
|
80 |
-
|
81 |
-
|
|
|
82 |
st.rerun()
|
83 |
|
84 |
show_image = st.toggle("📸 Show Images", value=True)
|
@@ -106,17 +94,17 @@ with tab1:
|
|
106 |
with chat_col:
|
107 |
st.markdown("### 💬 Ask a Pathology-Specific Question")
|
108 |
user_input = st.chat_input("Example: What are features of squamous cell carcinoma?")
|
109 |
-
if user_input:
|
110 |
-
st.session_state.messages.append({
|
111 |
-
|
112 |
-
|
|
|
113 |
st.session_state.pending_prompt = None
|
114 |
|
115 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
|
116 |
try:
|
117 |
-
if st.session_state.thread_id
|
118 |
-
|
119 |
-
st.session_state.thread_id = thread.id
|
120 |
|
121 |
client.beta.threads.messages.create(
|
122 |
thread_id=st.session_state.thread_id,
|
@@ -169,6 +157,7 @@ with tab1:
|
|
169 |
except Exception as e:
|
170 |
st.error(f"🖼️ Failed to load image: {e}")
|
171 |
|
|
|
172 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
173 |
with tab2:
|
174 |
ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
|
|
|
1 |
import streamlit as st
|
2 |
+
st.set_page_config(page_title="AI Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
|
3 |
+
|
4 |
import os
|
5 |
import time
|
6 |
import re
|
|
|
24 |
password = st.text_input("Password", type="password")
|
25 |
if st.button("Login"):
|
26 |
if VALID_USERS.get(email) == password:
|
27 |
+
st.session_state["authenticated"] = True
|
28 |
+
st.experimental_set_query_params(logged_in="1")
|
29 |
st.rerun()
|
30 |
else:
|
31 |
st.error("❌ Incorrect email or password.")
|
32 |
|
33 |
+
if not st.session_state.get("authenticated", False):
|
|
|
|
|
|
|
34 |
login()
|
35 |
st.stop()
|
36 |
|
37 |
+
# ------------------ App Title ------------------
|
|
|
38 |
st.title("🧬 AI Pathology Assistant")
|
39 |
|
40 |
# ------------------ Load OpenAI ------------------
|
|
|
48 |
# ------------------ Assistant Setup ------------------
|
49 |
ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB"
|
50 |
|
51 |
+
# ------------------ Session State Initialization ------------------
|
52 |
+
for key in ["messages", "thread_id", "image_urls", "pending_prompt", "image_url", "image_updated"]:
|
53 |
+
if key not in st.session_state:
|
54 |
+
st.session_state[key] = [] if key.endswith("s") else None if "url" in key else False
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# ------------------ Tabs ------------------
|
57 |
tab1, tab2 = st.tabs(["💬 Chat Assistant", "🖼️ Visual Reference Search"])
|
58 |
|
59 |
+
# ------------------ Tab 1: Chat Assistant ------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
with tab1:
|
61 |
with st.sidebar:
|
62 |
st.header("🔧 Tools")
|
63 |
if st.button("🧹 Clear Chat"):
|
64 |
+
for key in ["messages", "thread_id", "image_url", "image_updated", "pending_prompt"]:
|
65 |
+
st.session_state[key] = [] if key == "messages" else None if "url" in key else False
|
66 |
+
st.rerun()
|
67 |
+
|
68 |
+
if st.button("🚪 Logout"):
|
69 |
+
st.session_state.clear()
|
70 |
st.rerun()
|
71 |
|
72 |
show_image = st.toggle("📸 Show Images", value=True)
|
|
|
94 |
with chat_col:
|
95 |
st.markdown("### 💬 Ask a Pathology-Specific Question")
|
96 |
user_input = st.chat_input("Example: What are features of squamous cell carcinoma?")
|
97 |
+
if user_input or st.session_state.pending_prompt:
|
98 |
+
st.session_state.messages.append({
|
99 |
+
"role": "user",
|
100 |
+
"content": user_input or st.session_state.pending_prompt
|
101 |
+
})
|
102 |
st.session_state.pending_prompt = None
|
103 |
|
104 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
|
105 |
try:
|
106 |
+
if not st.session_state.thread_id:
|
107 |
+
st.session_state.thread_id = client.beta.threads.create().id
|
|
|
108 |
|
109 |
client.beta.threads.messages.create(
|
110 |
thread_id=st.session_state.thread_id,
|
|
|
157 |
except Exception as e:
|
158 |
st.error(f"🖼️ Failed to load image: {e}")
|
159 |
|
160 |
+
|
161 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
162 |
with tab2:
|
163 |
ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
|