Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -109,14 +109,23 @@ def ask_gpt4o_for_visualization(query, df, llm):
|
|
109 |
return None
|
110 |
|
111 |
def add_stats_to_figure(fig, df, y_axis, chart_type):
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
120 |
stats_text = (
|
121 |
f"π **Statistics**\n\n"
|
122 |
f"- **Min:** ${min_val:,.2f}\n"
|
@@ -126,42 +135,59 @@ def add_stats_to_figure(fig, df, y_axis, chart_type):
|
|
126 |
f"- **Std Dev:** ${std_dev_val:,.2f}"
|
127 |
)
|
128 |
|
129 |
-
#
|
130 |
-
if chart_type in ["bar", "line"
|
131 |
-
# Add annotation box
|
132 |
fig.add_annotation(
|
133 |
text=stats_text,
|
134 |
xref="paper", yref="paper",
|
135 |
-
x=1.
|
136 |
showarrow=False,
|
137 |
align="left",
|
138 |
font=dict(size=12, color="black"),
|
139 |
-
bordercolor="
|
140 |
borderwidth=1,
|
141 |
-
bgcolor="rgba(255, 255, 255, 0.
|
142 |
)
|
143 |
|
144 |
-
# Add horizontal lines
|
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
|
152 |
pass
|
153 |
|
154 |
elif chart_type == "pie":
|
155 |
-
# Pie charts
|
156 |
-
st.info("π Pie charts
|
157 |
-
|
|
|
|
|
|
|
|
|
158 |
else:
|
159 |
-
st.warning(f"β οΈ No
|
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()
|
|
|
109 |
return None
|
110 |
|
111 |
def add_stats_to_figure(fig, df, y_axis, chart_type):
|
112 |
+
"""
|
113 |
+
Add relevant statistical annotations to the visualization
|
114 |
+
based on the chart type.
|
115 |
+
"""
|
116 |
+
# Check if the y-axis column is numeric
|
117 |
+
if not pd.api.types.is_numeric_dtype(df[y_axis]):
|
118 |
+
st.warning(f"β οΈ Cannot compute statistics for non-numeric column: {y_axis}")
|
119 |
+
return fig
|
120 |
+
|
121 |
+
# Compute statistics for numeric data
|
122 |
min_val = df[y_axis].min()
|
123 |
max_val = df[y_axis].max()
|
124 |
avg_val = df[y_axis].mean()
|
125 |
median_val = df[y_axis].median()
|
126 |
std_dev_val = df[y_axis].std()
|
127 |
|
128 |
+
# Format the stats for display
|
129 |
stats_text = (
|
130 |
f"π **Statistics**\n\n"
|
131 |
f"- **Min:** ${min_val:,.2f}\n"
|
|
|
135 |
f"- **Std Dev:** ${std_dev_val:,.2f}"
|
136 |
)
|
137 |
|
138 |
+
# Apply stats only to relevant chart types
|
139 |
+
if chart_type in ["bar", "line"]:
|
140 |
+
# Add annotation box for bar and line charts
|
141 |
fig.add_annotation(
|
142 |
text=stats_text,
|
143 |
xref="paper", yref="paper",
|
144 |
+
x=1.02, y=1,
|
145 |
showarrow=False,
|
146 |
align="left",
|
147 |
font=dict(size=12, color="black"),
|
148 |
+
bordercolor="gray",
|
149 |
borderwidth=1,
|
150 |
+
bgcolor="rgba(255, 255, 255, 0.85)"
|
151 |
)
|
152 |
|
153 |
+
# Add horizontal reference lines
|
154 |
fig.add_hline(y=min_val, line_dash="dot", line_color="red", annotation_text="Min", annotation_position="bottom right")
|
155 |
fig.add_hline(y=median_val, line_dash="dash", line_color="orange", annotation_text="Median", annotation_position="top right")
|
156 |
fig.add_hline(y=avg_val, line_dash="dashdot", line_color="green", annotation_text="Avg", annotation_position="top right")
|
157 |
fig.add_hline(y=max_val, line_dash="dot", line_color="blue", annotation_text="Max", annotation_position="top right")
|
158 |
|
159 |
+
elif chart_type == "scatter":
|
160 |
+
# Add stats annotation only, no lines for scatter plots
|
161 |
+
fig.add_annotation(
|
162 |
+
text=stats_text,
|
163 |
+
xref="paper", yref="paper",
|
164 |
+
x=1.02, y=1,
|
165 |
+
showarrow=False,
|
166 |
+
align="left",
|
167 |
+
font=dict(size=12, color="black"),
|
168 |
+
bordercolor="gray",
|
169 |
+
borderwidth=1,
|
170 |
+
bgcolor="rgba(255, 255, 255, 0.85)"
|
171 |
+
)
|
172 |
+
|
173 |
elif chart_type == "box":
|
174 |
+
# Box plots inherently show distribution; no extra stats needed
|
175 |
pass
|
176 |
|
177 |
elif chart_type == "pie":
|
178 |
+
# Pie charts represent proportions, not suitable for stats
|
179 |
+
st.info("π Pie charts represent proportions. Additional stats are not applicable.")
|
180 |
+
|
181 |
+
elif chart_type == "heatmap":
|
182 |
+
# Heatmaps already reflect data intensity
|
183 |
+
st.info("π Heatmaps inherently reflect distribution. No additional stats added.")
|
184 |
+
|
185 |
else:
|
186 |
+
st.warning(f"β οΈ No statistical overlays applied for unsupported chart type: '{chart_type}'.")
|
187 |
|
188 |
return fig
|
189 |
|
190 |
|
|
|
191 |
# Dynamically generate Plotly visualizations based on GPT-4o suggestions
|
192 |
def generate_visualization(suggestion, df):
|
193 |
chart_type = suggestion.get("chart_type", "bar").lower()
|