-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
179 lines (140 loc) · 7.81 KB
/
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
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
from engine import Engine
import openai
import streamlit as st
def app():
# some stateful variables
if 'latest_insertions' not in st.session_state:
st.session_state['latest_insertions'] = None
if 'insertion_cancelled' not in st.session_state:
st.session_state['insertion_cancelled'] = False
# allowed categories
all_categories = ["Family", "Work", "Friends", "Shopping", "Health", "Finance", "Travel", "Home", "Pets", "Hobbies", "Reminders",
"Ideas", "Email", "Phone", "Address", "Other"]
default_categories = ["Family", "Work", "Friends", "Shopping", "Ideas", "Health", "Other"]
# setup the engine
@st.cache_resource
def create_engine():
return Engine(default_categories=default_categories)
engine = create_engine()
st.markdown("<h1 style='text-align: center; color: black;'>SLICEMATE</h1>", unsafe_allow_html=True)
st.markdown("<h6 style='text-align: center; color: black;'>Dump, Retrieve, and Explore your world of facts, reminders, purchases, prices, notes and more!</h1>", unsafe_allow_html=True)
# --------------------------------------------------------------------------------
# Sidebar
# --------------------------------------------------------------------------------
# get the OpenAI API key, wither from the OPENAI_API_KEY environment variable or from user input.
st.sidebar.header("Parameters")
token = st.sidebar.text_input('OpenAI API access token',
openai.api_key if openai.api_key is not None else '',
type='password',
help='Get it on https://beta.openai.com/')
selected_categories = st.sidebar.multiselect('Possible categories to consider when adding facts',
all_categories,
engine.allowed_categories())
engine.update_categories(selected_categories)
engine.set_openai_api_key(token)
# different tabs for searching and for data insertion
tab1, tab2 = st.tabs(["Search facts", "Add facts"])
# --------------------------------------------------------------------------------
# Search facts tab
# --------------------------------------------------------------------------------
with tab1:
query = st.text_input('Query', '', help='Type a few keywords to query.')
filter_col1, filter_col2, filter_col3 = st.columns(3)
with filter_col1:
categories_filter = st.multiselect(
'Filter by Category:',
engine.unique_categories_in_database(),
[])
with filter_col2:
entry_types_filter = st.multiselect(
'Filter by Type:',
engine.unique_entry_types_in_database(),
[])
with filter_col3:
people_filter = st.multiselect(
'Filter by People:',
engine.unique_people_in_database(),
[])
df_results = engine.query(query,
categories=categories_filter,
entry_types=entry_types_filter,
people=people_filter)
st.subheader("Results")
st.dataframe(df_results, use_container_width=True)
# download results
st.caption("You can download this view of the data as a CSV, TSV or Excel file.")
download_col1, download_col2, download_col3 = st.columns(3)
with download_col1:
generate_excel_download = st.button("Generate downloadable Excel")
with download_col2:
generate_csv_download = st.button("Generate downloadable CSV")
with download_col3:
generate_tsv_download = st.button("Generate downloadable TSV")
def export_selected_data(file_type):
return engine.export_data_to_binary(df_results, file_type=file_type)
if generate_csv_download:
st.download_button("Download This Data",
data=export_selected_data(file_type="csv"),
file_name="out.csv",
mime='text/csv')
if generate_tsv_download:
st.download_button("Download This Data",
data=export_selected_data(file_type="tsv"),
file_name="out.tsv",
mime='text/tsv')
if generate_excel_download:
st.download_button("Download This Data",
data=export_selected_data(file_type="excel"),
file_name="out.xlsx",
mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
# --------------------------------------------------------------------------------
# Insert facts tab
# --------------------------------------------------------------------------------
with tab2:
st.text("Type your facts, reminders, purchases, etc.")
with st.form("new_facts_form", clear_on_submit=True):
new_facts_utterance = st.text_input('New Facts', '', help='Write your facts here.')
add_facts = st.form_submit_button("Add facts")
manual_check = st.checkbox("Check before adding", value=False)
# placeholder for where the manual check pane will be
manual_check_pane = st.empty()
# auxiliary function to commit extraction, will be used more than once below
def aux_commit_extraction():
st.session_state['latest_insertions'] = engine.extracted_facts()
engine.commit()
# EXTRACT: If we don't have extracted facts yet, let's try to do that.
if not engine.has_extracted_facts():
if add_facts:
engine.extract_facts(new_facts_utterance)
# COMMIT: If now we have the extracted facts, prepare to commit or commit them directly.
if engine.has_extracted_facts():
# does the user wants to manually check the extracted facts?
if manual_check:
with manual_check_pane.container():
accept = st.button("Accept fact extraction")
cancel = st.button("Cancel fact extraction")
st.write("Extracted facts:")
st.write(engine.extracted_facts())
if accept:
aux_commit_extraction()
elif cancel:
engine.cancel()
st.session_state['insertion_cancelled'] = True
else: # no manual check needed, let's just commit
aux_commit_extraction()
# --------------------------------------------------------------------------------
# Status messages
# --------------------------------------------------------------------------------
if st.session_state['latest_insertions'] is not None:
st.success(f"Added {st.session_state['latest_insertions']}.")
st.session_state['latest_insertions'] = None
manual_check_pane.empty()
elif st.session_state['insertion_cancelled'] == True:
st.info("Insertion cancelled.")
st.session_state['insertion_cancelled'] = False
manual_check_pane.empty()
if __name__ == '__main__':
app()