DrishtiSharma commited on
Commit
f717ca9
·
verified ·
1 Parent(s): d22278f

Update app4.py

Browse files
Files changed (1) hide show
  1. app4.py +43 -19
app4.py CHANGED
@@ -76,32 +76,44 @@ if "df" in st.session_state and api_key and pandasai_api_key:
76
  st.write(df.head()) # Ensure the dataset preview is displayed only once
77
 
78
  # Set up PandasAI Agent
79
- agent = Agent(df)
 
 
 
 
80
 
81
  # Convert dataframe into documents
82
- documents = [
83
- Document(
84
- page_content=", ".join([f"{col}: {row[col]}" for col in df.columns]),
85
- metadata={"index": index}
86
- )
87
- for index, row in df.iterrows()
88
- ]
 
 
 
 
89
 
90
  # Set up RAG
91
- embeddings = OpenAIEmbeddings()
92
- vectorstore = FAISS.from_documents(documents, embeddings)
93
- retriever = vectorstore.as_retriever()
94
- qa_chain = RetrievalQA.from_chain_type(
95
- llm=ChatOpenAI(),
96
- chain_type="stuff",
97
- retriever=retriever
98
- )
 
 
 
 
99
 
100
  # Create tabs
101
  tab1, tab2, tab3 = st.tabs(["PandasAI Analysis", "RAG Q&A", "Data Visualization"])
102
 
103
  with tab1:
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:
@@ -109,9 +121,12 @@ if "df" in st.session_state and api_key and pandasai_api_key:
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:
@@ -119,9 +134,15 @@ if "df" in st.session_state and api_key and pandasai_api_key:
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")
125
  viz_question = st.text_input("What kind of graph would you like? (e.g., 'Show a scatter plot of salary vs experience')")
126
  if viz_question:
127
  try:
@@ -146,6 +167,9 @@ if "df" in st.session_state and api_key and pandasai_api_key:
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.")
 
76
  st.write(df.head()) # Ensure the dataset preview is displayed only once
77
 
78
  # Set up PandasAI Agent
79
+ try:
80
+ agent = Agent(df)
81
+ st.info("PandasAI Agent initialized successfully.")
82
+ except Exception as e:
83
+ st.error(f"Error initializing PandasAI Agent: {str(e)}")
84
 
85
  # Convert dataframe into documents
86
+ try:
87
+ documents = [
88
+ Document(
89
+ page_content=", ".join([f"{col}: {row[col]}" for col in df.columns]),
90
+ metadata={"index": index}
91
+ )
92
+ for index, row in df.iterrows()
93
+ ]
94
+ st.info("Documents created successfully for RAG.")
95
+ except Exception as e:
96
+ st.error(f"Error creating documents for RAG: {str(e)}")
97
 
98
  # Set up RAG
99
+ try:
100
+ embeddings = OpenAIEmbeddings()
101
+ vectorstore = FAISS.from_documents(documents, embeddings)
102
+ retriever = vectorstore.as_retriever()
103
+ qa_chain = RetrievalQA.from_chain_type(
104
+ llm=ChatOpenAI(),
105
+ chain_type="stuff",
106
+ retriever=retriever
107
+ )
108
+ st.info("RAG setup completed successfully.")
109
+ except Exception as e:
110
+ st.error(f"Error setting up RAG: {str(e)}")
111
 
112
  # Create tabs
113
  tab1, tab2, tab3 = st.tabs(["PandasAI Analysis", "RAG Q&A", "Data Visualization"])
114
 
115
  with tab1:
116
+ st.subheader("Data Analysis with PandasAI")
117
  pandas_question = st.text_input("Ask a question about the dataset (PandasAI):")
118
  if pandas_question:
119
  try:
 
121
  st.write("PandasAI Answer:", result)
122
  except Exception as e:
123
  st.error(f"PandasAI encountered an error: {str(e)}")
124
+ finally:
125
+ st.write("PandasAI intermediate output (if any):")
126
+ st.write(agent.last_output if hasattr(agent, "last_output") else "No intermediate output available.")
127
 
128
  with tab2:
129
+ st.subheader("Q&A with RAG")
130
  rag_question = st.text_input("Ask a question about the dataset (RAG):")
131
  if rag_question:
132
  try:
 
134
  st.write("RAG Answer:", result)
135
  except Exception as e:
136
  st.error(f"RAG encountered an error: {str(e)}")
137
+ finally:
138
+ st.write("RAG Intermediate Status:")
139
+ st.write({
140
+ "retriever": retriever,
141
+ "qa_chain": qa_chain
142
+ })
143
 
144
  with tab3:
145
+ st.subheader("Data Visualization")
146
  viz_question = st.text_input("What kind of graph would you like? (e.g., 'Show a scatter plot of salary vs experience')")
147
  if viz_question:
148
  try:
 
167
  st.write("Unable to generate the graph. Please try a different query.")
168
  except Exception as e:
169
  st.error(f"An error occurred during visualization: {str(e)}")
170
+ finally:
171
+ st.write("Visualization debug details:")
172
+ st.write({"viz_question": viz_question, "result": result})
173
  else:
174
  if not api_key:
175
  st.warning("Please set the OpenAI API key in environment variables.")