IAMTFRMZA commited on
Commit
6bb3fe2
·
verified ·
1 Parent(s): 7b2a5f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -26
app.py CHANGED
@@ -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 your actual assistant ID
52
 
53
  # ------------------ Session State ------------------
54
  if "messages" not in st.session_state:
@@ -132,16 +132,9 @@ with chat_col:
132
  for m in reversed(messages.data):
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)
139
- st.session_state.messages.append({"role": "assistant", "content": main_answer.strip()})
140
- st.session_state.messages.append({"role": "references", "content": references.strip()})
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
@@ -155,27 +148,25 @@ with chat_col:
155
  st.error(f"❌ Error: {e}")
156
 
157
  for msg in st.session_state.messages:
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}")
 
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:
 
132
  for m in reversed(messages.data):
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 GitHub image URLs
 
 
 
 
 
 
 
 
138
  image_matches = re.findall(
139
  r'https://raw\.githubusercontent\.com/AndrewLORTech/witspathologai/main/[^\s\n]+\.png',
140
  reply
 
148
  st.error(f"❌ Error: {e}")
149
 
150
  for msg in st.session_state.messages:
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}")