IAMTFRMZA commited on
Commit
f3c5e51
·
verified ·
1 Parent(s): 10e5f47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -134,9 +134,9 @@ with chat_col:
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
@@ -151,15 +151,23 @@ with chat_col:
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}")
 
 
134
  reply = m.content[0].text.value
135
  st.session_state.messages.append({"role": "assistant", "content": reply})
136
 
137
+ # 🔍 Extract all GitHub image URLs
138
  image_matches = re.findall(
139
+ r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n]+\.png',
140
  reply
141
  )
142
  st.session_state.image_urls = image_matches
 
151
  with st.chat_message(msg["role"]):
152
  st.markdown(msg["content"], unsafe_allow_html=True)
153
 
154
+ # ------------------ Image Preview with Safe Encoding ------------------
155
  with image_col:
156
  if show_image and st.session_state.image_urls:
157
  st.markdown("### 🖼️ Slide Previews")
158
+ for raw_url in st.session_state.image_urls:
159
  try:
160
+ url_parts = raw_url.split("githubusercontent.com/")
161
+ if len(url_parts) == 2:
162
+ encoded_path = quote(url_parts[1])
163
+ encoded_url = f"https://raw.githubusercontent.com/{encoded_path}"
164
+ else:
165
+ encoded_url = raw_url
166
+
167
+ r = requests.get(encoded_url)
168
  r.raise_for_status()
169
  img = Image.open(BytesIO(r.content))
170
+ st.image(img, caption=f"📷 {encoded_url.split('/')[-1]}", use_container_width=True)
171
  except Exception as e:
172
+ st.error(f"❌ Failed to load image from {raw_url}: {e}")
173
+