|
try: |
|
cleaned_df = preprocess_excel(tmp_path) |
|
vectorstore = build_vectorstore_from_dataframe(cleaned_df) |
|
qa = create_qa_pipeline(vectorstore) |
|
st.success("✅ File processed and chatbot ready! Ask your questions below.") |
|
|
|
if "chat_history" not in st.session_state: |
|
st.session_state.chat_history = [] |
|
|
|
with st.chat_message("assistant"): |
|
st.markdown("How can I help you with the inspection data?") |
|
|
|
user_prompt = st.chat_input("Ask a question like 'How many backlog items are marked Yes?' or 'List overdue inspections'.") |
|
|
|
if user_prompt: |
|
st.chat_message("user").markdown(user_prompt) |
|
with st.chat_message("assistant"): |
|
with st.spinner("Thinking..."): |
|
try: |
|
answer = qa.run(user_prompt) |
|
st.markdown(f"**Answer:** {answer}") |
|
st.session_state.chat_history.append((user_prompt, answer)) |
|
except Exception as e: |
|
st.error(f"Error: {e}") |
|
except Exception as e: |
|
st.error(f"Error processing file: {e}") |
|
finally: |
|
os.remove(tmp_path) |
|
else: |
|
st.info("Upload a file to get started.") |
|
|