DrishtiSharma commited on
Commit
1cd5fa4
·
verified ·
1 Parent(s): 1e597b5

Update app4.py

Browse files
Files changed (1) hide show
  1. app4.py +13 -11
app4.py CHANGED
@@ -12,7 +12,7 @@ import os
12
  # Set title
13
  st.title("Data Analyzer")
14
 
15
- # Add fields to input API keys via the sidebar
16
  api_key = os.getenv("OPENAI_API_KEY")
17
  pandasai_api_key = os.getenv("PANDASAI_API_KEY")
18
 
@@ -33,7 +33,6 @@ def load_dataset_into_session():
33
  try:
34
  st.session_state.df = pd.read_csv(file_path)
35
  st.success(f"File loaded successfully from '{file_path}'!")
36
- st.dataframe(st.session_state.df.head(10))
37
  except Exception as e:
38
  st.error(f"Error loading dataset from the repo directory: {e}")
39
 
@@ -51,7 +50,6 @@ def load_dataset_into_session():
51
  else:
52
  st.session_state.df = pd.DataFrame(dataset)
53
  st.success(f"Hugging Face Dataset '{dataset_name}' loaded successfully!")
54
- st.dataframe(st.session_state.df.head(10))
55
  except Exception as e:
56
  st.error(f"Error loading Hugging Face dataset: {e}")
57
 
@@ -62,7 +60,6 @@ def load_dataset_into_session():
62
  try:
63
  st.session_state.df = pd.read_csv(uploaded_file)
64
  st.success("File uploaded successfully!")
65
- st.dataframe(st.session_state.df.head(10))
66
  except Exception as e:
67
  st.error(f"Error reading uploaded file: {e}")
68
 
@@ -76,7 +73,7 @@ if "df" in st.session_state and api_key and pandasai_api_key:
76
 
77
  df = st.session_state.df
78
  st.write("Dataset Preview:")
79
- st.write(df.head())
80
 
81
  # Set up PandasAI Agent
82
  agent = Agent(df)
@@ -107,15 +104,21 @@ if "df" in st.session_state and api_key and pandasai_api_key:
107
  st.header("Data Analysis with PandasAI")
108
  pandas_question = st.text_input("Ask a question about the dataset (PandasAI):")
109
  if pandas_question:
110
- result = agent.chat(pandas_question)
111
- st.write("PandasAI Answer:", result)
 
 
 
112
 
113
  with tab2:
114
  st.header("Q&A with RAG")
115
  rag_question = st.text_input("Ask a question about the dataset (RAG):")
116
  if rag_question:
117
- result = qa_chain.run(rag_question)
118
- st.write("RAG Answer:", result)
 
 
 
119
 
120
  with tab3:
121
  st.header("Data Visualization")
@@ -142,8 +145,7 @@ if "df" in st.session_state and api_key and pandasai_api_key:
142
  else:
143
  st.write("Unable to generate the graph. Please try a different query.")
144
  except Exception as e:
145
- st.write(f"An error occurred: {str(e)}")
146
- st.write("Please try asking in a different way.")
147
  else:
148
  if not api_key:
149
  st.warning("Please set the OpenAI API key in environment variables.")
 
12
  # Set title
13
  st.title("Data Analyzer")
14
 
15
+ # API keys
16
  api_key = os.getenv("OPENAI_API_KEY")
17
  pandasai_api_key = os.getenv("PANDASAI_API_KEY")
18
 
 
33
  try:
34
  st.session_state.df = pd.read_csv(file_path)
35
  st.success(f"File loaded successfully from '{file_path}'!")
 
36
  except Exception as e:
37
  st.error(f"Error loading dataset from the repo directory: {e}")
38
 
 
50
  else:
51
  st.session_state.df = pd.DataFrame(dataset)
52
  st.success(f"Hugging Face Dataset '{dataset_name}' loaded successfully!")
 
53
  except Exception as e:
54
  st.error(f"Error loading Hugging Face dataset: {e}")
55
 
 
60
  try:
61
  st.session_state.df = pd.read_csv(uploaded_file)
62
  st.success("File uploaded successfully!")
 
63
  except Exception as e:
64
  st.error(f"Error reading uploaded file: {e}")
65
 
 
73
 
74
  df = st.session_state.df
75
  st.write("Dataset Preview:")
76
+ st.write(df.head()) # Ensure the dataset preview is displayed only once
77
 
78
  # Set up PandasAI Agent
79
  agent = Agent(df)
 
104
  st.header("Data Analysis with PandasAI")
105
  pandas_question = st.text_input("Ask a question about the dataset (PandasAI):")
106
  if pandas_question:
107
+ try:
108
+ result = agent.chat(pandas_question)
109
+ st.write("PandasAI Answer:", result)
110
+ except Exception as e:
111
+ st.error(f"PandasAI encountered an error: {str(e)}")
112
 
113
  with tab2:
114
  st.header("Q&A with RAG")
115
  rag_question = st.text_input("Ask a question about the dataset (RAG):")
116
  if rag_question:
117
+ try:
118
+ result = qa_chain.run(rag_question)
119
+ st.write("RAG Answer:", result)
120
+ except Exception as e:
121
+ st.error(f"RAG encountered an error: {str(e)}")
122
 
123
  with tab3:
124
  st.header("Data Visualization")
 
145
  else:
146
  st.write("Unable to generate the graph. Please try a different query.")
147
  except Exception as e:
148
+ st.error(f"An error occurred during visualization: {str(e)}")
 
149
  else:
150
  if not api_key:
151
  st.warning("Please set the OpenAI API key in environment variables.")