Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -115,26 +115,32 @@ def generate_visualization(suggestion, df):
|
|
115 |
y_axis = suggestion.get("y_axis")
|
116 |
group_by = suggestion.get("group_by")
|
117 |
|
118 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
if not x_axis or not y_axis:
|
120 |
-
st.warning("⚠️
|
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
|
132 |
-
plot_args = {
|
133 |
-
|
134 |
-
"x": x_axis,
|
135 |
-
"y": y_axis,
|
136 |
-
}
|
137 |
-
if group_by:
|
138 |
plot_args["color"] = group_by
|
139 |
|
140 |
try:
|
@@ -145,12 +151,17 @@ def generate_visualization(suggestion, df):
|
|
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 |
def add_stats_to_figure(fig, df, y_axis, chart_type):
|
155 |
# Calculate statistics
|
156 |
min_val = df[y_axis].min()
|
|
|
115 |
y_axis = suggestion.get("y_axis")
|
116 |
group_by = suggestion.get("group_by")
|
117 |
|
118 |
+
# Dynamically determine the best Y-axis if GPT-4o doesn't suggest one
|
119 |
+
if not y_axis:
|
120 |
+
numeric_columns = df.select_dtypes(include='number').columns.tolist()
|
121 |
+
|
122 |
+
if x_axis in numeric_columns:
|
123 |
+
# Avoid using the same column for both axes
|
124 |
+
numeric_columns.remove(x_axis)
|
125 |
+
|
126 |
+
# Prioritize the first available numeric column for y-axis
|
127 |
+
y_axis = numeric_columns[0] if numeric_columns else None
|
128 |
+
|
129 |
+
# Ensure both axes are identified
|
130 |
if not x_axis or not y_axis:
|
131 |
+
st.warning("⚠️ Unable to determine relevant columns for visualization.")
|
132 |
return None
|
133 |
|
134 |
# Dynamically select the Plotly function
|
135 |
plotly_function = getattr(px, chart_type, None)
|
136 |
|
|
|
137 |
if not plotly_function:
|
138 |
st.warning(f"⚠️ Unsupported chart type '{chart_type}' suggested by GPT-4o.")
|
139 |
return None
|
140 |
|
141 |
+
# Prepare dynamic plot arguments
|
142 |
+
plot_args = {"data_frame": df, "x": x_axis, "y": y_axis}
|
143 |
+
if group_by and group_by in df.columns:
|
|
|
|
|
|
|
|
|
144 |
plot_args["color"] = group_by
|
145 |
|
146 |
try:
|
|
|
151 |
xaxis_title=x_axis.replace('_', ' ').title(),
|
152 |
yaxis_title=y_axis.replace('_', ' ').title(),
|
153 |
)
|
154 |
+
|
155 |
+
# Apply statistics intelligently
|
156 |
+
fig = add_stats_to_figure(fig, df, y_axis, chart_type)
|
157 |
+
|
158 |
return fig
|
159 |
|
160 |
except Exception as e:
|
161 |
st.error(f"⚠️ Failed to generate visualization: {e}")
|
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()
|