-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
275 lines (217 loc) · 10.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import streamlit as st
from lida import Manager, TextGenerationConfig, llm
from lida.datamodel import Goal
import os
import pandas as pd
# make data dir if it doesn't exist
os.makedirs("data", exist_ok=True)
st.set_page_config(
page_title="LIDA: Automatic Generation of Visualizations and Infographics",
page_icon="📊",
)
st.write("# LIDA: Automatic Generation of Visualizations and Infographics using Large Language Models 📊")
st.sidebar.write("## Setup")
# Step 1 - Get OpenAI API key
openai_key = os.getenv("OPENAI_API_KEY")
if not openai_key:
openai_key = st.sidebar.text_input("Enter OpenAI API key:")
if openai_key:
display_key = openai_key[:2] + "*" * (len(openai_key) - 5) + openai_key[-3:]
st.sidebar.write(f"Current key: {display_key}")
else:
st.sidebar.write("Please enter OpenAI API key.")
else:
display_key = openai_key[:2] + "*" * (len(openai_key) - 5) + openai_key[-3:]
st.sidebar.write(f"OpenAI API key loaded from environment variable: {display_key}")
st.markdown(
"""
LIDA is a library for generating data visualizations and data-faithful infographics.
LIDA is grammar agnostic (will work with any programming language and visualization
libraries e.g. matplotlib, seaborn, altair, d3 etc) and works with multiple large language
model providers (OpenAI, Azure OpenAI, PaLM, Cohere, Huggingface). Details on the components
of LIDA are described in the [paper here](https://arxiv.org/abs/2303.02927) and in this
tutorial [notebook](notebooks/tutorial.ipynb). See the project page [here](https://microsoft.github.io/lida/) for updates!.
This demo shows how to use the LIDA python api with Streamlit. [More](/about).
----
""")
def generate_visualizations():
# Step 3 - Generate data summary
if openai_key and selected_dataset and selected_method:
lida = Manager(text_gen=llm("openai", api_key=openai_key))
textgen_config = TextGenerationConfig(
n=1,
temperature=temperature,
model=selected_model,
use_cache=use_cache)
st.write("## Summary")
# **** lida.summarize *****
summary = lida.summarize(
selected_dataset,
summary_method=selected_method,
textgen_config=textgen_config)
if "dataset_description" in summary:
st.write(summary["dataset_description"])
if "fields" in summary:
fields = summary["fields"]
nfields = []
for field in fields:
flatted_fields = {}
flatted_fields["column"] = field["column"]
# flatted_fields["dtype"] = field["dtype"]
for row in field["properties"].keys():
if row != "samples":
flatted_fields[row] = field["properties"][row]
else:
flatted_fields[row] = str(field["properties"][row])
# flatted_fields = {**flatted_fields, **field["properties"]}
nfields.append(flatted_fields)
nfields_df = pd.DataFrame(nfields)
st.write(nfields_df)
else:
st.write(str(summary))
# Step 4 - Generate goals
if summary:
st.sidebar.write("### Goal Selection")
num_goals = st.sidebar.slider(
"Number of goals to generate",
min_value=1,
max_value=10,
value=4)
own_goal = st.sidebar.checkbox("Add Your Own Goal")
# **** lida.goals *****
goals = lida.goals(summary, n=num_goals, textgen_config=textgen_config)
st.write(f"## Goals ({len(goals)})")
default_goal = goals[0].question
goal_questions = [goal.question for goal in goals]
if own_goal:
user_goal = st.sidebar.text_input("Describe Your Goal")
if user_goal:
new_goal = Goal(question=user_goal, visualization=user_goal, rationale="")
goals.append(new_goal)
goal_questions.append(new_goal.question)
selected_goal = st.selectbox('Choose a generated goal', options=goal_questions, index=0)
# st.markdown("### Selected Goal")
selected_goal_index = goal_questions.index(selected_goal)
st.write(goals[selected_goal_index])
selected_goal_object = goals[selected_goal_index]
# Step 5 - Generate visualizations
if selected_goal_object:
st.sidebar.write("## Visualization Library")
visualization_libraries = ["seaborn", "matplotlib", "plotly"]
selected_library = st.sidebar.selectbox(
'Choose a visualization library',
options=visualization_libraries,
index=0
)
# Update the visualization generation call to use the selected library.
st.write("## Visualizations")
# slider for number of visualizations
num_visualizations = st.sidebar.slider(
"Number of visualizations to generate",
min_value=1,
max_value=10,
value=2)
textgen_config = TextGenerationConfig(
n=num_visualizations, temperature=temperature,
model=selected_model,
use_cache=use_cache)
# **** lida.visualize *****
visualizations = lida.visualize(
summary=summary,
goal=selected_goal_object,
textgen_config=textgen_config,
library=selected_library)
viz_titles = [f'Visualization {i+1}' for i in range(len(visualizations))]
selected_viz_title = st.selectbox('Choose a visualization', options=viz_titles, index=0)
selected_viz = visualizations[viz_titles.index(selected_viz_title)]
if selected_viz.raster:
from PIL import Image
import io
import base64
imgdata = base64.b64decode(selected_viz.raster)
img = Image.open(io.BytesIO(imgdata))
st.image(img, caption=selected_viz_title, use_column_width=True)
st.write("### Visualization Code")
st.code(selected_viz.code)
# Step 2 - Select a dataset and summarization method
if openai_key:
# Initialize selected_dataset to None
selected_dataset = None
# select model from gpt-4 , gpt-3.5-turbo, gpt-3.5-turbo-16k
st.sidebar.write("## Text Generation Model")
models = ["gpt-4", "gpt-3.5-turbo", "gpt-3.5-turbo-16k"]
selected_model = st.sidebar.selectbox(
'Choose a model',
options=models,
index=0
)
# select temperature on a scale of 0.0 to 1.0
# st.sidebar.write("## Text Generation Temperature")
temperature = st.sidebar.slider(
"Temperature",
min_value=0.0,
max_value=1.0,
value=0.0)
# set use_cache in sidebar
use_cache = st.sidebar.checkbox("Use cache", value=True)
# Handle dataset selection and upload
st.sidebar.write("## Data Summarization")
st.sidebar.write("### Choose a dataset")
datasets = [
{"label": "Select a dataset", "url": None},
{"label": "Cars", "url": "https://raw.githubusercontent.com/uwdata/draco/master/data/cars.csv"},
{"label": "Weather", "url": "https://raw.githubusercontent.com/uwdata/draco/master/data/weather.json"},
]
selected_dataset_label = st.sidebar.selectbox(
'Choose a dataset',
options=[dataset["label"] for dataset in datasets],
index=0
)
upload_own_data = st.sidebar.checkbox("Upload your own data")
if upload_own_data:
uploaded_file = st.sidebar.file_uploader("Choose a CSV or JSON file", type=["csv", "json"])
if uploaded_file is not None:
# Get the original file name and extension
file_name, file_extension = os.path.splitext(uploaded_file.name)
# Load the data depending on the file type
if file_extension.lower() == ".csv":
data = pd.read_csv(uploaded_file)
elif file_extension.lower() == ".json":
data = pd.read_json(uploaded_file)
# Save the data using the original file name in the data dir
uploaded_file_path = os.path.join("data", uploaded_file.name)
data.to_csv(uploaded_file_path, index=False)
selected_dataset = uploaded_file_path
datasets.append({"label": file_name, "url": uploaded_file_path})
# st.sidebar.write("Uploaded file path: ", uploaded_file_path)
else:
selected_dataset = datasets[[dataset["label"]
for dataset in datasets].index(selected_dataset_label)]["url"]
if not selected_dataset:
st.info("To continue, select a dataset from the sidebar on the left or upload your own.")
st.sidebar.write("### Choose a summarization method")
# summarization_methods = ["default", "llm", "columns"]
summarization_methods = [
{"label": "llm",
"description":
"Uses the LLM to generate annotate the default summary, adding details such as semantic types for columns and dataset description"},
{"label": "default",
"description": "Uses dataset column statistics and column names as the summary"},
{"label": "columns", "description": "Uses the dataset column names as the summary"}]
# selected_method = st.sidebar.selectbox("Choose a method", options=summarization_methods)
selected_method_label = st.sidebar.selectbox(
'Choose a method',
options=[method["label"] for method in summarization_methods],
index=0
)
selected_method = summarization_methods[[
method["label"] for method in summarization_methods].index(selected_method_label)]["label"]
# add description of selected method in very small font to sidebar
selected_summary_method_description = summarization_methods[[
method["label"] for method in summarization_methods].index(selected_method_label)]["description"]
if selected_method:
st.sidebar.markdown(
f"<span> {selected_summary_method_description} </span>",
unsafe_allow_html=True)
# Generate visualizations
generate_visualizations()