IAMTFRMZA commited on
Commit
2621f7d
·
verified ·
1 Parent(s): befd3de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import time
4
  import re
5
  import requests
 
6
  from PIL import Image
7
  from io import BytesIO
8
  from openai import OpenAI
@@ -47,7 +48,7 @@ if not OPENAI_API_KEY:
47
  client = OpenAI(api_key=OPENAI_API_KEY)
48
 
49
  # ------------------ Assistant Configuration ------------------
50
- ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB" # Replace with your Assistant ID
51
 
52
  # ------------------ Session State ------------------
53
  if "messages" not in st.session_state:
@@ -133,7 +134,7 @@ with chat_col:
133
  reply = m.content[0].text.value
134
  st.session_state.messages.append({"role": "assistant", "content": reply})
135
 
136
- # 🔍 Extract GitHub raw image URLs
137
  image_matches = re.findall(
138
  r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n"]+\.png',
139
  reply
@@ -150,15 +151,23 @@ with chat_col:
150
  with st.chat_message(msg["role"]):
151
  st.markdown(msg["content"], unsafe_allow_html=True)
152
 
153
- # ------------------ Image Preview (Silent Fails) ------------------
154
  with image_col:
155
  if show_image and st.session_state.image_urls:
156
  st.markdown("### 🖼️ Slide Previews")
157
  for raw_url in st.session_state.image_urls:
158
  try:
159
- r = requests.get(raw_url, timeout=5)
 
 
 
 
 
 
 
 
160
  r.raise_for_status()
161
  img = Image.open(BytesIO(r.content))
162
- st.image(img, caption=f"📷 {raw_url.split('/')[-1]}", use_container_width=True)
163
  except Exception:
164
  continue # Silently skip broken images
 
3
  import time
4
  import re
5
  import requests
6
+ import urllib.parse
7
  from PIL import Image
8
  from io import BytesIO
9
  from openai import OpenAI
 
48
  client = OpenAI(api_key=OPENAI_API_KEY)
49
 
50
  # ------------------ Assistant Configuration ------------------
51
+ ASSISTANT_ID = "asst_jXDSjCG8LI4HEaFEcjFVq8KB"
52
 
53
  # ------------------ Session State ------------------
54
  if "messages" not in st.session_state:
 
134
  reply = m.content[0].text.value
135
  st.session_state.messages.append({"role": "assistant", "content": reply})
136
 
137
+ # Extract raw GitHub image URLs
138
  image_matches = re.findall(
139
  r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n"]+\.png',
140
  reply
 
151
  with st.chat_message(msg["role"]):
152
  st.markdown(msg["content"], unsafe_allow_html=True)
153
 
154
+ # ------------------ Image Preview (Smart URL 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
+ if raw_url.startswith("https://raw.githubusercontent.com/"):
161
+ base_url = "https://raw.githubusercontent.com/"
162
+ path = raw_url[len(base_url):]
163
+ encoded_path = urllib.parse.quote(path, safe="/")
164
+ encoded_url = base_url + encoded_path
165
+ else:
166
+ encoded_url = raw_url
167
+
168
+ r = requests.get(encoded_url, timeout=5)
169
  r.raise_for_status()
170
  img = Image.open(BytesIO(r.content))
171
+ st.image(img, caption=f"📷 {encoded_url.split('/')[-1]}", use_container_width=True)
172
  except Exception:
173
  continue # Silently skip broken images