Add bubble charts
Browse files- app.py +3 -1
- functions/chart_functions.py +17 -7
- tools.py +30 -21
app.py
CHANGED
@@ -72,6 +72,7 @@ with gr.Blocks(css=css, delete_cache=(3600,3600)) as demo:
|
|
72 |
["Can you generate a bar chart of education vs. average balance?"],
|
73 |
["Can you generate a table of levels of education versus average balance, percent married, percent with a loan, and percent in default?"],
|
74 |
["Can we predict the relationship between the number of contacts performed before this campaign and the average balance?"],
|
|
|
75 |
]
|
76 |
elif "online_retail_data" in filename:
|
77 |
example_questions = [
|
@@ -80,7 +81,8 @@ with gr.Blocks(css=css, delete_cache=(3600,3600)) as demo:
|
|
80 |
["Is revenue higher in the morning or afternoon?"],
|
81 |
["Can you generate a line graph of revenue per month?"],
|
82 |
["Can you generate a table of revenue per month?"],
|
83 |
-
["Can we predict how time of day affects
|
|
|
84 |
]
|
85 |
else:
|
86 |
try:
|
|
|
72 |
["Can you generate a bar chart of education vs. average balance?"],
|
73 |
["Can you generate a table of levels of education versus average balance, percent married, percent with a loan, and percent in default?"],
|
74 |
["Can we predict the relationship between the number of contacts performed before this campaign and the average balance?"],
|
75 |
+
["Can you plot the number of contacts performed before this campaign versus the duration and use balance as the size in a bubble chart?"]
|
76 |
]
|
77 |
elif "online_retail_data" in filename:
|
78 |
example_questions = [
|
|
|
81 |
["Is revenue higher in the morning or afternoon?"],
|
82 |
["Can you generate a line graph of revenue per month?"],
|
83 |
["Can you generate a table of revenue per month?"],
|
84 |
+
["Can we predict how time of day affects transaction value in this data set?"],
|
85 |
+
["Can you plot revenue per month with size being the number of units sold that month in a bubble chart?"]
|
86 |
]
|
87 |
else:
|
88 |
try:
|
functions/chart_functions.py
CHANGED
@@ -45,7 +45,8 @@ def llm_chart_data_scrub(data, layout):
|
|
45 |
return data_dict, layout_dict
|
46 |
|
47 |
def scatter_chart_fig(df, x_column: List[str], y_column: str, category: str="", trendline: str="",
|
48 |
-
trendline_options: List[dict]=[{}], marginal_x: str="", marginal_y: str=""
|
|
|
49 |
|
50 |
function_args = {"data_frame":df, "x":x_column, "y":y_column}
|
51 |
|
@@ -57,6 +58,9 @@ def scatter_chart_fig(df, x_column: List[str], y_column: str, category: str="",
|
|
57 |
function_args["marginal_x"] = marginal_x
|
58 |
if marginal_y:
|
59 |
function_args["marginal_y"] = marginal_y
|
|
|
|
|
|
|
60 |
if trendline_options:
|
61 |
trendline_options_dict = {}
|
62 |
if trendline_options and isinstance(trendline_options, list):
|
@@ -75,7 +79,8 @@ def scatter_chart_fig(df, x_column: List[str], y_column: str, category: str="",
|
|
75 |
return fig
|
76 |
|
77 |
def scatter_chart_generation_func(x_column: List[str], y_column: str, session_hash, data: List[dict]=[{}], layout: List[dict]=[{}],
|
78 |
-
category: str="", trendline: str="", trendline_options: List[dict]=[{}], marginal_x: str="", marginal_y: str=""
|
|
|
79 |
try:
|
80 |
dir_path = TEMP_DIR / str(session_hash)
|
81 |
chart_path = f'{dir_path}/chart.html'
|
@@ -85,21 +90,26 @@ def scatter_chart_generation_func(x_column: List[str], y_column: str, session_ha
|
|
85 |
|
86 |
initial_graph = scatter_chart_fig(df, x_column=x_column, y_column=y_column,
|
87 |
category=category, trendline=trendline, trendline_options=trendline_options,
|
88 |
-
marginal_x=marginal_x, marginal_y=marginal_y)
|
89 |
|
90 |
fig = initial_graph.to_dict()
|
91 |
|
92 |
-
|
|
|
93 |
|
94 |
-
|
95 |
-
print(layout_dict)
|
96 |
|
97 |
#Applying stylings and settings generated from LLM
|
98 |
if layout_dict:
|
99 |
fig["layout"] = layout_dict
|
100 |
|
|
|
|
|
|
|
|
|
|
|
101 |
for key, value in data_dict.items():
|
102 |
-
if key not in
|
103 |
for data_item in fig["data"]:
|
104 |
data_item[key] = value
|
105 |
|
|
|
45 |
return data_dict, layout_dict
|
46 |
|
47 |
def scatter_chart_fig(df, x_column: List[str], y_column: str, category: str="", trendline: str="",
|
48 |
+
trendline_options: List[dict]=[{}], marginal_x: str="", marginal_y: str="",
|
49 |
+
size: str=""):
|
50 |
|
51 |
function_args = {"data_frame":df, "x":x_column, "y":y_column}
|
52 |
|
|
|
58 |
function_args["marginal_x"] = marginal_x
|
59 |
if marginal_y:
|
60 |
function_args["marginal_y"] = marginal_y
|
61 |
+
if size:
|
62 |
+
df.loc[df[size] < 0, size] = 0
|
63 |
+
function_args["size"] = size
|
64 |
if trendline_options:
|
65 |
trendline_options_dict = {}
|
66 |
if trendline_options and isinstance(trendline_options, list):
|
|
|
79 |
return fig
|
80 |
|
81 |
def scatter_chart_generation_func(x_column: List[str], y_column: str, session_hash, data: List[dict]=[{}], layout: List[dict]=[{}],
|
82 |
+
category: str="", trendline: str="", trendline_options: List[dict]=[{}], marginal_x: str="", marginal_y: str="",
|
83 |
+
size: str=""):
|
84 |
try:
|
85 |
dir_path = TEMP_DIR / str(session_hash)
|
86 |
chart_path = f'{dir_path}/chart.html'
|
|
|
90 |
|
91 |
initial_graph = scatter_chart_fig(df, x_column=x_column, y_column=y_column,
|
92 |
category=category, trendline=trendline, trendline_options=trendline_options,
|
93 |
+
marginal_x=marginal_x, marginal_y=marginal_y, size=size)
|
94 |
|
95 |
fig = initial_graph.to_dict()
|
96 |
|
97 |
+
print(data)
|
98 |
+
print(layout)
|
99 |
|
100 |
+
data_dict,layout_dict = llm_chart_data_scrub(data,layout)
|
|
|
101 |
|
102 |
#Applying stylings and settings generated from LLM
|
103 |
if layout_dict:
|
104 |
fig["layout"] = layout_dict
|
105 |
|
106 |
+
data_ignore = ["x","y","type"]
|
107 |
+
|
108 |
+
if size:
|
109 |
+
data_ignore.append("marker")
|
110 |
+
|
111 |
for key, value in data_dict.items():
|
112 |
+
if key not in data_ignore:
|
113 |
for data_item in fig["data"]:
|
114 |
data_item[key] = value
|
115 |
|
tools.py
CHANGED
@@ -43,8 +43,8 @@ def tools_call(session_hash):
|
|
43 |
"name": "scatter_chart_generation_func",
|
44 |
"description": f"""This is a scatter plot generation tool useful to generate scatter plots from queried data from our SQL table called 'data_source'.
|
45 |
The data values will come from the columns of our query.csv (the 'x' and 'y' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
46 |
-
Returns an iframe string which will be displayed inline in our chat window. Do not edit the string returned
|
47 |
-
from the
|
48 |
to it for context if desired.""",
|
49 |
"parameters": {
|
50 |
"type": "object",
|
@@ -52,7 +52,7 @@ def tools_call(session_hash):
|
|
52 |
"data": {
|
53 |
"type": "array",
|
54 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
55 |
-
The array must contain a dictionary, any other format will not work.
|
56 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
57 |
Infer this from the user's message.""",
|
58 |
"items": {
|
@@ -95,7 +95,7 @@ def tools_call(session_hash):
|
|
95 |
"type": "array",
|
96 |
"description": """An array containing a dictionary that contains the 'trendline_options' portion of the plotly chart generation.
|
97 |
The 'lowess', 'rolling', and 'ewm' options require trendline_options to be included.
|
98 |
-
The array must contain a dictionary, any other format will not work.""",
|
99 |
"items": {
|
100 |
"type": "string",
|
101 |
}
|
@@ -123,7 +123,16 @@ def tools_call(session_hash):
|
|
123 |
"layout": {
|
124 |
"type": "array",
|
125 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
126 |
-
The array must contain a dictionary, any other format will not work.""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
"items": {
|
128 |
"type": "string",
|
129 |
}
|
@@ -139,8 +148,8 @@ def tools_call(session_hash):
|
|
139 |
"name": "line_chart_generation_func",
|
140 |
"description": f"""This is a line chart generation tool useful to generate line charts from queried data from our SQL table called 'data_source'.
|
141 |
The data values will come from the columns of our query.csv (the 'x' and 'y' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
142 |
-
Returns an iframe string which will be displayed inline in our chat window. Do not edit the string returned
|
143 |
-
from the
|
144 |
to it for context if desired.""",
|
145 |
"parameters": {
|
146 |
"type": "object",
|
@@ -148,7 +157,7 @@ def tools_call(session_hash):
|
|
148 |
"data": {
|
149 |
"type": "array",
|
150 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
151 |
-
The array must contain a dictionary, any other format will not work.
|
152 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
153 |
Infer this from the user's message.""",
|
154 |
"items": {
|
@@ -179,7 +188,7 @@ def tools_call(session_hash):
|
|
179 |
"layout": {
|
180 |
"type": "array",
|
181 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
182 |
-
The array must contain a dictionary, any other format will not work.""",
|
183 |
"items": {
|
184 |
"type": "string",
|
185 |
}
|
@@ -195,8 +204,8 @@ def tools_call(session_hash):
|
|
195 |
"name": "bar_chart_generation_func",
|
196 |
"description": f"""This is a bar chart generation tool useful to generate line charts from queried data from our SQL table called 'data_source'.
|
197 |
The data values will come from the columns of our query.csv (the 'x' and 'y' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
198 |
-
Returns an iframe string which will be displayed inline in our chat window. Do not edit the string returned
|
199 |
-
from the
|
200 |
to it for context if desired.""",
|
201 |
"parameters": {
|
202 |
"type": "object",
|
@@ -204,7 +213,7 @@ def tools_call(session_hash):
|
|
204 |
"data": {
|
205 |
"type": "array",
|
206 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
207 |
-
The array must contain a dictionary, any other format will not work.
|
208 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
209 |
Infer this from the user's message.""",
|
210 |
"items": {
|
@@ -251,7 +260,7 @@ def tools_call(session_hash):
|
|
251 |
"layout": {
|
252 |
"type": "array",
|
253 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
254 |
-
The array must contain a dictionary, any other format will not work.""",
|
255 |
"items": {
|
256 |
"type": "string",
|
257 |
}
|
@@ -267,8 +276,8 @@ def tools_call(session_hash):
|
|
267 |
"name": "pie_chart_generation_func",
|
268 |
"description": f"""This is a pie chart generation tool useful to generate pie charts from queried data from our SQL table called 'data_source'.
|
269 |
The data values will come from the columns of our query.csv (the 'values' and 'names' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
270 |
-
Returns an iframe string which will be displayed inline in our chat window. Do not edit the string returned
|
271 |
-
from the
|
272 |
to it for context if desired.""",
|
273 |
"parameters": {
|
274 |
"type": "object",
|
@@ -276,7 +285,7 @@ def tools_call(session_hash):
|
|
276 |
"data": {
|
277 |
"type": "array",
|
278 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
279 |
-
The array must contain a dictionary, any other format will not work.
|
280 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
281 |
Infer this from the user's message.""",
|
282 |
"items": {
|
@@ -300,7 +309,7 @@ def tools_call(session_hash):
|
|
300 |
"layout": {
|
301 |
"type": "array",
|
302 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
303 |
-
The array must contain a dictionary, any other format will not work.""",
|
304 |
"items": {
|
305 |
"type": "string",
|
306 |
}
|
@@ -316,8 +325,8 @@ def tools_call(session_hash):
|
|
316 |
"name": "histogram_generation_func",
|
317 |
"description": f"""This is a histogram generation tool useful to generate histograms from queried data from our SQL table called 'data_source'.
|
318 |
The data values will come from the columns of our query.csv (the 'values' and 'names' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
319 |
-
Returns an iframe string which will be displayed inline in our chat window. Do not edit the string returned
|
320 |
-
from the
|
321 |
to it for context if desired.""",
|
322 |
"parameters": {
|
323 |
"type": "object",
|
@@ -325,7 +334,7 @@ def tools_call(session_hash):
|
|
325 |
"data": {
|
326 |
"type": "array",
|
327 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
328 |
-
The array must contain a dictionary, any other format will not work.
|
329 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
330 |
Infer this from the user's message.""",
|
331 |
"items": {
|
@@ -375,7 +384,7 @@ def tools_call(session_hash):
|
|
375 |
"layout": {
|
376 |
"type": "array",
|
377 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
378 |
-
The array must contain a dictionary, any other format will not work.""",
|
379 |
"items": {
|
380 |
"type": "string",
|
381 |
}
|
|
|
43 |
"name": "scatter_chart_generation_func",
|
44 |
"description": f"""This is a scatter plot generation tool useful to generate scatter plots from queried data from our SQL table called 'data_source'.
|
45 |
The data values will come from the columns of our query.csv (the 'x' and 'y' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
46 |
+
Returns an iframe string which will be displayed inline in our chat window. Do not edit the iframe string returned
|
47 |
+
from the scatter_chart_generation_func function in any way and always display the iframe fully to the user in the chat window. You can add your own text supplementary
|
48 |
to it for context if desired.""",
|
49 |
"parameters": {
|
50 |
"type": "object",
|
|
|
52 |
"data": {
|
53 |
"type": "array",
|
54 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
55 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.
|
56 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
57 |
Infer this from the user's message.""",
|
58 |
"items": {
|
|
|
95 |
"type": "array",
|
96 |
"description": """An array containing a dictionary that contains the 'trendline_options' portion of the plotly chart generation.
|
97 |
The 'lowess', 'rolling', and 'ewm' options require trendline_options to be included.
|
98 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.""",
|
99 |
"items": {
|
100 |
"type": "string",
|
101 |
}
|
|
|
123 |
"layout": {
|
124 |
"type": "array",
|
125 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
126 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.""",
|
127 |
+
"items": {
|
128 |
+
"type": "string",
|
129 |
+
}
|
130 |
+
},
|
131 |
+
"size": {
|
132 |
+
"type": "string",
|
133 |
+
"description": f"""An optional column in our query.csv file that contain a parameter that will define the size of each plot point.
|
134 |
+
This is useful for a bubble chart where another value in our query can be represented by the size of the plotted point.
|
135 |
+
Values must be greater than or equal to 0 and so in our query, all values less than 0 should be set equal to zero.""",
|
136 |
"items": {
|
137 |
"type": "string",
|
138 |
}
|
|
|
148 |
"name": "line_chart_generation_func",
|
149 |
"description": f"""This is a line chart generation tool useful to generate line charts from queried data from our SQL table called 'data_source'.
|
150 |
The data values will come from the columns of our query.csv (the 'x' and 'y' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
151 |
+
Returns an iframe string which will be displayed inline in our chat window. Do not edit the iframe string returned
|
152 |
+
from the line_chart_generation_func function in any way and always display the iframe fully to the user in the chat window. You can add your own text supplementary
|
153 |
to it for context if desired.""",
|
154 |
"parameters": {
|
155 |
"type": "object",
|
|
|
157 |
"data": {
|
158 |
"type": "array",
|
159 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
160 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.
|
161 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
162 |
Infer this from the user's message.""",
|
163 |
"items": {
|
|
|
188 |
"layout": {
|
189 |
"type": "array",
|
190 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
191 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.""",
|
192 |
"items": {
|
193 |
"type": "string",
|
194 |
}
|
|
|
204 |
"name": "bar_chart_generation_func",
|
205 |
"description": f"""This is a bar chart generation tool useful to generate line charts from queried data from our SQL table called 'data_source'.
|
206 |
The data values will come from the columns of our query.csv (the 'x' and 'y' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
207 |
+
Returns an iframe string which will be displayed inline in our chat window. Do not edit the iframe string returned
|
208 |
+
from the bar_chart_generation_func function in any way and always display the iframe fully to the user in the chat window. You can add your own text supplementary
|
209 |
to it for context if desired.""",
|
210 |
"parameters": {
|
211 |
"type": "object",
|
|
|
213 |
"data": {
|
214 |
"type": "array",
|
215 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
216 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.
|
217 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
218 |
Infer this from the user's message.""",
|
219 |
"items": {
|
|
|
260 |
"layout": {
|
261 |
"type": "array",
|
262 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
263 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.""",
|
264 |
"items": {
|
265 |
"type": "string",
|
266 |
}
|
|
|
276 |
"name": "pie_chart_generation_func",
|
277 |
"description": f"""This is a pie chart generation tool useful to generate pie charts from queried data from our SQL table called 'data_source'.
|
278 |
The data values will come from the columns of our query.csv (the 'values' and 'names' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
279 |
+
Returns an iframe string which will be displayed inline in our chat window. Do not edit the iframe string returned
|
280 |
+
from the pie_chart_generation_func function in any way and always display the iframe fully to the user in the chat window. You can add your own text supplementary
|
281 |
to it for context if desired.""",
|
282 |
"parameters": {
|
283 |
"type": "object",
|
|
|
285 |
"data": {
|
286 |
"type": "array",
|
287 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
288 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.
|
289 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
290 |
Infer this from the user's message.""",
|
291 |
"items": {
|
|
|
309 |
"layout": {
|
310 |
"type": "array",
|
311 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
312 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.""",
|
313 |
"items": {
|
314 |
"type": "string",
|
315 |
}
|
|
|
325 |
"name": "histogram_generation_func",
|
326 |
"description": f"""This is a histogram generation tool useful to generate histograms from queried data from our SQL table called 'data_source'.
|
327 |
The data values will come from the columns of our query.csv (the 'values' and 'names' values of each graph) file but the layout section of the plotly dictionary objects will be generated by you.
|
328 |
+
Returns an iframe string which will be displayed inline in our chat window. Do not edit the iframe string returned
|
329 |
+
from the histogram_generation_func function in any way and always display the iframe fully to the user in the chat window. You can add your own text supplementary
|
330 |
to it for context if desired.""",
|
331 |
"parameters": {
|
332 |
"type": "object",
|
|
|
334 |
"data": {
|
335 |
"type": "array",
|
336 |
"description": """The array containing a dictionary that contains the 'data' portion of the plotly chart generation and will include the options requested by the user.
|
337 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.
|
338 |
Do not include the 'x' or 'y' portions of the object as this will come from the query.csv file generated by our SQLite query.
|
339 |
Infer this from the user's message.""",
|
340 |
"items": {
|
|
|
384 |
"layout": {
|
385 |
"type": "array",
|
386 |
"description": """An array containing a dictionary that contains the 'layout' portion of the plotly chart generation.
|
387 |
+
The array must contain a json formatted dictionary with outer brackets included, any other format will not work.""",
|
388 |
"items": {
|
389 |
"type": "string",
|
390 |
}
|