import streamlit as st from rag_query import query_rag, vector_store # Added vector_store import # Custom CSS for styling st.markdown(""" """, unsafe_allow_html=True) # Initialize session state for password and query history if "authenticated" not in st.session_state: st.session_state.authenticated = False if "query_history" not in st.session_state: st.session_state.query_history = [] # Store queries and responses if "retrieved_chunks" not in st.session_state: st.session_state.retrieved_chunks = [] # Store retrieved chunks if "new_query_input" not in st.session_state: st.session_state.new_query_input = "" # Store new query input # Main app title (shown on all screens) st.title("United Methodist Church Assistant") # Header with description and New Chat button col1, col2 = st.columns([3, 1]) with col1: st.write("Ask questions about The Book of Discipline 2020-2024!") with col2: if st.button("New Chat", key="new_chat"): st.session_state.query_history = [] st.session_state.retrieved_chunks = [] st.session_state.new_query_input = "" st.rerun() # Password form (shown only if not authenticated) if not st.session_state.authenticated: with st.form(key="password_form"): password = st.text_input("Enter password:", type="password", key="password") submit_password = st.form_submit_button("Login") if submit_password: if password == "umc2024": # Password as set st.session_state.authenticated = True st.rerun() # Refresh to show main app else: st.error("Incorrect password. Please try again.") # Main app content (shown only if authenticated) if st.session_state.authenticated: # Interactive input with sample questions sample_questions = [ "Can you tell me the history of United Methodist Church", "What does the Constitution say about inclusiveness?", "Tell me about John Wesley.", "Who were the key figures in the early history of Methodism in America, and what did they do?", "What does the United Methodist Church Constitution say about inclusiveness and racial justice?", "What is the role of the General Conference in the United Methodist Church?", "How did the United Methodist Church form through mergers over time?", "What challenges did the UMC face during the Civil War, and how did they impact the church?", "What is the UMC’s stance on ecumenical relations according to the Constitution?", "How has the UMC addressed issues of racism throughout its history?", "What changes has the UMC faced in the 21st century, particularly around gender and sexuality?", "How has the UMC expanded globally since its formation?", "What are the Special Sundays in the UMC, and how are they celebrated?", "How does the UMC observe Heritage Sunday, and what is its significance?", "What is the role of a Certified Lay Minister in the UMC, and how do they contribute to the church?", "How does the UMC organize its local churches, particularly in terms of financial responsibilities?", "How does the UMC support community outreach through its local church organization?", "What are the UMC’s guidelines for organizing a new local church or mission congregation?", "How has the UMC’s position on social issues like abortion evolved over time?", "What does the UMC say about social justice through its Special Sundays?" ] query = st.selectbox("Pick a question or type your own:", [""] + sample_questions, key="query_select") or st.text_input("Your question:", key="query_input") # Process query and display response if st.button("Submit"): with st.spinner("UMC AI Bot is thinking..."): # Retrieve chunks and response results = vector_store.similarity_search(query, k=8) # Assuming vector_store is accessible st.session_state.retrieved_chunks = results # Store retrieved chunks response = query_rag(query) st.session_state.query_history.append((query, response)) # Store query and response st.session_state.new_query_input = "" # Clear the new query input st.rerun() # Refresh to show response # Display query history for i, (q, r) in enumerate(st.session_state.query_history): st.markdown('
', unsafe_allow_html=True) st.markdown(f'
Query {i+1}: {q}
', unsafe_allow_html=True) st.markdown(r) # Display retrieved chunks in a dropdown if st.session_state.retrieved_chunks: chunk_options = [f"Chunk {j+1}: {chunk.page_content[:100]}..." for j, chunk in enumerate(st.session_state.retrieved_chunks)] selected_chunk = st.selectbox("View Retrieved Chunks:", ["Select a chunk"] + chunk_options, key=f"chunk_select_{i}") if selected_chunk != "Select a chunk": chunk_index = int(selected_chunk.split(":")[0].split(" ")[1]) - 1 chunk = st.session_state.retrieved_chunks[chunk_index] st.markdown("**Retrieved Chunk Details:**") st.markdown(f"**Source:** {chunk.metadata['source']}") if chunk.metadata["part"]: st.markdown(f"**Part:** {chunk.metadata['part']}") st.markdown(f"**Heading:** {chunk.metadata['heading']}") if chunk.metadata["paragraph_number"]: st.markdown(f"**Paragraph {chunk.metadata['paragraph_number']}:** {chunk.metadata['paragraph_title']}") st.markdown(f"**Text:** {chunk.page_content}") st.markdown('
', unsafe_allow_html=True) st.markdown("---") # New prompt window with sample questions if st.session_state.query_history: # Show only after at least one query st.markdown("### Explore More Insights") selected_query = st.selectbox("Pick another question or type your own:", [""] + sample_questions, key="new_query_select") custom_query = st.text_input("What else would you like to know?", value=st.session_state.new_query_input, key=f"new_query_input_{len(st.session_state.query_history)}") # Use custom query if provided, otherwise use selected query new_query = custom_query if custom_query else selected_query if st.button("Submit New Query"): if new_query: # Ensure a query is provided with st.spinner("UMC AI Bot is thinking..."): results = vector_store.similarity_search(new_query, k=8) st.session_state.retrieved_chunks = results response = query_rag(new_query) st.session_state.query_history.append((new_query, response)) st.session_state.new_query_input = "" # Clear the input for the next query st.rerun() # Refresh to show new response else: st.warning("Please select a question or enter a new one.") # Sidebar with logo, text, and instructions (always shown) st.sidebar.image("UMC_Logo.png", width=200) # Adjusted logo size for sidebar st.sidebar.markdown('

United Methodist Communications

', unsafe_allow_html=True) st.sidebar.markdown('
', unsafe_allow_html=True) # Add space st.sidebar.header("Instructions") st.sidebar.write("Enter a question about The United Methodist Church's Book Of Discipline 2020-2024. The assistant will retrieve relevant sections and explain them in simple language, citing parts, paragraphs, and titles as needed.") st.sidebar.header("About This AI Bot") st.sidebar.markdown(""" The United Methodist Church Discipline Assistant is an AI-powered tool designed to help you explore *The Book of Discipline of The United Methodist Church 2020-2024*. By leveraging Retrieval-Augmented Generation (RAG), the assistant retrieves relevant sections from the Book of Discipline and explains them in simple language, making it an invaluable resource for church leaders, members, and researchers. **How It Can Help:** - **Historical Insights:** Learn about the UMC’s origins, key figures like John Wesley, and its global expansion. - **Constitutional Guidance:** Understand the UMC’s governance, inclusiveness, and social principles. - **Practical Applications:** Explore guidelines for local church organization, Special Sundays, and community outreach. - **Social and Ethical Stances:** Discover the UMC’s positions on issues like marriage, social justice, and more. **Limitations:** Please note that the dataset for this assistant covers *The Book of Discipline 2020-2024* up to Paragraph 269 on page 223. Content beyond this point, including later paragraphs or sections, is not included in the current dataset. For the most comprehensive information, refer directly to the full Book of Discipline. """) # Footer st.markdown('', unsafe_allow_html=True)