DrishtiSharma commited on
Commit
53bbc86
Β·
verified Β·
1 Parent(s): 8ecac18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -10
app.py CHANGED
@@ -86,27 +86,31 @@ if st.session_state.df is not None and st.session_state.show_preview:
86
  st.subheader("πŸ“‚ Dataset Preview")
87
  st.dataframe(st.session_state.df.head())
88
 
89
- """# Ask GPT-4o for Visualization Suggestions
 
 
90
  def ask_gpt4o_for_visualization(query, df, llm):
91
  columns = ', '.join(df.columns)
92
  prompt = f"""
93
- Analyze the query and suggest the best visualization.
94
  Query: "{query}"
95
  Available Columns: {columns}
96
- Respond in this JSON format:
97
- {{
98
- "chart_type": "bar/box/line/scatter",
99
- "x_axis": "column_name",
100
- "y_axis": "column_name",
101
- "group_by": "optional_column_name"
102
- }}
 
 
103
  """
104
  response = llm.generate(prompt)
105
  try:
106
  return json.loads(response)
107
  except json.JSONDecodeError:
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
  """
@@ -241,6 +245,56 @@ def generate_visualization(suggestion, df):
241
  st.error(f"⚠️ Failed to generate visualization: {e}")
242
  return None
243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
 
245
  # Function to create TXT file
246
  def create_text_report_with_viz_temp(report, conclusion, visualizations):
 
86
  st.subheader("πŸ“‚ Dataset Preview")
87
  st.dataframe(st.session_state.df.head())
88
 
89
+
90
+
91
+
92
  def ask_gpt4o_for_visualization(query, df, llm):
93
  columns = ', '.join(df.columns)
94
  prompt = f"""
95
+ Analyze the query and suggest one or more relevant visualizations.
96
  Query: "{query}"
97
  Available Columns: {columns}
98
+ Respond in this JSON format (as a list if multiple suggestions):
99
+ [
100
+ {{
101
+ "chart_type": "bar/box/line/scatter",
102
+ "x_axis": "column_name",
103
+ "y_axis": "column_name",
104
+ "group_by": "optional_column_name"
105
+ }}
106
+ ]
107
  """
108
  response = llm.generate(prompt)
109
  try:
110
  return json.loads(response)
111
  except json.JSONDecodeError:
112
  st.error("⚠️ GPT-4o failed to generate a valid suggestion.")
113
+ return None
114
 
115
  def add_stats_to_figure(fig, df, y_axis, chart_type):
116
  """
 
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.
251
+ Handles both single and multiple suggestions.
252
+ """
253
+ visualizations = []
254
+
255
+ for suggestion in suggestions:
256
+ fig = generate_visualization(suggestion, df)
257
+ if fig:
258
+ # Apply chart-specific statistics
259
+ fig = add_stats_to_figure(fig, df, suggestion["y_axis"], suggestion["chart_type"])
260
+ visualizations.append(fig)
261
+
262
+ if not visualizations and suggestions:
263
+ st.warning("⚠️ No valid visualization found. Displaying the most relevant one.")
264
+ best_suggestion = suggestions[0]
265
+ fig = generate_visualization(best_suggestion, df)
266
+ fig = add_stats_to_figure(fig, df, best_suggestion["y_axis"], best_suggestion["chart_type"])
267
+ visualizations.append(fig)
268
+
269
+ return visualizations
270
+
271
+
272
+ def handle_visualization_suggestions(suggestions, df):
273
+ """
274
+ Determines whether to generate a single or multiple visualizations.
275
+ """
276
+ visualizations = []
277
+
278
+ # If multiple suggestions, generate multiple plots
279
+ if isinstance(suggestions, list) and len(suggestions) > 1:
280
+ visualizations = generate_multiple_visualizations(suggestions, df)
281
+
282
+ # If only one suggestion, generate a single plot
283
+ elif isinstance(suggestions, dict) or (isinstance(suggestions, list) and len(suggestions) == 1):
284
+ suggestion = suggestions[0] if isinstance(suggestions, list) else suggestions
285
+ fig = generate_visualization(suggestion, df)
286
+ if fig:
287
+ visualizations.append(fig)
288
+
289
+ # Handle cases when no visualization could be generated
290
+ if not visualizations:
291
+ st.warning("⚠️ Unable to generate any visualization based on the suggestion.")
292
+
293
+ # Display all generated visualizations
294
+ for fig in visualizations:
295
+ st.plotly_chart(fig, use_container_width=True)
296
+
297
+
298
 
299
  # Function to create TXT file
300
  def create_text_report_with_viz_temp(report, conclusion, visualizations):