|
import streamlit as st |
|
|
|
|
|
import pandas as pd |
|
import plotly.express as px |
|
|
|
from cryptography.fernet import Fernet |
|
import time |
|
|
|
import io |
|
from transformers import pipeline |
|
from streamlit_extras.stylable_container import stylable_container |
|
import json |
|
|
|
|
|
|
|
from io import StringIO |
|
|
|
|
|
|
|
with st.sidebar: |
|
with stylable_container( |
|
key="test_button", |
|
css_styles=""" |
|
button { |
|
background-color: yellow; |
|
border: 1px solid black; |
|
padding: 5px; |
|
color: black; |
|
} |
|
""", |
|
): |
|
st.button("DEMO APP") |
|
|
|
|
|
expander = st.expander("**Important notes on the Google Sheet Table Question Answering (QA) App**") |
|
expander.write(''' |
|
|
|
**Supported File Formats** |
|
This app works with public URLs of Google Sheets. Google Sheets must not exceed 3,000 rows. |
|
|
|
|
|
**How to Use** |
|
Paste the public URL of your Google Sheet. Press the 'Fetch Data' button, then type your question into the text area provided and click the 'Retrieve your answer' button. Always press the 'Fetch Data' button before typing a question. |
|
|
|
|
|
**Usage Limits** |
|
You can ask up to 5 questions. |
|
|
|
|
|
**Subscription Management** |
|
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] |
|
|
|
|
|
**Authorization** |
|
For security purposes, your authorization access expires hourly. To restore access, click the "Request Authorization" button. |
|
|
|
|
|
**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. |
|
For any errors or inquiries, please contact us at [email protected] |
|
|
|
|
|
''') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'fernet_key' not in st.session_state: |
|
st.session_state.fernet_key = Fernet.generate_key() |
|
|
|
key = st.session_state.fernet_key |
|
|
|
|
|
|
|
def generate_fernet_token(key, data): |
|
fernet = Fernet(key) |
|
token = fernet.encrypt(data.encode()) |
|
return token |
|
|
|
def validate_fernet_token(key, token, ttl_seconds): |
|
|
|
fernet = Fernet(key) |
|
try: |
|
decrypted_data = fernet.decrypt(token, ttl=ttl_seconds).decode() |
|
return decrypted_data, None |
|
except Exception as e: |
|
return None, f"Expired token: {e}" |
|
|
|
if 'question_attempts' not in st.session_state: |
|
st.session_state['question_attempts'] = 0 |
|
|
|
max_attempts = 5 |
|
|
|
|
|
def clear_text(): |
|
st.session_state["url"] = "" |
|
|
|
|
|
|
|
|
|
st.subheader("Google Sheet Question Answering (QA)", divider = "green") |
|
|
|
url = st.text_input("Enter Google Sheet URL:", key="url") |
|
st.button("Clear URL", on_click=clear_text) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if st.button("Fetch Data"): |
|
if url: |
|
try: |
|
spreadsheet_key = url.split('/d/')[1].split('/')[0] |
|
csv_url = f'https://docs.google.com/spreadsheets/d/{spreadsheet_key}/export?format=csv' |
|
df = pd.read_csv(csv_url, na_filter=False) |
|
|
|
|
|
|
|
st.dataframe(df) |
|
st.write("_number of rows_", df.shape[0]) |
|
st.write("_number of columns_", df.shape[1]) |
|
st.session_state.df = df |
|
|
|
if df.shape[0] > 3000: |
|
st.warning ("Google Sheets must not exceed 3,000 rows.") |
|
st.stop() |
|
|
|
|
|
except Exception as e: |
|
st.error(f"Error fetching data: {e}") |
|
else: |
|
st.warning("Please enter a Google Sheet URL.") |
|
|
|
st.divider() |
|
|
|
|
|
|
|
|
|
|
|
if 'fernet_token' not in st.session_state: |
|
if 'df' in st.session_state: |
|
df = st.session_state.df |
|
st.session_state.fernet_token = generate_fernet_token(key, df.to_json()) |
|
else: |
|
st.stop() |
|
|
|
decrypted_data_streamlit, error_streamlit = validate_fernet_token(key, st.session_state.fernet_token, ttl_seconds=3600) |
|
|
|
if error_streamlit: |
|
st.warning("Please press Request Authorization.") |
|
if st.button("Request Authorization"): |
|
if 'df' in st.session_state: |
|
df = st.session_state.df |
|
st.session_state.fernet_token = generate_fernet_token(key, df.to_json()) |
|
st.success("Authorization granted") |
|
decrypted_data_streamlit, error_streamlit = validate_fernet_token(key, st.session_state.fernet_token, ttl_seconds=3600) |
|
if error_streamlit: |
|
st.error(f"Your authorization has expired: {error_streamlit}") |
|
st.stop() |
|
if error_streamlit: |
|
st.error("Please paste the public URL of your Google Sheet.") |
|
st.stop() |
|
else: |
|
try: |
|
df = pd.read_json(decrypted_data_streamlit) |
|
except Exception as e: |
|
st.error(f"Error decoding data: {e}") |
|
st.stop() |
|
else: |
|
st.error(f"Your authorization has expired: {error_streamlit}") |
|
st.stop() |
|
|
|
|
|
|
|
|
|
def clear_question(): |
|
st.session_state["question"] = "" |
|
|
|
question = st.text_input("Type your question here:", key="question") |
|
st.button("Clear question", on_click=clear_question) |
|
|
|
|
|
if 'df' in st.session_state: |
|
df = st.session_state.df |
|
if st.button("Retrieve your answer"): |
|
if st.session_state.question_attempts < max_attempts: |
|
try: |
|
tqa = pipeline(task="table-question-answering", model="microsoft/tapex-large-finetuned-wtq") |
|
answer = tqa(table=df, query=question)['answer'] |
|
st.write(f"Answer: {answer}") |
|
st.session_state.question_attempts += 1 |
|
except Exception as e: |
|
st.error(f"Error retrieving answer: {e}") |
|
else: |
|
st.error("Maximum question attempts reached.") |
|
else: |
|
st.warning("Please fetch data first.") |
|
|
|
st.divider() |
|
st.write(f"Number of times you asked a question: {st.session_state['question_attempts']}/{max_attempts}") |
|
|