bluenevus commited on
Commit
8e7c411
·
verified ·
1 Parent(s): 5659852

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -7,7 +7,7 @@ from PIL import Image, ImageDraw
7
  import google.generativeai as genai
8
  import traceback
9
 
10
- def process_file(file, instructions, api_key):
11
  try:
12
  # Initialize Gemini
13
  genai.configure(api_key=api_key)
@@ -24,9 +24,10 @@ def process_file(file, instructions, api_key):
24
  Data columns: {list(df.columns)}
25
  Data shape: {df.shape}
26
  Instructions: {instructions}
 
27
 
28
- Based on this, create 3 appropriate visualizations that provide meaningful insights. For each visualization:
29
- 1. Choose the most suitable plot type (bar, line, scatter, hist, pie, heatmap)
30
  2. Determine appropriate data aggregation (e.g., top 5 categories, monthly averages)
31
  3. Select relevant columns for x-axis, y-axis, and any additional dimensions (color, size)
32
  4. Provide a clear, concise title that explains the insight
@@ -36,9 +37,9 @@ def process_file(file, instructions, api_key):
36
 
37
  Return your response as a Python list of dictionaries:
38
  [
39
- {{"title": "...", "plot_type": "...", "x": "...", "y": "...", "agg_func": "...", "top_n": ..., "additional": {{"color": "...", "size": "..."}}}},
40
- {{"title": "...", "plot_type": "...", "x": "...", "y": "...", "agg_func": "...", "top_n": ..., "additional": {{"color": "...", "size": "..."}}}},
41
- {{"title": "...", "plot_type": "...", "x": "...", "y": "...", "agg_func": "...", "top_n": ..., "additional": {{"color": "...", "size": "..."}}}}
42
  ]
43
  """)
44
 
@@ -71,18 +72,18 @@ def process_file(file, instructions, api_key):
71
  if 'top_n' in plot and plot['top_n']:
72
  plot_df = plot_df.nlargest(plot['top_n'], plot['y'])
73
 
74
- if plot['plot_type'] == 'bar':
75
  plot_df.plot(kind='bar', x=plot['x'], y=plot['y'], ax=ax)
76
- elif plot['plot_type'] == 'line':
77
  plot_df.plot(kind='line', x=plot['x'], y=plot['y'], ax=ax)
78
- elif plot['plot_type'] == 'scatter':
79
  plot_df.plot(kind='scatter', x=plot['x'], y=plot['y'], ax=ax,
80
  c=plot['additional'].get('color'), s=plot_df[plot['additional'].get('size', 'y')])
81
- elif plot['plot_type'] == 'hist':
82
  plot_df[plot['x']].hist(ax=ax, bins=20)
83
- elif plot['plot_type'] == 'pie':
84
  plot_df.plot(kind='pie', y=plot['y'], labels=plot_df[plot['x']], ax=ax, autopct='%1.1f%%')
85
- elif plot['plot_type'] == 'heatmap':
86
  pivot_df = plot_df.pivot(index=plot['x'], columns=plot['additional']['color'], values=plot['y'])
87
  ax.imshow(pivot_df, cmap='YlOrRd')
88
  ax.set_xticks(range(len(pivot_df.columns)))
@@ -91,7 +92,7 @@ def process_file(file, instructions, api_key):
91
  ax.set_yticklabels(pivot_df.index)
92
 
93
  ax.set_title(plot['title'])
94
- if plot['plot_type'] != 'pie':
95
  ax.set_xlabel(plot['x'])
96
  ax.set_ylabel(plot['y'])
97
  plt.tight_layout()
@@ -121,13 +122,14 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
121
  instructions = gr.Textbox(label="Analysis Instructions", placeholder="Describe the analysis you want...")
122
 
123
  api_key = gr.Textbox(label="Gemini API Key", type="password")
 
124
  submit = gr.Button("Generate Insights", variant="primary")
125
 
126
  output_images = [gr.Image(label=f"Visualization {i+1}") for i in range(3)]
127
 
128
  submit.click(
129
  process_file,
130
- inputs=[file, instructions, api_key],
131
  outputs=output_images
132
  )
133
 
 
7
  import google.generativeai as genai
8
  import traceback
9
 
10
+ def process_file(file, instructions, api_key, graph_type):
11
  try:
12
  # Initialize Gemini
13
  genai.configure(api_key=api_key)
 
24
  Data columns: {list(df.columns)}
25
  Data shape: {df.shape}
26
  Instructions: {instructions}
27
+ Graph Type: {graph_type}
28
 
29
+ Based on this, create 3 appropriate visualizations of the specified type that provide meaningful insights. For each visualization:
30
+ 1. Use the specified plot type: {graph_type}
31
  2. Determine appropriate data aggregation (e.g., top 5 categories, monthly averages)
32
  3. Select relevant columns for x-axis, y-axis, and any additional dimensions (color, size)
33
  4. Provide a clear, concise title that explains the insight
 
37
 
38
  Return your response as a Python list of dictionaries:
39
  [
40
+ {{"title": "...", "plot_type": "{graph_type}", "x": "...", "y": "...", "agg_func": "...", "top_n": ..., "additional": {{"color": "...", "size": "..."}}}},
41
+ {{"title": "...", "plot_type": "{graph_type}", "x": "...", "y": "...", "agg_func": "...", "top_n": ..., "additional": {{"color": "...", "size": "..."}}}},
42
+ {{"title": "...", "plot_type": "{graph_type}", "x": "...", "y": "...", "agg_func": "...", "top_n": ..., "additional": {{"color": "...", "size": "..."}}}}
43
  ]
44
  """)
45
 
 
72
  if 'top_n' in plot and plot['top_n']:
73
  plot_df = plot_df.nlargest(plot['top_n'], plot['y'])
74
 
75
+ if graph_type == 'bar':
76
  plot_df.plot(kind='bar', x=plot['x'], y=plot['y'], ax=ax)
77
+ elif graph_type == 'line':
78
  plot_df.plot(kind='line', x=plot['x'], y=plot['y'], ax=ax)
79
+ elif graph_type == 'scatter':
80
  plot_df.plot(kind='scatter', x=plot['x'], y=plot['y'], ax=ax,
81
  c=plot['additional'].get('color'), s=plot_df[plot['additional'].get('size', 'y')])
82
+ elif graph_type == 'hist':
83
  plot_df[plot['x']].hist(ax=ax, bins=20)
84
+ elif graph_type == 'pie':
85
  plot_df.plot(kind='pie', y=plot['y'], labels=plot_df[plot['x']], ax=ax, autopct='%1.1f%%')
86
+ elif graph_type == 'heatmap':
87
  pivot_df = plot_df.pivot(index=plot['x'], columns=plot['additional']['color'], values=plot['y'])
88
  ax.imshow(pivot_df, cmap='YlOrRd')
89
  ax.set_xticks(range(len(pivot_df.columns)))
 
92
  ax.set_yticklabels(pivot_df.index)
93
 
94
  ax.set_title(plot['title'])
95
+ if graph_type != 'pie':
96
  ax.set_xlabel(plot['x'])
97
  ax.set_ylabel(plot['y'])
98
  plt.tight_layout()
 
122
  instructions = gr.Textbox(label="Analysis Instructions", placeholder="Describe the analysis you want...")
123
 
124
  api_key = gr.Textbox(label="Gemini API Key", type="password")
125
+ graph_type = gr.Dropdown(choices=["bar", "line", "scatter", "hist", "pie", "heatmap"], label="Graph Type")
126
  submit = gr.Button("Generate Insights", variant="primary")
127
 
128
  output_images = [gr.Image(label=f"Visualization {i+1}") for i in range(3)]
129
 
130
  submit.click(
131
  process_file,
132
+ inputs=[file, instructions, api_key, graph_type],
133
  outputs=output_images
134
  )
135