Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -52,41 +52,51 @@ with col1:
|
|
52 |
|
53 |
# ------------------ Chat Panel (Right) ------------------
|
54 |
with col2:
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
st.chat_message(latest_msg["role"]).write(latest_msg["content"])
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
# ------------------ Chat Input Handling ------------------
|
65 |
if prompt := st.chat_input("Type your question about the document..."):
|
66 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
67 |
|
68 |
try:
|
69 |
-
# Create thread if
|
70 |
if st.session_state.thread_id is None:
|
71 |
thread = client.beta.threads.create()
|
72 |
st.session_state.thread_id = thread.id
|
73 |
|
74 |
thread_id = st.session_state.thread_id
|
75 |
|
76 |
-
# Send
|
77 |
client.beta.threads.messages.create(
|
78 |
thread_id=thread_id,
|
79 |
role="user",
|
80 |
content=prompt
|
81 |
)
|
82 |
|
83 |
-
#
|
84 |
run = client.beta.threads.runs.create(
|
85 |
thread_id=thread_id,
|
86 |
assistant_id=ASSISTANT_ID
|
87 |
)
|
88 |
|
89 |
-
# Wait for
|
90 |
with st.spinner("Assistant is thinking..."):
|
91 |
while True:
|
92 |
run_status = client.beta.threads.runs.retrieve(
|
@@ -97,7 +107,7 @@ with col2:
|
|
97 |
break
|
98 |
time.sleep(1)
|
99 |
|
100 |
-
#
|
101 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
102 |
assistant_message = None
|
103 |
for message in reversed(messages.data):
|
@@ -107,7 +117,7 @@ with col2:
|
|
107 |
|
108 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
109 |
|
110 |
-
#
|
111 |
image_match = re.search(
|
112 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
113 |
assistant_message
|
|
|
52 |
|
53 |
# ------------------ Chat Panel (Right) ------------------
|
54 |
with col2:
|
55 |
+
# Pair user + assistant messages
|
56 |
+
paired_messages = []
|
57 |
+
buffer = []
|
|
|
58 |
|
59 |
+
for msg in st.session_state.messages:
|
60 |
+
buffer.append(msg)
|
61 |
+
if msg["role"] == "assistant" and len(buffer) == 2:
|
62 |
+
paired_messages.append(buffer.copy())
|
63 |
+
buffer.clear()
|
64 |
+
|
65 |
+
# If last message is from user and no assistant reply yet
|
66 |
+
if buffer:
|
67 |
+
paired_messages.append(buffer.copy())
|
68 |
+
|
69 |
+
# Show pairs in reverse order (latest on top)
|
70 |
+
for pair in reversed(paired_messages):
|
71 |
+
for msg in pair:
|
72 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
73 |
|
74 |
# ------------------ Chat Input Handling ------------------
|
75 |
if prompt := st.chat_input("Type your question about the document..."):
|
76 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
77 |
|
78 |
try:
|
79 |
+
# Create thread if not exists
|
80 |
if st.session_state.thread_id is None:
|
81 |
thread = client.beta.threads.create()
|
82 |
st.session_state.thread_id = thread.id
|
83 |
|
84 |
thread_id = st.session_state.thread_id
|
85 |
|
86 |
+
# Send user message to assistant
|
87 |
client.beta.threads.messages.create(
|
88 |
thread_id=thread_id,
|
89 |
role="user",
|
90 |
content=prompt
|
91 |
)
|
92 |
|
93 |
+
# Run assistant
|
94 |
run = client.beta.threads.runs.create(
|
95 |
thread_id=thread_id,
|
96 |
assistant_id=ASSISTANT_ID
|
97 |
)
|
98 |
|
99 |
+
# Wait for assistant to respond
|
100 |
with st.spinner("Assistant is thinking..."):
|
101 |
while True:
|
102 |
run_status = client.beta.threads.runs.retrieve(
|
|
|
107 |
break
|
108 |
time.sleep(1)
|
109 |
|
110 |
+
# Retrieve assistant response
|
111 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
112 |
assistant_message = None
|
113 |
for message in reversed(messages.data):
|
|
|
117 |
|
118 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
119 |
|
120 |
+
# Extract image URL if present
|
121 |
image_match = re.search(
|
122 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
123 |
assistant_message
|