Update app.py
Browse files
app.py
CHANGED
|
@@ -33,21 +33,9 @@ class DataAnalysisAgent:
|
|
| 33 |
self.task_agent = TaskAgent()
|
| 34 |
self.analysis_agent = AnalysisAgent()
|
| 35 |
|
| 36 |
-
|
| 37 |
-
# Visualization configuration
|
| 38 |
-
self.chart_styles = {
|
| 39 |
-
'trend_analysis': {'type': 'line', 'color': '#4C72B0'},
|
| 40 |
-
'clustering': {'type': 'scatter', 'color': '#DD8452'},
|
| 41 |
-
'distribution': {'type': 'hist', 'color': '#55A868'}
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
def preprocess_data(self, data):
|
| 45 |
-
"""Clean and prepare uploaded data"""
|
| 46 |
-
# Handle missing values
|
| 47 |
data = data.dropna(axis=1, how='all')
|
| 48 |
data = data.fillna(data.mean(numeric_only=True))
|
| 49 |
-
|
| 50 |
-
# Convert datetime columns
|
| 51 |
for col in data.columns:
|
| 52 |
if data[col].dtype == 'object':
|
| 53 |
try:
|
|
@@ -57,132 +45,96 @@ class DataAnalysisAgent:
|
|
| 57 |
return data
|
| 58 |
|
| 59 |
def parse_query(self, query):
|
| 60 |
-
|
| 61 |
-
# Classify query intent
|
| 62 |
-
result = self.nlp_pipeline(query)[0]
|
| 63 |
-
intent = result['label']
|
| 64 |
-
|
| 65 |
-
# Map to analysis tasks
|
| 66 |
-
task_mapping = {
|
| 67 |
-
'trend': 'trend_analysis',
|
| 68 |
-
'cluster': 'clustering',
|
| 69 |
-
'distribution': 'distribution',
|
| 70 |
-
'compare': 'comparison'
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
# Extract parameters using simple rule-based parsing
|
| 74 |
-
params = {}
|
| 75 |
-
if 'by' in query:
|
| 76 |
-
params['group_by'] = query.split('by')[-1].strip()
|
| 77 |
-
|
| 78 |
-
return {
|
| 79 |
-
'task': task_mapping.get(intent, 'trend_analysis'),
|
| 80 |
-
'params': params
|
| 81 |
-
}
|
| 82 |
|
| 83 |
def analyze(self, data, task):
|
| 84 |
-
"""Perform data analysis based on task"""
|
| 85 |
if task['task'] == 'trend_analysis':
|
| 86 |
-
# Time series analysis
|
| 87 |
group_col = task['params'].get('group_by', data.columns[0])
|
| 88 |
return data.groupby(group_col).mean().reset_index()
|
| 89 |
-
|
| 90 |
elif task['task'] == 'clustering':
|
| 91 |
-
|
| 92 |
-
numeric_cols = data.select_dtypes(include='number').columns
|
| 93 |
kmeans = KMeans(n_clusters=3)
|
| 94 |
data['cluster'] = kmeans.fit_predict(data[numeric_cols])
|
| 95 |
return data
|
| 96 |
-
|
| 97 |
elif task['task'] == 'distribution':
|
| 98 |
-
# Statistical distribution
|
| 99 |
return data.describe()
|
| 100 |
-
|
| 101 |
return data
|
| 102 |
|
| 103 |
def visualize(self, data, task):
|
| 104 |
-
"""Generate appropriate visualization"""
|
| 105 |
-
chart_type = self.chart_styles[task['task']]['type']
|
| 106 |
-
fig, ax = plt.subplots(figsize=(8, 4))
|
| 107 |
-
|
| 108 |
try:
|
| 109 |
-
|
|
|
|
| 110 |
data.plot(kind='line', ax=ax, title='Trend Analysis')
|
| 111 |
-
elif
|
| 112 |
ax.scatter(data.iloc[:, 0], data.iloc[:, 1], c=data['cluster'])
|
| 113 |
ax.set_title('Cluster Analysis')
|
| 114 |
-
|
| 115 |
-
data.
|
| 116 |
-
ax.set_title('Distribution Analysis')
|
| 117 |
-
|
| 118 |
-
plt.tight_layout()
|
| 119 |
|
| 120 |
-
|
| 121 |
temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 122 |
plt.savefig(temp_file.name)
|
| 123 |
plt.close()
|
| 124 |
return temp_file.name
|
| 125 |
-
|
| 126 |
except Exception as e:
|
| 127 |
print(f"Visualization error: {e}")
|
| 128 |
return None
|
| 129 |
|
| 130 |
-
def generate_insights(self, data, task):
|
| 131 |
-
"""Create natural language explanations"""
|
| 132 |
-
insights = []
|
| 133 |
-
|
| 134 |
-
if task['task'] == 'trend_analysis':
|
| 135 |
-
max_value = data.iloc[:, -1].max()
|
| 136 |
-
min_value = data.iloc[:, -1].min()
|
| 137 |
-
insights.append(
|
| 138 |
-
f"The data shows peaks up to {max_value:.2f} and lows around {min_value:.2f}"
|
| 139 |
-
)
|
| 140 |
-
|
| 141 |
-
elif task['task'] == 'clustering':
|
| 142 |
-
cluster_dist = data['cluster'].value_counts().to_dict()
|
| 143 |
-
insights.append(
|
| 144 |
-
f"Data distribution across clusters: {cluster_dist}"
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
-
return "\n".join(insights) if insights else "No significant patterns detected"
|
| 148 |
-
|
| 149 |
def process_data(file, query):
|
| 150 |
-
"""Main processing function for Gradio interface"""
|
| 151 |
agent = DataAnalysisAgent()
|
| 152 |
|
| 153 |
try:
|
| 154 |
-
#
|
|
|
|
|
|
|
|
|
|
| 155 |
df = pd.read_csv(file.name)
|
| 156 |
df = agent.preprocess_data(df)
|
| 157 |
|
| 158 |
-
# Process query
|
| 159 |
task = agent.parse_query(query)
|
| 160 |
-
|
| 161 |
-
# Perform analysis
|
| 162 |
analyzed_data = agent.analyze(df, task)
|
| 163 |
|
| 164 |
-
|
| 165 |
-
insights = agent.generate_insights(analyzed_data, task)
|
| 166 |
chart_path = agent.visualize(analyzed_data, task)
|
| 167 |
|
| 168 |
return insights, chart_path if chart_path else None
|
| 169 |
|
| 170 |
except Exception as e:
|
| 171 |
-
return f"Error
|
| 172 |
|
| 173 |
-
# Gradio Interface
|
| 174 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 175 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
with gr.Row():
|
| 178 |
with gr.Column():
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
with gr.Column():
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
submit_btn.click(
|
| 188 |
fn=process_data,
|
|
@@ -192,14 +144,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 192 |
|
| 193 |
gr.Examples(
|
| 194 |
examples=[
|
| 195 |
-
["sample_sales_data.csv", "Show sales trends
|
| 196 |
-
["customer_data.csv", "Cluster customers by
|
| 197 |
],
|
| 198 |
-
fn=process_data,
|
| 199 |
inputs=[file_input, query_input],
|
| 200 |
outputs=[insights_output, chart_output],
|
|
|
|
| 201 |
cache_examples=False
|
| 202 |
)
|
| 203 |
|
| 204 |
if __name__ == "__main__":
|
| 205 |
-
demo.launch(debug=True)
|
|
|
|
| 33 |
self.task_agent = TaskAgent()
|
| 34 |
self.analysis_agent = AnalysisAgent()
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def preprocess_data(self, data):
|
|
|
|
|
|
|
| 37 |
data = data.dropna(axis=1, how='all')
|
| 38 |
data = data.fillna(data.mean(numeric_only=True))
|
|
|
|
|
|
|
| 39 |
for col in data.columns:
|
| 40 |
if data[col].dtype == 'object':
|
| 41 |
try:
|
|
|
|
| 45 |
return data
|
| 46 |
|
| 47 |
def parse_query(self, query):
|
| 48 |
+
return self.task_agent.parse_query(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def analyze(self, data, task):
|
|
|
|
| 51 |
if task['task'] == 'trend_analysis':
|
|
|
|
| 52 |
group_col = task['params'].get('group_by', data.columns[0])
|
| 53 |
return data.groupby(group_col).mean().reset_index()
|
|
|
|
| 54 |
elif task['task'] == 'clustering':
|
| 55 |
+
numeric_cols = data.select_dtypes(include=np.number).columns
|
|
|
|
| 56 |
kmeans = KMeans(n_clusters=3)
|
| 57 |
data['cluster'] = kmeans.fit_predict(data[numeric_cols])
|
| 58 |
return data
|
|
|
|
| 59 |
elif task['task'] == 'distribution':
|
|
|
|
| 60 |
return data.describe()
|
|
|
|
| 61 |
return data
|
| 62 |
|
| 63 |
def visualize(self, data, task):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 66 |
+
if task['task'] == 'trend_analysis':
|
| 67 |
data.plot(kind='line', ax=ax, title='Trend Analysis')
|
| 68 |
+
elif task['task'] == 'clustering':
|
| 69 |
ax.scatter(data.iloc[:, 0], data.iloc[:, 1], c=data['cluster'])
|
| 70 |
ax.set_title('Cluster Analysis')
|
| 71 |
+
else:
|
| 72 |
+
data.plot(kind='bar', ax=ax, title='Distribution Analysis')
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
plt.tight_layout()
|
| 75 |
temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 76 |
plt.savefig(temp_file.name)
|
| 77 |
plt.close()
|
| 78 |
return temp_file.name
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Visualization error: {e}")
|
| 81 |
return None
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def process_data(file, query):
|
|
|
|
| 84 |
agent = DataAnalysisAgent()
|
| 85 |
|
| 86 |
try:
|
| 87 |
+
# Validate CSV file
|
| 88 |
+
if not file.name.endswith('.csv'):
|
| 89 |
+
raise ValueError("Please upload a CSV file")
|
| 90 |
+
|
| 91 |
df = pd.read_csv(file.name)
|
| 92 |
df = agent.preprocess_data(df)
|
| 93 |
|
|
|
|
| 94 |
task = agent.parse_query(query)
|
|
|
|
|
|
|
| 95 |
analyzed_data = agent.analyze(df, task)
|
| 96 |
|
| 97 |
+
insights = agent.analysis_agent.generate_insights(analyzed_data)
|
|
|
|
| 98 |
chart_path = agent.visualize(analyzed_data, task)
|
| 99 |
|
| 100 |
return insights, chart_path if chart_path else None
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
+
return f"Error: {str(e)}", None
|
| 104 |
|
| 105 |
+
# Gradio Interface with CSV-specific upload
|
| 106 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 107 |
+
gr.Markdown("""
|
| 108 |
+
# Intelligent Data Analysis Agent
|
| 109 |
+
**Upload your CSV file and ask questions about your data**
|
| 110 |
+
""")
|
| 111 |
|
| 112 |
with gr.Row():
|
| 113 |
with gr.Column():
|
| 114 |
+
gr.Markdown("## Step 1: Upload your CSV file")
|
| 115 |
+
file_input = gr.File(
|
| 116 |
+
label="Select CSV File",
|
| 117 |
+
file_types=[".csv"],
|
| 118 |
+
type="filepath"
|
| 119 |
+
)
|
| 120 |
+
gr.Markdown("## Step 2: Ask your question")
|
| 121 |
+
query_input = gr.Textbox(
|
| 122 |
+
label="Data Question",
|
| 123 |
+
placeholder="e.g., 'Show sales trends by region'"
|
| 124 |
+
)
|
| 125 |
+
submit_btn = gr.Button("Analyze", variant="primary")
|
| 126 |
|
| 127 |
with gr.Column():
|
| 128 |
+
gr.Markdown("## Analysis Results")
|
| 129 |
+
insights_output = gr.Textbox(
|
| 130 |
+
label="Insights",
|
| 131 |
+
interactive=False,
|
| 132 |
+
lines=10
|
| 133 |
+
)
|
| 134 |
+
chart_output = gr.Image(
|
| 135 |
+
label="Visualization",
|
| 136 |
+
show_label=True
|
| 137 |
+
)
|
| 138 |
|
| 139 |
submit_btn.click(
|
| 140 |
fn=process_data,
|
|
|
|
| 144 |
|
| 145 |
gr.Examples(
|
| 146 |
examples=[
|
| 147 |
+
[["sample_sales_data.csv"], "Show sales trends over time"],
|
| 148 |
+
[["customer_data.csv"], "Cluster customers by purchase habits"]
|
| 149 |
],
|
|
|
|
| 150 |
inputs=[file_input, query_input],
|
| 151 |
outputs=[insights_output, chart_output],
|
| 152 |
+
fn=process_data,
|
| 153 |
cache_examples=False
|
| 154 |
)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
+
demo.queue().launch(debug=True)
|