Update app.py
Browse files
app.py
CHANGED
@@ -8,61 +8,49 @@ import json
|
|
8 |
import plotly.express as px
|
9 |
|
10 |
st.subheader("AI CSV and XLSX Data Analyzer", divider="blue")
|
11 |
-
st.link_button("by nlpblogs", "https://nlpblogs.com", type
|
12 |
|
13 |
expander = st.expander("**Important notes on the AI CSV and XLSX Data Analyzer**")
|
14 |
-
expander.write(
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
You can ask up to 5 questions.
|
27 |
-
|
28 |
-
**Subscription Management:**
|
29 |
-
This app offers a one-day free trial, followed by a one-day subscription, expiring after 24 hours. If you are interested in building your own AI CSV and XLSX Data Analyzer, 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 info@nlpblogs.com
|
30 |
-
|
31 |
-
**Customization:**
|
32 |
-
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.
|
33 |
-
|
34 |
-
**File Handling and Errors:**
|
35 |
-
(a) The app may provide an inaccurate answer if the information is missing from the relevant cell. (b) The app may display an error message if your file has errors, date values or float numbers (0.5, 1.2, 4.5 etc.).
|
36 |
-
|
37 |
-
For any errors or inquiries, please contact us at info@nlpblogs.com
|
38 |
-
|
39 |
-
''')
|
40 |
-
|
41 |
|
42 |
with st.sidebar:
|
43 |
container = st.container(border=True)
|
44 |
-
container.write(
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
max_attempts = 5
|
53 |
|
54 |
-
|
55 |
-
|
|
|
56 |
|
57 |
if upload_file is not None:
|
58 |
-
file_extension = upload_file.name.split(
|
59 |
try:
|
60 |
-
if file_extension ==
|
61 |
df_original = pd.read_csv(upload_file, na_filter=False)
|
62 |
-
|
63 |
-
elif file_extension == 'xlsx':
|
64 |
df_original = pd.read_excel(upload_file, na_filter=False)
|
65 |
-
|
66 |
else:
|
67 |
st.warning("Unsupported file type.")
|
68 |
st.stop()
|
@@ -71,23 +59,26 @@ if upload_file is not None:
|
|
71 |
st.error("Error: The file contains missing values.")
|
72 |
st.stop()
|
73 |
else:
|
74 |
-
st.session_state.df_original = df_original
|
75 |
|
76 |
all_columns = df_original.columns.tolist()
|
77 |
st.divider()
|
78 |
-
st.write(
|
79 |
-
|
|
|
|
|
80 |
parent_column = st.selectbox("Select the parent column:", all_columns)
|
81 |
value_column = st.selectbox("Select the value column:", all_columns)
|
82 |
-
|
83 |
if parent_column and value_column:
|
84 |
if parent_column == value_column:
|
85 |
-
st.warning(
|
|
|
|
|
86 |
else:
|
87 |
-
df_treemap = df_original.copy()
|
|
|
88 |
path_columns = [px.Constant("all"), parent_column, value_column]
|
89 |
-
fig = px.treemap(df_treemap,
|
90 |
-
path=path_columns)
|
91 |
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
|
92 |
st.subheader("Tree Map", divider="blue")
|
93 |
st.plotly_chart(fig)
|
@@ -111,28 +102,55 @@ if upload_file is not None:
|
|
111 |
st.stop()
|
112 |
|
113 |
st.divider()
|
114 |
-
|
115 |
|
116 |
def clear_question():
|
117 |
st.session_state["question"] = ""
|
118 |
|
119 |
-
|
|
|
|
|
|
|
120 |
st.button("Clear question", on_click=clear_question)
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
if st.button("Retrieve your answer"):
|
124 |
-
if st.session_state[
|
125 |
st.error(f"You have asked {max_attempts} questions. Maximum question attempts reached.")
|
126 |
st.stop()
|
127 |
-
st.session_state[
|
128 |
-
|
129 |
with st.spinner("Wait for it...", show_time=True):
|
130 |
-
time.sleep(
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
st.divider()
|
136 |
-
st.write(
|
|
|
|
|
137 |
|
138 |
|
|
|
8 |
import plotly.express as px
|
9 |
|
10 |
st.subheader("AI CSV and XLSX Data Analyzer", divider="blue")
|
11 |
+
st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
|
12 |
|
13 |
expander = st.expander("**Important notes on the AI CSV and XLSX Data Analyzer**")
|
14 |
+
expander.write(
|
15 |
+
"""
|
16 |
+
**Supported File Formats:** This app accepts files in .csv and .xlsx formats.
|
17 |
+
**How to Use:** Upload your file first. Select two different columns from your data to visualize in a Tree Map. Then, type your question into the text area provided and click the 'Retrieve your answer' button.
|
18 |
+
**Tree Map:** Your uploaded data is presented in an interactive Tree Map for visual exploration. Click on any area within the map to access specific data insights.
|
19 |
+
**Usage Limits:** You can ask up to 5 questions.
|
20 |
+
**Subscription Management:** This app offers a one-day free trial, followed by a one-day subscription, expiring after 24 hours. If you are interested in building your own AI CSV and XLSX Data Analyzer, 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 info@nlpblogs.com
|
21 |
+
**Customization:** 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.
|
22 |
+
**File Handling and Errors:** (a) The app may provide an inaccurate answer if the information is missing from the relevant cell. (b) The app may display an error message if your file has errors, date values or float numbers (0.5, 1.2, 4.5 etc.).
|
23 |
+
For any errors or inquiries, please contact us at info@nlpblogs.com
|
24 |
+
"""
|
25 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
with st.sidebar:
|
28 |
container = st.container(border=True)
|
29 |
+
container.write(
|
30 |
+
"**Question-Answering (QA)** is the task of retrieving the answer to a question from a given text (knowledge base), which is used as context."
|
31 |
+
)
|
32 |
+
st.subheader("Related NLP Web Apps", divider="blue")
|
33 |
+
st.link_button(
|
34 |
+
"AI Google Sheet Data Analyzer",
|
35 |
+
"https://nlpblogs.com/shop/table-question-answering-qa/google-sheet-qa-demo-app/",
|
36 |
+
type="primary",
|
37 |
+
)
|
38 |
+
|
39 |
+
if "question_attempts" not in st.session_state:
|
40 |
+
st.session_state["question_attempts"] = 0
|
41 |
max_attempts = 5
|
42 |
|
43 |
+
upload_file = st.file_uploader(
|
44 |
+
"Upload your file. Accepted file formats include: .csv, .xlsx", type=["csv", "xlsx"]
|
45 |
+
)
|
46 |
|
47 |
if upload_file is not None:
|
48 |
+
file_extension = upload_file.name.split(".")[-1].lower()
|
49 |
try:
|
50 |
+
if file_extension == "csv":
|
51 |
df_original = pd.read_csv(upload_file, na_filter=False)
|
52 |
+
elif file_extension == "xlsx":
|
|
|
53 |
df_original = pd.read_excel(upload_file, na_filter=False)
|
|
|
54 |
else:
|
55 |
st.warning("Unsupported file type.")
|
56 |
st.stop()
|
|
|
59 |
st.error("Error: The file contains missing values.")
|
60 |
st.stop()
|
61 |
else:
|
62 |
+
st.session_state.df_original = df_original
|
63 |
|
64 |
all_columns = df_original.columns.tolist()
|
65 |
st.divider()
|
66 |
+
st.write(
|
67 |
+
"Select two different columns from your data to visualize in a **Tree Map**. "
|
68 |
+
)
|
69 |
+
|
70 |
parent_column = st.selectbox("Select the parent column:", all_columns)
|
71 |
value_column = st.selectbox("Select the value column:", all_columns)
|
|
|
72 |
if parent_column and value_column:
|
73 |
if parent_column == value_column:
|
74 |
+
st.warning(
|
75 |
+
"Warning: You have selected the same column for both the parent and value column. Please select two different columns from your data."
|
76 |
+
)
|
77 |
else:
|
78 |
+
df_treemap = df_original.copy()
|
79 |
+
|
80 |
path_columns = [px.Constant("all"), parent_column, value_column]
|
81 |
+
fig = px.treemap(df_treemap, path=path_columns)
|
|
|
82 |
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
|
83 |
st.subheader("Tree Map", divider="blue")
|
84 |
st.plotly_chart(fig)
|
|
|
102 |
st.stop()
|
103 |
|
104 |
st.divider()
|
105 |
+
|
106 |
|
107 |
def clear_question():
|
108 |
st.session_state["question"] = ""
|
109 |
|
110 |
+
|
111 |
+
question = st.text_input(
|
112 |
+
"Type your question here and then press **Retrieve your answer**:", key="question"
|
113 |
+
)
|
114 |
st.button("Clear question", on_click=clear_question)
|
115 |
|
116 |
+
# --- Sampling Implementation ---
|
117 |
+
SAMPLE_SIZE = 500 # Define the number of rows to sample
|
118 |
+
if "df_original" in st.session_state:
|
119 |
+
df_for_qa = st.session_state.df_original
|
120 |
+
if df_for_qa.shape[0] > SAMPLE_SIZE:
|
121 |
+
st.warning(f"The uploaded file has {df_for_qa.shape[0]} rows. For faster processing and to avoid memory issues, a sample of {SAMPLE_SIZE} rows will be used for question answering.")
|
122 |
+
df_for_qa = df_for_qa.sample(n=SAMPLE_SIZE, random_state=42) # Set random_state for reproducibility
|
123 |
+
else:
|
124 |
+
st.info("The file size is within the limit, using the entire dataset for question answering.")
|
125 |
+
else:
|
126 |
+
df_for_qa = None
|
127 |
+
# --- End of Sampling Implementation ---
|
128 |
|
129 |
if st.button("Retrieve your answer"):
|
130 |
+
if st.session_state["question_attempts"] >= max_attempts:
|
131 |
st.error(f"You have asked {max_attempts} questions. Maximum question attempts reached.")
|
132 |
st.stop()
|
133 |
+
st.session_state["question_attempts"] += 1
|
134 |
+
|
135 |
with st.spinner("Wait for it...", show_time=True):
|
136 |
+
time.sleep(2) # Reduced sleep time for better responsiveness
|
137 |
+
|
138 |
+
if df_for_qa is not None:
|
139 |
+
try:
|
140 |
+
tqa = pipeline(
|
141 |
+
task="table-question-answering",
|
142 |
+
model="microsoft/tapex-large-finetuned-wtq",
|
143 |
+
)
|
144 |
+
answer = tqa(table=df_for_qa, query=question)["answer"]
|
145 |
+
st.write(answer)
|
146 |
+
except Exception as e:
|
147 |
+
st.error(f"An error occurred during question answering: {e}")
|
148 |
+
else:
|
149 |
+
st.warning("Please upload a file first.")
|
150 |
|
151 |
st.divider()
|
152 |
+
st.write(
|
153 |
+
f"Number of questions asked: {st.session_state['question_attempts']}/{max_attempts}"
|
154 |
+
)
|
155 |
|
156 |
|