Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
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 |
-
|
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(
|
159 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
160 |
|
161 |
-
# ------------------ Image
|
162 |
with image_col:
|
163 |
if show_image and st.session_state.image_urls:
|
164 |
st.markdown("### 🖼️ Slide Previews")
|
165 |
-
for
|
166 |
try:
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
180 |
except Exception as e:
|
181 |
-
st.error(f"❌ Failed to load image {
|
|
|
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}")
|