DrishtiSharma commited on
Commit
9fca070
Β·
verified Β·
1 Parent(s): 50e8a3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -53
app.py CHANGED
@@ -108,6 +108,60 @@ def ask_gpt4o_for_visualization(query, df, llm):
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()
@@ -162,59 +216,6 @@ def generate_visualization(suggestion, df):
162
  return None
163
 
164
 
165
- def add_stats_to_figure(fig, df, y_axis, chart_type):
166
- # Calculate statistics
167
- min_val = df[y_axis].min()
168
- max_val = df[y_axis].max()
169
- avg_val = df[y_axis].mean()
170
- median_val = df[y_axis].median()
171
- std_dev_val = df[y_axis].std()
172
-
173
- # Stats summary text
174
- stats_text = (
175
- f"πŸ“Š **Statistics**\n\n"
176
- f"- **Min:** ${min_val:,.2f}\n"
177
- f"- **Max:** ${max_val:,.2f}\n"
178
- f"- **Average:** ${avg_val:,.2f}\n"
179
- f"- **Median:** ${median_val:,.2f}\n"
180
- f"- **Std Dev:** ${std_dev_val:,.2f}"
181
- )
182
-
183
- # Charts suitable for stats annotations
184
- if chart_type in ["bar", "line", "scatter"]:
185
- # Add annotation box
186
- fig.add_annotation(
187
- text=stats_text,
188
- xref="paper", yref="paper",
189
- x=1.05, y=1,
190
- showarrow=False,
191
- align="left",
192
- font=dict(size=12, color="black"),
193
- bordercolor="black",
194
- borderwidth=1,
195
- bgcolor="rgba(255, 255, 255, 0.8)"
196
- )
197
-
198
- # Add horizontal lines for min, median, avg, max
199
- fig.add_hline(y=min_val, line_dash="dot", line_color="red", annotation_text="Min", annotation_position="bottom right")
200
- fig.add_hline(y=median_val, line_dash="dash", line_color="orange", annotation_text="Median", annotation_position="top right")
201
- fig.add_hline(y=avg_val, line_dash="dashdot", line_color="green", annotation_text="Avg", annotation_position="top right")
202
- fig.add_hline(y=max_val, line_dash="dot", line_color="blue", annotation_text="Max", annotation_position="top right")
203
-
204
- elif chart_type == "box":
205
- # Box plots already show distribution (no extra stats needed)
206
- pass
207
-
208
- elif chart_type == "pie":
209
- # Pie charts don't need statistical overlays
210
- st.info("πŸ“Š Pie charts focus on proportions. No additional stats displayed.")
211
-
212
- else:
213
- st.warning(f"⚠️ No stats added for unsupported chart type: {chart_type}")
214
-
215
- return fig
216
-
217
-
218
  # Function to create TXT file
219
  def create_text_report_with_viz_temp(report, conclusion, visualizations):
220
  content = f"### Analysis Report\n\n{report}\n\n### Visualizations\n"
 
108
  st.error("⚠️ GPT-4o failed to generate a valid suggestion.")
109
  return None
110
 
111
+ def add_stats_to_figure(fig, df, y_axis, chart_type):
112
+ # Calculate statistics
113
+ min_val = df[y_axis].min()
114
+ max_val = df[y_axis].max()
115
+ avg_val = df[y_axis].mean()
116
+ median_val = df[y_axis].median()
117
+ std_dev_val = df[y_axis].std()
118
+
119
+ # Stats summary text
120
+ stats_text = (
121
+ f"πŸ“Š **Statistics**\n\n"
122
+ f"- **Min:** ${min_val:,.2f}\n"
123
+ f"- **Max:** ${max_val:,.2f}\n"
124
+ f"- **Average:** ${avg_val:,.2f}\n"
125
+ f"- **Median:** ${median_val:,.2f}\n"
126
+ f"- **Std Dev:** ${std_dev_val:,.2f}"
127
+ )
128
+
129
+ # Charts suitable for stats annotations
130
+ if chart_type in ["bar", "line", "scatter"]:
131
+ # Add annotation box
132
+ fig.add_annotation(
133
+ text=stats_text,
134
+ xref="paper", yref="paper",
135
+ x=1.05, y=1,
136
+ showarrow=False,
137
+ align="left",
138
+ font=dict(size=12, color="black"),
139
+ bordercolor="black",
140
+ borderwidth=1,
141
+ bgcolor="rgba(255, 255, 255, 0.8)"
142
+ )
143
+
144
+ # Add horizontal lines for min, median, avg, max
145
+ fig.add_hline(y=min_val, line_dash="dot", line_color="red", annotation_text="Min", annotation_position="bottom right")
146
+ fig.add_hline(y=median_val, line_dash="dash", line_color="orange", annotation_text="Median", annotation_position="top right")
147
+ fig.add_hline(y=avg_val, line_dash="dashdot", line_color="green", annotation_text="Avg", annotation_position="top right")
148
+ fig.add_hline(y=max_val, line_dash="dot", line_color="blue", annotation_text="Max", annotation_position="top right")
149
+
150
+ elif chart_type == "box":
151
+ # Box plots already show distribution (no extra stats needed)
152
+ pass
153
+
154
+ elif chart_type == "pie":
155
+ # Pie charts don't need statistical overlays
156
+ st.info("πŸ“Š Pie charts focus on proportions. No additional stats displayed.")
157
+
158
+ else:
159
+ st.warning(f"⚠️ No stats added for unsupported chart type: {chart_type}")
160
+
161
+ return fig
162
+
163
+
164
+
165
  # Dynamically generate Plotly visualizations based on GPT-4o suggestions
166
  def generate_visualization(suggestion, df):
167
  chart_type = suggestion.get("chart_type", "bar").lower()
 
216
  return None
217
 
218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
  # Function to create TXT file
220
  def create_text_report_with_viz_temp(report, conclusion, visualizations):
221
  content = f"### Analysis Report\n\n{report}\n\n### Visualizations\n"