IAMTFRMZA commited on
Commit
10e5f47
ยท
verified ยท
1 Parent(s): 6863c9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
app.py CHANGED
@@ -48,17 +48,15 @@ if not OPENAI_API_KEY:
48
  client = OpenAI(api_key=OPENAI_API_KEY)
49
 
50
  # ------------------ Assistant Configuration ------------------
51
- ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB" # ๐Ÿ” Replace with your actual OpenAI assistant ID
52
 
53
  # ------------------ Session State ------------------
54
  if "messages" not in st.session_state:
55
  st.session_state.messages = []
56
  if "thread_id" not in st.session_state:
57
  st.session_state.thread_id = None
58
- if "image_url" not in st.session_state:
59
- st.session_state.image_url = None
60
- if "image_updated" not in st.session_state:
61
- st.session_state.image_updated = False
62
  if "pending_prompt" not in st.session_state:
63
  st.session_state.pending_prompt = None
64
 
@@ -68,12 +66,11 @@ with st.sidebar:
68
  if st.button("๐Ÿงน Clear Chat"):
69
  st.session_state.messages = []
70
  st.session_state.thread_id = None
71
- st.session_state.image_url = None
72
- st.session_state.image_updated = False
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}"
@@ -136,13 +133,13 @@ with chat_col:
136
  if m.role == "assistant":
137
  reply = m.content[0].text.value
138
  st.session_state.messages.append({"role": "assistant", "content": reply})
139
- match = re.search(r'Document Reference:\s*(.*?),\s*Page\s*(\d+)', reply)
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.")
@@ -154,13 +151,15 @@ with chat_col:
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:
161
- r = requests.get(st.session_state.image_url)
162
- r.raise_for_status()
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}")
 
 
 
48
  client = OpenAI(api_key=OPENAI_API_KEY)
49
 
50
  # ------------------ Assistant Configuration ------------------
51
+ ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB" # Replace with your Assistant ID
52
 
53
  # ------------------ Session State ------------------
54
  if "messages" not in st.session_state:
55
  st.session_state.messages = []
56
  if "thread_id" not in st.session_state:
57
  st.session_state.thread_id = None
58
+ if "image_urls" not in st.session_state:
59
+ st.session_state.image_urls = []
 
 
60
  if "pending_prompt" not in st.session_state:
61
  st.session_state.pending_prompt = None
62
 
 
66
  if st.button("๐Ÿงน Clear Chat"):
67
  st.session_state.messages = []
68
  st.session_state.thread_id = None
69
+ st.session_state.image_urls = []
 
70
  st.session_state.pending_prompt = None
71
  st.rerun()
72
 
73
+ show_image = st.toggle("๐Ÿ“ธ Show Slide Images", value=True)
74
  keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
75
  if st.button("๐Ÿ”Ž Search") and keyword:
76
  st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
 
133
  if m.role == "assistant":
134
  reply = m.content[0].text.value
135
  st.session_state.messages.append({"role": "assistant", "content": reply})
136
+
137
+ # ๐Ÿง  Extract ALL image URLs from assistant reply
138
+ image_matches = re.findall(
139
+ r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^ \n]+\.png',
140
+ reply
141
+ )
142
+ st.session_state.image_urls = image_matches
143
  break
144
  else:
145
  st.error("โŒ Assistant failed to respond.")
 
151
  with st.chat_message(msg["role"]):
152
  st.markdown(msg["content"], unsafe_allow_html=True)
153
 
154
+ # ------------------ Right Column: Image Previews ------------------
155
  with image_col:
156
+ if show_image and st.session_state.image_urls:
157
+ st.markdown("### ๐Ÿ–ผ๏ธ Slide Previews")
158
+ for url in st.session_state.image_urls:
159
+ try:
160
+ r = requests.get(url)
161
+ r.raise_for_status()
162
+ img = Image.open(BytesIO(r.content))
163
+ st.image(img, caption=f"๐Ÿ“ท {url.split('/')[-1]}", use_container_width=True)
164
+ except Exception as e:
165
+ st.error(f"โŒ Failed to load image from {url}: {e}")