Update app.py
Browse files
app.py
CHANGED
@@ -79,7 +79,7 @@ class DataAnalysisAgent(CodeAgent):
|
|
79 |
|
80 |
Use the provided tools to analyze this specific dataset and return detailed results.
|
81 |
"""
|
82 |
-
return super().run(enhanced_prompt, data=self.dataset, **kwargs)
|
83 |
|
84 |
|
85 |
@tool
|
@@ -204,16 +204,15 @@ def suggest_features(data: pd.DataFrame) -> str:
|
|
204 |
@tool
|
205 |
def describe_data(data: pd.DataFrame) -> str:
|
206 |
"""Generates a comprehensive descriptive statistics report for the entire DataFrame.
|
207 |
-
|
208 |
Args:
|
209 |
-
|
210 |
-
|
211 |
Returns:
|
212 |
-
|
213 |
"""
|
214 |
-
|
215 |
-
return data.describe(include='all').to_string()
|
216 |
|
|
|
217 |
|
218 |
|
219 |
@tool
|
@@ -232,19 +231,19 @@ def execute_code(code_string: str, data: pd.DataFrame) -> str:
|
|
232 |
|
233 |
# Execute the code with the passed variables
|
234 |
exec(code_string, local_vars)
|
235 |
-
|
236 |
if "result" in local_vars:
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
else:
|
247 |
-
|
248 |
|
249 |
except Exception as e:
|
250 |
return f"Error executing code: {str(e)}"
|
@@ -279,10 +278,10 @@ def main():
|
|
279 |
if "agent" not in st.session_state:
|
280 |
st.session_state["agent"] = None
|
281 |
if "custom_code" not in st.session_state:
|
282 |
-
st.session_state[
|
283 |
|
284 |
uploaded_file = st.file_uploader("Choose a CSV, Excel, or JSON file", type=["csv", "xlsx", "xls", "json"])
|
285 |
-
|
286 |
if uploaded_file:
|
287 |
with st.spinner("Loading and processing your data..."):
|
288 |
data = load_data(uploaded_file)
|
@@ -297,7 +296,7 @@ def main():
|
|
297 |
analyze_categorical_columns,
|
298 |
suggest_features,
|
299 |
describe_data,
|
300 |
-
execute_code
|
301 |
],
|
302 |
model=GroqLLM(),
|
303 |
additional_authorized_imports=["pandas", "numpy", "matplotlib", "seaborn"],
|
@@ -316,7 +315,7 @@ def main():
|
|
316 |
"Correlation Analysis",
|
317 |
"Categorical Analysis",
|
318 |
"Feature Engineering",
|
319 |
-
|
320 |
"Custom Code",
|
321 |
"Custom Question",
|
322 |
],
|
@@ -364,29 +363,29 @@ def main():
|
|
364 |
"of the data."
|
365 |
)
|
366 |
st.write(result)
|
367 |
-
|
368 |
elif analysis_type == "Custom Code":
|
369 |
-
st.session_state[
|
370 |
placeholder="Enter your Python code here...",
|
371 |
language="python",
|
372 |
theme="github",
|
373 |
key="code_editor",
|
374 |
-
value=st.session_state[
|
375 |
)
|
376 |
if st.button("Run Code"):
|
377 |
-
|
378 |
result = st.session_state["agent"].run(
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
if isinstance(result, str) and result.startswith("data:image"):
|
383 |
st.image(f"{result}")
|
384 |
else:
|
385 |
-
|
386 |
-
|
387 |
elif analysis_type == "Custom Question":
|
388 |
-
|
389 |
-
|
390 |
with st.spinner("Analyzing..."):
|
391 |
result = st.session_state["agent"].run(question, stream=True) # Pass stream argument here
|
392 |
st.write(result)
|
|
|
79 |
|
80 |
Use the provided tools to analyze this specific dataset and return detailed results.
|
81 |
"""
|
82 |
+
return super().run(enhanced_prompt, data=self.dataset, **kwargs) # Pass data as argument
|
83 |
|
84 |
|
85 |
@tool
|
|
|
204 |
@tool
|
205 |
def describe_data(data: pd.DataFrame) -> str:
|
206 |
"""Generates a comprehensive descriptive statistics report for the entire DataFrame.
|
207 |
+
|
208 |
Args:
|
209 |
+
data: A pandas DataFrame containing the dataset to analyze.
|
210 |
+
|
211 |
Returns:
|
212 |
+
str: String representation of the descriptive statistics
|
213 |
"""
|
|
|
|
|
214 |
|
215 |
+
return data.describe(include="all").to_string()
|
216 |
|
217 |
|
218 |
@tool
|
|
|
231 |
|
232 |
# Execute the code with the passed variables
|
233 |
exec(code_string, local_vars)
|
234 |
+
|
235 |
if "result" in local_vars:
|
236 |
+
if isinstance(local_vars["result"], (pd.DataFrame, pd.Series)):
|
237 |
+
return local_vars["result"].to_string()
|
238 |
+
elif isinstance(local_vars["result"], plt.Figure):
|
239 |
+
buf = io.BytesIO()
|
240 |
+
local_vars["result"].savefig(buf, format="png")
|
241 |
+
plt.close(local_vars["result"])
|
242 |
+
return f"data:image/png;base64,{base64.b64encode(buf.getvalue()).decode()}"
|
243 |
+
else:
|
244 |
+
return str(local_vars["result"])
|
245 |
else:
|
246 |
+
return "Code executed successfully, but no variable called 'result' was assigned."
|
247 |
|
248 |
except Exception as e:
|
249 |
return f"Error executing code: {str(e)}"
|
|
|
278 |
if "agent" not in st.session_state:
|
279 |
st.session_state["agent"] = None
|
280 |
if "custom_code" not in st.session_state:
|
281 |
+
st.session_state["custom_code"] = ""
|
282 |
|
283 |
uploaded_file = st.file_uploader("Choose a CSV, Excel, or JSON file", type=["csv", "xlsx", "xls", "json"])
|
284 |
+
|
285 |
if uploaded_file:
|
286 |
with st.spinner("Loading and processing your data..."):
|
287 |
data = load_data(uploaded_file)
|
|
|
296 |
analyze_categorical_columns,
|
297 |
suggest_features,
|
298 |
describe_data,
|
299 |
+
execute_code,
|
300 |
],
|
301 |
model=GroqLLM(),
|
302 |
additional_authorized_imports=["pandas", "numpy", "matplotlib", "seaborn"],
|
|
|
315 |
"Correlation Analysis",
|
316 |
"Categorical Analysis",
|
317 |
"Feature Engineering",
|
318 |
+
"Data Description",
|
319 |
"Custom Code",
|
320 |
"Custom Question",
|
321 |
],
|
|
|
363 |
"of the data."
|
364 |
)
|
365 |
st.write(result)
|
366 |
+
|
367 |
elif analysis_type == "Custom Code":
|
368 |
+
st.session_state["custom_code"] = st_ace(
|
369 |
placeholder="Enter your Python code here...",
|
370 |
language="python",
|
371 |
theme="github",
|
372 |
key="code_editor",
|
373 |
+
value=st.session_state["custom_code"],
|
374 |
)
|
375 |
if st.button("Run Code"):
|
376 |
+
with st.spinner("Executing custom code..."):
|
377 |
result = st.session_state["agent"].run(
|
378 |
+
f"Execute the following code and return any 'result' variable"
|
379 |
+
f"```python\n{st.session_state['custom_code']}\n```"
|
380 |
+
)
|
381 |
if isinstance(result, str) and result.startswith("data:image"):
|
382 |
st.image(f"{result}")
|
383 |
else:
|
384 |
+
st.write(result)
|
385 |
+
|
386 |
elif analysis_type == "Custom Question":
|
387 |
+
question = st.text_input("What would you like to know about your data?")
|
388 |
+
if question:
|
389 |
with st.spinner("Analyzing..."):
|
390 |
result = st.session_state["agent"].run(question, stream=True) # Pass stream argument here
|
391 |
st.write(result)
|