This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.py
251 lines (207 loc) Β· 9.96 KB
/
components.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
import streamlit as st
import pandas as pd
from connectors import *
import numpy as np
from processing import *
from util import *
import plotly.express as px
def hero_section():
st.title('π lexiscore')
st.markdown('A nutritional label for food for thought.')
def sidebar_section():
st.sidebar.header('settings')
st.session_state['conceptarium_url'] = st.sidebar.text_input(
'conceptarium URL')
st.session_state['access_token'] = st.sidebar.text_input(
'access token', type='password')
def add_section(parent):
parent.markdown('#### β add food for thought')
days = None
folder = None
type = parent.selectbox('What type of food for thought would you like to add?', (
'π£ RSS (OPML)', 'π Bookmarks (HTML Export)', 'π PDF', 'π EPUB', 'π Plain Text'))
if type == 'π£ RSS (OPML)':
days = parent.number_input(
'How many days does this stuff keep?', step=1)
files = parent.file_uploader(
'Place your food for thought here:', type=['xml', 'opml'])
elif type == 'π Bookmarks (HTML Export)':
folder = parent.text_input('How\'s the bookmark folder called?')
days = parent.number_input(
'How many days does this stuff keep?', step=1)
files = parent.file_uploader(
'Place your food for thought here:', type=['html'])
elif type == 'π PDF':
files = parent.file_uploader(
'Place your food for thought here:', accept_multiple_files=True, type='pdf')
elif type == 'π EPUB':
files = parent.file_uploader(
'Place your food for thought here:', accept_multiple_files=True, type='epub')
elif type == 'π Plain Text':
files = parent.file_uploader(
'Place your food for thought here:', accept_multiple_files=True, type=['text', 'txt', 'md'])
parent.caption('')
if not os.path.exists('tmp'):
os.makedirs('tmp')
if parent.button('add item'):
if files == None:
parent.warning('Please add file!')
else:
if not isinstance(files, list):
files = [files]
for file in files:
f = open(os.path.join('tmp', file.name), 'wb+')
f.write(file.getbuffer())
f.close()
path = os.path.join('tmp', file.name)
with st.spinner('Fetching content...'):
if type == 'π£ RSS (OPML)':
item_type = 'π£ RSS'
data = fetch_from_opml(path, days)
elif type == 'π Bookmarks (HTML Export)':
item_type = 'π Bookmark'
data = fetch_from_bookmarks(path, folder, days)
elif type == 'π PDF':
item_type = 'π PDF'
data = fetch_from_pdf(path)
elif type == 'π EPUB':
item_type = 'π EPUB'
data = fetch_from_epub(path)
elif type == 'π Plain Text':
item_type = 'π Plain Text'
data = fetch_from_plaintext(path)
for k, v in data.items():
if len(v[0].split()) / 250 > 1:
new_entry = pd.DataFrame([[item_type, k, round(len(v[0].split()) / 250), None, None, None, v[0], v[1], v[2]]], columns=[
'type', 'title', 'reading time', 'skill', 'challenge', 'lexiscore', 'text', 'raw', 'filename'])
st.session_state['data'] = st.session_state['data'].append(
new_entry, ignore_index=True)
st.experimental_rerun()
def cart_section(parent):
parent.markdown('#### π cart')
parent.markdown('')
parent.table(st.session_state['data'][[
'type', 'title', 'reading time', 'skill', 'challenge', 'lexiscore']])
if st.session_state['data'].shape[0] > 0:
with parent.expander('distribution'):
fig = px.scatter(st.session_state['data'], x='skill', y='challenge', hover_data=[
'title', 'lexiscore'], color_discrete_sequence=['#228b22'])
st.plotly_chart(fig)
st.download_button(
'download raw', st.session_state['data'].to_csv(), 'lexiscore.csv')
if parent.button('start labeling'):
for idx, row in st.session_state['data'].iterrows():
if row['lexiscore'] is None:
with st.spinner('Determining the nutritional value of "' + row['title'] + '"...'):
content_paragraphs = get_paragraphs(row['text'])
content_embeddings = get_embeddings(content_paragraphs)
# print('---')
# print('\n\n'.join(content_paragraphs))
if len(content_paragraphs) > 1:
results = get_closest_thoughts(content_embeddings)
skill = get_skill(results)
challenge = get_challenge(
results, content_paragraphs)
raw_challenge = get_raw_challenge(
content_paragraphs)
challenge = -(raw_challenge -
challenge) / raw_challenge
alpha = np.arctan2(
(challenge + 0.15), (skill - 0.375))
lexiscore = np.abs(alpha - 0.8) // 0.25
if lexiscore >= 4:
lexiscore = 'E'
elif lexiscore == 3:
lexiscore = 'D'
elif lexiscore == 2:
lexiscore = 'C'
elif lexiscore == 1:
lexiscore = 'B'
else:
lexiscore = 'A'
st.session_state['data'].loc[idx]['skill'] = skill
st.session_state['data'].loc[idx]['challenge'] = challenge
st.session_state['data'].loc[idx]['lexiscore'] = lexiscore
else:
print('no paragraphs:',
row['title'], '---', row['text'], '---')
st.experimental_rerun()
def meal_prep_section(parent):
if st.session_state['data'].shape[0] > 0:
parent.markdown('')
parent.markdown('#### π± meal prep')
parent.image('assets/lexiscores.png')
lexiscores = ['A', 'B', 'C', 'D', 'E']
min_lexiscore = parent.select_slider(
'Specify the minimum lexiscore to use for meal prep:', lexiscores)
allowed_lexiscores = lexiscores[:lexiscores.index(min_lexiscore) + 1]
include_predictions = parent.checkbox(
'Include labels in output document?', True)
if parent.button('start'):
selection = [
e in allowed_lexiscores for e in st.session_state['data']['lexiscore']]
selection = st.session_state['data'][selection]
if selection.shape[0] == 0:
parent.warning(
'No item qualifies for this selection, please lower the bar!')
else:
html = '<h1>π± meal prep</h1><hr><div><br/><br/></div>'
for idx, row in selection.iterrows():
if include_predictions:
html += '<img width="20%" src="file://' + \
os.path.abspath(
'assets/' + row['lexiscore'] + '.png') + '"><br/>'
html += '<h1>π₯ ' + row['title'] + '</h1>'
html += '<li><b>β±οΈ ' + \
str(row['reading time']) + '</b> minutes</li>'
#html += '<li>π lexiscore <b>' + row['lexiscore'] + '</b></li>'
html += '<hr><div><br/><br/></div>'
if row['type'] != 'π PDF':
html += row['raw']
else:
pix_paths = pdf_to_images(row['filename'])
for pix_path in pix_paths:
html += '<img src="file://' + \
os.path.abspath(pix_path) + '"><br/>'
html += '<div><br/><br/></div><hr>'
# f = open(os.path.abspath('mealprep.html'), 'w+')
# f.write(html)
parent.info(
'Meal prep complete! Please use the button below to download the results.')
parent.download_button(
label='download', data=html, file_name='mealprep.html', mime='text/html')
def footer_section():
hide_streamlit_style = '''
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
'''
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
footer = '''
---
<style>
button {
border: 4px solid;
border-color: #228b22;
border-radius: 4px;
background-color: #228b22;
color: #fffffd;
font-weight: bold;
padding-left: 5px;
padding-right: 5px;
}
</style>
<center>
<div>
<a href="https://paulbricman.com/contact"><button>send feedback</button></a>
<a href="https://github.com/paulbricman/lexiscore"><button>learn more</button></a>
<a href="https://github.com/sponsors/paulbricman"><button>support me π€</button></a>
</div>
</center>
'''
st.markdown(footer, unsafe_allow_html=True)
'''
>>> data['lexiscore'] = np.abs(data['alpha']-0.6) // (np.std(data['alpha'] / 2))
>>> px.scatter(data, x='skill', y='challenge', hover_data=['title', 'lexiscore']).show()
'''