Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -49,20 +49,21 @@ col1, col2 = st.columns([1, 2])
|
|
49 |
# ------------------ Left Panel: Image ------------------
|
50 |
with col1:
|
51 |
if show_image and st.session_state.image_url:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
# ------------------ Right Panel: Chat ------------------
|
62 |
with col2:
|
63 |
-
prompt = st.chat_input("Type your question about the document...")
|
64 |
|
65 |
-
# Display chat history
|
66 |
paired_messages = []
|
67 |
buffer = []
|
68 |
for msg in st.session_state.messages:
|
@@ -76,31 +77,28 @@ with col2:
|
|
76 |
for pair in reversed(paired_messages):
|
77 |
for msg in pair:
|
78 |
with st.chat_message(msg["role"]):
|
79 |
-
st.
|
80 |
|
81 |
if prompt:
|
82 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
83 |
|
84 |
try:
|
85 |
-
# Create thread
|
86 |
if st.session_state.thread_id is None:
|
87 |
thread = client.beta.threads.create()
|
88 |
st.session_state.thread_id = thread.id
|
89 |
|
90 |
-
# Send user message
|
91 |
client.beta.threads.messages.create(
|
92 |
thread_id=st.session_state.thread_id,
|
93 |
role="user",
|
94 |
content=prompt
|
95 |
)
|
96 |
|
97 |
-
# Run assistant
|
98 |
run = client.beta.threads.runs.create(
|
99 |
thread_id=st.session_state.thread_id,
|
100 |
assistant_id=ASSISTANT_ID
|
101 |
)
|
102 |
|
103 |
-
with st.spinner("Assistant is thinking..."):
|
104 |
while True:
|
105 |
run_status = client.beta.threads.runs.retrieve(
|
106 |
thread_id=st.session_state.thread_id,
|
@@ -110,7 +108,6 @@ with col2:
|
|
110 |
break
|
111 |
time.sleep(1)
|
112 |
|
113 |
-
# Retrieve assistant reply
|
114 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
115 |
assistant_message = None
|
116 |
for message in reversed(messages.data):
|
@@ -120,7 +117,6 @@ with col2:
|
|
120 |
|
121 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
122 |
|
123 |
-
# ✅ Parse Document Reference line and generate correct image URL
|
124 |
match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
|
125 |
if match:
|
126 |
doc_name = match.group(1).strip()
|
@@ -136,4 +132,13 @@ with col2:
|
|
136 |
st.rerun()
|
137 |
|
138 |
except Exception as e:
|
139 |
-
st.error(f"❌ Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
# ------------------ Left Panel: Image ------------------
|
50 |
with col1:
|
51 |
if show_image and st.session_state.image_url:
|
52 |
+
with st.spinner("🖼️ Loading image preview..."):
|
53 |
+
try:
|
54 |
+
response = requests.get(st.session_state.image_url)
|
55 |
+
response.raise_for_status()
|
56 |
+
img = Image.open(BytesIO(response.content))
|
57 |
+
st.image(img, caption="📑 Extracted Page", use_container_width=True)
|
58 |
+
st.session_state.image_updated = False
|
59 |
+
except Exception as e:
|
60 |
+
st.warning(f"⚠️ Failed to load image from URL:\n{st.session_state.image_url}\n\nError: {e}")
|
61 |
|
62 |
# ------------------ Right Panel: Chat ------------------
|
63 |
with col2:
|
64 |
+
prompt = st.chat_input("Type your question about the document or choose from FAQs below...")
|
65 |
|
66 |
+
# Display chat history in a cleaner bubble layout
|
67 |
paired_messages = []
|
68 |
buffer = []
|
69 |
for msg in st.session_state.messages:
|
|
|
77 |
for pair in reversed(paired_messages):
|
78 |
for msg in pair:
|
79 |
with st.chat_message(msg["role"]):
|
80 |
+
st.markdown(f"{msg['content']}", unsafe_allow_html=True)
|
81 |
|
82 |
if prompt:
|
83 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
84 |
|
85 |
try:
|
|
|
86 |
if st.session_state.thread_id is None:
|
87 |
thread = client.beta.threads.create()
|
88 |
st.session_state.thread_id = thread.id
|
89 |
|
|
|
90 |
client.beta.threads.messages.create(
|
91 |
thread_id=st.session_state.thread_id,
|
92 |
role="user",
|
93 |
content=prompt
|
94 |
)
|
95 |
|
|
|
96 |
run = client.beta.threads.runs.create(
|
97 |
thread_id=st.session_state.thread_id,
|
98 |
assistant_id=ASSISTANT_ID
|
99 |
)
|
100 |
|
101 |
+
with st.spinner("🤖 Assistant is thinking..."):
|
102 |
while True:
|
103 |
run_status = client.beta.threads.runs.retrieve(
|
104 |
thread_id=st.session_state.thread_id,
|
|
|
108 |
break
|
109 |
time.sleep(1)
|
110 |
|
|
|
111 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.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 |
match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
|
121 |
if match:
|
122 |
doc_name = match.group(1).strip()
|
|
|
132 |
st.rerun()
|
133 |
|
134 |
except Exception as e:
|
135 |
+
st.error(f"❌ Error: {str(e)}")
|
136 |
+
|
137 |
+
# ------------------ Suggested FAQs Section ------------------
|
138 |
+
st.markdown("""
|
139 |
+
<hr>
|
140 |
+
<b>🧠 Suggested FAQs:</b>
|
141 |
+
- What are the mechanical services on page 3?
|
142 |
+
- Summarize the demolition schedule.
|
143 |
+
- List the safety compliance steps.
|
144 |
+
""")
|