File size: 5,693 Bytes
60ac372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbbf15c
60ac372
 
 
 
 
 
 
bbbf15c
 
 
28719ee
bbbf15c
 
 
 
 
 
 
60ac372
 
 
 
 
 
 
 
1207dc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60ac372
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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()