File size: 7,050 Bytes
e9c0cce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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

# Global variable to store uploaded file
uploaded_file = None

def main():
    global uploaded_file
    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":
        data_profiling()
    elif option == "CSV Chatbot":
        csv_chatbot()
    elif option == "View Data":
        view_data()
    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.")

def data_profiling():
    global uploaded_file
    st.title("Data Profiling App")
    if uploaded_file is None:
        uploaded_file = st.file_uploader("Upload CSV file", type=["csv", "xlsx"])
        if uploaded_file is None:
            st.warning("Please upload a CSV or Excel file.")
            st.stop()  # Stop execution if no file uploaded

    if uploaded_file.name.endswith('.xlsx'):
        # Load Excel file into pandas DataFrame
        df_excel = pd.read_excel(uploaded_file)
        # Save DataFrame as CSV
        csv_filename = uploaded_file.name.replace('.xlsx', '.csv')
        df_excel.to_csv(csv_filename, index=False)
        st.success(f"Excel file converted to CSV: {csv_filename}")
        # Set uploaded file to the converted CSV file
        uploaded_file = open(csv_filename, 'rb')

    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 csv_chatbot():
    global uploaded_file
    st.sidebar.title("OpenAI Settings")
    st.title("Personal Assistant")
    st.text("A BR CREATION")
    st.warning("Also try Google PALM @ [PALMCSV](https://palmcsvbot.streamlit.app/)")
    st.image("chatbot.jpg", caption="Chatbot", width=178)
   
    if uploaded_file is None:
        uploaded_file = st.file_uploader("Upload CSV file", type=["csv", "xlsx"])
        if uploaded_file is None:
            st.warning("Please upload a CSV or Excel file.")
            st.stop()  # Stop execution if no file uploaded

    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
    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 view_data():
    global uploaded_file
    st.title("Data Viewer Portal")
    if uploaded_file is None:
        uploaded_file = st.file_uploader("Upload CSV file", type=["csv", "xlsx"])
        if uploaded_file is None:
            st.warning("Please upload a CSV or Excel file.")
            st.stop()  # Stop execution if no file uploaded

    if uploaded_file.name.endswith('.xlsx'):
        # Load Excel file into pandas DataFrame
        df_excel = pd.read_excel(uploaded_file)
        # Save DataFrame as CSV
        csv_filename = uploaded_file.name.replace('.xlsx', '.csv')
        df_excel.to_csv(csv_filename, index=False)
        st.success(f"Excel file converted to CSV: {csv_filename}")
        # Set uploaded file to the converted CSV file
        uploaded_file = open(csv_filename, 'rb')

    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():
    global uploaded_file
    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():
        if uploaded_file is not None:
            return pd.read_csv(uploaded_file)
        return None

    if uploaded_file is None:
        uploaded_file = st.file_uploader("Upload a CSV file", type=["csv", "xlsx"])
        if uploaded_file is None:
            st.warning("Please upload a CSV or Excel file.")
            st.stop()  # Stop execution if no file uploaded

    if uploaded_file.name.endswith('.xlsx'):
        # Load Excel file into pandas DataFrame
        df_excel = pd.read_excel(uploaded_file)
        # Save DataFrame as CSV
        csv_filename = uploaded_file.name.replace('.xlsx', '.csv')
        df_excel.to_csv(csv_filename, index=False)
        st.success(f"Excel file converted to CSV: {csv_filename}")
        # Set uploaded file to the converted CSV file
        uploaded_file = open(csv_filename, 'rb')

    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()