AITOOL / app.py
aiscientist's picture
Update app.py
28719ee verified
raw
history blame
5.69 kB
import pandas as pd
import streamlit.components.v1 as components
import streamlit as st
from ydata_profiling import ProfileReport
from streamlit_pandas_profiling import st_profile_report
from langchain.llms.openai import OpenAI
from langchain_experimental.agents import create_csv_agent
from langchain.agents.agent_types import AgentType
import time
import os
from mitosheet.streamlit.v1 import spreadsheet
from pygwalker.api.streamlit import init_streamlit_comm, get_streamlit_html
st.set_page_config(
page_title="AI TOOL",
layout="wide"
)
def main():
st.sidebar.title("App Options")
option = st.sidebar.selectbox("Choose an option", ["View Instructions", "View Data","Data Profiling","Tableau AI", "CSV Chatbot"])
if option == "View Instructions":
show_instructions()
elif option == "Data Profiling":
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
if uploaded_file is None:
st.warning("Please upload a CSV file.")
st.stop() # Stop execution if no file uploaded
else:
data_profiling(uploaded_file)
elif option == "CSV Chatbot":
openai_api_key = st.text_input("Enter your OpenAI API Key", type="password")
if not openai_api_key:
st.warning("You should have an OpenAI API key to continue. Get one at [OpenAI API Keys](https://platform.openai.com/api-keys)")
st.stop()
os.environ['OPENAI_API_KEY'] = openai_api_key
personal_assistant()
elif option == "View Data":
virtual_excel_sheet()
elif option == "Tableau AI":
tableau_ai()
def show_instructions():
st.title("Welcome to the AI TOOL - Made for MDH")
st.write("This tool offers several functionalities to help you analyze and work with your data.")
st.write("Please select an option from the sidebar to proceed:")
st.write("- **View Data:** Upload a CSV file and view its contents.")
st.write("- **Data Profiling:** Upload a CSV file to generate a data profiling report.")
st.write("- **CSV Chatbot:** Interact with a chatbot to get insights from your CSV data.")
st.write("- **Tableau AI:** Upload a CSV file to visualize it using Tableau AI.")
st.write("- **View Instructions:** View these instructions again.")
st.markdown(
"""
<a href="https://www.linkedin.com/in/your_linkedin_profile/" target="_blank">
<img src="https://github.com/dheereshagrwal/colored-icons/blob/master/public/icons/linkedin/linkedin.svg" width="30" height="30"/>
</a>
<a href="https://github.com/your_github_username" target="_blank">
<img src="https://img.icons8.com/fluency/48/000000/github.png" width="30" height="30"/>
</a>
""",
unsafe_allow_html=True
)
def data_profiling(uploaded_file):
st.title("Data Profiling App")
df = pd.read_csv(uploaded_file)
st.dataframe(df)
# Generate and display the data profile report
pr = ProfileReport(df, title="Report")
st_profile_report(pr)
def personal_assistant():
st.sidebar.title("OpenAI Settings")
st.title("Personal Assistant")
st.text("A BR CREATION")
st.image("chatbot.jpg", caption="Chatbot", width=178)
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
if uploaded_file is None:
st.warning("Please upload a CSV file.")
st.stop() # Stop execution if no file uploaded
llm = OpenAI(temperature=0)
agent = create_csv_agent(
llm,
uploaded_file,
verbose=False,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)
predefined_questions = ["How many rows are there in the dataset?", "Explain the dataset."]
selected_question = st.selectbox("Select a question", ["Select a question"] + predefined_questions)
custom_question = st.text_input("Or ask a custom question")
if st.button("Ask"):
if selected_question != "Select a question":
query = selected_question
elif custom_question.strip() != "":
query = custom_question.strip()
else:
st.warning("Please select a predefined question or ask a custom question.")
return
start = time.time()
answer = agent.run(query)
end = time.time()
st.write(answer)
st.write(f"Answer (took {round(end - start, 2)} s.)")
def virtual_excel_sheet():
st.title("Data Viewer Portal")
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
if uploaded_file is None:
st.warning("Please upload a CSV file.")
st.stop() # Stop execution if no file uploaded
df = pd.read_csv(uploaded_file)
# Convert the dataframe to a list of dictionaries
dataframe = df.to_dict(orient="records")
# Display the dataframe in a Mito spreadsheet
final_dfs, code = spreadsheet(dataframe)
def tableau_ai():
st.title("Virtual Tableau AI Tool")
init_streamlit_comm()
# Function to get PygWalker HTML
@st.cache_data
def get_pyg_html(df: pd.DataFrame) -> str:
html = get_streamlit_html(df, use_kernel_calc=True, debug=False)
return html
# Function to get user uploaded DataFrame
def get_user_uploaded_data():
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
if uploaded_file is not None:
return pd.read_csv(uploaded_file)
return None
df = get_user_uploaded_data()
if df is not None:
components.html(get_pyg_html(df), width=1300, height=1000, scrolling=True)
else:
st.write("Please upload a CSV file to proceed.")
if __name__ == "__main__":
main()