-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_day29_app.py
540 lines (404 loc) · 16.2 KB
/
streamlit_day29_app.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# Import Streamlit and Pandas
import streamlit as st
import pandas as pd
# Import for API calls
import requests
# Import for navbar
from streamlit_option_menu import option_menu
# Import for dyanmic tagging
from streamlit_tags import st_tags, st_tags_sidebar
# Imports for aggrid
from st_aggrid import AgGrid
from st_aggrid import AgGrid
import pandas as pd
from st_aggrid.grid_options_builder import GridOptionsBuilder
from st_aggrid.shared import JsCode
from st_aggrid import GridUpdateMode, DataReturnMode
# Import for keyboard shortcuts
from dashboard_utils.gui import keyboard_to_url
from dashboard_utils.gui import load_keyboard_class
#######################################################
# The code below is to control the layout width of the app.
if "widen" not in st.session_state:
layout = "centered"
else:
layout = "wide" if st.session_state.widen else "centered"
#######################################################
# The code below is for the title and logo.
st.set_page_config(layout=layout, page_title="Zero-Shot Text Classifier", page_icon="🤗")
#######################################################
# The class below is for adding some formatting to the shortcut button on the left sidebar.
load_keyboard_class()
#######################################################
# Set up session state so app interactions don't reset the app.
if not "valid_inputs_received" in st.session_state:
st.session_state["valid_inputs_received"] = False
#######################################################
# The block of code below is to display the title, logos and introduce the app.
c1, c2 = st.columns([0.4, 2])
with c1:
st.image(
"logo.png",
width=110,
)
with c2:
st.caption("")
st.title("Zero-Shot Text Classifier")
st.sidebar.image(
"30days_logo.png",
)
st.write("")
st.markdown(
"""
Classify keyphrases fast and on-the-fly with this mighty app. No ML training needed!
Create classifying labels (e.g. `Positive`, `Negative` and `Neutral`), paste your keyphrases, and you're off!
"""
)
st.write("")
st.sidebar.write("")
#######################################################
# The code below is to display the menu bar.ß
with st.sidebar:
selected = option_menu(
"",
["Demo (5 phrases max)", "Unlocked Mode"],
icons=["bi-joystick", "bi-key-fill"],
menu_icon="",
default_index=0,
)
#######################################################
# The code below is to display the shortcuts.
st.sidebar.header("Shortcuts")
st.sidebar.write(
'<span class="kbdx">G</span> GitHub',
unsafe_allow_html=True,
)
st.sidebar.write(
'<span class="kbdx"> . </span> GitHub Dev (VS Code)',
unsafe_allow_html=True,
)
#######################################################
# The block of code below is to display information about Streamlit.
st.sidebar.markdown("---")
# Sidebar
st.sidebar.header("About")
st.sidebar.markdown(
"""
App created by [Datachaz](https://twitter.com/DataChaz) using 🎈[Streamlit](https://streamlit.io/) and [HuggingFace](https://huggingface.co/inference-api)'s [Distilbart-mnli-12-3](https://huggingface.co/valhalla/distilbart-mnli-12-3) model.
"""
)
st.sidebar.markdown(
"[Streamlit](https://streamlit.io) is a Python library that allows the creation of interactive, data-driven web applications in Python."
)
st.sidebar.header("Resources")
st.sidebar.markdown(
"""
- [Streamlit Documentation](https://docs.streamlit.io/)
- [Cheat sheet](https://docs.streamlit.io/library/cheatsheet)
- [Book](https://www.amazon.com/dp/180056550X) (Getting Started with Streamlit for Data Science)
- [Blog](https://blog.streamlit.io/how-to-master-streamlit-for-data-science/) (How to master Streamlit for data science)
"""
)
st.sidebar.header("Deploy")
st.sidebar.markdown(
"You can quickly deploy Streamlit apps using [Streamlit Cloud](https://streamlit.io/cloud) in just a few clicks."
)
def main():
st.caption("")
if selected == "Demo (5 phrases max)":
API_KEY = st.secrets["API_KEY"]
API_URL = (
"https://api-inference.huggingface.co/models/valhalla/distilbart-mnli-12-3"
)
headers = {"Authorization": f"Bearer {API_KEY}"}
with st.form(key="my_form"):
multiselectComponent = st_tags(
label="",
text="Add labels - 3 max",
value=["Transactional", "Informational", "Navigational"],
suggestions=[
"Informational",
"Transactional",
"Navigational",
"Positive",
"Negative",
"Neutral",
],
maxtags=3,
)
new_line = "\n"
nums = [
"I want to buy something in this store",
"How to ask a question about a product",
"Request a refund through the Google Play store",
"I have a broken screen, what should I do?",
"Can I have the link to the product?",
]
sample = f"{new_line.join(map(str, nums))}"
linesDeduped2 = []
MAX_LINES = 5
text = st.text_area(
"Enter keyphrases to classify",
sample,
height=200,
key="2",
help="At least two keyphrases for the classifier to work, one per line, "
+ str(MAX_LINES)
+ " keyphrases max as part of the demo",
)
lines = text.split("\n") # A list of lines
linesList = []
for x in lines:
linesList.append(x)
linesList = list(dict.fromkeys(linesList)) # Remove dupes
linesList = list(filter(None, linesList)) # Remove empty
if len(linesList) > MAX_LINES:
st.info(
f"❄️ Only the first "
+ str(MAX_LINES)
+ " keyprases will be reviewed. Unlock that limit by switching to 'Unlocked Mode'"
)
linesList = linesList[:MAX_LINES]
submit_button = st.form_submit_button(label="Submit")
if not submit_button and not st.session_state.valid_inputs_received:
st.stop()
elif submit_button and not text:
st.warning("❄️ There is no keyphrases to classify")
st.session_state.valid_inputs_received = False
st.stop()
elif submit_button and not multiselectComponent:
st.warning("❄️ You have not added any labels, please add some! ")
st.session_state.valid_inputs_received = False
st.stop()
elif submit_button and len(multiselectComponent) == 1:
st.warning("❄️ Please make sure to add at least two labels for classification")
st.session_state.valid_inputs_received = False
st.stop()
elif submit_button or st.session_state.valid_inputs_received:
if submit_button:
st.session_state.valid_inputs_received = True
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
# Unhash to check status codes from the API response
# st.write(response.status_code)
return response.json()
listtest = ["I want a refund", "I have a question"]
listToAppend = []
for row in linesList:
output2 = query(
{
"inputs": row,
"parameters": {"candidate_labels": multiselectComponent},
"options": {"wait_for_model": True},
}
)
listToAppend.append(output2)
df = pd.DataFrame.from_dict(output2)
st.success("✅ Done!")
df = pd.DataFrame.from_dict(listToAppend)
st.caption("")
st.markdown("### Check classifier results")
st.caption("")
st.checkbox(
"Widen layout",
key="widen",
help="Tick this box to toggle the layout to 'Wide' mode",
)
st.caption("")
# This is a list comprehension to convert the decimals to percentages
f = [[f"{x:.2%}" for x in row] for row in df["scores"]]
# This code is for re-integrating the labels back into the dataframe
df["classification scores"] = f
df.drop("scores", inplace=True, axis=1)
# This code is to rename the columns
df.rename(columns={"sequence": "keyphrase"}, inplace=True)
# The code below is for Ag-grid
gb = GridOptionsBuilder.from_dataframe(df)
# enables pivoting on all columns
gb.configure_default_column(
enablePivot=True, enableValue=True, enableRowGroup=True
)
gb.configure_selection(selection_mode="multiple", use_checkbox=True)
gb.configure_side_bar()
gridOptions = gb.build()
response = AgGrid(
df,
gridOptions=gridOptions,
enable_enterprise_modules=True,
update_mode=GridUpdateMode.MODEL_CHANGED,
data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
height=300,
fit_columns_on_grid_load=False,
configure_side_bar=True,
)
# The code below is for the download button
cs, c1 = st.columns([2, 2])
with cs:
@st.cache
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode("utf-8")
csv = convert_df(df) #
st.download_button(
label="Download results as CSV",
data=csv,
file_name="results.csv",
mime="text/csv",
)
elif selected == "Unlocked Mode":
with st.form(key="my_form"):
API_KEY2 = st.text_input(
"Enter your 🤗 HuggingFace API key",
help="Once you created you HuggiginFace account, you can get your free API token in your settings page: https://huggingface.co/settings/tokens",
)
API_URL = (
"https://api-inference.huggingface.co/models/valhalla/distilbart-mnli-12-3"
)
headers = {"Authorization": f"Bearer {API_KEY2}"}
multiselectComponent = st_tags(
label="",
text="Add labels - 3 max",
value=["Transactional", "Informational", "Navigational"],
suggestions=[
"Informational",
"Transactional",
"Navigational",
"Positive",
"Negative",
"Neutral",
],
maxtags=3,
)
new_line = "\n"
nums = [
"I want to buy something in this store",
"How to ask a question about a product",
"Request a refund through the Google Play store",
"I have a broken screen, what should I do?",
"Can I have the link to the product?",
]
sample = f"{new_line.join(map(str, nums))}"
linesDeduped2 = []
MAX_LINES_FULL = 50
text = st.text_area(
"Enter keyphrases to classify",
sample,
height=200,
key="2",
help="At least two keyphrases for the classifier to work, one per line, "
+ str(MAX_LINES_FULL)
+ " keyphrases max in 'unlocked mode'. You can tweak 'MAX_LINES_FULL' in the code to change this",
)
lines = text.split("\n") # A list of lines
linesList = []
for x in lines:
linesList.append(x)
linesList = list(dict.fromkeys(linesList)) # Remove dupes from list
linesList = list(filter(None, linesList)) # Remove empty lines from list
if len(linesList) > MAX_LINES_FULL:
st.info(
f"❄️ Note that only the first "
+ str(MAX_LINES_FULL)
+ " keyprases will be reviewed to preserve performance. Fork the repo and tweak 'MAX_LINES_FULL' in the code to increase that limit."
)
linesList = linesList[:MAX_LINES_FULL]
submit_button = st.form_submit_button(label="Submit")
if not submit_button and not st.session_state.valid_inputs_received:
st.stop()
elif submit_button and not text:
st.warning("❄️ There is no keyphrases to classify")
st.session_state.valid_inputs_received = False
st.stop()
elif submit_button and not multiselectComponent:
st.warning("❄️ You have not added any labels, please add some! ")
st.session_state.valid_inputs_received = False
st.stop()
elif submit_button and len(multiselectComponent) == 1:
st.warning("❄️ Please make sure to add at least two labels for classification")
st.session_state.valid_inputs_received = False
st.stop()
elif submit_button or st.session_state.valid_inputs_received:
try:
if submit_button:
st.session_state.valid_inputs_received = True
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
# Unhash to check status codes from the API response
# st.write(response.status_code)
return response.json()
listtest = ["I want a refund", "I have a question"]
listToAppend = []
for row in linesList:
output2 = query(
{
"inputs": row,
"parameters": {"candidate_labels": multiselectComponent},
"options": {"wait_for_model": True},
}
)
listToAppend.append(output2)
df = pd.DataFrame.from_dict(output2)
st.success("✅ Done!")
df = pd.DataFrame.from_dict(listToAppend)
st.caption("")
st.markdown("### Check classifier results")
st.caption("")
st.checkbox(
"Widen layout",
key="widen",
help="Tick this box to toggle the layout to 'Wide' mode",
)
# This is a list comprehension to convert the decimals to percentages
f = [[f"{x:.2%}" for x in row] for row in df["scores"]]
# This code is for re-integrating the labels back into the dataframe
df["classification scores"] = f
df.drop("scores", inplace=True, axis=1)
# This code is to rename the columns
df.rename(columns={"sequence": "keyphrase"}, inplace=True)
# The code below is for Ag-grid
gb = GridOptionsBuilder.from_dataframe(df)
# enables pivoting on all columns
gb.configure_default_column(
enablePivot=True, enableValue=True, enableRowGroup=True
)
gb.configure_selection(selection_mode="multiple", use_checkbox=True)
gb.configure_side_bar()
gridOptions = gb.build()
response = AgGrid(
df,
gridOptions=gridOptions,
enable_enterprise_modules=True,
update_mode=GridUpdateMode.MODEL_CHANGED,
data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
height=300,
fit_columns_on_grid_load=False,
configure_side_bar=True,
)
# The code below is for the download button
cs, c1 = st.columns([2, 2])
with cs:
@st.cache
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode("utf-8")
csv = convert_df(df) #
st.caption("")
st.download_button(
label="Download results as CSV",
data=csv,
file_name="results.csv",
mime="text/csv",
)
except ValueError as ve:
st.warning("❄️ Add a valid HuggingFace API key in the text box above ☝️")
st.stop()
if __name__ == "__main__":
main()
keyboard_to_url(
key="g",
url="https://github.com/CharlyWargnier/zero-shot-classifier/blob/main/streamlit_app.py",
)
keyboard_to_url(
key_code=190,
url="https://github.dev/CharlyWargnier/zero-shot-classifier/blob/main/streamlit_app.py",
)