DrishtiSharma commited on
Commit
b1e654d
·
verified ·
1 Parent(s): 3dc0491

Update app3.py

Browse files
Files changed (1) hide show
  1. app3.py +14 -7
app3.py CHANGED
@@ -3,6 +3,7 @@ import pandas as pd
3
  import plotly.express as px
4
  from datasets import load_dataset
5
  from pandasai import Agent
 
6
  from langchain_community.embeddings.openai import OpenAIEmbeddings
7
  from langchain_community.vectorstores import FAISS
8
  from langchain_openai import ChatOpenAI
@@ -32,8 +33,11 @@ if missing_keys:
32
  f"The following API key(s) are missing: {missing_keys_str}. Please set them in the environment."
33
  )
34
 
 
 
 
35
  # Title of the app
36
- st.title("Data Analyzer")
37
 
38
  # Function to load datasets into session
39
  def load_dataset_into_session():
@@ -62,7 +66,6 @@ def load_dataset_into_session():
62
  if st.button("Load Hugging Face Dataset"):
63
  try:
64
  dataset = load_dataset(dataset_name, split="train", trust_remote_code=True)
65
- # Convert Hugging Face dataset to Pandas DataFrame
66
  if hasattr(dataset, "to_pandas"):
67
  st.session_state.df = dataset.to_pandas()
68
  else:
@@ -97,7 +100,8 @@ if st.session_state.df is not None:
97
  df = st.session_state.df
98
  try:
99
  # Initialize PandasAI Agent
100
- agent = Agent(df)
 
101
 
102
  # Convert DataFrame to documents for RAG
103
  documents = [
@@ -132,9 +136,13 @@ if st.session_state.df is not None:
132
  if pandas_question:
133
  try:
134
  result = agent.chat(pandas_question)
135
- st.write("PandasAI Answer:", result)
 
 
 
136
  except Exception as e:
137
  st.error(f"Error during PandasAI Analysis: {e}")
 
138
 
139
  # Tab 2: RAG Q&A
140
  with tab2:
@@ -146,6 +154,7 @@ if st.session_state.df is not None:
146
  st.write("RAG Answer:", result)
147
  except Exception as e:
148
  st.error(f"Error during RAG Q&A: {e}")
 
149
 
150
  # Tab 3: Data Visualization
151
  with tab3:
@@ -157,7 +166,6 @@ if st.session_state.df is not None:
157
  try:
158
  result = agent.chat(viz_question)
159
 
160
- # Extract Python code for visualization
161
  import re
162
 
163
  code_pattern = r"```python\n(.*?)\n```"
@@ -165,9 +173,8 @@ if st.session_state.df is not None:
165
 
166
  if code_match:
167
  viz_code = code_match.group(1)
168
- # Replace matplotlib (plt) code with Plotly (px)
169
  viz_code = viz_code.replace("plt.", "px.")
170
- exec(viz_code) # Execute the visualization code
171
  st.plotly_chart(fig)
172
  else:
173
  st.warning("Could not generate a graph. Try a different query.")
 
3
  import plotly.express as px
4
  from datasets import load_dataset
5
  from pandasai import Agent
6
+ from pandasai.llm.openai import OpenAI
7
  from langchain_community.embeddings.openai import OpenAIEmbeddings
8
  from langchain_community.vectorstores import FAISS
9
  from langchain_openai import ChatOpenAI
 
33
  f"The following API key(s) are missing: {missing_keys_str}. Please set them in the environment."
34
  )
35
 
36
+ logger.debug(f"OPENAI_API_KEY: {api_key}")
37
+ logger.debug(f"PANDASAI_API_KEY: {pandasai_api_key}")
38
+
39
  # Title of the app
40
+ st.title("PandasAI Data Analyzer with RAG")
41
 
42
  # Function to load datasets into session
43
  def load_dataset_into_session():
 
66
  if st.button("Load Hugging Face Dataset"):
67
  try:
68
  dataset = load_dataset(dataset_name, split="train", trust_remote_code=True)
 
69
  if hasattr(dataset, "to_pandas"):
70
  st.session_state.df = dataset.to_pandas()
71
  else:
 
100
  df = st.session_state.df
101
  try:
102
  # Initialize PandasAI Agent
103
+ llm = OpenAI(api_key=pandasai_api_key)
104
+ agent = Agent(df, llm=llm)
105
 
106
  # Convert DataFrame to documents for RAG
107
  documents = [
 
136
  if pandas_question:
137
  try:
138
  result = agent.chat(pandas_question)
139
+ if result:
140
+ st.write("PandasAI Answer:", result)
141
+ else:
142
+ st.warning("PandasAI returned no result. Try another question.")
143
  except Exception as e:
144
  st.error(f"Error during PandasAI Analysis: {e}")
145
+ logger.error(f"PandasAI Analysis error: {e}")
146
 
147
  # Tab 2: RAG Q&A
148
  with tab2:
 
154
  st.write("RAG Answer:", result)
155
  except Exception as e:
156
  st.error(f"Error during RAG Q&A: {e}")
157
+ logger.error(f"RAG Q&A error: {e}")
158
 
159
  # Tab 3: Data Visualization
160
  with tab3:
 
166
  try:
167
  result = agent.chat(viz_question)
168
 
 
169
  import re
170
 
171
  code_pattern = r"```python\n(.*?)\n```"
 
173
 
174
  if code_match:
175
  viz_code = code_match.group(1)
 
176
  viz_code = viz_code.replace("plt.", "px.")
177
+ exec(viz_code)
178
  st.plotly_chart(fig)
179
  else:
180
  st.warning("Could not generate a graph. Try a different query.")