Sambhatnagar commited on
Commit
c81dd60
·
verified ·
1 Parent(s): 2fe04e4
Files changed (1) hide show
  1. app.py +279 -0
app.py ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from rag_query import query_rag, vector_store
3
+
4
+ # Custom CSS for styling
5
+ st.markdown("""
6
+ <style>
7
+ @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap');
8
+
9
+ .main {
10
+ background-color: #f5f5f5;
11
+ font-family: 'Roboto', sans-serif;
12
+ }
13
+ .stTextInput > div > div > input {
14
+ border: 2px solid #C8102E;
15
+ border-radius: 5px;
16
+ background-color: #FFFFFF;
17
+ padding: 10px;
18
+ font-size: 16px;
19
+ }
20
+ .stSelectbox > div > div > select {
21
+ border: 2px solid #C8102E;
22
+ border-radius: 5px;
23
+ background-color: #FFFFFF;
24
+ padding: 10px;
25
+ font-size: 16px;
26
+ }
27
+ .stButton > button {
28
+ background-color: #333333; /* Dark grey */
29
+ color: white;
30
+ border-radius: 5px;
31
+ border: none;
32
+ padding: 10px 20px;
33
+ font-size: 16px;
34
+ transition: background-color 0.3s;
35
+ }
36
+ .stButton > button:hover {
37
+ background-color: #C8102E; /* Green on hover */
38
+ color: white;
39
+ }
40
+ .new-chat-button > button {
41
+ background-color: #FF0000; /* Red */
42
+ color: white;
43
+ border-radius: 5px;
44
+ border: none;
45
+ padding: 10px 20px;
46
+ font-size: 16px;
47
+ transition: background-color 0.3s;
48
+ }
49
+ .new-chat-button > button:hover {
50
+ background-color: #CC0000; /* Darker red on hover */
51
+ color: white;
52
+ }
53
+ .umc-header {
54
+ font-size: 24px;
55
+ font-weight: bold;
56
+ margin-top: 10px;
57
+ margin-bottom: 20px;
58
+ color: #333333;
59
+ }
60
+ .spacer {
61
+ margin-top: 30px;
62
+ }
63
+ .chunk-dropdown {
64
+ margin-top: 20px;
65
+ margin-bottom: 20px;
66
+ }
67
+ .header-banner {
68
+ background-color: #FFFFFF;
69
+ padding: 10px 20px;
70
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
71
+ display: flex;
72
+ align-items: center;
73
+ justify-content: space-between;
74
+ margin-bottom: 20px;
75
+ }
76
+ .header-text {
77
+ font-size: 28px;
78
+ font-weight: bold;
79
+ color: #C8102E;
80
+ }
81
+ .query-card {
82
+ background-color: #FFFFFF;
83
+ border-radius: 10px;
84
+ padding: 15px;
85
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
86
+ margin-bottom: 20px;
87
+ }
88
+ .query-title {
89
+ font-size: 18px;
90
+ font-weight: bold;
91
+ color: #333333;
92
+ border-bottom: 2px solid #C8102E;
93
+ padding-bottom: 5px;
94
+ margin-bottom: 10px;
95
+ }
96
+ .sidebar {
97
+ background-color: #E8ECEF;
98
+ padding: 20px;
99
+ }
100
+ .divider {
101
+ border-top: 1px solid #C8102E;
102
+ margin: 10px 0;
103
+ }
104
+ .footer {
105
+ background-color: #E8ECEF;
106
+ padding: 10px;
107
+ text-align: center;
108
+ box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.1);
109
+ margin-top: 20px;
110
+ font-size: 14px;
111
+ color: #333333;
112
+ }
113
+ .footer a {
114
+ color: #C8102E;
115
+ text-decoration: none;
116
+ }
117
+ .footer a:hover {
118
+ text-decoration: underline;
119
+ }
120
+ @media (max-width: 768px) {
121
+ .header-banner {
122
+ flex-direction: column;
123
+ align-items: flex-start;
124
+ }
125
+ .header-text {
126
+ font-size: 20px;
127
+ margin-top: 10px;
128
+ }
129
+ }
130
+ </style>
131
+ """, unsafe_allow_html=True)
132
+
133
+ # Initialize session state for password and query history
134
+ if "authenticated" not in st.session_state:
135
+ st.session_state.authenticated = False
136
+ if "query_history" not in st.session_state:
137
+ st.session_state.query_history = [] # Store queries and responses
138
+ if "retrieved_chunks" not in st.session_state:
139
+ st.session_state.retrieved_chunks = [] # Store retrieved chunks
140
+ if "new_query_input" not in st.session_state:
141
+ st.session_state.new_query_input = "" # Store new query input
142
+
143
+ # Header banner with logo and text
144
+ st.markdown('<div class="header-banner">', unsafe_allow_html=True)
145
+ col1, col2 = st.columns([1, 4])
146
+ with col1:
147
+ st.image("UMC_Logo.png", width=100) # Smaller logo for header
148
+ with col2:
149
+ st.markdown('<p class="header-text">United Methodist Communications</p>', unsafe_allow_html=True)
150
+ st.markdown('</div>', unsafe_allow_html=True)
151
+
152
+ # Main app title (shown on all screens)
153
+ st.title("United Methodist Church Discipline Assistant")
154
+
155
+ # Header with description and New Chat button
156
+ col1, col2 = st.columns([3, 1])
157
+ with col1:
158
+ st.write("Ask questions about The Book of Discipline 2020-2024, including its Constitution and history!")
159
+ with col2:
160
+ if st.button("New Chat", key="new_chat"):
161
+ st.session_state.query_history = []
162
+ st.session_state.retrieved_chunks = []
163
+ st.session_state.new_query_input = ""
164
+ st.rerun()
165
+
166
+ # Password form (shown only if not authenticated)
167
+ if not st.session_state.authenticated:
168
+ with st.form(key="password_form"):
169
+ password = st.text_input("Enter password:", type="password", key="password")
170
+ submit_password = st.form_submit_button("Login")
171
+ if submit_password:
172
+ if password == "umc2024": # Password as set
173
+ st.session_state.authenticated = True
174
+ st.rerun() # Refresh to show main app
175
+ else:
176
+ st.error("Incorrect password. Please try again.")
177
+
178
+ # Main app content (shown only if authenticated)
179
+ if st.session_state.authenticated:
180
+ # Interactive input with sample questions
181
+ sample_questions = [
182
+ "Who were the key figures in the early history of Methodism in America, and what did they do?",
183
+ "What does the United Methodist Church Constitution say about inclusiveness and racial justice?",
184
+ "What is the role of the General Conference in the United Methodist Church?",
185
+ "How did the United Methodist Church form through mergers over time?",
186
+ "What challenges did the UMC face during the Civil War, and how did they impact the church?",
187
+ "What is the UMC’s stance on ecumenical relations according to the Constitution?",
188
+ "How has the UMC addressed issues of racism throughout its history?",
189
+ "What changes has the UMC faced in the 21st century, particularly around gender and sexuality?",
190
+ "How has the UMC expanded globally since its formation?",
191
+ "What does the UMC say about social justice through its Special Sundays?",
192
+ "How does the UMC support Native American ministries according to its organizational practices?",
193
+ "What is the UMC’s stance on marriage and homosexuality in the 21st century?",
194
+ "How has the UMC’s position on social issues like abortion evolved over time?",
195
+ "What are the Special Sundays in the UMC, and how are they celebrated?",
196
+ "How does the UMC observe Heritage Sunday, and what is its significance?",
197
+ "What is the role of a Certified Lay Minister in the UMC, and how do they contribute to the church?",
198
+ "How does the UMC organize its local churches, particularly in terms of financial responsibilities?",
199
+ "How does the UMC support community outreach through its local church organization?",
200
+ "What are the UMC’s guidelines for organizing a new local church or mission congregation?"
201
+ ]
202
+ query = st.selectbox("Pick a question or type your own:", [""] + sample_questions, key="query_select") or st.text_input("Your question:", key="query_input")
203
+
204
+ # Process query and display response
205
+ if st.button("Submit"):
206
+ with st.spinner("Fetching response..."):
207
+ # Retrieve chunks and response
208
+ results = vector_store.similarity_search(query, k=5)
209
+ st.session_state.retrieved_chunks = results # Store retrieved chunks
210
+ response = query_rag(query)
211
+ st.session_state.query_history.append((query, response)) # Store query and response
212
+ st.session_state.new_query_input = "" # Clear the new query input
213
+ st.rerun() # Refresh to show response
214
+
215
+ # Display query history
216
+ for i, (q, r) in enumerate(st.session_state.query_history):
217
+ st.markdown('<div class="query-card">', unsafe_allow_html=True)
218
+ st.markdown(f'<div class="query-title">Query {i+1}: {q}</div>', unsafe_allow_html=True)
219
+ st.markdown(r)
220
+ # Display retrieved chunks in a dropdown
221
+ if st.session_state.retrieved_chunks:
222
+ chunk_options = [f"Chunk {j+1}: {chunk.page_content[:100]}..." for j, chunk in enumerate(st.session_state.retrieved_chunks)]
223
+ selected_chunk = st.selectbox("View Retrieved Chunks:", ["Select a chunk"] + chunk_options, key=f"chunk_select_{i}")
224
+ if selected_chunk != "Select a chunk":
225
+ chunk_index = int(selected_chunk.split(":")[0].split(" ")[1]) - 1
226
+ chunk = st.session_state.retrieved_chunks[chunk_index]
227
+ st.markdown("**Retrieved Chunk Details:**")
228
+ st.markdown(f"**Source:** {chunk.metadata['source']}")
229
+ if chunk.metadata["part"]:
230
+ st.markdown(f"**Part:** {chunk.metadata['part']}")
231
+ st.markdown(f"**Heading:** {chunk.metadata['heading']}")
232
+ if chunk.metadata["paragraph_number"]:
233
+ st.markdown(f"**Paragraph {chunk.metadata['paragraph_number']}:** {chunk.metadata['paragraph_title']}")
234
+ st.markdown(f"**Text:** {chunk.page_content}")
235
+ st.markdown('</div>', unsafe_allow_html=True)
236
+ st.markdown("---")
237
+
238
+ # New prompt window with sample questions
239
+ if st.session_state.query_history: # Show only after at least one query
240
+ st.markdown("### Explore More Insights")
241
+ selected_query = st.selectbox("Pick another question or type your own:", [""] + sample_questions, key="new_query_select")
242
+ 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)}")
243
+ # Use custom query if provided, otherwise use selected query
244
+ new_query = custom_query if custom_query else selected_query
245
+ if st.button("Submit New Query"):
246
+ if new_query: # Ensure a query is provided
247
+ with st.spinner("UMCOM AI Bot is thinking..."):
248
+ results = vector_store.similarity_search(new_query, k=5)
249
+ st.session_state.retrieved_chunks = results
250
+ response = query_rag(new_query)
251
+ st.session_state.query_history.append((new_query, response))
252
+ st.session_state.new_query_input = "" # Clear the input for the next query
253
+ st.rerun() # Refresh to show new response
254
+ else:
255
+ st.warning("Please select a question or enter a new one.")
256
+
257
+ # Sidebar with logo, text, and instructions (always shown)
258
+ st.sidebar.image("UMC_Logo.png", width=200) # Adjusted logo size for sidebar
259
+ st.sidebar.markdown('<p class="umc-header">United Methodist Communications</p>', unsafe_allow_html=True)
260
+ st.sidebar.markdown('<div class="divider"></div>', unsafe_allow_html=True) # Add divider
261
+ st.sidebar.header("Instructions")
262
+ 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.")
263
+ st.sidebar.header("About")
264
+ st.sidebar.markdown("""
265
+ ### About This AI Bot
266
+
267
+ 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*. Whether you're seeking insights into the UMC's history, constitutional principles, organizational structure, social justice initiatives, or worship practices, this bot provides clear, user-friendly answers with precise citations. 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.
268
+
269
+ **How It Can Help:**
270
+ - **Historical Insights:** Learn about the UMC’s origins, key figures like John Wesley, and its global expansion.
271
+ - **Constitutional Guidance:** Understand the UMC’s governance, inclusiveness, and social principles.
272
+ - **Practical Applications:** Explore guidelines for local church organization, Special Sundays, and community outreach.
273
+ - **Social and Ethical Stances:** Discover the UMC’s positions on issues like marriage, social justice, and more.
274
+
275
+ **Limitations:**
276
+ 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.
277
+ """)
278
+ # Footer
279
+ st.markdown('<div class="footer">Powered by <a href="https://www.eksource.com/" target="_blank">ekSource Technologies, Inc</a></div>', unsafe_allow_html=True)