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