pavanmutha commited on
Commit
815346d
·
verified ·
1 Parent(s): e482373

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -42
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 and analyze the structure.
76
- 2. Automatically detect key numeric and categorical columns.
77
  3. Perform:
78
- - Basic descriptive statistics.
79
- - Null and duplicate checks.
80
- - Insightful relationships between key columns.
81
- - At least 3 visualizations showing important trends.
82
- 4. Derive at least 3 actionable real-world insights.
83
- 5. Save all visualizations to ./figures/ directory.
84
- 6. Ensure all visualizations are at least 8x6 and saved at 150+ dpi.
85
- Return a JSON object with keys:
86
- - 'insights': clean bullet-point insights.
87
- - 'figures': list of file paths of generated visualizations.
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
- insights = "No insights generated."
98
- image_paths = []
 
99
 
100
- # Handle different return formats from agent
101
  if isinstance(result, str):
102
  try:
103
  result = json.loads(result)
104
- except json.JSONDecodeError:
105
- return "Failed to parse result from agent.", []
106
-
107
 
108
  if isinstance(result, dict):
109
- raw_insights = result.get("insights", "No insights generated.")
110
- insights = "\n".join(raw_insights) if isinstance(raw_insights, list) else raw_insights
 
111
  image_paths = result.get("figures", [])
112
-
113
- print("Received image paths:", image_paths)
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