DrishtiSharma commited on
Commit
bfd009e
Β·
verified Β·
1 Parent(s): cd9789b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -19
app.py CHANGED
@@ -86,6 +86,71 @@ if st.session_state.df is not None and st.session_state.show_preview:
86
  st.subheader("πŸ“‚ Dataset Preview")
87
  st.dataframe(st.session_state.df.head())
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  # Function to create TXT file
90
  def create_text_report_with_viz_temp(report, conclusion, visualizations):
91
  content = f"### Analysis Report\n\n{report}\n\n### Visualizations\n"
@@ -112,6 +177,59 @@ def create_text_report_with_viz_temp(report, conclusion, visualizations):
112
  return temp_txt.name
113
 
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  # Function to create PDF with report text and visualizations
116
  def create_pdf_report_with_viz(report, conclusion, visualizations):
117
  pdf = FPDF()
@@ -301,27 +419,11 @@ if st.session_state.df is not None:
301
  st.markdown(report_result if report_result else "⚠️ No Report Generated.")
302
 
303
  # Step 4: Generate Visualizations
304
- visualizations = []
305
 
306
- fig_salary = px.box(st.session_state.df, x="job_title", y="salary_in_usd",
307
- title="Salary Distribution by Job Title")
308
- visualizations.append(fig_salary)
309
-
310
- fig_experience = px.bar(
311
- st.session_state.df.groupby("experience_level")["salary_in_usd"].mean().reset_index(),
312
- x="experience_level", y="salary_in_usd",
313
- title="Average Salary by Experience Level"
314
- )
315
- visualizations.append(fig_experience)
316
-
317
- fig_employment = px.box(st.session_state.df, x="employment_type", y="salary_in_usd",
318
- title="Salary Distribution by Employment Type")
319
- visualizations.append(fig_employment)
320
 
321
  # Step 5: Insert Visual Insights
322
  st.markdown("### Visual Insights")
323
- for fig in visualizations:
324
- st.plotly_chart(fig, use_container_width=True)
325
 
326
  # Step 6: Display Concise Conclusion
327
  #st.markdown("#### Conclusion")
@@ -355,5 +457,4 @@ else:
355
  # Sidebar Reference
356
  with st.sidebar:
357
  st.header("πŸ“š Reference:")
358
- st.markdown("[SQL Agents w CrewAI & Llama 3 - Plaban Nayak](https://github.com/plaban1981/Agents/blob/main/SQL_Agents_with_CrewAI_and_Llama_3.ipynb)")
359
-
 
86
  st.subheader("πŸ“‚ Dataset Preview")
87
  st.dataframe(st.session_state.df.head())
88
 
89
+ # Ask GPT-4o for Visualization Suggestions
90
+ def ask_gpt4o_for_visualization(query, df, llm):
91
+ columns = ', '.join(df.columns)
92
+ prompt = f"""
93
+ Analyze the query and suggest the best visualization.
94
+ Query: "{query}"
95
+ Available Columns: {columns}
96
+ Respond in this JSON format:
97
+ {{
98
+ "chart_type": "bar/box/line/scatter",
99
+ "x_axis": "column_name",
100
+ "y_axis": "column_name",
101
+ "group_by": "optional_column_name"
102
+ }}
103
+ """
104
+ response = llm.generate(prompt)
105
+ try:
106
+ return json.loads(response)
107
+ except json.JSONDecodeError:
108
+ st.error("⚠️ GPT-4o failed to generate a valid suggestion.")
109
+ return None
110
+
111
+ # Dynamically generate Plotly visualizations based on GPT-4o suggestions
112
+ def generate_visualization(suggestion, df):
113
+ chart_type = suggestion.get("chart_type", "bar").lower()
114
+ x_axis = suggestion.get("x_axis")
115
+ y_axis = suggestion.get("y_axis")
116
+ group_by = suggestion.get("group_by")
117
+
118
+ # Ensure required inputs are available
119
+ if not x_axis or not y_axis:
120
+ st.warning("⚠️ GPT-4o did not provide enough information for the visualization.")
121
+ return None
122
+
123
+ # Dynamically select the Plotly function
124
+ plotly_function = getattr(px, chart_type, None)
125
+
126
+ # Handle unsupported chart types gracefully
127
+ if not plotly_function:
128
+ st.warning(f"⚠️ Unsupported chart type '{chart_type}' suggested by GPT-4o.")
129
+ return None
130
+
131
+ # Prepare dynamic parameters for Plotly function
132
+ plot_args = {
133
+ "data_frame": df,
134
+ "x": x_axis,
135
+ "y": y_axis,
136
+ }
137
+ if group_by:
138
+ plot_args["color"] = group_by
139
+
140
+ try:
141
+ # Generate the dynamic visualization
142
+ fig = plotly_function(**plot_args)
143
+ fig.update_layout(
144
+ title=f"{chart_type.title()} Plot of {y_axis.replace('_', ' ').title()} by {x_axis.replace('_', ' ').title()}",
145
+ xaxis_title=x_axis.replace('_', ' ').title(),
146
+ yaxis_title=y_axis.replace('_', ' ').title(),
147
+ )
148
+ return fig
149
+
150
+ except Exception as e:
151
+ st.error(f"⚠️ Failed to generate visualization: {e}")
152
+ return None
153
+
154
  # Function to create TXT file
155
  def create_text_report_with_viz_temp(report, conclusion, visualizations):
156
  content = f"### Analysis Report\n\n{report}\n\n### Visualizations\n"
 
177
  return temp_txt.name
178
 
179
 
180
+ def add_stats_to_figure(fig, df, y_axis, chart_type):
181
+ # Calculate statistics
182
+ min_val = df[y_axis].min()
183
+ max_val = df[y_axis].max()
184
+ avg_val = df[y_axis].mean()
185
+ median_val = df[y_axis].median()
186
+ std_dev_val = df[y_axis].std()
187
+
188
+ # Stats summary text
189
+ stats_text = (
190
+ f"πŸ“Š **Statistics**\n\n"
191
+ f"- **Min:** ${min_val:,.2f}\n"
192
+ f"- **Max:** ${max_val:,.2f}\n"
193
+ f"- **Average:** ${avg_val:,.2f}\n"
194
+ f"- **Median:** ${median_val:,.2f}\n"
195
+ f"- **Std Dev:** ${std_dev_val:,.2f}"
196
+ )
197
+
198
+ # Charts suitable for stats annotations
199
+ if chart_type in ["bar", "line", "scatter"]:
200
+ # Add annotation box
201
+ fig.add_annotation(
202
+ text=stats_text,
203
+ xref="paper", yref="paper",
204
+ x=1.05, y=1,
205
+ showarrow=False,
206
+ align="left",
207
+ font=dict(size=12, color="black"),
208
+ bordercolor="black",
209
+ borderwidth=1,
210
+ bgcolor="rgba(255, 255, 255, 0.8)"
211
+ )
212
+
213
+ # Add horizontal lines for min, median, avg, max
214
+ fig.add_hline(y=min_val, line_dash="dot", line_color="red", annotation_text="Min", annotation_position="bottom right")
215
+ fig.add_hline(y=median_val, line_dash="dash", line_color="orange", annotation_text="Median", annotation_position="top right")
216
+ fig.add_hline(y=avg_val, line_dash="dashdot", line_color="green", annotation_text="Avg", annotation_position="top right")
217
+ fig.add_hline(y=max_val, line_dash="dot", line_color="blue", annotation_text="Max", annotation_position="top right")
218
+
219
+ elif chart_type == "box":
220
+ # Box plots already show distribution (no extra stats needed)
221
+ pass
222
+
223
+ elif chart_type == "pie":
224
+ # Pie charts don't need statistical overlays
225
+ st.info("πŸ“Š Pie charts focus on proportions. No additional stats displayed.")
226
+
227
+ else:
228
+ st.warning(f"⚠️ No stats added for unsupported chart type: {chart_type}")
229
+
230
+ return fig
231
+
232
+
233
  # Function to create PDF with report text and visualizations
234
  def create_pdf_report_with_viz(report, conclusion, visualizations):
235
  pdf = FPDF()
 
419
  st.markdown(report_result if report_result else "⚠️ No Report Generated.")
420
 
421
  # Step 4: Generate Visualizations
 
422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
 
424
  # Step 5: Insert Visual Insights
425
  st.markdown("### Visual Insights")
426
+
 
427
 
428
  # Step 6: Display Concise Conclusion
429
  #st.markdown("#### Conclusion")
 
457
  # Sidebar Reference
458
  with st.sidebar:
459
  st.header("πŸ“š Reference:")
460
+ st.markdown("[SQL Agents w CrewAI & Llama 3 - Plaban Nayak](https://github.com/plaban1981/Agents/blob/main/SQL_Agents_with_CrewAI_and_Llama_3.ipynb)")