nlpblogs's picture
Update app.py
cb5868e verified
raw
history blame
8.93 kB
import streamlit as st
import time
import pandas as pd
import io
from transformers import pipeline
from streamlit_extras.stylable_container import stylable_container
import json
import plotly.express as px
st.subheader("AI CSV and XLSX Data Analyzer", divider="blue")
st.link_button("by nlpblogs", "https://nlpblogs.com", type = "tertiary")
expander = st.expander("**Important notes on the AI CSV and XLSX Data Analyzer**")
expander.write('''
**Supported File Formats:**
This app accepts files in .csv and .xlsx formats.
**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.
**Usage Limits:**
You can ask up to 5 questions.
**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 [email protected]
**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.
**File Handling and Errors:**
The app may display an error message if your file has errors or date values.
For any errors or inquiries, please contact us at [email protected]
''')
with st.sidebar:
container = st.container(border=True)
container.write("**Question-Answering (QA)** is the task of retrieving the answer to a question from a given text (knowledge base), which is used as context.")
st.subheader("Related NLP Web Apps", divider = "blue")
st.link_button("AI Google Sheet Data Analyzer", "https://nlpblogs.com/shop/table-question-answering-qa/google-sheet-qa-demo-app/", type = "primary")
if 'question_attempts' not in st.session_state:
st.session_state['question_attempts'] = 0
max_attempts = 5
upload_file = st.file_uploader("Upload your file. Accepted file formats include: .csv, .xlsx", type=['csv', 'xlsx'])
if upload_file is not None:
file_extension = upload_file.name.split('.')[-1].lower()
if file_extension == 'csv':
try:
df = pd.read_csv(upload_file, na_filter=False)
if df.isnull().values.any():
st.error("Error: The CSV file contains missing values.")
st.stop()
else:
new_columns = [f'column_{i+1}' for i in range(len(df.columns))]
df.columns = new_columns
all_columns = df.columns.tolist()
st.subheader("Select columns for the Tree Map", divider="blue")
parent_column = st.selectbox("Select the parent column:", all_columns)
value_column = st.selectbox("Select the value column:", all_columns)
if parent_column and value_column:
if parent_column == value_column:
st.warning("Warning: You have selected the same column for both the parent and value column. Please select two different columns from your data.")
elif parent_column and value_column:
path_columns = [px.Constant("all"), parent_column, value_column]
fig = px.treemap(df,
path=path_columns)
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
st.subheader("Tree map", divider="red")
st.plotly_chart(fig)
st.session_state.df = df
except pd.errors.ParserError:
st.error("Error: The CSV file is not readable or is incorrectly formatted.")
st.stop()
except UnicodeDecodeError:
st.error("Error: The CSV file could not be decoded.")
st.stop()
except Exception as e:
st.error(f"An unexpected error occurred while reading CSV: {e}")
st.stop()
elif file_extension == 'xlsx':
try:
df = pd.read_excel(upload_file, na_filter=False)
if df.isnull().values.any():
st.error("Error: The Excel file contains missing values.")
st.stop()
else:
new_columns = [f'column_{i+1}' for i in range(len(df.columns))]
df.columns = new_columns
all_columns = df.columns.tolist()
st.subheader("Select columns for the Tree Map", divider="blue")
parent_column = st.selectbox("Select the parent column:", all_columns)
value_column = st.selectbox("Select the value column:", all_columns)
if parent_column and value_column:
if parent_column == value_column:
st.warning("Warning: You have selected the same column for both the parent and value column. Please select two different columns from your data.")
elif parent_column and value_column:
path_columns = [px.Constant("all"), parent_column, value_column]
fig = px.treemap(df,
path=path_columns)
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
st.subheader("Tree map", divider="red")
st.plotly_chart(fig)
st.session_state.df = df
except ValueError:
st.error("Error: The Excel file is not readable or is incorrectly formatted.")
st.stop()
except Exception as e:
st.error(f"An unexpected error occurred while reading Excel: {e}")
st.stop()
else:
st.warning("Unsupported file type.")
st.stop()
st.divider()
if upload_file is not None:
file_extension = upload_file.name.split('.')[-1].lower()
if file_extension == 'csv':
try:
df = pd.read_csv(upload_file, na_filter=False)
if df.isnull().values.any():
st.error("Error: The CSV file contains missing values.")
st.stop()
else:
st.dataframe(df, key="csv_dataframe")
st.write("_number of rows_", df.shape[0])
st.write("_number of columns_", df.shape[1])
st.session_state.df = df
except pd.errors.ParserError:
st.error("Error: The CSV file is not readable or is incorrectly formatted.")
st.stop()
except UnicodeDecodeError:
st.error("Error: The CSV file could not be decoded.")
st.stop()
except Exception as e:
st.error(f"An unexpected error occurred while reading CSV: {e}")
st.stop()
elif file_extension == 'xlsx':
try:
df = pd.read_excel(upload_file, na_filter=False)
if df.isnull().values.any():
st.error("Error: The Excel file contains missing values.")
st.stop()
else:
st.dataframe(df, key="excel_dataframe")
st.write("_number of rows_", df.shape[0])
st.write("_number of columns_", df.shape[1])
st.session_state.df = df
except ValueError:
st.error("Error: The Excel file is not readable or is incorrectly formatted.")
st.stop()
except Exception as e:
st.error(f"An unexpected error occurred while reading Excel: {e}")
st.stop()
else:
st.warning("Unsupported file type.")
st.stop()
def clear_question():
st.session_state["question"] = ""
question = st.text_input("Type your question here and then press **Retrieve your answer**:", key="question")
st.button("Clear question", on_click=clear_question)
if st.button("Retrieve your answer"):
if st.session_state['question_attempts'] >= max_attempts:
st.error(f"You have asked {max_attempts} questions. Maximum question attempts reached.")
st.stop()
st.session_state['question_attempts'] += 1
with st.spinner("Wait for it...", show_time=True):
time.sleep(5)
if df is not None:
tqa = pipeline(task="table-question-answering", model="microsoft/tapex-large-finetuned-wtq")
st.write(tqa(table=df, query=question)['answer'])
st.divider()
st.write(f"Number of questions asked: {st.session_state['question_attempts']}/{max_attempts}")