IAMTFRMZA commited on
Commit
89fd98c
Β·
verified Β·
1 Parent(s): 4b00180

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
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("Explore histology, anatomy, and pathology documents using AI + OCR")
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 = "asst_jXDSjCG8LI4HEaFEcjFVq8KB" # Replace with actual 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("πŸ” Pathology Tools")
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("πŸ–ΌοΈ Show Slide Image", value=True)
77
- keyword = st.text_input("Keyword Search (e.g. mitosis, infarct, carcinoma)")
78
- if st.button("πŸ”Ž Search Keyword") and keyword:
79
  st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
80
 
81
- section = st.text_input("🧠 Section Lookup (e.g. Connective Tissue, Inflammation)")
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("βš™οΈ Common Queries", actions)
94
  if action != actions[0]:
95
  st.session_state.pending_prompt = action
96
 
97
- # ------------------ Main Chat Layout ------------------
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 pathology data..."):
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/c2ozschlaegerforrestdale/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.")
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 Viewer ------------------
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"πŸ–ΌοΈ Image failed: {e}")
 
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}")