def main(): # Initialize session state variables if "conversation" not in st.session_state: st.session_state.conversation = None if "chat_history" not in st.session_state: st.session_state.chat_history = None load_dotenv() st.set_page_config(page_title="Chat with Documents", page_icon=":books:") # Sidebar Login/Register with st.sidebar: st.subheader("Login/Register") choice = st.radio("Select an option", ["Login", "Register"]) username = st.text_input("Username") password = st.text_input("Password", type="password") if choice == "Register": if st.button("Register"): if username and password: register_user(username, password) else: st.warning("Please provide both username and password.") elif choice == "Login": if st.button("Login"): if username and password: if authenticate_user(username, password): st.success("Login successful!") st.session_state.logged_in_user = username else: st.error("Invalid username or password.") else: st.warning("Please provide both username and password.") # Main app after login if "logged_in_user" in st.session_state: st.header(f"Welcome, {st.session_state.logged_in_user}!") user_question = st.text_input("Ask a question about your documents:") if user_question: handle_userinput(user_question) with st.sidebar: st.subheader("Your Documents") uploaded_files = st.file_uploader( "Upload your PDFs, Word docs, and text files here", accept_multiple_files=True ) if st.button("Process"): with st.spinner("Processing..."): raw_text = "" pdf_files = [file for file in uploaded_files if file.type == "application/pdf"] word_files = [file for file in uploaded_files if file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"] text_files = [file for file in uploaded_files if file.type == "text/plain"] if pdf_files: raw_text += get_pdf_text(pdf_files) if word_files: raw_text += get_word_text(word_files) if text_files: raw_text += get_text_file_text(text_files) text_chunks = get_text_chunks(raw_text) vectorstore = get_vectorstore(text_chunks) st.session_state.conversation = get_conversation_chain(vectorstore)if __name__ == "__main__": main()