IAMTFRMZA commited on
Commit
7b2a5f2
·
verified ·
1 Parent(s): 41c50e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -2,6 +2,9 @@ import streamlit as st
2
  import os
3
  import time
4
  import re
 
 
 
5
  from urllib.parse import quote
6
  from openai import OpenAI
7
 
@@ -130,7 +133,6 @@ with chat_col:
130
  if m.role == "assistant":
131
  reply = m.content[0].text.value
132
 
133
- # ✂️ Split output into main and references
134
  split_marker = "---"
135
  if split_marker in reply:
136
  main_answer, references = reply.split(split_marker, 1)
@@ -139,7 +141,7 @@ with chat_col:
139
  else:
140
  st.session_state.messages.append({"role": "assistant", "content": reply})
141
 
142
- # Extract raw GitHub image URLs
143
  image_matches = re.findall(
144
  r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n]+\.png',
145
  reply
@@ -156,17 +158,24 @@ with chat_col:
156
  with st.chat_message("assistant" if msg["role"] != "references" else "📄"):
157
  st.markdown(msg["content"], unsafe_allow_html=True)
158
 
159
- # ------------------ Image Viewer (with encoded URLs) ------------------
160
  with image_col:
161
  if show_image and st.session_state.image_urls:
162
  st.markdown("### 🖼️ Slide Previews")
163
  for i, raw_url in enumerate(st.session_state.image_urls):
164
  try:
165
- if "raw.githubusercontent.com" in raw_url:
166
- base = "https://raw.githubusercontent.com/"
167
- path = raw_url.replace(base, "")
168
- encoded_path = "/".join(quote(p) for p in path.split("/"))
169
- encoded_url = base + encoded_path
170
- st.image(encoded_url, caption=f"Slide {i + 1}", use_container_width=True)
 
 
 
 
 
 
 
171
  except Exception as e:
172
  st.error(f"❌ Failed to load image {i + 1}: {e}")
 
2
  import os
3
  import time
4
  import re
5
+ import requests
6
+ from PIL import Image
7
+ from io import BytesIO
8
  from urllib.parse import quote
9
  from openai import OpenAI
10
 
 
133
  if m.role == "assistant":
134
  reply = m.content[0].text.value
135
 
 
136
  split_marker = "---"
137
  if split_marker in reply:
138
  main_answer, references = reply.split(split_marker, 1)
 
141
  else:
142
  st.session_state.messages.append({"role": "assistant", "content": reply})
143
 
144
+ # Extract raw GitHub URLs
145
  image_matches = re.findall(
146
  r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n]+\.png',
147
  reply
 
158
  with st.chat_message("assistant" if msg["role"] != "references" else "📄"):
159
  st.markdown(msg["content"], unsafe_allow_html=True)
160
 
161
+ # ------------------ Image Viewer (requests + PIL fallback) ------------------
162
  with image_col:
163
  if show_image and st.session_state.image_urls:
164
  st.markdown("### 🖼️ Slide Previews")
165
  for i, raw_url in enumerate(st.session_state.image_urls):
166
  try:
167
+ # Encode the full URL path
168
+ base = "https://raw.githubusercontent.com/"
169
+ path = raw_url.replace(base, "")
170
+ encoded_path = "/".join(quote(p) for p in path.split("/"))
171
+ encoded_url = base + encoded_path
172
+
173
+ # Fetch image with requests
174
+ response = requests.get(encoded_url)
175
+ if response.status_code == 200:
176
+ img = Image.open(BytesIO(response.content))
177
+ st.image(img, caption=f"Slide {i + 1}", use_container_width=True)
178
+ else:
179
+ st.warning(f"⚠️ Could not load image {i + 1}")
180
  except Exception as e:
181
  st.error(f"❌ Failed to load image {i + 1}: {e}")