Spaces:
Build error
Build error
yourusername
commited on
Commit
•
e88b792
1
Parent(s):
64a1ca0
:rocket: add app
Browse files
app.py
CHANGED
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2021 The HuggingFace Team. All rights reserved.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
import logging
|
16 |
+
from os import mkdir
|
17 |
+
from os.path import isdir
|
18 |
+
|
19 |
+
import streamlit as st
|
20 |
+
|
21 |
+
from data_measurements import dataset_statistics, dataset_utils
|
22 |
+
from data_measurements import streamlit_utils as st_utils
|
23 |
+
|
24 |
+
logs = logging.getLogger(__name__)
|
25 |
+
logs.setLevel(logging.WARNING)
|
26 |
+
logs.propagate = False
|
27 |
+
|
28 |
+
if not logs.handlers:
|
29 |
+
|
30 |
+
# Logging info to log file
|
31 |
+
file = logging.FileHandler("./log_files/app.log")
|
32 |
+
fileformat = logging.Formatter("%(asctime)s:%(message)s")
|
33 |
+
file.setLevel(logging.INFO)
|
34 |
+
file.setFormatter(fileformat)
|
35 |
+
|
36 |
+
# Logging debug messages to stream
|
37 |
+
stream = logging.StreamHandler()
|
38 |
+
streamformat = logging.Formatter("[data_measurements_tool] %(message)s")
|
39 |
+
stream.setLevel(logging.WARNING)
|
40 |
+
stream.setFormatter(streamformat)
|
41 |
+
|
42 |
+
logs.addHandler(file)
|
43 |
+
logs.addHandler(stream)
|
44 |
+
|
45 |
+
st.set_page_config(
|
46 |
+
page_title="Demo to showcase dataset metrics",
|
47 |
+
page_icon="https://huggingface.co/front/assets/huggingface_logo.svg",
|
48 |
+
layout="wide",
|
49 |
+
initial_sidebar_state="auto",
|
50 |
+
)
|
51 |
+
|
52 |
+
# colorblind-friendly colors
|
53 |
+
colors = [
|
54 |
+
"#332288",
|
55 |
+
"#117733",
|
56 |
+
"#882255",
|
57 |
+
"#AA4499",
|
58 |
+
"#CC6677",
|
59 |
+
"#44AA99",
|
60 |
+
"#DDCC77",
|
61 |
+
"#88CCEE",
|
62 |
+
]
|
63 |
+
|
64 |
+
CACHE_DIR = dataset_utils.CACHE_DIR
|
65 |
+
# String names we are using (not coming from the stored dataset).
|
66 |
+
OUR_TEXT_FIELD = dataset_utils.OUR_TEXT_FIELD
|
67 |
+
OUR_LABEL_FIELD = dataset_utils.OUR_LABEL_FIELD
|
68 |
+
TOKENIZED_FIELD = dataset_utils.TOKENIZED_FIELD
|
69 |
+
EMBEDDING_FIELD = dataset_utils.EMBEDDING_FIELD
|
70 |
+
LENGTH_FIELD = dataset_utils.LENGTH_FIELD
|
71 |
+
# TODO: Allow users to specify this.
|
72 |
+
_MIN_VOCAB_COUNT = 10
|
73 |
+
_SHOW_TOP_N_WORDS = 10
|
74 |
+
|
75 |
+
|
76 |
+
@st.cache(
|
77 |
+
hash_funcs={
|
78 |
+
dataset_statistics.DatasetStatisticsCacheClass: lambda dstats: dstats.cache_path
|
79 |
+
},
|
80 |
+
allow_output_mutation=True,
|
81 |
+
)
|
82 |
+
def load_or_prepare(ds_args, show_embeddings, use_cache=False):
|
83 |
+
"""
|
84 |
+
Takes the dataset arguments from the GUI and uses them to load a dataset from the Hub or, if
|
85 |
+
a cache for those arguments is available, to load it from the cache.
|
86 |
+
Args:
|
87 |
+
ds_args (dict): the dataset arguments defined via the streamlit app GUI
|
88 |
+
show_embeddings (Bool): whether embeddings should we loaded and displayed for this dataset
|
89 |
+
use_cache (Bool) : whether the cache is used by default or not
|
90 |
+
Returns:
|
91 |
+
dstats: the computed dataset statistics (from the dataset_statistics class)
|
92 |
+
"""
|
93 |
+
if not isdir(CACHE_DIR):
|
94 |
+
logs.warning("Creating cache")
|
95 |
+
# We need to preprocess everything.
|
96 |
+
# This should eventually all go into a prepare_dataset CLI
|
97 |
+
mkdir(CACHE_DIR)
|
98 |
+
if use_cache:
|
99 |
+
logs.warning("Using cache")
|
100 |
+
dstats = dataset_statistics.DatasetStatisticsCacheClass(CACHE_DIR, **ds_args)
|
101 |
+
logs.warning("Loading Dataset")
|
102 |
+
dstats.load_or_prepare_dataset(use_cache=use_cache)
|
103 |
+
logs.warning("Extracting Labels")
|
104 |
+
dstats.load_or_prepare_labels(use_cache=use_cache)
|
105 |
+
logs.warning("Computing Text Lengths")
|
106 |
+
dstats.load_or_prepare_text_lengths(use_cache=use_cache)
|
107 |
+
logs.warning("Extracting Vocabulary")
|
108 |
+
dstats.load_or_prepare_vocab(use_cache=use_cache)
|
109 |
+
logs.warning("Calculating General Statistics...")
|
110 |
+
dstats.load_or_prepare_general_stats(use_cache=use_cache)
|
111 |
+
logs.warning("Completed Calculation.")
|
112 |
+
logs.warning("Calculating Fine-Grained Statistics...")
|
113 |
+
if show_embeddings:
|
114 |
+
logs.warning("Loading Embeddings")
|
115 |
+
dstats.load_or_prepare_embeddings(use_cache=use_cache)
|
116 |
+
print(dstats.fig_tree)
|
117 |
+
# TODO: This has now been moved to calculation when the npmi widget is loaded.
|
118 |
+
logs.warning("Loading Terms for nPMI")
|
119 |
+
dstats.load_or_prepare_npmi_terms(use_cache=use_cache)
|
120 |
+
logs.warning("Loading Zipf")
|
121 |
+
dstats.load_or_prepare_zipf(use_cache=use_cache)
|
122 |
+
return dstats
|
123 |
+
|
124 |
+
|
125 |
+
def show_column(dstats, ds_name_to_dict, show_embeddings, column_id, use_cache=True):
|
126 |
+
"""
|
127 |
+
Function for displaying the elements in the right column of the streamlit app.
|
128 |
+
Args:
|
129 |
+
ds_name_to_dict (dict): the dataset name and options in dictionary form
|
130 |
+
show_embeddings (Bool): whether embeddings should we loaded and displayed for this dataset
|
131 |
+
column_id (str): what column of the dataset the analysis is done on
|
132 |
+
use_cache (Bool): whether the cache is used by default or not
|
133 |
+
Returns:
|
134 |
+
The function displays the information using the functions defined in the st_utils class.
|
135 |
+
"""
|
136 |
+
# Note that at this point we assume we can use cache; default value is True.
|
137 |
+
# start showing stuff
|
138 |
+
title_str = f"### Showing{column_id}: {dstats.dset_name} - {dstats.dset_config} - {'-'.join(dstats.text_field)}"
|
139 |
+
st.markdown(title_str)
|
140 |
+
logs.info("showing header")
|
141 |
+
st_utils.expander_header(dstats, ds_name_to_dict, column_id)
|
142 |
+
logs.info("showing general stats")
|
143 |
+
st_utils.expander_general_stats(dstats, _SHOW_TOP_N_WORDS, column_id)
|
144 |
+
st_utils.expander_label_distribution(dstats.label_df, dstats.fig_labels, column_id)
|
145 |
+
st_utils.expander_text_lengths(
|
146 |
+
dstats.tokenized_df,
|
147 |
+
dstats.fig_tok_length,
|
148 |
+
dstats.avg_length,
|
149 |
+
dstats.std_length,
|
150 |
+
OUR_TEXT_FIELD,
|
151 |
+
LENGTH_FIELD,
|
152 |
+
column_id,
|
153 |
+
)
|
154 |
+
st_utils.expander_text_duplicates(dstats.text_dup_counts_df, column_id)
|
155 |
+
|
156 |
+
# We do the loading of these after the others in order to have some time
|
157 |
+
# to compute while the user works with the details above.
|
158 |
+
# Uses an interaction; handled a bit differently than other widgets.
|
159 |
+
logs.info("showing npmi widget")
|
160 |
+
npmi_stats = dataset_statistics.nPMIStatisticsCacheClass(
|
161 |
+
dstats, use_cache=use_cache
|
162 |
+
)
|
163 |
+
available_terms = npmi_stats.get_available_terms(use_cache=use_cache)
|
164 |
+
st_utils.npmi_widget(
|
165 |
+
column_id, available_terms, npmi_stats, _MIN_VOCAB_COUNT, use_cache=use_cache
|
166 |
+
)
|
167 |
+
logs.info("showing zipf")
|
168 |
+
st_utils.expander_zipf(dstats.z, dstats.zipf_fig, column_id)
|
169 |
+
if show_embeddings:
|
170 |
+
st_utils.expander_text_embeddings(
|
171 |
+
dstats.text_dset,
|
172 |
+
dstats.fig_tree,
|
173 |
+
dstats.node_list,
|
174 |
+
dstats.embeddings,
|
175 |
+
OUR_TEXT_FIELD,
|
176 |
+
column_id,
|
177 |
+
)
|
178 |
+
|
179 |
+
|
180 |
+
def main():
|
181 |
+
""" Sidebar description and selection """
|
182 |
+
ds_name_to_dict = dataset_utils.get_dataset_info_dicts()
|
183 |
+
st.title("Data Measurements Tool")
|
184 |
+
# Get the sidebar details
|
185 |
+
st_utils.sidebar_header()
|
186 |
+
# Set up naming, configs, and cache path.
|
187 |
+
compare_mode = st.sidebar.checkbox("Comparison mode")
|
188 |
+
|
189 |
+
# When not doing new development, use the cache.
|
190 |
+
use_cache = True
|
191 |
+
# TODO: Better handling of this eg, st.sidebar.checkbox("Show clustering")=
|
192 |
+
show_embeddings = st.sidebar.checkbox("Show embeddings")
|
193 |
+
# List of datasets for which embeddings are hard to compute:
|
194 |
+
|
195 |
+
if compare_mode:
|
196 |
+
logs.warning("Using Comparison Mode")
|
197 |
+
dataset_args_left = st_utils.sidebar_selection(ds_name_to_dict, " A")
|
198 |
+
dataset_args_right = st_utils.sidebar_selection(ds_name_to_dict, " B")
|
199 |
+
left_col, _, right_col = st.columns([10, 1, 10])
|
200 |
+
dstats_left = load_or_prepare(
|
201 |
+
dataset_args_left, show_embeddings, use_cache=use_cache
|
202 |
+
)
|
203 |
+
with left_col:
|
204 |
+
show_column(dstats_left, ds_name_to_dict, show_embeddings, " A")
|
205 |
+
dstats_right = load_or_prepare(
|
206 |
+
dataset_args_right, show_embeddings, use_cache=use_cache
|
207 |
+
)
|
208 |
+
with right_col:
|
209 |
+
show_column(dstats_right, ds_name_to_dict, show_embeddings, " B")
|
210 |
+
else:
|
211 |
+
logs.warning("Using Single Dataset Mode")
|
212 |
+
dataset_args = st_utils.sidebar_selection(ds_name_to_dict, "")
|
213 |
+
dstats = load_or_prepare(dataset_args, show_embeddings, use_cache=use_cache)
|
214 |
+
show_column(dstats, ds_name_to_dict, show_embeddings, "")
|
215 |
+
|
216 |
+
|
217 |
+
if __name__ == "__main__":
|
218 |
+
main()
|