Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -194,41 +194,52 @@ def add_stats_to_figure(fig, df, y_axis, chart_type):
|
|
| 194 |
|
| 195 |
# Dynamically generate Plotly visualizations based on GPT-4o suggestions
|
| 196 |
def generate_visualization(suggestion, df):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
chart_type = suggestion.get("chart_type", "bar").lower()
|
| 198 |
x_axis = suggestion.get("x_axis")
|
| 199 |
y_axis = suggestion.get("y_axis")
|
| 200 |
group_by = suggestion.get("group_by")
|
| 201 |
|
| 202 |
-
#
|
| 203 |
if not y_axis:
|
| 204 |
numeric_columns = df.select_dtypes(include='number').columns.tolist()
|
| 205 |
-
|
|
|
|
| 206 |
if x_axis in numeric_columns:
|
| 207 |
-
# Avoid using the same column for both axes
|
| 208 |
numeric_columns.remove(x_axis)
|
| 209 |
-
|
| 210 |
-
# Prioritize the first available numeric column for y-axis
|
| 211 |
-
y_axis = numeric_columns[0] if numeric_columns else None
|
| 212 |
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
if not x_axis or not y_axis:
|
| 215 |
-
st.warning("β οΈ Unable to determine
|
| 216 |
return None
|
| 217 |
|
| 218 |
-
# Dynamically select the Plotly function
|
| 219 |
plotly_function = getattr(px, chart_type, None)
|
| 220 |
-
|
| 221 |
if not plotly_function:
|
| 222 |
st.warning(f"β οΈ Unsupported chart type '{chart_type}' suggested by GPT-4o.")
|
| 223 |
return None
|
| 224 |
|
| 225 |
-
# Prepare dynamic plot arguments
|
| 226 |
plot_args = {"data_frame": df, "x": x_axis, "y": y_axis}
|
| 227 |
if group_by and group_by in df.columns:
|
| 228 |
plot_args["color"] = group_by
|
| 229 |
|
| 230 |
try:
|
| 231 |
-
# Generate the
|
| 232 |
fig = plotly_function(**plot_args)
|
| 233 |
fig.update_layout(
|
| 234 |
title=f"{chart_type.title()} Plot of {y_axis.replace('_', ' ').title()} by {x_axis.replace('_', ' ').title()}",
|
|
@@ -236,8 +247,8 @@ def generate_visualization(suggestion, df):
|
|
| 236 |
yaxis_title=y_axis.replace('_', ' ').title(),
|
| 237 |
)
|
| 238 |
|
| 239 |
-
# Apply statistics intelligently
|
| 240 |
-
fig =
|
| 241 |
|
| 242 |
return fig
|
| 243 |
|
|
@@ -245,6 +256,7 @@ def generate_visualization(suggestion, df):
|
|
| 245 |
st.error(f"β οΈ Failed to generate visualization: {e}")
|
| 246 |
return None
|
| 247 |
|
|
|
|
| 248 |
def generate_multiple_visualizations(suggestions, df):
|
| 249 |
"""
|
| 250 |
Generates one or more visualizations based on GPT-4o's suggestions.
|
|
|
|
| 194 |
|
| 195 |
# Dynamically generate Plotly visualizations based on GPT-4o suggestions
|
| 196 |
def generate_visualization(suggestion, df):
|
| 197 |
+
"""
|
| 198 |
+
Generate a Plotly visualization based on GPT-4o's suggestion.
|
| 199 |
+
If the Y-axis is missing, infer it intelligently.
|
| 200 |
+
"""
|
| 201 |
chart_type = suggestion.get("chart_type", "bar").lower()
|
| 202 |
x_axis = suggestion.get("x_axis")
|
| 203 |
y_axis = suggestion.get("y_axis")
|
| 204 |
group_by = suggestion.get("group_by")
|
| 205 |
|
| 206 |
+
# Step 1: Infer Y-axis if not provided
|
| 207 |
if not y_axis:
|
| 208 |
numeric_columns = df.select_dtypes(include='number').columns.tolist()
|
| 209 |
+
|
| 210 |
+
# Avoid using the same column for both axes
|
| 211 |
if x_axis in numeric_columns:
|
|
|
|
| 212 |
numeric_columns.remove(x_axis)
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
+
# Smart guess: prioritize salary or relevant metrics if available
|
| 215 |
+
priority_columns = ["salary_in_usd", "income", "earnings", "revenue"]
|
| 216 |
+
for col in priority_columns:
|
| 217 |
+
if col in numeric_columns:
|
| 218 |
+
y_axis = col
|
| 219 |
+
break
|
| 220 |
+
|
| 221 |
+
# Fallback to the first numeric column if no priority columns exist
|
| 222 |
+
if not y_axis and numeric_columns:
|
| 223 |
+
y_axis = numeric_columns[0]
|
| 224 |
+
|
| 225 |
+
# Step 2: Validate axes
|
| 226 |
if not x_axis or not y_axis:
|
| 227 |
+
st.warning("β οΈ Unable to determine appropriate columns for visualization.")
|
| 228 |
return None
|
| 229 |
|
| 230 |
+
# Step 3: Dynamically select the Plotly function
|
| 231 |
plotly_function = getattr(px, chart_type, None)
|
|
|
|
| 232 |
if not plotly_function:
|
| 233 |
st.warning(f"β οΈ Unsupported chart type '{chart_type}' suggested by GPT-4o.")
|
| 234 |
return None
|
| 235 |
|
| 236 |
+
# Step 4: Prepare dynamic plot arguments
|
| 237 |
plot_args = {"data_frame": df, "x": x_axis, "y": y_axis}
|
| 238 |
if group_by and group_by in df.columns:
|
| 239 |
plot_args["color"] = group_by
|
| 240 |
|
| 241 |
try:
|
| 242 |
+
# Step 5: Generate the visualization
|
| 243 |
fig = plotly_function(**plot_args)
|
| 244 |
fig.update_layout(
|
| 245 |
title=f"{chart_type.title()} Plot of {y_axis.replace('_', ' ').title()} by {x_axis.replace('_', ' ').title()}",
|
|
|
|
| 247 |
yaxis_title=y_axis.replace('_', ' ').title(),
|
| 248 |
)
|
| 249 |
|
| 250 |
+
# Step 6: Apply statistics intelligently
|
| 251 |
+
fig = add_statistics_to_visualization(fig, df, y_axis, chart_type)
|
| 252 |
|
| 253 |
return fig
|
| 254 |
|
|
|
|
| 256 |
st.error(f"β οΈ Failed to generate visualization: {e}")
|
| 257 |
return None
|
| 258 |
|
| 259 |
+
|
| 260 |
def generate_multiple_visualizations(suggestions, df):
|
| 261 |
"""
|
| 262 |
Generates one or more visualizations based on GPT-4o's suggestions.
|