Spaces:
Sleeping
Sleeping
Update dummy_funcs.py
Browse files- dummy_funcs.py +137 -0
dummy_funcs.py
CHANGED
|
@@ -214,3 +214,140 @@ def handle_visualization_suggestions(suggestions, df):
|
|
| 214 |
# Display all generated visualizations
|
| 215 |
for fig in visualizations:
|
| 216 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
# Display all generated visualizations
|
| 215 |
for fig in visualizations:
|
| 216 |
st.plotly_chart(fig, use_container_width=True)
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
-----------------
|
| 223 |
+
|
| 224 |
+
def ask_gpt4o_for_visualization(query, df, llm, retries=2):
|
| 225 |
+
import json
|
| 226 |
+
|
| 227 |
+
# Identify numeric and categorical columns
|
| 228 |
+
numeric_columns = df.select_dtypes(include='number').columns.tolist()
|
| 229 |
+
categorical_columns = df.select_dtypes(exclude='number').columns.tolist()
|
| 230 |
+
|
| 231 |
+
# Enhanced Prompt with More Examples
|
| 232 |
+
prompt = f"""
|
| 233 |
+
Analyze the following query and suggest the most suitable visualization(s) using the dataset.
|
| 234 |
+
|
| 235 |
+
**Query:** "{query}"
|
| 236 |
+
|
| 237 |
+
**Numeric Columns (for Y-axis):** {', '.join(numeric_columns) if numeric_columns else 'None'}
|
| 238 |
+
**Categorical Columns (for X-axis or grouping):** {', '.join(categorical_columns) if categorical_columns else 'None'}
|
| 239 |
+
|
| 240 |
+
Suggest visualizations in this exact JSON format:
|
| 241 |
+
[
|
| 242 |
+
{{
|
| 243 |
+
"chart_type": "bar/box/line/scatter/pie/heatmap",
|
| 244 |
+
"x_axis": "categorical_or_time_column",
|
| 245 |
+
"y_axis": "numeric_column",
|
| 246 |
+
"group_by": "optional_column_for_grouping",
|
| 247 |
+
"title": "Title of the chart",
|
| 248 |
+
"description": "Why this chart is suitable"
|
| 249 |
+
}}
|
| 250 |
+
]
|
| 251 |
+
|
| 252 |
+
**Examples:**
|
| 253 |
+
- For salary distribution:
|
| 254 |
+
{{
|
| 255 |
+
"chart_type": "box",
|
| 256 |
+
"x_axis": "job_title",
|
| 257 |
+
"y_axis": "salary_in_usd",
|
| 258 |
+
"group_by": "experience_level",
|
| 259 |
+
"title": "Salary Distribution by Job Title and Experience",
|
| 260 |
+
"description": "A box plot showing salary ranges across job titles and experience levels."
|
| 261 |
+
}}
|
| 262 |
+
|
| 263 |
+
- For company size comparison:
|
| 264 |
+
{{
|
| 265 |
+
"chart_type": "bar",
|
| 266 |
+
"x_axis": "company_size",
|
| 267 |
+
"y_axis": "salary_in_usd",
|
| 268 |
+
"group_by": null,
|
| 269 |
+
"title": "Average Salary by Company Size",
|
| 270 |
+
"description": "A bar chart comparing the average salaries across different company sizes."
|
| 271 |
+
}}
|
| 272 |
+
|
| 273 |
+
- For revenue trends over time:
|
| 274 |
+
{{
|
| 275 |
+
"chart_type": "line",
|
| 276 |
+
"x_axis": "year",
|
| 277 |
+
"y_axis": "revenue",
|
| 278 |
+
"group_by": null,
|
| 279 |
+
"title": "Revenue Growth Over Years",
|
| 280 |
+
"description": "A line chart showing the trend of revenue over the years."
|
| 281 |
+
}}
|
| 282 |
+
|
| 283 |
+
- For market share breakdown:
|
| 284 |
+
{{
|
| 285 |
+
"chart_type": "pie",
|
| 286 |
+
"x_axis": "market_segment",
|
| 287 |
+
"y_axis": null,
|
| 288 |
+
"group_by": null,
|
| 289 |
+
"title": "Market Share by Segment",
|
| 290 |
+
"description": "A pie chart showing the distribution of market share across various segments."
|
| 291 |
+
}}
|
| 292 |
+
|
| 293 |
+
- For correlation analysis:
|
| 294 |
+
{{
|
| 295 |
+
"chart_type": "scatter",
|
| 296 |
+
"x_axis": "years_of_experience",
|
| 297 |
+
"y_axis": "salary_in_usd",
|
| 298 |
+
"group_by": "job_title",
|
| 299 |
+
"title": "Experience vs Salary by Job Title",
|
| 300 |
+
"description": "A scatter plot showing the relationship between years of experience and salary across job titles."
|
| 301 |
+
}}
|
| 302 |
+
|
| 303 |
+
- For data density:
|
| 304 |
+
{{
|
| 305 |
+
"chart_type": "heatmap",
|
| 306 |
+
"x_axis": "department",
|
| 307 |
+
"y_axis": "region",
|
| 308 |
+
"group_by": null,
|
| 309 |
+
"title": "Employee Distribution by Department and Region",
|
| 310 |
+
"description": "A heatmap showing the concentration of employees across departments and regions."
|
| 311 |
+
}}
|
| 312 |
+
|
| 313 |
+
Only suggest visualizations that make sense for the data and the query.
|
| 314 |
+
"""
|
| 315 |
+
|
| 316 |
+
for attempt in range(retries + 1):
|
| 317 |
+
try:
|
| 318 |
+
# Generate response from the model
|
| 319 |
+
response = llm.generate(prompt)
|
| 320 |
+
|
| 321 |
+
# Load JSON response
|
| 322 |
+
suggestions = json.loads(response)
|
| 323 |
+
|
| 324 |
+
# Validate response structure
|
| 325 |
+
if isinstance(suggestions, list):
|
| 326 |
+
valid_suggestions = [
|
| 327 |
+
s for s in suggestions if all(k in s for k in ["chart_type", "x_axis", "y_axis"])
|
| 328 |
+
]
|
| 329 |
+
if valid_suggestions:
|
| 330 |
+
return valid_suggestions
|
| 331 |
+
else:
|
| 332 |
+
st.warning("β οΈ GPT-4o did not suggest valid visualizations.")
|
| 333 |
+
return None
|
| 334 |
+
|
| 335 |
+
elif isinstance(suggestions, dict):
|
| 336 |
+
if all(k in suggestions for k in ["chart_type", "x_axis", "y_axis"]):
|
| 337 |
+
return [suggestions]
|
| 338 |
+
else:
|
| 339 |
+
st.warning("β οΈ GPT-4o's suggestion is incomplete.")
|
| 340 |
+
return None
|
| 341 |
+
|
| 342 |
+
except json.JSONDecodeError:
|
| 343 |
+
st.warning(f"β οΈ Attempt {attempt + 1}: GPT-4o returned invalid JSON.")
|
| 344 |
+
except Exception as e:
|
| 345 |
+
st.error(f"β οΈ Error during GPT-4o call: {e}")
|
| 346 |
+
|
| 347 |
+
# Retry if necessary
|
| 348 |
+
if attempt < retries:
|
| 349 |
+
st.info("π Retrying visualization suggestion...")
|
| 350 |
+
|
| 351 |
+
st.error("β Failed to generate a valid visualization after multiple attempts.")
|
| 352 |
+
return None
|
| 353 |
+
|