Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -42,48 +42,51 @@ if st.sidebar.button("π Clear Chat"):
|
|
42 |
show_image = st.sidebar.checkbox("π Show Document Image", value=True)
|
43 |
|
44 |
# ------------------ Split Layout ------------------
|
45 |
-
col1, col2 = st.columns([1, 2])
|
46 |
|
47 |
# ------------------ Image Panel (Left) ------------------
|
48 |
with col1:
|
49 |
if show_image and st.session_state.image_url:
|
50 |
st.image(st.session_state.image_url, caption="π Extracted Page", use_container_width=True)
|
51 |
-
st.session_state.image_updated = False
|
52 |
|
53 |
# ------------------ Chat Panel (Right) ------------------
|
54 |
with col2:
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
st.chat_message(role).write(content)
|
59 |
|
60 |
-
#
|
|
|
|
|
|
|
|
|
61 |
if prompt := st.chat_input("Type your question about the document..."):
|
62 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
63 |
-
st.chat_message("user").write(prompt)
|
64 |
|
65 |
try:
|
66 |
-
#
|
67 |
if st.session_state.thread_id is None:
|
68 |
thread = client.beta.threads.create()
|
69 |
st.session_state.thread_id = thread.id
|
70 |
|
71 |
thread_id = st.session_state.thread_id
|
72 |
|
73 |
-
# Send
|
74 |
client.beta.threads.messages.create(
|
75 |
thread_id=thread_id,
|
76 |
role="user",
|
77 |
content=prompt
|
78 |
)
|
79 |
|
80 |
-
#
|
81 |
run = client.beta.threads.runs.create(
|
82 |
thread_id=thread_id,
|
83 |
assistant_id=ASSISTANT_ID
|
84 |
)
|
85 |
|
86 |
-
# Wait for
|
87 |
with st.spinner("Assistant is thinking..."):
|
88 |
while True:
|
89 |
run_status = client.beta.threads.runs.retrieve(
|
@@ -102,10 +105,9 @@ with col2:
|
|
102 |
assistant_message = message.content[0].text.value
|
103 |
break
|
104 |
|
105 |
-
st.chat_message("assistant").write(assistant_message)
|
106 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
107 |
|
108 |
-
#
|
109 |
image_match = re.search(
|
110 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
111 |
assistant_message
|
@@ -113,7 +115,8 @@ with col2:
|
|
113 |
if image_match:
|
114 |
st.session_state.image_url = image_match.group(0)
|
115 |
st.session_state.image_updated = True
|
116 |
-
|
|
|
117 |
|
118 |
except Exception as e:
|
119 |
st.error(f"β Error: {str(e)}")
|
|
|
42 |
show_image = st.sidebar.checkbox("π Show Document Image", value=True)
|
43 |
|
44 |
# ------------------ Split Layout ------------------
|
45 |
+
col1, col2 = st.columns([1, 2])
|
46 |
|
47 |
# ------------------ Image Panel (Left) ------------------
|
48 |
with col1:
|
49 |
if show_image and st.session_state.image_url:
|
50 |
st.image(st.session_state.image_url, caption="π Extracted Page", use_container_width=True)
|
51 |
+
st.session_state.image_updated = False
|
52 |
|
53 |
# ------------------ Chat Panel (Right) ------------------
|
54 |
with col2:
|
55 |
+
# Display latest message block separately at the top (if available)
|
56 |
+
if st.session_state.messages:
|
57 |
+
latest_msg = st.session_state.messages[-1]
|
58 |
+
st.chat_message(latest_msg["role"]).write(latest_msg["content"])
|
59 |
|
60 |
+
# Show the rest of the chat history (excluding latest)
|
61 |
+
for message in st.session_state.messages[:-1]:
|
62 |
+
st.chat_message(message["role"]).write(message["content"])
|
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 it doesn't exist
|
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 prompt
|
77 |
client.beta.threads.messages.create(
|
78 |
thread_id=thread_id,
|
79 |
role="user",
|
80 |
content=prompt
|
81 |
)
|
82 |
|
83 |
+
# Trigger run
|
84 |
run = client.beta.threads.runs.create(
|
85 |
thread_id=thread_id,
|
86 |
assistant_id=ASSISTANT_ID
|
87 |
)
|
88 |
|
89 |
+
# Wait for completion
|
90 |
with st.spinner("Assistant is thinking..."):
|
91 |
while True:
|
92 |
run_status = client.beta.threads.runs.retrieve(
|
|
|
105 |
assistant_message = message.content[0].text.value
|
106 |
break
|
107 |
|
|
|
108 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
109 |
|
110 |
+
# Handle image from response if exists
|
111 |
image_match = re.search(
|
112 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
113 |
assistant_message
|
|
|
115 |
if image_match:
|
116 |
st.session_state.image_url = image_match.group(0)
|
117 |
st.session_state.image_updated = True
|
118 |
+
|
119 |
+
st.rerun()
|
120 |
|
121 |
except Exception as e:
|
122 |
st.error(f"β Error: {str(e)}")
|