Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -35,11 +35,11 @@ if not st.session_state.authenticated:
|
|
35 |
st.stop()
|
36 |
|
37 |
# ------------------ App Configuration ------------------
|
38 |
-
st.set_page_config(page_title="Pathology Assistant", layout="wide", initial_sidebar_state="collapsed")
|
39 |
st.title("𧬠AI Pathology Assistant")
|
40 |
-
st.caption("
|
41 |
|
42 |
-
# ------------------ Load 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.")
|
@@ -48,7 +48,7 @@ if not OPENAI_API_KEY:
|
|
48 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
49 |
|
50 |
# ------------------ Assistant Configuration ------------------
|
51 |
-
ASSISTANT_ID = "
|
52 |
|
53 |
# ------------------ Session State ------------------
|
54 |
if "messages" not in st.session_state:
|
@@ -64,7 +64,7 @@ if "pending_prompt" not in st.session_state:
|
|
64 |
|
65 |
# ------------------ Sidebar ------------------
|
66 |
with st.sidebar:
|
67 |
-
st.header("
|
68 |
if st.button("π§Ή Clear Chat"):
|
69 |
st.session_state.messages = []
|
70 |
st.session_state.thread_id = None
|
@@ -73,12 +73,12 @@ with st.sidebar:
|
|
73 |
st.session_state.pending_prompt = None
|
74 |
st.rerun()
|
75 |
|
76 |
-
show_image = st.toggle("
|
77 |
-
keyword = st.text_input("Keyword Search
|
78 |
-
if st.button("π Search
|
79 |
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
|
80 |
|
81 |
-
section = st.text_input("
|
82 |
if section:
|
83 |
st.session_state.pending_prompt = f"Summarize or list key points from section: {section}"
|
84 |
|
@@ -90,11 +90,11 @@ with st.sidebar:
|
|
90 |
"Extract diagnostic markers",
|
91 |
"Summarize embryology stages"
|
92 |
]
|
93 |
-
action = st.selectbox("
|
94 |
if action != actions[0]:
|
95 |
st.session_state.pending_prompt = action
|
96 |
|
97 |
-
# ------------------ Main Chat
|
98 |
chat_col, image_col = st.columns([2, 1])
|
99 |
|
100 |
with chat_col:
|
@@ -123,7 +123,7 @@ with chat_col:
|
|
123 |
assistant_id=ASSISTANT_ID
|
124 |
)
|
125 |
|
126 |
-
with st.spinner("π¬ Analyzing
|
127 |
while True:
|
128 |
status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
|
129 |
if status.status in ("completed", "failed", "cancelled"):
|
@@ -140,12 +140,12 @@ with chat_col:
|
|
140 |
if match:
|
141 |
doc, page = match.group(1).strip(), int(match.group(2))
|
142 |
folder = quote(doc)
|
143 |
-
img_url = f"https://raw.githubusercontent.com/AndrewLORTech/
|
144 |
st.session_state.image_url = img_url
|
145 |
st.session_state.image_updated = True
|
146 |
break
|
147 |
else:
|
148 |
-
st.error("β Assistant failed.")
|
149 |
st.rerun()
|
150 |
except Exception as e:
|
151 |
st.error(f"β Error: {e}")
|
@@ -154,7 +154,7 @@ with chat_col:
|
|
154 |
with st.chat_message(msg["role"]):
|
155 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
156 |
|
157 |
-
# ------------------ Image
|
158 |
with image_col:
|
159 |
if show_image and st.session_state.image_url:
|
160 |
try:
|
@@ -163,4 +163,4 @@ with image_col:
|
|
163 |
img = Image.open(BytesIO(r.content))
|
164 |
st.image(img, caption="π§Ύ OCR Page Image", use_container_width=True)
|
165 |
except Exception as e:
|
166 |
-
st.error(f"πΌοΈ
|
|
|
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.")
|
|
|
48 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
49 |
|
50 |
# ------------------ Assistant Configuration ------------------
|
51 |
+
ASSISTANT_ID = "asst_YOUR_PATHOLOGY_ASSISTANT_ID" # π Replace with your actual OpenAI assistant ID
|
52 |
|
53 |
# ------------------ Session State ------------------
|
54 |
if "messages" not in st.session_state:
|
|
|
64 |
|
65 |
# ------------------ Sidebar ------------------
|
66 |
with st.sidebar:
|
67 |
+
st.header("π§ͺ Pathology Tools")
|
68 |
if st.button("π§Ή Clear Chat"):
|
69 |
st.session_state.messages = []
|
70 |
st.session_state.thread_id = None
|
|
|
73 |
st.session_state.pending_prompt = None
|
74 |
st.rerun()
|
75 |
|
76 |
+
show_image = st.toggle("πΈ Show Slide Image", value=True)
|
77 |
+
keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
|
78 |
+
if st.button("π Search") and keyword:
|
79 |
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
|
80 |
|
81 |
+
section = st.text_input("Section Lookup", placeholder="e.g. Connective Tissue")
|
82 |
if section:
|
83 |
st.session_state.pending_prompt = f"Summarize or list key points from section: {section}"
|
84 |
|
|
|
90 |
"Extract diagnostic markers",
|
91 |
"Summarize embryology stages"
|
92 |
]
|
93 |
+
action = st.selectbox("Common Pathology Queries", actions)
|
94 |
if action != actions[0]:
|
95 |
st.session_state.pending_prompt = action
|
96 |
|
97 |
+
# ------------------ Main Chat UI ------------------
|
98 |
chat_col, image_col = st.columns([2, 1])
|
99 |
|
100 |
with chat_col:
|
|
|
123 |
assistant_id=ASSISTANT_ID
|
124 |
)
|
125 |
|
126 |
+
with st.spinner("π¬ Analyzing..."):
|
127 |
while True:
|
128 |
status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
|
129 |
if status.status in ("completed", "failed", "cancelled"):
|
|
|
140 |
if match:
|
141 |
doc, page = match.group(1).strip(), int(match.group(2))
|
142 |
folder = quote(doc)
|
143 |
+
img_url = f"https://raw.githubusercontent.com/AndrewLORTech/witspathologai/main/{folder}/{folder}_page_{page:04d}.png"
|
144 |
st.session_state.image_url = img_url
|
145 |
st.session_state.image_updated = True
|
146 |
break
|
147 |
else:
|
148 |
+
st.error("β Assistant failed to respond.")
|
149 |
st.rerun()
|
150 |
except Exception as e:
|
151 |
st.error(f"β Error: {e}")
|
|
|
154 |
with st.chat_message(msg["role"]):
|
155 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
156 |
|
157 |
+
# ------------------ Right Column: OCR Page Image ------------------
|
158 |
with image_col:
|
159 |
if show_image and st.session_state.image_url:
|
160 |
try:
|
|
|
163 |
img = Image.open(BytesIO(r.content))
|
164 |
st.image(img, caption="π§Ύ OCR Page Image", use_container_width=True)
|
165 |
except Exception as e:
|
166 |
+
st.error(f"πΌοΈ Failed to load image: {e}")
|