Update app4.py
Browse files
app4.py
CHANGED
@@ -19,6 +19,21 @@ pandasai_api_key = os.getenv("PANDASAI_API_KEY")
|
|
19 |
if not api_key or not pandasai_api_key:
|
20 |
st.warning("API keys for OpenAI or PandasAI are missing. Ensure both keys are set in environment variables.")
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Function to load datasets into session
|
23 |
def load_dataset_into_session():
|
24 |
input_option = st.radio(
|
@@ -32,6 +47,7 @@ def load_dataset_into_session():
|
|
32 |
if st.button("Load Dataset"):
|
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}")
|
@@ -49,6 +65,7 @@ def load_dataset_into_session():
|
|
49 |
st.session_state.df = dataset.to_pandas()
|
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}")
|
@@ -59,6 +76,7 @@ def load_dataset_into_session():
|
|
59 |
if uploaded_file:
|
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}")
|
@@ -119,11 +137,10 @@ if "df" in st.session_state and api_key and pandasai_api_key:
|
|
119 |
try:
|
120 |
result = agent.chat(pandas_question)
|
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")
|
@@ -134,12 +151,6 @@ if "df" in st.session_state and api_key and pandasai_api_key:
|
|
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")
|
@@ -147,7 +158,6 @@ if "df" in st.session_state and api_key and pandasai_api_key:
|
|
147 |
if viz_question:
|
148 |
try:
|
149 |
result = agent.chat(viz_question)
|
150 |
-
|
151 |
# Extract Python code from PandasAI response
|
152 |
import re
|
153 |
code_pattern = r'```python\n(.*?)\n```'
|
@@ -155,21 +165,13 @@ if "df" in st.session_state and api_key and pandasai_api_key:
|
|
155 |
|
156 |
if code_match:
|
157 |
viz_code = code_match.group(1)
|
158 |
-
|
159 |
-
# Replace matplotlib with plotly
|
160 |
-
viz_code = viz_code.replace('plt.', 'px.')
|
161 |
-
viz_code = viz_code.replace('plt.show()', 'fig = px.scatter(df, x=x, y=y)')
|
162 |
-
|
163 |
-
# Execute the modified code
|
164 |
exec(viz_code)
|
165 |
-
st.plotly_chart(fig)
|
166 |
else:
|
167 |
-
st.write("Unable to generate the graph.
|
|
|
|
|
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.")
|
|
|
19 |
if not api_key or not pandasai_api_key:
|
20 |
st.warning("API keys for OpenAI or PandasAI are missing. Ensure both keys are set in environment variables.")
|
21 |
|
22 |
+
# Add session reset button
|
23 |
+
if st.button("Reset Session"):
|
24 |
+
for key in list(st.session_state.keys()):
|
25 |
+
del st.session_state[key]
|
26 |
+
st.experimental_rerun()
|
27 |
+
|
28 |
+
# Function to validate and clean dataset
|
29 |
+
def validate_and_clean_dataset(df):
|
30 |
+
# Rename columns for consistency
|
31 |
+
df.columns = [col.strip().lower().replace(" ", "_") for col in df.columns]
|
32 |
+
# Check for missing values
|
33 |
+
if df.isnull().values.any():
|
34 |
+
st.warning("Dataset contains missing values. Consider cleaning the data.")
|
35 |
+
return df
|
36 |
+
|
37 |
# Function to load datasets into session
|
38 |
def load_dataset_into_session():
|
39 |
input_option = st.radio(
|
|
|
47 |
if st.button("Load Dataset"):
|
48 |
try:
|
49 |
st.session_state.df = pd.read_csv(file_path)
|
50 |
+
st.session_state.df = validate_and_clean_dataset(st.session_state.df)
|
51 |
st.success(f"File loaded successfully from '{file_path}'!")
|
52 |
except Exception as e:
|
53 |
st.error(f"Error loading dataset from the repo directory: {e}")
|
|
|
65 |
st.session_state.df = dataset.to_pandas()
|
66 |
else:
|
67 |
st.session_state.df = pd.DataFrame(dataset)
|
68 |
+
st.session_state.df = validate_and_clean_dataset(st.session_state.df)
|
69 |
st.success(f"Hugging Face Dataset '{dataset_name}' loaded successfully!")
|
70 |
except Exception as e:
|
71 |
st.error(f"Error loading Hugging Face dataset: {e}")
|
|
|
76 |
if uploaded_file:
|
77 |
try:
|
78 |
st.session_state.df = pd.read_csv(uploaded_file)
|
79 |
+
st.session_state.df = validate_and_clean_dataset(st.session_state.df)
|
80 |
st.success("File uploaded successfully!")
|
81 |
except Exception as e:
|
82 |
st.error(f"Error reading uploaded file: {e}")
|
|
|
137 |
try:
|
138 |
result = agent.chat(pandas_question)
|
139 |
st.write("PandasAI Answer:", result)
|
140 |
+
if hasattr(agent, "last_output"):
|
141 |
+
st.write("PandasAI Intermediate Output:", agent.last_output)
|
142 |
except Exception as e:
|
143 |
st.error(f"PandasAI encountered an error: {str(e)}")
|
|
|
|
|
|
|
144 |
|
145 |
with tab2:
|
146 |
st.subheader("Q&A with RAG")
|
|
|
151 |
st.write("RAG Answer:", result)
|
152 |
except Exception as e:
|
153 |
st.error(f"RAG encountered an error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
with tab3:
|
156 |
st.subheader("Data Visualization")
|
|
|
158 |
if viz_question:
|
159 |
try:
|
160 |
result = agent.chat(viz_question)
|
|
|
161 |
# Extract Python code from PandasAI response
|
162 |
import re
|
163 |
code_pattern = r'```python\n(.*?)\n```'
|
|
|
165 |
|
166 |
if code_match:
|
167 |
viz_code = code_match.group(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
exec(viz_code)
|
|
|
169 |
else:
|
170 |
+
st.write("Unable to generate the graph. Showing fallback example.")
|
171 |
+
fig = px.scatter(df, x=df.columns[0], y=df.columns[1])
|
172 |
+
st.plotly_chart(fig)
|
173 |
except Exception as e:
|
174 |
st.error(f"An error occurred during visualization: {str(e)}")
|
|
|
|
|
|
|
175 |
else:
|
176 |
if not api_key:
|
177 |
st.warning("Please set the OpenAI API key in environment variables.")
|