poemsforaphrodite commited on
Commit
34fca31
β€’
1 Parent(s): 08e6df4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -29
app.py CHANGED
@@ -13,8 +13,9 @@ from streamlit_extras.add_vertical_space import add_vertical_space
13
  from streamlit_extras.switch_page_button import switch_page
14
  import json
15
  import pandas as pd
 
16
  # ---------------------- Configuration ----------------------
17
- st.set_page_config(page_title="Building Regulations Chatbot", layout="wide", page_icon="πŸ—οΈ", initial_sidebar_state="expanded")
18
  # Load environment variables from .env file
19
  load_dotenv()
20
 
@@ -155,22 +156,22 @@ def chat_with_assistant(file_ids, user_message):
155
 
156
  # ---------------------- Streamlit App ----------------------
157
 
158
- page = st.sidebar.selectbox("Choose a page", ["Home", "Documents", "Admin"])
159
 
160
  if page == "Home":
161
- st.title("πŸ—οΈ Building Regulations Chatbot", anchor=False)
162
 
163
  # Sidebar improvements
164
  with st.sidebar:
165
- colored_header("πŸ“„ Selected Documents", description="Documents for chat")
166
- if 'selected_pdfs' in st.session_state and st.session_state.selected_pdfs:
167
- for pdf in st.session_state.selected_pdfs:
168
- st.write(f"- {pdf['doc_title']}")
169
  else:
170
  st.write("No documents selected. Please go to the Documents page.")
171
 
172
  # Main chat area improvements
173
- colored_header("πŸ’¬ Chat", description="Ask questions about building regulations")
174
 
175
  # Display chat messages with improved styling
176
  for chat in st.session_state.chat_history:
@@ -225,7 +226,7 @@ if page == "Home":
225
  st.caption("Β© 2023 Your Company Name")
226
 
227
  elif page == "Documents":
228
- st.title("πŸ“š Document Selection")
229
 
230
  city_code_input = st.text_input("Enter city code:", key="city_code_input")
231
  load_documents_button = st.button("Load Documents", key="load_documents_button")
@@ -258,40 +259,59 @@ elif page == "Documents":
258
  })
259
 
260
  # Add a checkbox column to the DataFrame at the beginning
261
- df.insert(0, "Checkbox", False)
262
 
263
- # Display the DataFrame as an editable table
264
- edited_df = st.data_editor(
 
 
 
 
 
 
 
 
 
265
  df,
266
- hide_index=True,
267
- num_rows="dynamic",
268
- column_config={
269
- "Checkbox": st.column_config.CheckboxColumn(required=True),
270
- "File Href": st.column_config.LinkColumn("File Href")
271
- },
272
- disabled=["Municipality", "Abbreviation", "Doc Title", "File Title", "File Href", "Enactment Date", "Prio"]
273
  )
 
 
 
274
 
275
- # Get the selected PDFs
276
- selected_pdfs = edited_df[edited_df["Checkbox"]].to_dict('records')
277
 
278
  if st.button("Process Selected PDFs"):
279
- if selected_pdfs:
280
- st.session_state.selected_pdfs = selected_pdfs
 
281
  st.session_state.assistant_id = create_assistant()
282
  with st.spinner("Processing PDFs and creating/updating assistant..."):
283
  file_ids = []
284
- for pdf in selected_pdfs:
285
- file_name = download_pdf(pdf['File Href'])
 
 
 
 
 
 
 
286
  if file_name:
287
  file_path = f"./{file_name}"
288
  file_id = upload_file_to_openai(file_path)
289
  if file_id:
290
  file_ids.append(file_id)
291
  else:
292
- st.warning(f"Failed to upload {pdf['Doc Title']}. Skipping this file.")
293
  else:
294
- st.warning(f"Failed to download {pdf['Doc Title']}. Skipping this file.")
 
295
  st.session_state.file_ids = file_ids
296
  st.success("PDFs processed successfully. You can now chat on the Home page.")
297
  else:
@@ -301,8 +321,8 @@ elif page == "Documents":
301
  switch_page("Home")
302
 
303
  elif page == "Admin":
304
- st.title("πŸ”§ Admin Panel")
305
- colored_header("Vector Stores Information", description="List of all vector stores")
306
 
307
  vector_stores = get_vector_stores()
308
  json_vector_stores = json.dumps([vs.model_dump() for vs in vector_stores])
 
13
  from streamlit_extras.switch_page_button import switch_page
14
  import json
15
  import pandas as pd
16
+ from st_aggrid import AgGrid, GridOptionsBuilder, GridUpdateMode, DataReturnMode
17
  # ---------------------- Configuration ----------------------
18
+ st.set_page_config(page_title="Building Regulations Chatbot", layout="wide", initial_sidebar_state="expanded")
19
  # Load environment variables from .env file
20
  load_dotenv()
21
 
 
156
 
157
  # ---------------------- Streamlit App ----------------------
158
 
159
+ page = st.sidebar.selectbox("Choose a page", ["Documents", "Home", "Admin"])
160
 
161
  if page == "Home":
162
+ st.title("Building Regulations Chatbot", anchor=False)
163
 
164
  # Sidebar improvements
165
  with st.sidebar:
166
+ colored_header("Selected Documents", description="Documents for chat")
167
+ if 'selected_pdfs' in st.session_state and not st.session_state.selected_pdfs.empty:
168
+ for _, pdf in st.session_state.selected_pdfs.iterrows():
169
+ st.write(f"- {pdf['Doc Title']}")
170
  else:
171
  st.write("No documents selected. Please go to the Documents page.")
172
 
173
  # Main chat area improvements
174
+ colored_header("Chat", description="Ask questions about building regulations")
175
 
176
  # Display chat messages with improved styling
177
  for chat in st.session_state.chat_history:
 
226
  st.caption("Β© 2023 Your Company Name")
227
 
228
  elif page == "Documents":
229
+ st.title("Document Selection")
230
 
231
  city_code_input = st.text_input("Enter city code:", key="city_code_input")
232
  load_documents_button = st.button("Load Documents", key="load_documents_button")
 
259
  })
260
 
261
  # Add a checkbox column to the DataFrame at the beginning
262
+ df.insert(0, "Select", False)
263
 
264
+ # Configure grid options
265
+ gb = GridOptionsBuilder.from_dataframe(df)
266
+ gb.configure_default_column(enablePivot=True, enableValue=True, enableRowGroup=True)
267
+ gb.configure_column("Select", header_name="Select", cellRenderer='checkboxRenderer')
268
+ gb.configure_column("File Href", cellRenderer='linkRenderer')
269
+ gb.configure_selection(selection_mode="multiple", use_checkbox=True)
270
+ gb.configure_side_bar()
271
+ gridOptions = gb.build()
272
+
273
+ # Display the AgGrid
274
+ grid_response = AgGrid(
275
  df,
276
+ gridOptions=gridOptions,
277
+ enable_enterprise_modules=True,
278
+ update_mode=GridUpdateMode.MODEL_CHANGED,
279
+ data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
280
+ fit_columns_on_grid_load=False,
 
 
281
  )
282
+
283
+ # Get the selected rows
284
+ selected_rows = grid_response['selected_rows']
285
 
286
+ # Debug: Print the structure of selected_rows
287
+ st.write("Debug - Selected Rows Structure:", selected_rows)
288
 
289
  if st.button("Process Selected PDFs"):
290
+ if len(selected_rows) > 0: # Check if there are any selected rows
291
+ # Convert selected_rows to a DataFrame
292
+ st.session_state.selected_pdfs = pd.DataFrame(selected_rows)
293
  st.session_state.assistant_id = create_assistant()
294
  with st.spinner("Processing PDFs and creating/updating assistant..."):
295
  file_ids = []
296
+
297
+ for _, pdf in st.session_state.selected_pdfs.iterrows():
298
+ # Debug: Print each pdf item
299
+ st.write("Debug - PDF item:", pdf)
300
+
301
+ file_href = pdf['File Href']
302
+ doc_title = pdf['Doc Title']
303
+
304
+ file_name = download_pdf(file_href)
305
  if file_name:
306
  file_path = f"./{file_name}"
307
  file_id = upload_file_to_openai(file_path)
308
  if file_id:
309
  file_ids.append(file_id)
310
  else:
311
+ st.warning(f"Failed to upload {doc_title}. Skipping this file.")
312
  else:
313
+ st.warning(f"Failed to download {doc_title}. Skipping this file.")
314
+
315
  st.session_state.file_ids = file_ids
316
  st.success("PDFs processed successfully. You can now chat on the Home page.")
317
  else:
 
321
  switch_page("Home")
322
 
323
  elif page == "Admin":
324
+ st.title("Admin Panel")
325
+ st.header("Vector Stores Information")
326
 
327
  vector_stores = get_vector_stores()
328
  json_vector_stores = json.dumps([vs.model_dump() for vs in vector_stores])