File size: 1,361 Bytes
29fca42
 
 
 
9dd1541
29fca42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
        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.")