Sambhatnagar commited on
Commit
4e7d114
·
verified ·
1 Parent(s): a60df80

New App.py with the retrieval documents

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from rag_query import query_rag
3
+
4
+ # Custom CSS for styling
5
+ st.markdown("""
6
+ <style>
7
+ .main {background-color: #f5f5f5;}
8
+ .stTextInput > div > div > input {border: 2px solid #4CAF50; border-radius: 5px;}
9
+ .stSelectbox > div > div > select {border: 2px solid #4CAF50; border-radius: 5px;}
10
+ .stButton > button {
11
+ background-color: #333333; /* Dark grey */
12
+ color: white;
13
+ border-radius: 5px;
14
+ border: none;
15
+ padding: 10px 20px;
16
+ font-size: 16px;
17
+ }
18
+ .stButton > button:hover {
19
+ background-color: #4CAF50; /* Green on hover */
20
+ color: white;
21
+ }
22
+ .umc-header {
23
+ font-size: 24px; /* Adjusted font size for sidebar */
24
+ font-weight: bold;
25
+ margin-top: 10px; /* Space between logo and text */
26
+ margin-bottom: 20px; /* Space before Instructions */
27
+ }
28
+ .spacer {
29
+ margin-top: 30px; /* Space between text and Instructions */
30
+ }
31
+ .chunk-dropdown {
32
+ margin-top: 20px;
33
+ margin-bottom: 20px;
34
+ }
35
+ </style>
36
+ """, unsafe_allow_html=True)
37
+
38
+ # Initialize session state for password and query history
39
+ if "authenticated" not in st.session_state:
40
+ st.session_state.authenticated = False
41
+ if "query_history" not in st.session_state:
42
+ st.session_state.query_history = [] # Store queries and responses
43
+ if "retrieved_chunks" not in st.session_state:
44
+ st.session_state.retrieved_chunks = [] # Store retrieved chunks
45
+
46
+ # Main app title (shown on all screens)
47
+ st.title("United Methodist Church Discipline Assistant")
48
+ st.write("Ask questions about The Book of Discipline 2020-2024!")
49
+
50
+ # Password form (shown only if not authenticated)
51
+ if not st.session_state.authenticated:
52
+ with st.form(key="password_form"):
53
+ password = st.text_input("Enter password:", type="password", key="password")
54
+ submit_password = st.form_submit_button("Login")
55
+ if submit_password:
56
+ if password == "umc2024": # Password as set
57
+ st.session_state.authenticated = True
58
+ st.experimental_rerun() # Refresh to show main app
59
+ else:
60
+ st.error("Incorrect password. Please try again.")
61
+
62
+ # Main app content (shown only if authenticated)
63
+ if st.session_state.authenticated:
64
+ # Interactive input with sample questions
65
+ sample_questions = [
66
+ "Can you tell me the history of United Methodist Church",
67
+ "What does the Constitution say about inclusiveness?",
68
+ "Tell me about John Wesley."
69
+ ]
70
+ query = st.selectbox("Pick a question or type your own:", [""] + sample_questions, key="query_select") or st.text_input("Your question:", key="query_input")
71
+
72
+ # Process query and display response
73
+ if st.button("Submit"):
74
+ with st.spinner("Fetching response..."):
75
+ # Retrieve chunks and response
76
+ results = vector_store.similarity_search(query, k=5) # Assuming vector_store is accessible
77
+ st.session_state.retrieved_chunks = results # Store retrieved chunks
78
+ response = query_rag(query)
79
+ st.session_state.query_history.append((query, response)) # Store query and response
80
+
81
+ # Display query history
82
+ for i, (q, r) in enumerate(st.session_state.query_history):
83
+ st.markdown(f"**Query {i+1}: {q}**")
84
+ st.markdown(r)
85
+ # Display retrieved chunks in a dropdown
86
+ if st.session_state.retrieved_chunks:
87
+ chunk_options = [f"Chunk {j+1}: {chunk.page_content[:100]}..." for j, chunk in enumerate(st.session_state.retrieved_chunks)]
88
+ selected_chunk = st.selectbox("View Retrieved Chunks:", ["Select a chunk"] + chunk_options, key=f"chunk_select_{i}")
89
+ if selected_chunk != "Select a chunk":
90
+ chunk_index = int(selected_chunk.split(":")[0].split(" ")[1]) - 1
91
+ chunk = st.session_state.retrieved_chunks[chunk_index]
92
+ st.markdown("**Retrieved Chunk Details:**")
93
+ st.markdown(f"**Source:** {chunk.metadata['source']}")
94
+ if chunk.metadata["part"]:
95
+ st.markdown(f"**Part:** {chunk.metadata['part']}")
96
+ st.markdown(f"**Heading:** {chunk.metadata['heading']}")
97
+ if chunk.metadata["paragraph_number"]:
98
+ st.markdown(f"**Paragraph {chunk.metadata['paragraph_number']}:** {chunk.metadata['paragraph_title']}")
99
+ st.markdown(f"**Text:** {chunk.page_content}")
100
+ st.markdown("---")
101
+
102
+ # New prompt window with sample questions
103
+ if st.session_state.query_history: # Show only after at least one query
104
+ st.markdown("### Ask Another Question")
105
+ new_query = st.selectbox("Pick another question or type your own:", [""] + sample_questions, key="new_query_select") or st.text_input("Your new question:", key="new_query_input")
106
+ if st.button("Submit New Query"):
107
+ with st.spinner("Fetching response..."):
108
+ results = vector_store.similarity_search(new_query, k=5)
109
+ st.session_state.retrieved_chunks = results
110
+ response = query_rag(new_query)
111
+ st.session_state.query_history.append((new_query, response))
112
+ st.experimental_rerun() # Refresh to show new response
113
+
114
+ # Sidebar with logo, text, and instructions (always shown)
115
+ st.sidebar.image("UMC_Logo.png", width=200) # Adjusted logo size for sidebar
116
+ st.sidebar.markdown('<p class="umc-header">United Methodist Communications</p>', unsafe_allow_html=True)
117
+ st.sidebar.markdown('<div class="spacer"></div>', unsafe_allow_html=True) # Add space
118
+ st.sidebar.header("Instructions")
119
+ 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.")
120
+ st.sidebar.header("About")
121
+ st.sidebar.write("Powered by United Methodist Communications.")