Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -48,17 +48,15 @@ if not OPENAI_API_KEY:
|
|
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:
|
55 |
st.session_state.messages = []
|
56 |
if "thread_id" not in st.session_state:
|
57 |
st.session_state.thread_id = None
|
58 |
-
if "
|
59 |
-
st.session_state.
|
60 |
-
if "image_updated" not in st.session_state:
|
61 |
-
st.session_state.image_updated = False
|
62 |
if "pending_prompt" not in st.session_state:
|
63 |
st.session_state.pending_prompt = None
|
64 |
|
@@ -68,12 +66,11 @@ with st.sidebar:
|
|
68 |
if st.button("๐งน Clear Chat"):
|
69 |
st.session_state.messages = []
|
70 |
st.session_state.thread_id = None
|
71 |
-
st.session_state.
|
72 |
-
st.session_state.image_updated = False
|
73 |
st.session_state.pending_prompt = None
|
74 |
st.rerun()
|
75 |
|
76 |
-
show_image = st.toggle("๐ธ Show Slide
|
77 |
keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
|
78 |
if st.button("๐ Search") and keyword:
|
79 |
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
|
@@ -136,13 +133,13 @@ with chat_col:
|
|
136 |
if m.role == "assistant":
|
137 |
reply = m.content[0].text.value
|
138 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
break
|
147 |
else:
|
148 |
st.error("โ Assistant failed to respond.")
|
@@ -154,13 +151,15 @@ with chat_col:
|
|
154 |
with st.chat_message(msg["role"]):
|
155 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
156 |
|
157 |
-
# ------------------ Right Column:
|
158 |
with image_col:
|
159 |
-
if show_image and st.session_state.
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
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:
|
55 |
st.session_state.messages = []
|
56 |
if "thread_id" not in st.session_state:
|
57 |
st.session_state.thread_id = None
|
58 |
+
if "image_urls" not in st.session_state:
|
59 |
+
st.session_state.image_urls = []
|
|
|
|
|
60 |
if "pending_prompt" not in st.session_state:
|
61 |
st.session_state.pending_prompt = None
|
62 |
|
|
|
66 |
if st.button("๐งน Clear Chat"):
|
67 |
st.session_state.messages = []
|
68 |
st.session_state.thread_id = None
|
69 |
+
st.session_state.image_urls = []
|
|
|
70 |
st.session_state.pending_prompt = None
|
71 |
st.rerun()
|
72 |
|
73 |
+
show_image = st.toggle("๐ธ Show Slide Images", value=True)
|
74 |
keyword = st.text_input("Keyword Search", placeholder="e.g. mitosis, carcinoma")
|
75 |
if st.button("๐ Search") and keyword:
|
76 |
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
|
|
|
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 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
|
143 |
break
|
144 |
else:
|
145 |
st.error("โ Assistant failed to respond.")
|
|
|
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}")
|