Maria Tsilimos commited on
Commit
bacbf5b
·
unverified ·
1 Parent(s): 3522284

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -0
app.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from cryptography.fernet import Fernet
3
+ import time
4
+ import pandas as pd
5
+ import io
6
+ from transformers import pipeline
7
+ from streamlit_extras.stylable_container import stylable_container
8
+ import json
9
+
10
+ st.subheader("Table Question Answering (QA)", divider="blue")
11
+
12
+ # generate Fernet key
13
+ if 'fernet_key' not in st.session_state:
14
+ st.session_state.fernet_key = Fernet.generate_key()
15
+
16
+ key = st.session_state.fernet_key
17
+
18
+
19
+ # function for generating and validating fernet key
20
+ def generate_fernet_token(key, data):
21
+ fernet = Fernet(key)
22
+ token = fernet.encrypt(data.encode())
23
+ return token
24
+
25
+ def validate_fernet_token(key, token, ttl_seconds):
26
+
27
+ fernet = Fernet(key)
28
+ try:
29
+ decrypted_data = fernet.decrypt(token, ttl=ttl_seconds).decode()
30
+ return decrypted_data, None
31
+ except Exception as e:
32
+ return None, f"Expired token: {e}"
33
+
34
+ # sidebar
35
+ with st.sidebar:
36
+ with stylable_container(
37
+ key="test_button",
38
+ css_styles="""
39
+ button {
40
+ background-color: yellow;
41
+ border: 1px solid black;
42
+ padding: 5px;
43
+ color: black;
44
+ }
45
+ """,
46
+ ):
47
+ st.button("DEMO APP")
48
+
49
+
50
+ expander = st.expander("**Important notes on the Table Question Answering (QA) App**")
51
+ expander.write('''
52
+
53
+ **Supported File Formats**
54
+ This app accepts files in .csv and .xlsx formats.
55
+
56
+ **How to Use**
57
+ Upload your file first. Then, type your question into the text area provided and click the 'Retrieve your answer' button.
58
+
59
+ **Usage Limits**
60
+ You can ask up to 5 questions.
61
+
62
+ **Subscription Management**
63
+ This demo app offers a one-day subscription, expiring after 24 hours. If you are interested in building your own Table Question Answering (QA) Web App, we invite you to explore our NLP Web App Store on our website. You can select your desired features, place your order, and we will deliver your custom app in five business days. If you wish to delete your Account with us, please contact us at [email protected]
64
+
65
+ **Authorization**
66
+ For security purposes, your authorization access expires hourly. To restore access, click the 'Request Authorization' button.
67
+
68
+ **Customization**
69
+ To change the app's background color to white or black, click the three-dot menu on the right-hand side of your app, go to Settings and then Choose app theme, colors and fonts.
70
+
71
+ **File Handling and Errors**
72
+ The app may display an error message if your file has errors or date values.
73
+ For any errors or inquiries, please contact us at [email protected]
74
+
75
+ ''')
76
+
77
+
78
+ # count attempts based on questions
79
+ if 'question_attempts' not in st.session_state:
80
+ st.session_state['question_attempts'] = 0
81
+
82
+ max_attempts = 5
83
+
84
+ # upload file
85
+ upload_file = st.file_uploader("Upload your file. Accepted file formats include: .csv, .xlsx", type=['csv', 'xlsx'])
86
+
87
+
88
+ if upload_file is not None:
89
+ file_extension = upload_file.name.split('.')[-1].lower()
90
+ if file_extension == 'csv':
91
+ try:
92
+ df = pd.read_csv(upload_file, na_filter=False)
93
+ if df.isnull().values.any():
94
+ st.error("Error: The CSV file contains missing values.")
95
+ st.stop()
96
+ else:
97
+ st.dataframe(df, key="csv_dataframe")
98
+ st.write("_number of rows_", df.shape[0])
99
+ st.write("_number of columns_", df.shape[1])
100
+ st.session_state.df = df
101
+ except pd.errors.ParserError:
102
+ st.error("Error: The CSV file is not readable or is incorrectly formatted.")
103
+ st.stop()
104
+ except UnicodeDecodeError:
105
+ st.error("Error: The CSV file could not be decoded.")
106
+ st.stop()
107
+ except Exception as e:
108
+ st.error(f"An unexpected error occurred while reading CSV: {e}")
109
+ st.stop()
110
+ elif file_extension == 'xlsx':
111
+ try:
112
+ df = pd.read_excel(upload_file, na_filter=False)
113
+
114
+ if df.isnull().values.any():
115
+ st.error("Error: The Excel file contains missing values.")
116
+ st.stop()
117
+ else:
118
+ st.dataframe(df, key="excel_dataframe")
119
+ st.write("_number of rows_", df.shape[0])
120
+ st.write("_number of columns_", df.shape[1])
121
+ st.session_state.df = df
122
+ except ValueError:
123
+ st.error("Error: The Excel file is not readable or is incorrectly formatted.")
124
+ st.stop()
125
+ except Exception as e:
126
+ st.error(f"An unexpected error occurred while reading Excel: {e}")
127
+ st.stop()
128
+ else:
129
+ st.warning("Unsupported file type.")
130
+ st.stop()
131
+
132
+ # generate and validate Fernet token for the current file
133
+ if 'fernet_token' not in st.session_state:
134
+ if 'df' in st.session_state:
135
+ df = st.session_state.df
136
+
137
+ st.session_state.fernet_token = generate_fernet_token(key, df.to_json())
138
+ else:
139
+ st.stop()
140
+
141
+ decrypted_data_streamlit, error_streamlit = validate_fernet_token(key, st.session_state.fernet_token, ttl_seconds=3600)
142
+
143
+ if error_streamlit:
144
+ st.warning("Please press Request Authorization. Please note that a file should be uploaded before you press Request Authorization.")
145
+ if st.button("Request Authorization"):
146
+ if 'df' in st.session_state:
147
+ df = st.session_state.df
148
+ st.session_state.fernet_token = generate_fernet_token(key, df.to_json())
149
+ st.success("Authorization granted")
150
+ decrypted_data_streamlit, error_streamlit = validate_fernet_token(key, st.session_state.fernet_token, ttl_seconds=3600)
151
+ if error_streamlit:
152
+ st.error(f"Your authorization has expired: {error_streamlit}")
153
+ st.stop()
154
+ if error_streamlit:
155
+ st.error("Please upload a file.")
156
+ st.stop()
157
+ else:
158
+ try:
159
+ df = pd.read_json(decrypted_data_streamlit)
160
+ except Exception as e:
161
+ st.error(f"Error decoding data: {e}")
162
+ st.stop()
163
+ else:
164
+ st.error(f"Your authorization has expired: {error_streamlit}")
165
+ st.stop()
166
+
167
+ st.divider()
168
+
169
+ # ask question
170
+ def clear_question():
171
+ st.session_state["question"] = ""
172
+
173
+ question = st.text_input("Type your question here and then press **Retrieve your answer**:", key="question")
174
+ st.button("Clear question", on_click=clear_question)
175
+
176
+ #retrive answer
177
+ if st.button("Retrieve your answer"):
178
+ if st.session_state['question_attempts'] >= max_attempts:
179
+ st.error(f"You have asked {max_attempts} questions. Maximum question attempts reached.")
180
+ st.stop()
181
+ st.session_state['question_attempts'] += 1
182
+ if error_streamlit:
183
+ st.warning("Please enter a question before retrieving the answer.")
184
+ else:
185
+ with st.spinner('Wait for it...'):
186
+ time.sleep(2)
187
+ if df is not None:
188
+ tqa = pipeline(task="table-question-answering", model="microsoft/tapex-large-finetuned-wtq")
189
+ st.write(tqa(table=df, query=question)['answer'])
190
+
191
+ st.divider()
192
+ st.write(f"Number of questions asked: {st.session_state['question_attempts']}/{max_attempts}")