Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,9 @@ from PIL import Image
|
|
7 |
from io import BytesIO
|
8 |
from openai import OpenAI
|
9 |
|
|
|
|
|
|
|
10 |
# ------------------ Authentication ------------------
|
11 |
VALID_USERS = {
|
12 |
"[email protected]": "Pass.123",
|
@@ -33,10 +36,6 @@ if not st.session_state.authenticated:
|
|
33 |
login()
|
34 |
st.stop()
|
35 |
|
36 |
-
# ------------------ App Config ------------------
|
37 |
-
st.set_page_config(page_title="AI Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
|
38 |
-
st.title("🧬 AI Pathology Assistant")
|
39 |
-
|
40 |
# ------------------ Load OpenAI ------------------
|
41 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
42 |
if not OPENAI_API_KEY:
|
@@ -48,30 +47,26 @@ client = OpenAI(api_key=OPENAI_API_KEY)
|
|
48 |
# ------------------ Assistant Setup ------------------
|
49 |
ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB"
|
50 |
|
51 |
-
# ------------------ State ------------------
|
52 |
-
|
53 |
-
st.session_state
|
54 |
-
if "
|
55 |
-
|
56 |
-
|
57 |
-
st.session_state.image_urls = []
|
58 |
-
if "pending_prompt" not in st.session_state:
|
59 |
-
st.session_state.pending_prompt = None
|
60 |
-
|
61 |
-
# ------------------ Tabs ------------------
|
62 |
tab1, tab2 = st.tabs(["💬 Chat Assistant", "🖼️ Visual Reference Search"])
|
63 |
|
|
|
64 |
with tab1:
|
65 |
-
# Sidebar
|
66 |
with st.sidebar:
|
67 |
st.header("🧪 Pathology Tools")
|
68 |
-
|
69 |
if st.button("🧹 Clear Chat"):
|
70 |
for k in ["messages", "thread_id", "image_urls", "pending_prompt"]:
|
71 |
st.session_state[k] = [] if k.endswith("s") else None
|
72 |
st.rerun()
|
73 |
|
74 |
show_image = st.toggle("📸 Show Images", value=True)
|
|
|
75 |
keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
|
76 |
if st.button("🔎 Search") and keyword:
|
77 |
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
|
@@ -97,14 +92,12 @@ with tab1:
|
|
97 |
st.markdown("### 💬 Ask a Pathology-Specific Question")
|
98 |
user_input = st.chat_input("Example: What are features of squamous cell carcinoma?")
|
99 |
|
100 |
-
# Append input or pending prompt
|
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 |
-
# Trigger assistant run
|
108 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
|
109 |
try:
|
110 |
if not st.session_state.thread_id:
|
@@ -133,13 +126,10 @@ with tab1:
|
|
133 |
for m in reversed(responses):
|
134 |
if m.role == "assistant":
|
135 |
reply = m.content[0].text.value.strip()
|
136 |
-
|
137 |
-
# De-dupe reply
|
138 |
if not any(reply in msg["content"] or msg["content"] in reply
|
139 |
for msg in st.session_state.messages if msg["role"] == "assistant"):
|
140 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
141 |
|
142 |
-
# Image detection
|
143 |
images = re.findall(
|
144 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n"]+\.png',
|
145 |
reply
|
@@ -152,12 +142,10 @@ with tab1:
|
|
152 |
except Exception as e:
|
153 |
st.error(f"❌ Error: {e}")
|
154 |
|
155 |
-
# Display conversation
|
156 |
for msg in st.session_state.messages:
|
157 |
with st.chat_message(msg["role"]):
|
158 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
159 |
|
160 |
-
# Optional follow-ups
|
161 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "assistant":
|
162 |
last = st.session_state.messages[-1]["content"]
|
163 |
if "Some Possible Questions:" in last:
|
@@ -179,6 +167,7 @@ with tab1:
|
|
179 |
except Exception:
|
180 |
st.warning(f"⚠️ Failed to load image: {url}")
|
181 |
|
|
|
182 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
183 |
with tab2:
|
184 |
ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
|
|
|
7 |
from io import BytesIO
|
8 |
from openai import OpenAI
|
9 |
|
10 |
+
# ------------------ Page Config ------------------
|
11 |
+
st.set_page_config(page_title="AI Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
|
12 |
+
|
13 |
# ------------------ Authentication ------------------
|
14 |
VALID_USERS = {
|
15 |
"[email protected]": "Pass.123",
|
|
|
36 |
login()
|
37 |
st.stop()
|
38 |
|
|
|
|
|
|
|
|
|
39 |
# ------------------ Load OpenAI ------------------
|
40 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
41 |
if not OPENAI_API_KEY:
|
|
|
47 |
# ------------------ Assistant Setup ------------------
|
48 |
ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB"
|
49 |
|
50 |
+
# ------------------ State Initialization ------------------
|
51 |
+
for key in ["messages", "thread_id", "image_urls", "pending_prompt"]:
|
52 |
+
if key not in st.session_state:
|
53 |
+
st.session_state[key] = [] if key.endswith("s") else None
|
54 |
+
|
55 |
+
# ------------------ UI Tabs ------------------
|
|
|
|
|
|
|
|
|
|
|
56 |
tab1, tab2 = st.tabs(["💬 Chat Assistant", "🖼️ Visual Reference Search"])
|
57 |
|
58 |
+
# ------------------ Tab 1: Chat Assistant ------------------
|
59 |
with tab1:
|
|
|
60 |
with st.sidebar:
|
61 |
st.header("🧪 Pathology Tools")
|
62 |
+
|
63 |
if st.button("🧹 Clear Chat"):
|
64 |
for k in ["messages", "thread_id", "image_urls", "pending_prompt"]:
|
65 |
st.session_state[k] = [] if k.endswith("s") else None
|
66 |
st.rerun()
|
67 |
|
68 |
show_image = st.toggle("📸 Show Images", value=True)
|
69 |
+
|
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}"
|
|
|
92 |
st.markdown("### 💬 Ask a Pathology-Specific Question")
|
93 |
user_input = st.chat_input("Example: What are features of squamous cell carcinoma?")
|
94 |
|
|
|
95 |
if user_input:
|
96 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
97 |
elif st.session_state.pending_prompt:
|
98 |
st.session_state.messages.append({"role": "user", "content": st.session_state.pending_prompt})
|
99 |
st.session_state.pending_prompt = None
|
100 |
|
|
|
101 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
|
102 |
try:
|
103 |
if not st.session_state.thread_id:
|
|
|
126 |
for m in reversed(responses):
|
127 |
if m.role == "assistant":
|
128 |
reply = m.content[0].text.value.strip()
|
|
|
|
|
129 |
if not any(reply in msg["content"] or msg["content"] in reply
|
130 |
for msg in st.session_state.messages if msg["role"] == "assistant"):
|
131 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
132 |
|
|
|
133 |
images = re.findall(
|
134 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n"]+\.png',
|
135 |
reply
|
|
|
142 |
except Exception as e:
|
143 |
st.error(f"❌ Error: {e}")
|
144 |
|
|
|
145 |
for msg in st.session_state.messages:
|
146 |
with st.chat_message(msg["role"]):
|
147 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
148 |
|
|
|
149 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "assistant":
|
150 |
last = st.session_state.messages[-1]["content"]
|
151 |
if "Some Possible Questions:" in last:
|
|
|
167 |
except Exception:
|
168 |
st.warning(f"⚠️ Failed to load image: {url}")
|
169 |
|
170 |
+
|
171 |
# ------------------ Tab 2: Visual Reference Search ------------------
|
172 |
with tab2:
|
173 |
ASSISTANT_ID = "asst_9v09zgizdcuuhNdcFQpRo9RO"
|