Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -181,47 +181,42 @@ with tab1:
|
|
| 181 |
except Exception as e:
|
| 182 |
st.error(f"πΌοΈ Image failed: {e}")
|
| 183 |
|
| 184 |
-
# ------------------ Technical Tab ------------------
|
| 185 |
# ------------------ Technical Tab ------------------
|
| 186 |
with tab2:
|
| 187 |
ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
|
| 188 |
-
|
| 189 |
if "tech_messages" not in st.session_state:
|
| 190 |
st.session_state.tech_messages = []
|
|
|
|
|
|
|
| 191 |
if "tech_results" not in st.session_state:
|
| 192 |
st.session_state.tech_results = []
|
| 193 |
-
|
| 194 |
-
st.session_state.tech_lightbox = None
|
| 195 |
|
| 196 |
tech_input = st.chat_input("Ask about plans, drawings or components")
|
| 197 |
-
|
| 198 |
if tech_input:
|
| 199 |
-
# Clear old thread/results each time
|
| 200 |
st.session_state.tech_messages.append({"role": "user", "content": tech_input})
|
| 201 |
-
st.session_state.tech_results = []
|
| 202 |
|
|
|
|
| 203 |
try:
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
|
| 208 |
-
# Send user message
|
| 209 |
client.beta.threads.messages.create(
|
| 210 |
-
thread_id=tech_thread_id,
|
| 211 |
role="user",
|
| 212 |
-
content=
|
| 213 |
)
|
| 214 |
|
| 215 |
-
# Run assistant
|
| 216 |
run = client.beta.threads.runs.create(
|
| 217 |
-
thread_id=tech_thread_id,
|
| 218 |
assistant_id=ASSISTANT_ID
|
| 219 |
)
|
| 220 |
|
| 221 |
with st.spinner("π Searching technical drawings..."):
|
| 222 |
while True:
|
| 223 |
run_status = client.beta.threads.runs.retrieve(
|
| 224 |
-
thread_id=tech_thread_id,
|
| 225 |
run_id=run.id
|
| 226 |
)
|
| 227 |
if run_status.status in ("completed", "failed", "cancelled"):
|
|
@@ -229,29 +224,25 @@ with tab2:
|
|
| 229 |
time.sleep(1)
|
| 230 |
|
| 231 |
if run_status.status == "completed":
|
| 232 |
-
messages = client.beta.threads.messages.list(thread_id=tech_thread_id)
|
| 233 |
for msg in reversed(messages.data):
|
| 234 |
if msg.role == "assistant":
|
| 235 |
content = msg.content[0].text.value
|
| 236 |
st.session_state.tech_messages.append({"role": "assistant", "content": content})
|
| 237 |
-
|
| 238 |
-
# Parse JSON from assistant response
|
| 239 |
try:
|
| 240 |
-
st.session_state.tech_results = json.loads(content.strip("json ")
|
| 241 |
-
except
|
| 242 |
-
st.
|
| 243 |
break
|
| 244 |
except Exception as e:
|
| 245 |
st.error(f"β Technical Assistant Error: {e}")
|
| 246 |
|
| 247 |
-
# Expander for filtering and pagination
|
| 248 |
with st.expander("π§ Options (Filter + Pagination)", expanded=False):
|
| 249 |
disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
|
| 250 |
selected = st.selectbox("π Filter by discipline", ["All"] + disciplines)
|
| 251 |
page_size = 8
|
| 252 |
page = st.number_input("Page", min_value=1, step=1, value=1)
|
| 253 |
|
| 254 |
-
# Display results
|
| 255 |
if st.session_state.tech_results:
|
| 256 |
st.subheader("π Results")
|
| 257 |
results = [r for r in st.session_state.tech_results if selected == "All" or r.get("discipline") == selected]
|
|
@@ -272,7 +263,6 @@ with tab2:
|
|
| 272 |
st.session_state.tech_lightbox = None
|
| 273 |
st.rerun()
|
| 274 |
else:
|
| 275 |
-
# Only show chat messages if no results
|
| 276 |
for msg in st.session_state.tech_messages:
|
| 277 |
with st.chat_message(msg["role"]):
|
| 278 |
-
st.markdown(msg["content"], unsafe_allow_html=True)
|
|
|
|
| 181 |
except Exception as e:
|
| 182 |
st.error(f"πΌοΈ Image failed: {e}")
|
| 183 |
|
|
|
|
| 184 |
# ------------------ Technical Tab ------------------
|
| 185 |
with tab2:
|
| 186 |
ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
|
|
|
|
| 187 |
if "tech_messages" not in st.session_state:
|
| 188 |
st.session_state.tech_messages = []
|
| 189 |
+
if "tech_thread_id" not in st.session_state:
|
| 190 |
+
st.session_state.tech_thread_id = None
|
| 191 |
if "tech_results" not in st.session_state:
|
| 192 |
st.session_state.tech_results = []
|
| 193 |
+
st.session_state.tech_lightbox = None
|
|
|
|
| 194 |
|
| 195 |
tech_input = st.chat_input("Ask about plans, drawings or components")
|
|
|
|
| 196 |
if tech_input:
|
|
|
|
| 197 |
st.session_state.tech_messages.append({"role": "user", "content": tech_input})
|
|
|
|
| 198 |
|
| 199 |
+
if st.session_state.tech_messages and st.session_state.tech_messages[-1]["role"] == "user":
|
| 200 |
try:
|
| 201 |
+
if st.session_state.tech_thread_id is None:
|
| 202 |
+
thread = client.beta.threads.create()
|
| 203 |
+
st.session_state.tech_thread_id = thread.id
|
| 204 |
|
|
|
|
| 205 |
client.beta.threads.messages.create(
|
| 206 |
+
thread_id=st.session_state.tech_thread_id,
|
| 207 |
role="user",
|
| 208 |
+
content=st.session_state.tech_messages[-1]["content"]
|
| 209 |
)
|
| 210 |
|
|
|
|
| 211 |
run = client.beta.threads.runs.create(
|
| 212 |
+
thread_id=st.session_state.tech_thread_id,
|
| 213 |
assistant_id=ASSISTANT_ID
|
| 214 |
)
|
| 215 |
|
| 216 |
with st.spinner("π Searching technical drawings..."):
|
| 217 |
while True:
|
| 218 |
run_status = client.beta.threads.runs.retrieve(
|
| 219 |
+
thread_id=st.session_state.tech_thread_id,
|
| 220 |
run_id=run.id
|
| 221 |
)
|
| 222 |
if run_status.status in ("completed", "failed", "cancelled"):
|
|
|
|
| 224 |
time.sleep(1)
|
| 225 |
|
| 226 |
if run_status.status == "completed":
|
| 227 |
+
messages = client.beta.threads.messages.list(thread_id=st.session_state.tech_thread_id)
|
| 228 |
for msg in reversed(messages.data):
|
| 229 |
if msg.role == "assistant":
|
| 230 |
content = msg.content[0].text.value
|
| 231 |
st.session_state.tech_messages.append({"role": "assistant", "content": content})
|
|
|
|
|
|
|
| 232 |
try:
|
| 233 |
+
st.session_state.tech_results = json.loads(content.strip("json "))
|
| 234 |
+
except:
|
| 235 |
+
st.session_state.tech_results = []
|
| 236 |
break
|
| 237 |
except Exception as e:
|
| 238 |
st.error(f"β Technical Assistant Error: {e}")
|
| 239 |
|
|
|
|
| 240 |
with st.expander("π§ Options (Filter + Pagination)", expanded=False):
|
| 241 |
disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
|
| 242 |
selected = st.selectbox("π Filter by discipline", ["All"] + disciplines)
|
| 243 |
page_size = 8
|
| 244 |
page = st.number_input("Page", min_value=1, step=1, value=1)
|
| 245 |
|
|
|
|
| 246 |
if st.session_state.tech_results:
|
| 247 |
st.subheader("π Results")
|
| 248 |
results = [r for r in st.session_state.tech_results if selected == "All" or r.get("discipline") == selected]
|
|
|
|
| 263 |
st.session_state.tech_lightbox = None
|
| 264 |
st.rerun()
|
| 265 |
else:
|
|
|
|
| 266 |
for msg in st.session_state.tech_messages:
|
| 267 |
with st.chat_message(msg["role"]):
|
| 268 |
+
st.markdown(msg["content"], unsafe_allow_html=True)
|