Update app.py
Browse files
app.py
CHANGED
|
@@ -52,7 +52,7 @@ additional_notes = "Please note: Perform a comprehensive analysis including visu
|
|
| 52 |
agent = CodeAgent(
|
| 53 |
tools=[],
|
| 54 |
model=model,
|
| 55 |
-
additional_authorized_imports=["numpy", "pandas", "matplotlib.pyplot", "seaborn"]
|
| 56 |
)
|
| 57 |
|
| 58 |
|
|
@@ -60,69 +60,54 @@ agent = CodeAgent(
|
|
| 60 |
# Gradio Gallery and visualization output
|
| 61 |
visual_output = gr.Gallery(label="Generated Visualizations", columns=3, height=600, object_fit="contain")
|
| 62 |
|
| 63 |
-
# Fix in the `run_agent` function to handle agent results correctly
|
| 64 |
def run_agent(_):
|
|
|
|
| 65 |
if df_global is None:
|
| 66 |
return "Please upload a file first.", []
|
| 67 |
|
|
|
|
| 68 |
from tempfile import NamedTemporaryFile
|
| 69 |
temp_file = NamedTemporaryFile(delete=False, suffix=".csv")
|
| 70 |
df_global.to_csv(temp_file.name, index=False)
|
| 71 |
temp_file.close()
|
| 72 |
|
|
|
|
| 73 |
prompt = """
|
| 74 |
You are an expert data analyst.
|
| 75 |
-
1. Load the provided dataset
|
| 76 |
-
2. Automatically detect
|
| 77 |
3. Perform:
|
| 78 |
-
- Basic
|
| 79 |
-
- Null
|
| 80 |
-
-
|
| 81 |
-
-
|
| 82 |
-
4.
|
| 83 |
-
5.
|
| 84 |
-
|
| 85 |
-
Return a JSON
|
| 86 |
-
|
| 87 |
-
|
| 88 |
"""
|
| 89 |
|
| 90 |
-
result = agent.run(
|
| 91 |
-
prompt,
|
| 92 |
-
additional_args={"source_file": temp_file.name}
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
os.makedirs("figures", exist_ok=True)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
# Handle different return formats from agent
|
| 101 |
if isinstance(result, str):
|
| 102 |
try:
|
| 103 |
result = json.loads(result)
|
| 104 |
-
except
|
| 105 |
-
return "
|
| 106 |
-
|
| 107 |
|
| 108 |
if isinstance(result, dict):
|
| 109 |
-
raw_insights = result.get("insights",
|
| 110 |
-
insights = "\n".join(raw_insights) if isinstance(raw_insights, list) else raw_insights
|
|
|
|
| 111 |
image_paths = result.get("figures", [])
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
images = []
|
| 116 |
-
for path in image_paths:
|
| 117 |
-
try:
|
| 118 |
-
img = Image.open(path)
|
| 119 |
-
images.append(img)
|
| 120 |
-
except Exception as e:
|
| 121 |
-
print(f"Error loading image {path}: {e}")
|
| 122 |
-
|
| 123 |
-
return insights, images
|
| 124 |
-
|
| 125 |
-
return "Unexpected output format from agent.", []
|
| 126 |
|
| 127 |
|
| 128 |
|
|
|
|
| 52 |
agent = CodeAgent(
|
| 53 |
tools=[],
|
| 54 |
model=model,
|
| 55 |
+
additional_authorized_imports=["numpy", "pandas", "matplotlib.pyplot", "seaborn", "os"]
|
| 56 |
)
|
| 57 |
|
| 58 |
|
|
|
|
| 60 |
# Gradio Gallery and visualization output
|
| 61 |
visual_output = gr.Gallery(label="Generated Visualizations", columns=3, height=600, object_fit="contain")
|
| 62 |
|
|
|
|
| 63 |
def run_agent(_):
|
| 64 |
+
|
| 65 |
if df_global is None:
|
| 66 |
return "Please upload a file first.", []
|
| 67 |
|
| 68 |
+
# Save the dataset temporarily
|
| 69 |
from tempfile import NamedTemporaryFile
|
| 70 |
temp_file = NamedTemporaryFile(delete=False, suffix=".csv")
|
| 71 |
df_global.to_csv(temp_file.name, index=False)
|
| 72 |
temp_file.close()
|
| 73 |
|
| 74 |
+
# Prompt for the agent
|
| 75 |
prompt = """
|
| 76 |
You are an expert data analyst.
|
| 77 |
+
1. Load the provided dataset using: df = pd.read_csv(source_file)
|
| 78 |
+
2. Automatically detect numeric and categorical columns.
|
| 79 |
3. Perform:
|
| 80 |
+
- Basic statistics
|
| 81 |
+
- Null/duplicate checks
|
| 82 |
+
- Correlation analysis
|
| 83 |
+
- 3+ visualizations
|
| 84 |
+
4. Extract 3+ bullet-point insights.
|
| 85 |
+
5. Ensure all figures are saved to ./figures/ directory (create it if needed).
|
| 86 |
+
Use at least 8x6 inches at 150+ dpi.
|
| 87 |
+
6. Return a JSON with:
|
| 88 |
+
- 'insights': list of insights
|
| 89 |
+
- 'figures': list of figure file paths
|
| 90 |
"""
|
| 91 |
|
| 92 |
+
result = agent.run(prompt, additional_args={"source_file": temp_file.name})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Parse and process output
|
| 95 |
+
insights = "No insights returned."
|
| 96 |
+
images = []
|
| 97 |
|
|
|
|
| 98 |
if isinstance(result, str):
|
| 99 |
try:
|
| 100 |
result = json.loads(result)
|
| 101 |
+
except Exception:
|
| 102 |
+
return "Agent returned invalid JSON.", []
|
|
|
|
| 103 |
|
| 104 |
if isinstance(result, dict):
|
| 105 |
+
raw_insights = result.get("insights", [])
|
| 106 |
+
insights = "\n".join(raw_insights) if isinstance(raw_insights, list) else str(raw_insights)
|
| 107 |
+
|
| 108 |
image_paths = result.get("figures", [])
|
| 109 |
+
print("🔍 Image paths
|
| 110 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
|
| 113 |
|