()
| 350 | |
| 351 | # Function to display chat with dataset page |
| 352 | def chat_with_dataset(): |
| 353 | from langchain.agents import AgentType |
| 354 | from langchain.agents import create_pandas_dataframe_agent |
| 355 | from langchain.callbacks import StreamlitCallbackHandler |
| 356 | from langchain.chat_models import ChatOpenAI |
| 357 | import streamlit as st |
| 358 | import pandas as pd |
| 359 | import os |
| 360 | |
| 361 | |
| 362 | file_formats = { |
| 363 | "csv": pd.read_csv, |
| 364 | "xls": pd.read_excel, |
| 365 | "xlsx": pd.read_excel, |
| 366 | "xlsm": pd.read_excel, |
| 367 | "xlsb": pd.read_excel, |
| 368 | } |
| 369 | |
| 370 | def clear_submit(): |
| 371 | """ |
| 372 | Clear the Submit Button State |
| 373 | Returns: |
| 374 | """ |
| 375 | st.session_state["submit"] = False |
| 376 | |
| 377 | @st.cache_data() |
| 378 | def load_data(uploaded_file): |
| 379 | """ |
| 380 | Load data from the uploaded file based on its extension. |
| 381 | """ |
| 382 | try: |
| 383 | ext = os.path.splitext(uploaded_file.name)[1][1:].lower() |
| 384 | except: |
| 385 | ext = uploaded_file.split(".")[-1] |
| 386 | if ext in file_formats: |
| 387 | return file_formats[ext](uploaded_file) |
| 388 | else: |
| 389 | st.error(f"Unsupported file format: {ext}") |
| 390 | return None |
| 391 | |
| 392 | st.title("Chat with your dataset") |
| 393 | st.info("Asking one question at a time will result in a better output") |
| 394 | |
| 395 | uploaded_file = st.file_uploader( |
| 396 | "Upload a Data file", |
| 397 | type=list(file_formats.keys()), |
| 398 | help="Various File formats are Support", |
| 399 | on_change=clear_submit, |
| 400 | ) |
| 401 | |
| 402 | df = None # Initialize df to None outside the if block |
| 403 | |
| 404 | if uploaded_file: |
| 405 | df = load_data(uploaded_file) # df will be assigned a value if uploaded_file is truthy |
| 406 | |
| 407 | if df is None: # Check if df is still None before proceeding |
| 408 | st.warning("No data file uploaded or there was an error in loading the data.") |
| 409 | return # Exit the function early if df is None |
nothing calls this directly
no test coverage detected